diff --git a/src/main/java/com/techsor/datacenter/sender/components/CommonOpt.java b/src/main/java/com/techsor/datacenter/sender/components/CommonOpt.java index a7c0a45..ddedac6 100644 --- a/src/main/java/com/techsor/datacenter/sender/components/CommonOpt.java +++ b/src/main/java/com/techsor/datacenter/sender/components/CommonOpt.java @@ -104,12 +104,11 @@ public class CommonOpt { - public TopCompanyInfo getCompanyIdByDevicePrefix(String prefix) { - String redisKey = "f10:deviceCompanyMapping"; + public TopCompanyInfo getCompanyIdByDevicePrefix(String redisKey, String prefix, Long categoryId) { String companyId = (String) redisTemplate.opsForHash().get(redisKey, prefix); if (companyId==null){ //Try from mysql - companyId = getCompanyIdByDevicePrefixFromAllDB(prefix); + companyId = getCompanyIdByDevicePrefixFromAllDB(prefix, categoryId); if (companyId.equals("")){ log.error("Can't find topCompanyId for deviceId prefix: "+prefix+", Please notice if this device is not login on platform."); return null; @@ -132,7 +131,7 @@ public class CommonOpt { return topCompanyInfo; } - private String getCompanyIdByDevicePrefixFromAllDB(String prefix) { + private String getCompanyIdByDevicePrefixFromAllDB(String prefix, Long categoryId) { String companyId = ""; List companyEntityList = companyInfoDao.getCompanyList(); for (CompanyEntity companyItem : companyEntityList){ @@ -146,7 +145,7 @@ public class CommonOpt { } //只处理一级企业 log.info("[getCompanyIdByDevicePrefixFromAllDB] prefix:"+prefix+" , comanyId:"+tempCompanyId); - List deviceList = deviceDao.getDeviceInfoByDevicePrefixWithDB(prefix,tempCompanyId); + List deviceList = deviceDao.getDeviceInfoByDevicePrefixWithDB(prefix,tempCompanyId, categoryId); if (deviceList.size()<=0){ log.info("[getCompanyIdByDevicePrefixFromAllDB] deviceList size is 0"); continue; diff --git a/src/main/java/com/techsor/datacenter/sender/dao/DeviceDao.java b/src/main/java/com/techsor/datacenter/sender/dao/DeviceDao.java index 7da21f2..40e52db 100644 --- a/src/main/java/com/techsor/datacenter/sender/dao/DeviceDao.java +++ b/src/main/java/com/techsor/datacenter/sender/dao/DeviceDao.java @@ -231,17 +231,26 @@ public class DeviceDao { return dataList; } - @Cacheable(value = "DeviceDao::getDeviceInfoByDevicePrefixWithDB", key = "#prefix + '::' + #companyId") - public List getDeviceInfoByDevicePrefixWithDB(String prefix,String companyId){ + @Cacheable(value = "DeviceDao::getDeviceInfoByDevicePrefixWithDB", key = "#prefix + '::' + #companyId+ '::' + #categoryId") + public List getDeviceInfoByDevicePrefixWithDB(String prefix,String companyId, Long categoryId){ if (StringUtils.isEmpty(prefix)){ return new ArrayList<>(); } String sql = ""; - sql = "SELECT id,company_id,device_id,device_sn,type_id,space_id,building_id,device_name," - + "project_id,floor_id,asset_id " - + "FROM data_center_aeon_" + companyId + ".device_info " - + "WHERE device_id LIKE '" + prefix.replace("_", "\\_") + "%' " - + "AND flag != 1"; + sql = "SELECT device_info.id," + + "device_info.company_id," + + "device_info.device_id," + + "device_info.device_sn," + + "device_info.type_id," + + "device_info.device_name,"+ + "device_info.project_id," + + "device_info.asset_id " + + " FROM data_center_aeon_" + companyId + ".device_info " + + " INNER JOIN data_center_aeon_" + companyId + ".type " + + " ON type.id = device_info.type_id " + + " WHERE device_id LIKE '" + prefix.replace("_", "\\_") + "%' " + + " AND type.device_category_id = " + categoryId + + " AND device_info.flag != 1"; List dataList = jdbcTemplate.query(sql, (rs, rowNum) -> { DeviceEntity item = new DeviceEntity(); @@ -250,11 +259,8 @@ public class DeviceDao { item.setDeviceId(rs.getString("device_id")); item.setDeviceSN(rs.getString("device_sn")); item.setTypeId(rs.getInt("type_id")); - item.setSpaceId(rs.getLong("space_id")); - item.setBuildingId(rs.getLong("building_id")); item.setDeviceName(rs.getString("device_name")); item.setProjectId(rs.getLong("project_id")); - item.setFloorId(rs.getLong("floor_id")); item.setAssetId(rs.getLong("asset_id")); return item; }); diff --git a/src/main/java/com/techsor/datacenter/sender/service/CsdjService.java b/src/main/java/com/techsor/datacenter/sender/service/CsdjService.java index 7b3ce93..ccfb460 100644 --- a/src/main/java/com/techsor/datacenter/sender/service/CsdjService.java +++ b/src/main/java/com/techsor/datacenter/sender/service/CsdjService.java @@ -11,6 +11,7 @@ import com.techsor.datacenter.sender.dao.DeviceDao; import com.techsor.datacenter.sender.dto.DeviceInfoVO; import com.techsor.datacenter.sender.entitiy.DeviceEntity; import com.techsor.datacenter.sender.entitiy.DynamodbEntity; +import com.techsor.datacenter.sender.entitiy.company.TopCompanyInfo; import com.techsor.datacenter.sender.entitiy.csdj.*; import com.techsor.datacenter.sender.utils.csdj.CsdjNotificationParser; import com.fasterxml.jackson.core.JsonProcessingException; @@ -49,8 +50,15 @@ public class CsdjService { String terminalId = csdjEntity.getTerminalId(); - String topCompanyId = commonOpt.getTopCompanyId(terminalId); - if (topCompanyId == null || topCompanyId.equals("0")) { + String deviceCompanyMappingRedisKey = "csdj:deviceCompanyMapping"; + TopCompanyInfo topCompanyInfo= commonOpt.getCompanyIdByDevicePrefix(deviceCompanyMappingRedisKey, terminalId + "-", 1012L); + if (topCompanyInfo==null || "0".equals(topCompanyInfo.getTopCompanyId())){ + log.error("[F10] no companyId is found========================>>>> {}",terminalId); + return; + } + + String topCompanyId = topCompanyInfo.getTopCompanyId(); + if (topCompanyId == null) { log.error("no companyId is found========================>>>> {}", terminalId); return; } @@ -59,13 +67,13 @@ public class CsdjService { log.info("Use datasource for company:" + topCompanyId); - List deviceEntitys = this.deviceDao.getDeviceInfoByDeviceId(terminalId); - this.guavaRedisCache.incrementDailyDeviceIdCount(terminalId); - if (CollectionUtil.isEmpty(deviceEntitys)) { - log.error("no deviceEntity is found========================>>>> {}", terminalId); - return; - } - DeviceEntity deviceEntity = deviceEntitys.get(0); +// List deviceEntitys = this.deviceDao.getDeviceInfoByDeviceId(terminalId); +// this.guavaRedisCache.incrementDailyDeviceIdCount(terminalId); +// if (CollectionUtil.isEmpty(deviceEntitys)) { +// log.error("no deviceEntity is found========================>>>> {}", terminalId); +// return; +// } +// DeviceEntity deviceEntity = deviceEntitys.get(0); // 1. 解析通報数据 CsdjNotificationData notificationData = csdjNotificationParser.parse(csdjEntity.getData()); @@ -102,15 +110,15 @@ public class CsdjService { Map deviceAlertHistoryMap = new HashMap<>(); if (digitalTerminals != null && !digitalTerminals.isEmpty()) { for (CsdjDigitalInputTerminal terminal : digitalTerminals) { - Map.Entry entry = processDigitalTerminal(csdjEntity, notificationData, terminal, deviceEntity); - if (entry.getValue() != null) { + Map.Entry entry = processDigitalTerminal(csdjEntity, notificationData, terminal); + if (null != entry && entry.getValue() != null) { deviceAlertHistoryMap.put(entry.getKey(), entry.getValue()); } } } // 5. 更新 csdj_device_realtime 表 - Long companyId = deviceEntity.getCompanyId(); + Long companyId = Long.valueOf(topCompanyInfo.getSelfCompanyId()); saveDeviceRealtime(terminalId, companyId, csdjEntity.getData(), notificationData, deviceAlertHistoryMap); log.info("[CSDJ] 处理 CSDJ 数据完成,terminalId:{}", csdjEntity.getTerminalId()); @@ -120,7 +128,7 @@ public class CsdjService { } private Map.Entry processDigitalTerminal(CsdjEntity csdjEntity, CsdjNotificationData notificationData, - CsdjDigitalInputTerminal terminal, DeviceEntity deviceEntity) { + CsdjDigitalInputTerminal terminal) { String terminalId = csdjEntity.getTerminalId(); String deviceId = terminal.getDeviceId(); Integer terminalNo = terminal.getTerminalNo(); @@ -130,6 +138,15 @@ public class CsdjService { log.info("[CSDJ] デジタル入力端子,terminalId:{}, deviceId:{}, No:{}, state:{}, isAlarm:{}", terminalId, deviceId, terminalNo, stateText, isAlarm); + List deviceEntitys = this.deviceDao.getDeviceInfoByDeviceId(deviceId); + this.guavaRedisCache.incrementDailyDeviceIdCount(terminalId); + if (CollectionUtil.isEmpty(deviceEntitys)) { + log.error("no deviceEntity is found========================>>>> {}", terminalId); + return null; + } + DeviceEntity deviceEntity = deviceEntitys.get(0); + + Long companyId = deviceEntity.getCompanyId(); CsdjAlarmHistory csdjAlarmHistory = new CsdjAlarmHistory(); diff --git a/src/main/java/com/techsor/datacenter/sender/service/F10Service.java b/src/main/java/com/techsor/datacenter/sender/service/F10Service.java index f6fc667..47e6f90 100644 --- a/src/main/java/com/techsor/datacenter/sender/service/F10Service.java +++ b/src/main/java/com/techsor/datacenter/sender/service/F10Service.java @@ -113,7 +113,8 @@ public class F10Service { } } - TopCompanyInfo topCompanyInfo= commonOpt.getCompanyIdByDevicePrefix(f10ProcessingData.getGatewayId() + "_"); + String deviceCompanyMappingRedisKey = "f10:deviceCompanyMapping"; + TopCompanyInfo topCompanyInfo= commonOpt.getCompanyIdByDevicePrefix(deviceCompanyMappingRedisKey, f10ProcessingData.getGatewayId() + "_", 1011L); if (topCompanyInfo==null || "0".equals(topCompanyInfo.getTopCompanyId())){ log.error("[F10] no companyId is found========================>>>> {}",f10ProcessingData.getGatewayId()); return;