6 changed files with 365 additions and 3 deletions
@ -0,0 +1,79 @@ |
|||||
|
package com.techsor.datacenter.sender.dao; |
||||
|
|
||||
|
import com.techsor.datacenter.sender.entitiy.f10.F10JournalLog; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import org.springframework.jdbc.core.JdbcTemplate; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
@Component |
||||
|
public class F10Dao { |
||||
|
|
||||
|
@Resource |
||||
|
private JdbcTemplate jdbcTemplate ; |
||||
|
|
||||
|
public void insertF10Log(F10JournalLog f10JournalLog) { |
||||
|
String sql = "INSERT INTO f10_journal_log (" + |
||||
|
"device_id, company_id, raw_hex, raw_text, serial_no, mode, " + |
||||
|
"signal_seq, receive_date, receive_time, channel_no, test_flag, block_no, block_name, " + |
||||
|
"signal_type1, signal_type2, signal_label, signal_status, " + |
||||
|
"course_no, area_code, course_code, standby_name, physical_addr, " + |
||||
|
"customer_name, phone, remark, display_color, error_reason, line_type, area_name, " + |
||||
|
"send_date, send_time, customer_no, display_data1, display_data2, " + |
||||
|
"instruction_flag, signal_code, card_no, card_type, mansion_building, mansion_room, option_field, " + |
||||
|
"created_at, created_time" + |
||||
|
") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; |
||||
|
|
||||
|
jdbcTemplate.update(sql, |
||||
|
f10JournalLog.getDeviceId(), |
||||
|
f10JournalLog.getCompanyId(), |
||||
|
f10JournalLog.getRawHex(), |
||||
|
f10JournalLog.getRawText(), |
||||
|
f10JournalLog.getSerialNo(), |
||||
|
f10JournalLog.getMode(), |
||||
|
|
||||
|
f10JournalLog.getSignalSeq(), |
||||
|
f10JournalLog.getReceiveDate(), |
||||
|
f10JournalLog.getReceiveTime(), |
||||
|
f10JournalLog.getChannelNo(), |
||||
|
f10JournalLog.getTestFlag(), |
||||
|
f10JournalLog.getBlockNo(), |
||||
|
f10JournalLog.getBlockName(), |
||||
|
|
||||
|
f10JournalLog.getSignalType1(), |
||||
|
f10JournalLog.getSignalType2(), |
||||
|
f10JournalLog.getSignalLabel(), |
||||
|
f10JournalLog.getSignalStatus(), |
||||
|
|
||||
|
f10JournalLog.getCourseNo(), |
||||
|
f10JournalLog.getAreaCode(), |
||||
|
f10JournalLog.getCourseCode(), |
||||
|
f10JournalLog.getStandbyName(), |
||||
|
f10JournalLog.getPhysicalAddr(), |
||||
|
|
||||
|
f10JournalLog.getCustomerName(), |
||||
|
f10JournalLog.getPhone(), |
||||
|
f10JournalLog.getRemark(), |
||||
|
f10JournalLog.getDisplayColor(), |
||||
|
f10JournalLog.getErrorReason(), |
||||
|
f10JournalLog.getLineType(), |
||||
|
f10JournalLog.getAreaName(), |
||||
|
|
||||
|
f10JournalLog.getSendDate(), |
||||
|
f10JournalLog.getSendTime(), |
||||
|
f10JournalLog.getCustomerNo(), |
||||
|
f10JournalLog.getDisplayData1(), |
||||
|
f10JournalLog.getDisplayData2(), |
||||
|
|
||||
|
f10JournalLog.getInstructionFlag(), |
||||
|
f10JournalLog.getSignalCode(), |
||||
|
f10JournalLog.getCardNo(), |
||||
|
f10JournalLog.getCardType(), |
||||
|
f10JournalLog.getMansionBuilding(), |
||||
|
f10JournalLog.getMansionRoom(), |
||||
|
f10JournalLog.getOptionField(), |
||||
|
|
||||
|
System.currentTimeMillis(), |
||||
|
new java.sql.Timestamp(System.currentTimeMillis()) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,79 @@ |
|||||
|
package com.techsor.datacenter.sender.entitiy.f10; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.time.LocalDateTime; |
||||
|
|
||||
|
@Data |
||||
|
public class F10JournalLog { |
||||
|
|
||||
|
// ===== 主键 =====
|
||||
|
private Long id; |
||||
|
|
||||
|
// ===== 设备 =====
|
||||
|
private String deviceId; |
||||
|
private Long companyId; |
||||
|
|
||||
|
// ===== 原始数据 =====
|
||||
|
private String rawHex; |
||||
|
private String rawText; |
||||
|
|
||||
|
// ===== 报文头 =====
|
||||
|
private String serialNo; |
||||
|
private String mode; |
||||
|
|
||||
|
// ===== 1~35字段 =====
|
||||
|
private String signalSeq; |
||||
|
private String receiveDate; |
||||
|
private String receiveTime; |
||||
|
|
||||
|
private String channelNo; |
||||
|
private String testFlag; |
||||
|
private String blockNo; |
||||
|
private String blockName; |
||||
|
|
||||
|
private String signalType1; |
||||
|
private String signalType2; |
||||
|
private String signalLabel; |
||||
|
private String signalStatus; |
||||
|
|
||||
|
private String courseNo; |
||||
|
private String areaCode; |
||||
|
private String courseCode; |
||||
|
|
||||
|
private String standbyName; |
||||
|
private String physicalAddr; |
||||
|
|
||||
|
private String customerName; |
||||
|
private String phone; |
||||
|
private String remark; |
||||
|
|
||||
|
private String displayColor; |
||||
|
private String errorReason; |
||||
|
private String lineType; |
||||
|
|
||||
|
private String areaName; |
||||
|
|
||||
|
private String sendDate; |
||||
|
private String sendTime; |
||||
|
|
||||
|
private String customerNo; |
||||
|
|
||||
|
private String displayData1; |
||||
|
private String displayData2; |
||||
|
|
||||
|
private String instructionFlag; |
||||
|
private String signalCode; |
||||
|
|
||||
|
private String cardNo; |
||||
|
private String cardType; |
||||
|
|
||||
|
private String mansionBuilding; |
||||
|
private String mansionRoom; |
||||
|
|
||||
|
private String optionField; |
||||
|
|
||||
|
// ===== 系统字段 =====
|
||||
|
private Long createdAt; // 毫秒时间戳
|
||||
|
private LocalDateTime createdTime; // 标准时间
|
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
package com.techsor.datacenter.sender.entitiy.f10; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class RedisStreamEntity { |
||||
|
|
||||
|
private int partition;; |
||||
|
private String hexRawData; |
||||
|
private String payload; |
||||
|
private long ts; |
||||
|
|
||||
|
} |
||||
@ -0,0 +1,172 @@ |
|||||
|
package com.techsor.datacenter.sender.service; |
||||
|
|
||||
|
import cn.hutool.core.collection.CollectionUtil; |
||||
|
import com.techsor.datacenter.sender.components.CommonOpt; |
||||
|
import com.techsor.datacenter.sender.components.GuavaRedisCache; |
||||
|
import com.techsor.datacenter.sender.config.DataSourceContextHolder; |
||||
|
import com.techsor.datacenter.sender.dao.DeviceDao; |
||||
|
import com.techsor.datacenter.sender.dao.F10Dao; |
||||
|
import com.techsor.datacenter.sender.entitiy.DeviceEntity; |
||||
|
import com.techsor.datacenter.sender.entitiy.f10.F10JournalLog; |
||||
|
import com.techsor.datacenter.sender.entitiy.f10.RedisStreamEntity; |
||||
|
import jakarta.annotation.Resource; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.nio.charset.Charset; |
||||
|
import java.util.Arrays; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Service("F10Service") |
||||
|
public class F10Service { |
||||
|
|
||||
|
private static final Charset SHIFT_JIS = Charset.forName("Shift-JIS"); |
||||
|
|
||||
|
@Autowired |
||||
|
private CommonOpt commonOpt; |
||||
|
@Resource |
||||
|
private GuavaRedisCache guavaRedisCache; |
||||
|
|
||||
|
@Autowired |
||||
|
private F10Dao f10Dao; |
||||
|
@Autowired |
||||
|
private DeviceDao deviceDao; |
||||
|
|
||||
|
|
||||
|
|
||||
|
public void handle(RedisStreamEntity rawEntity) { |
||||
|
String hexLog = rawEntity.getHexRawData().trim(); |
||||
|
byte[] src = hexStringToByteArray(hexLog); |
||||
|
//(STX:1 + Serial:4 + Mode:2 + 正文:320 + ETX:1)
|
||||
|
if (src.length < 328) { |
||||
|
log.warn("原始报文长度不足328字节"); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
try { |
||||
|
F10JournalLog f10JournalLog = parseToEntity(rawEntity.getHexRawData()); |
||||
|
|
||||
|
//前缀_26契約先番号_4チャンネル番号
|
||||
|
//前缀还没定,先【26契約先番号_4チャンネル番号】
|
||||
|
f10JournalLog.setDeviceId(f10JournalLog.getCustomerNo() + "_" + f10JournalLog.getChannelNo()); |
||||
|
|
||||
|
String topCompanyId= commonOpt.getTopCompanyId(f10JournalLog.getDeviceId()); |
||||
|
if (topCompanyId==null || topCompanyId.equals("0")){ |
||||
|
return; |
||||
|
} |
||||
|
DataSourceContextHolder.clearCurrentDataSourceKey(); |
||||
|
DataSourceContextHolder.setCurrentDataSourceKey("dataSourceForCompany_"+topCompanyId); |
||||
|
|
||||
|
log.info("Use datasource for company:"+topCompanyId); |
||||
|
List<DeviceEntity> deviceEntity=this.deviceDao.getDeviceInfoByDeviceId(f10JournalLog.getDeviceId()); |
||||
|
this.guavaRedisCache.incrementDailyDeviceIdCount(f10JournalLog.getDeviceId()); |
||||
|
if (CollectionUtil.isEmpty(deviceEntity)){ |
||||
|
log.error("no deviceEntity is found========================>>>> {}",f10JournalLog.getDeviceId()); |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
Long companyId = deviceEntity.get(0).getCompanyId(); |
||||
|
|
||||
|
f10JournalLog.setCompanyId(companyId); |
||||
|
f10Dao.insertF10Log(f10JournalLog); |
||||
|
|
||||
|
} catch (Exception e) { |
||||
|
log.error("F10解析入库失败", e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static F10JournalLog parseToEntity(String hexLog) { |
||||
|
byte[] src = hexStringToByteArray(hexLog); |
||||
|
|
||||
|
int offset = 0; |
||||
|
|
||||
|
// 跳过 STX
|
||||
|
offset += 1; |
||||
|
|
||||
|
String serialNo = new String(Arrays.copyOfRange(src, offset, offset + 4), SHIFT_JIS); |
||||
|
String mode = new String(Arrays.copyOfRange(src, offset, offset + 2), SHIFT_JIS); |
||||
|
|
||||
|
int bodyStart = 7; |
||||
|
int bodyEnd = src.length - 1; |
||||
|
byte[] body = Arrays.copyOfRange(src, bodyStart, bodyEnd); |
||||
|
String rawText = new String(body, SHIFT_JIS).trim(); |
||||
|
|
||||
|
F10JournalLog f10JournalLog = new F10JournalLog(); |
||||
|
f10JournalLog.setRawHex(hexLog); |
||||
|
f10JournalLog.setRawText(rawText); |
||||
|
f10JournalLog.setSerialNo(serialNo); |
||||
|
f10JournalLog.setMode(mode); |
||||
|
|
||||
|
offset = 7; |
||||
|
|
||||
|
f10JournalLog.setSignalSeq(getTargetData(src, offset, 9)); offset += 9; |
||||
|
f10JournalLog.setReceiveDate(getTargetData(src, offset, 8)); offset += 8; |
||||
|
f10JournalLog.setReceiveTime(getTargetData(src, offset, 6)); offset += 6; |
||||
|
|
||||
|
f10JournalLog.setChannelNo(getTargetData(src, offset, 3)); offset += 3; |
||||
|
f10JournalLog.setTestFlag(getTargetData(src, offset, 1)); offset += 1; |
||||
|
f10JournalLog.setBlockNo(getTargetData(src, offset, 2)); offset += 2; |
||||
|
f10JournalLog.setBlockName(getTargetData(src, offset, 30)); offset += 30; |
||||
|
|
||||
|
f10JournalLog.setSignalType1(getTargetData(src, offset, 2)); offset += 2; |
||||
|
f10JournalLog.setSignalType2(getTargetData(src, offset, 2)); offset += 2; |
||||
|
f10JournalLog.setSignalLabel(getTargetData(src, offset, 2)); offset += 2; |
||||
|
f10JournalLog.setSignalStatus(getTargetData(src, offset, 1)); offset += 1; |
||||
|
|
||||
|
f10JournalLog.setCourseNo(getTargetData(src, offset, 3)); offset += 3; |
||||
|
f10JournalLog.setAreaCode(getTargetData(src, offset, 2)); offset += 2; |
||||
|
f10JournalLog.setCourseCode(getTargetData(src, offset, 3)); offset += 3; |
||||
|
|
||||
|
f10JournalLog.setStandbyName(getTargetData(src, offset, 20)); offset += 20; |
||||
|
f10JournalLog.setPhysicalAddr(getTargetData(src, offset, 8)); offset += 8; |
||||
|
|
||||
|
f10JournalLog.setCustomerName(getTargetData(src, offset, 30)); offset += 30; |
||||
|
f10JournalLog.setPhone(getTargetData(src, offset, 20)); offset += 20; |
||||
|
f10JournalLog.setRemark(getTargetData(src, offset, 60)); offset += 60; |
||||
|
|
||||
|
f10JournalLog.setDisplayColor(getTargetData(src, offset, 2)); offset += 2; |
||||
|
f10JournalLog.setErrorReason(getTargetData(src, offset, 1)); offset += 1; |
||||
|
f10JournalLog.setLineType(getTargetData(src, offset, 2)); offset += 2; |
||||
|
|
||||
|
f10JournalLog.setAreaName(getTargetData(src, offset, 12)); offset += 12; |
||||
|
|
||||
|
f10JournalLog.setSendDate(getTargetData(src, offset, 8)); offset += 8; |
||||
|
f10JournalLog.setSendTime(getTargetData(src, offset, 6)); offset += 6; |
||||
|
|
||||
|
f10JournalLog.setCustomerNo(getTargetData(src, offset, 8)); offset += 8; |
||||
|
|
||||
|
f10JournalLog.setDisplayData1(getTargetData(src, offset, 20)); offset += 20; |
||||
|
f10JournalLog.setDisplayData2(getTargetData(src, offset, 20)); offset += 20; |
||||
|
|
||||
|
f10JournalLog.setInstructionFlag(getTargetData(src, offset, 1)); offset += 1; |
||||
|
f10JournalLog.setSignalCode(getTargetData(src, offset, 4)); offset += 4; |
||||
|
|
||||
|
f10JournalLog.setCardNo(getTargetData(src, offset, 7)); offset += 7; |
||||
|
f10JournalLog.setCardType(getTargetData(src, offset, 1)); offset += 1; |
||||
|
|
||||
|
f10JournalLog.setMansionBuilding(getTargetData(src, offset, 2)); offset += 2; |
||||
|
f10JournalLog.setMansionRoom(getTargetData(src, offset, 4)); offset += 4; |
||||
|
|
||||
|
f10JournalLog.setOptionField(getTargetData(src, offset, 10)); |
||||
|
|
||||
|
return f10JournalLog; |
||||
|
} |
||||
|
|
||||
|
private static String getTargetData(byte[] src, int start, int len) { |
||||
|
return new String(Arrays.copyOfRange(src, start, start + len), SHIFT_JIS).trim(); |
||||
|
} |
||||
|
|
||||
|
private static byte[] hexStringToByteArray(String s) { |
||||
|
s = s.replace(" ", ""); |
||||
|
int len = s.length(); |
||||
|
byte[] data = new byte[len / 2]; |
||||
|
for (int i = 0; i < len; i += 2) { |
||||
|
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) |
||||
|
+ Character.digit(s.charAt(i + 1), 16)); |
||||
|
} |
||||
|
return data; |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue