|
|
@@ -41,45 +41,43 @@ public class IotStatusService {
|
|
|
private MqttManager mqttManager;
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * iots 设备链接通道状态同步
|
|
|
- */
|
|
|
- private Map<String, Long> stringLongMap = new HashMap<>();
|
|
|
-
|
|
|
@PostConstruct
|
|
|
void init() {
|
|
|
// 目前基于已经实现将最新数据放到DeviceConfig的设备,没有的将进行实现
|
|
|
- //validateDeviceType.add(IotDeviceDictConst.TYPE_YF_CBD);// 云飞测报灯
|
|
|
+ validateDeviceType.add(IotDeviceDictConst.TYPE_YF_CBD);// 云飞测报灯
|
|
|
validateDeviceType.add(IotDeviceDictConst.TYPE_YF_BZY);// 云飞孢子仪
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_ZHAO_HE_SFJ);// 赵赫水肥机
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_GSSQ);// 新浦会管式墒情
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_LDSW_JC);// 新浦会雷达水位监测
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_SZZX_JC);// 新浦会水质在线监测
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_WSHJ_JC);// 新浦会温室环境监测
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_YF_XYCB_2);// 性诱测报2.0
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_YF_GXZW);// 根系作物
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_TRSH_CL);// 新普惠土壤水势
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_WSKZ);// 新浦会温室环境控制
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_HPF_WSMJ_ZNKG);// 海普发智能温室控制
|
|
|
+ validateDeviceType.add(IotDeviceDictConst.TYPE_YF_SCD);// 云飞杀虫灯
|
|
|
+ validateDeviceType.add(IotDeviceDictConst.TYPE_YF_SQZ);// 云飞墒情站
|
|
|
+ validateDeviceType.add(IotDeviceDictConst.TYPE_YF_QXZ);// 云飞环境监测
|
|
|
+
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_ZHAO_HE_SFJ);// 赵赫水肥机
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_GSSQ);// 新浦会管式墒情
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_LDSW_JC);// 新浦会雷达水位监测
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_SZZX_JC);// 新浦会水质在线监测
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_WSHJ_JC);// 新浦会温室环境监测
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_YF_XYCB_2);// 性诱测报2.0
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_YF_GXZW);// 根系作物
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_TRSH_CL);// 新普惠土壤水势
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_XPH_WSKZ);// 新浦会温室环境控制
|
|
|
+ //validateDeviceType.add(IotDeviceDictConst.TYPE_HPF_WSMJ_ZNKG);// 海普发智能温室控制
|
|
|
|
|
|
//validateDeviceType.add(IotDeviceDictConst.TYPE_BY_SFJ);// 云飞水肥机
|
|
|
//validateDeviceType.add(IotDeviceDictConst.TYPE_HT_SFJ);// 宏泰水肥机
|
|
|
|
|
|
//validateDeviceType.add(IotDeviceDictConst.TYPE_YF_QXZ);//云飞气象站
|
|
|
- validateDeviceType.add(IotDeviceDictConst.TYPE_YF_SCD);// 云飞杀虫灯
|
|
|
- try{
|
|
|
- //selectSfStatusByAll();
|
|
|
- }catch (Exception e){
|
|
|
+ try {
|
|
|
+ validateStatusByDevType();
|
|
|
+ } catch (Exception e) {
|
|
|
log.error("【设备检测】异常", e);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 定期根据类型查设备最新设备数据,是否长时间不上报。,并进行重新订阅
|
|
|
+ * 定期根据类型查设备最新设备数据,是否长时间不上报。
|
|
|
*/
|
|
|
- //@Scheduled(cron = "0 0 */1 * * ?")
|
|
|
- public void selectSfStatusByAll() throws MqttException {
|
|
|
+ @Scheduled(cron = "0 0 */1 * * ?")
|
|
|
+ public void validateStatusByDevType(){
|
|
|
printMqttStatus();
|
|
|
Iterator<String> iterator = validateDeviceType.iterator();
|
|
|
while (iterator.hasNext()) {
|
|
|
@@ -90,9 +88,7 @@ public class IotStatusService {
|
|
|
|
|
|
/***
|
|
|
* 一个一个类型检查
|
|
|
- * 原因:统计每个类型正常和错误的差值,用于判断是否整体重启
|
|
|
- * 1. 同一个mqtt有些数据正常上报,有些不正常,则考虑订阅问题,排除mqtt链接问题
|
|
|
- * 2. 同一个mqtt 都不正常,考虑重启mqtt
|
|
|
+ * 根据时间标记离线
|
|
|
* @param type
|
|
|
*/
|
|
|
void validateDeviceStatus(String type) {
|
|
|
@@ -102,8 +98,6 @@ public class IotStatusService {
|
|
|
// 查出来待检查的设备。根据类型
|
|
|
List<IotDeviceStatusResVo> iotDeviceStatusResVos = iIotDeviceService.selectAllDeviceConfigStatus(param);
|
|
|
log.info("【设备检测】【设备类型{} 】设备:{} 个", type, iotDeviceStatusResVos.size());
|
|
|
- int totalNum = iotDeviceStatusResVos.size();
|
|
|
- int errorNum = 0;
|
|
|
for (IotDeviceStatusResVo iotDeviceStatusResVo : iotDeviceStatusResVos) {
|
|
|
try {
|
|
|
// 获取设备类型id,存在的在进行检查,因为有些设备不是基于device config 上报
|
|
|
@@ -114,126 +108,35 @@ public class IotStatusService {
|
|
|
String createdTime = iotDeviceStatusResVo.getDevCreateddate();
|
|
|
String vTime = StringUtils.isEmpty(updateTime) ? createdTime : updateTime;
|
|
|
String devCode = iotDeviceStatusResVo.getDevCode();
|
|
|
- String firmBizId = iotDeviceStatusResVo.getFirmBid();
|
|
|
- String devtypeBid = iotDeviceStatusResVo.getDevtypeBid();
|
|
|
String devName = iotDeviceStatusResVo.getDevName();
|
|
|
String devBid = iotDeviceStatusResVo.getDevBid();
|
|
|
String devStatus = iotDeviceStatusResVo.getDevStatus();
|
|
|
// 检查时间上报间隔是否异常,30分钟没有,则异常
|
|
|
- if (validateTime(vTime, 60)) {
|
|
|
- // 1. 当前在线的进行检查;
|
|
|
- if(devStatus.equals("1")){
|
|
|
- // 更新状态
|
|
|
- IotDevice iotDevice = new IotDevice();
|
|
|
- iotDevice.setDevBid(devBid);
|
|
|
- iotDevice.setDevStatus("0");
|
|
|
- iotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
|
|
|
- iotDevice.setDevModifieddate(DateUtils.dateTimeNow());
|
|
|
-
|
|
|
- iIotDeviceService.updateIotDevice(iotDevice);
|
|
|
- log.info("【设备检测】【设备状态异常更新-设备类型{} 】设备标识:{} 设备编号 {} 设备名称 {} 异常状态 {}", type, devBid, devCode, devName, iotDeviceStatusResVo.getDevStatus());
|
|
|
- }
|
|
|
-
|
|
|
- // 2. 如果离线的:订阅的主题还在那还能收到上报数据,变为在线,如果是没有了,那就一直离线了:
|
|
|
- // so:不管之前在线,还是一直离线先都尝试重新链接订阅
|
|
|
- //if ("1".equals(iotDeviceStatusResVo.getDevStatus())) {
|
|
|
- // 获取该类型用的mqttcore 问:会不会没有呢,不会,系统初始化时候必须先建立
|
|
|
- /* MqttCore mqttCore = mqttManager.getMqttCoreByFirmAndDevType(firmBizId, devtypeBid);
|
|
|
- if (mqttCore != null) {
|
|
|
- log.info("【设备检测】【尝试重连】:设备:{} {} 上次时间:{},30分钟没有更新数据,厂家:{},厂家名称:{},设备类型:{}", devName, devCode, vTime, firmBizId, mqttCore.getFirmName(), devtypeBid);
|
|
|
- reConnection(mqttCore.getServiceName(), devCode, devName, devtypeBid);
|
|
|
- errorNum += 1;
|
|
|
- } else {
|
|
|
- log.error("【设备检测】【重连异常】【重视!!!】未找到服务 设备:{} code: {} 厂家id:{},厂家名称:{},设备类型:{}", devName, devCode, firmBizId, devtypeBid);
|
|
|
- }*/
|
|
|
- } else {
|
|
|
- cleanStatCount(devCode, iotDeviceStatusResVo.getDevName());
|
|
|
+ if (!validateTime(vTime, 60)) {
|
|
|
log.info("【设备检测】【正常】:设备:{} {} 上次时间:{}", iotDeviceStatusResVo.getDevName(), iotDeviceStatusResVo.getDevCode(), vTime);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1. 当前在线的进行检查;
|
|
|
+ if (devStatus.equals("1")) {
|
|
|
+ // 更新状态
|
|
|
+ IotDevice iotDevice = new IotDevice();
|
|
|
+ iotDevice.setDevBid(devBid);
|
|
|
+ iotDevice.setDevStatus("0");
|
|
|
+ iotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
|
|
|
+ iotDevice.setDevModifieddate(DateUtils.dateTimeNow());
|
|
|
+ iIotDeviceService.updateIotDevice(iotDevice);
|
|
|
+ log.info("【设备检测】【设备状态异常更新-设备类型{} 】设备标识:{} 设备编号 {} 设备名称 {} 异常状态 {}", type, devBid, devCode, devName, iotDeviceStatusResVo.getDevStatus());
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.info("【设备检测】【程序异常】:设备:{} {} {} {}", iotDeviceStatusResVo.getDevName(), iotDeviceStatusResVo.getDevCode(), iotDeviceStatusResVo, e);
|
|
|
}
|
|
|
}
|
|
|
- // 检查是否整体重置
|
|
|
- log.info("【设备检测】【结果:设备类型{} 】 一共{}个,失败:{} ", type, totalNum, errorNum);
|
|
|
- if (totalNum == errorNum) {
|
|
|
- log.error("【设备检测】【重视!!!】【结果:设备类型{} 】全部失败{}:考虑重启 ", type, errorNum);
|
|
|
- //reStartMqtt();
|
|
|
- }
|
|
|
log.info("【设备检测结束 类型:{}】【 设备:{} 个#########################", type, iotDeviceStatusResVos.size());
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 重新链接
|
|
|
- *
|
|
|
- * @param serviceName
|
|
|
- * @param devCode
|
|
|
- * @param devName
|
|
|
- * @param devType
|
|
|
- * @throws MqttException
|
|
|
- */
|
|
|
- @Deprecated
|
|
|
- void reConnection(String serviceName, String devCode, String devName, String devType) throws MqttException {
|
|
|
- // 用于复用前人写的手动订阅和取消,虽然我们这里只有一个id
|
|
|
- Map<String, List<String>> serviceNameMap = new HashMap<>();
|
|
|
- List<String> ids = new ArrayList<>();
|
|
|
- ids.add(devCode);
|
|
|
- serviceNameMap.put(serviceName, ids);
|
|
|
- // 最多重连20次,策略待定
|
|
|
- if (stringLongMap.containsKey(devCode)) {
|
|
|
- if (stringLongMap.get(devCode) < 20) {
|
|
|
- //mqttManager.unsubscribeByBatch(serviceNameMap);
|
|
|
- //mqttManager.subscribeByBatch(serviceNameMap);
|
|
|
- statCount(devCode, devName);
|
|
|
- log.info("【设备检测】【异常】【重连】:设备:devName: {} ,devCode: {}, devType:{}", devName, devCode, devType);
|
|
|
- }
|
|
|
- } else {
|
|
|
- //mqttManager.unsubscribeByBatch(serviceNameMap);
|
|
|
- //mqttManager.subscribeByBatch(serviceNameMap);
|
|
|
- statCount(devCode, devName);
|
|
|
- log.info("【设备检测】【异常】【重连】:设备:devName: {} ,devCode: {}, devType:{}", devName, devCode, devType);
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 最保守方法进行重启mqtt,重新加载配置
|
|
|
- *
|
|
|
- * @throws MqttException
|
|
|
- */
|
|
|
- void reStartMqtt() throws MqttException {
|
|
|
- // mqttManager.init();
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 临时统计重新订阅次数
|
|
|
- * 注意:后续成功后需要清除
|
|
|
- *
|
|
|
- * @param devCode
|
|
|
- * @param name
|
|
|
- */
|
|
|
- void statCount(String devCode, String name) {
|
|
|
- if (stringLongMap.containsKey(devCode)) {
|
|
|
- stringLongMap.replace(devCode, stringLongMap.get(devCode) + 1);
|
|
|
- } else {
|
|
|
- stringLongMap.put(devCode, 1L);
|
|
|
- }
|
|
|
- log.info("【设备检测】【重连】统计次数:设备:{} {} 次数:{} {}", name, devCode, stringLongMap.get(devCode));
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 临时统计重新订阅次数
|
|
|
- */
|
|
|
- void cleanStatCount(String devCode, String name) {
|
|
|
- if (stringLongMap.containsKey(devCode)) {
|
|
|
- log.info("【设备检测】【重连】统计次数:设备:{} {} 次数:{} {}", name, devCode, stringLongMap.get(devCode));
|
|
|
- stringLongMap.remove(devCode);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* 超过10分钟返回true
|
|
|
* true:一直没收到
|
|
|
*
|