| | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.thread.ThreadUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.fastjson.TypeReference; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.mina.core.service.IoAcceptor; |
| | | import org.apache.mina.core.service.IoHandler; |
| | | import org.apache.mina.core.session.IoSession; |
| | | import org.apache.shiro.SecurityUtils; |
| | | import org.eclipse.paho.client.mqttv3.MqttClient; |
| | | import org.eclipse.paho.client.mqttv3.MqttException; |
| | | import org.eclipse.paho.client.mqttv3.MqttMessage; |
| | | import org.jeecg.common.api.CommonAPI; |
| | | import org.jeecg.common.api.vo.Result; |
| | | import org.jeecg.common.config.TenantContext; |
| | | import org.jeecg.common.config.mqtoken.UserTokenContext; |
| | | import org.jeecg.common.constant.CommonCacheConstant; |
| | | import org.jeecg.common.constant.CommonConstant; |
| | | import org.jeecg.common.constant.MqttConstant; |
| | | import org.jeecg.common.system.util.JwtUtil; |
| | | import org.jeecg.common.system.vo.LoginUser; |
| | | import org.jeecg.common.util.DateUtils; |
| | | import org.jeecg.common.util.RedisUtil; |
| | | import org.jeecg.common.util.SpringContextUtils; |
| | | import org.jeecg.modules.dry.common.CacheConstants; |
| | | import org.jeecg.modules.dry.entity.*; |
| | | import org.jeecg.modules.dry.mqtt.MqMessage; |
| | | import org.jeecg.modules.dry.mqtt.MqttUtil; |
| | | import org.jeecg.modules.dry.service.*; |
| | | import org.jeecg.modules.dry.socket.ServerHandler; |
| | | import org.jeecg.modules.dry.socket.SocketServerConfig; |
| | | import org.jeecg.modules.dry.util.DryUtil; |
| | | import org.jeecg.modules.dry.vo.*; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.security.auth.login.LoginContext; |
| | | import java.io.IOException; |
| | | import java.io.ObjectOutputStream; |
| | | import java.net.Socket; |
| | | import java.text.DecimalFormat; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | |
| | | |
| | | @Override |
| | | public Result<?> fitFultRecord(RealTimeDataVo vo) { |
| | | TenantContext.setTenant(vo.getTenantid()+""); |
| | | ThreadUtil.execute(() -> { |
| | | try { |
| | | //è§£æå卿¥è¦æ°æ® |
| | | List<DryFaultRecord> faultRecords1 = fitFault(vo.getEqp_fault(), vo.getWorkorder(), vo.getTenantid(), vo.getMachineid(), 1); |
| | | List<DryFaultRecord> faultRecords2 = fitFault(vo.getEqp_warning(), vo.getWorkorder(), vo.getTenantid(), vo.getMachineid(), 2); |
| | | faultRecords1.addAll(faultRecords2); |
| | | JSONObject json = new JSONObject(); |
| | | json.put("data",JSON.toJSONString(faultRecords1)); |
| | | |
| | | if(faultRecords1.isEmpty()) return; |
| | | MqttMessage mqttMessage = new MqttMessage(); |
| | | mqttMessage.setQos(0); |
| | | mqttMessage.setPayload((JSON.toJSONString(json).getBytes())); |
| | | mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_FAULT_DATA,mqttMessage); |
| | | |
| | | //å¤çç»æåï¼å°redisä¸å®æ¶æ°æ®åéè³äºæå¡å¨ |
| | | Map<Object, Object> toCloudFaultMap = redisUtil.hmget(MqttConstant.MQTT_REAL_FAULT); |
| | | if(!toCloudFaultMap.isEmpty()){ |
| | | MqMessage< Map<Object, Object>> message = new MqMessage<>(); |
| | | message.setData(toCloudFaultMap); |
| | | message.setTentId(vo.getTenantid()+""); |
| | | MqttMessage mqttMessage = new MqttMessage(); |
| | | mqttMessage.setQos(0); |
| | | mqttMessage.setPayload(JSON.toJSONString(message).getBytes()); |
| | | mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_REAL_FAULT_DATA,mqttMessage); |
| | | } |
| | | |
| | | |
| | | //è¦ä¿åçå岿
é |
| | | if(!faultRecords1.isEmpty()){ |
| | | MqMessage<List<DryFaultRecord>> message = new MqMessage<>(); |
| | | message.setData(faultRecords1); |
| | | message.setTentId(vo.getTenantid()+""); |
| | | MqttMessage mqttMessage = new MqttMessage(); |
| | | mqttMessage.setQos(0); |
| | | mqttMessage.setPayload((JSON.toJSONString(message).getBytes())); |
| | | mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_FAULT_DATA,mqttMessage); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | //æ°æ®æ ·æ¬ï¼"eqp_fault": "æ»çéè¶
æ¶-æ¥è¦,飿ºè¿æµæ¥è¦,æ»çåè¶
æ¶-æ¥è¦,é£ç®±åæ¥è¦", |
| | | System.err.println((faultType == 1 ? "ç±»åï¼æ
é" : "ç±»åï¼æ¥è¦") + DateUtils.formatDateTime()+"--"+fault); |
| | | //redisä¸çæ
é |
| | | Map<Object, Object> rFauMap = redisUtil.hmget(MqttConstant.MQTT_EQP_FAULT); |
| | | Map<Object, Object> rFauMap = redisUtil.hmget(MqttConstant.MQTT_REAL_FAULT); |
| | | Map<String, Object> redFauMap = rFauMap.entrySet().stream() |
| | | .collect(Collectors.toMap( |
| | | entry -> entry.getKey().toString(), // é®è½¬æ¢ä¸ºå符串 |
| | |
| | | } |
| | | //1.è§£ææ°æ® |
| | | String[] eqpFaults = fault.split(","); |
| | | Map<String,DryFaultRecord> addFauMap = new HashMap<>(); |
| | | Map<String,DryFaultRecord> realFauMap = new HashMap<>(); |
| | | for (int i = 0; i < eqpFaults.length; i++) { |
| | | String eqpFault = eqpFaults[i]; |
| | | //é¿å
空å符串 |
| | | if(StringUtils.isEmpty(eqpFault)) continue; |
| | | if(StringUtils.isEmpty(eqpFault.trim())) continue; |
| | | //1.1æ£æ¥mqtt䏿¯å¦å·²åå¨è¿ä¸ªæ
é |
| | | String redisKey = String.format("%s_%s_%s", tenantId, machineId,eqpFault); |
| | | String rFault = (String) redisUtil.get(redisKey); |
| | | String redisKey = String.format("%s_%s_%s", tenantId, machineId,eqpFault).trim(); |
| | | |
| | | |
| | | realFauMap.put(redisKey, new DryFaultRecord()); |
| | | DryFaultRecordVo rFault = (DryFaultRecordVo) redisUtil.hget(MqttConstant.MQTT_REAL_FAULT,redisKey); |
| | | //1.2妿redisä¸åå¨ååå
¥ï¼åæ
éå¼å§ï¼ |
| | | if(rFault ==null){ |
| | | //ç»è£
ç¼åæ°æ® |
| | | DryFaultRecord faultRecord = new DryFaultRecord(orderId,eqpFault,faultType,new Date(),null); |
| | | realFauMap.put(redisKey,faultRecord); |
| | | // DryFaultRecord faultRecord = new DryFaultRecord(orderId,tenantId,eqpFault,faultType,new Date(),null); |
| | | // addFauMap.put(redisKey,faultRecord); |
| | | Map<String, DryEquipment> equipmentMap = equipmentService.queryEquByTenantId(tenantId); |
| | | String tenantName = (String) redisUtil.hget(CommonCacheConstant.SYS_CACHE_TENANT, tenantId + ""); |
| | | DryFaultRecordVo vo = new DryFaultRecordVo(orderId,tenantId,eqpFault,faultType,new Date(),null,1,equipmentMap.get(machineId).getName(),tenantName); |
| | | addFauMap.put(redisKey,vo); |
| | | }else { |
| | | //å¦ææ°æ®å·²åå¨ï¼ä¸è®¡æ°å¤§äº1å°±é置计æ°ï¼è®¡æ°3次åå¤å®æ
éç»æï¼3次ä¹å鿰䏿¥æ
é说ææ
éè¿å¨æç» éè¦éæ°è®¡æ°ï¼ |
| | | if(rFault.getECount()!=null && rFault.getECount() > 1){ |
| | | rFault.setECount(1); |
| | | redisUtil.hset(MqttConstant.MQTT_REAL_FAULT,redisKey,rFault); |
| | | System.err.println("æ¥è¦æ¬¡æ°éç½® clear clear ï¼key-"+redisKey); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | //1.3ç¼åè³redis |
| | | //åå¹¶æ°æ® |
| | | realFauMap.forEach((key, value) -> redFauMap.putIfAbsent(key, value)); |
| | | addFauMap.forEach((key, value) -> redFauMap.putIfAbsent(key, value)); |
| | | //æ²¡ææ°æ
éæ°æ®ä¸ç¨è¦ç |
| | | if(!realFauMap.isEmpty()){ |
| | | redisUtil.hmset(MqttConstant.MQTT_EQP_FAULT,redFauMap); |
| | | if(!addFauMap.isEmpty()){ |
| | | redisUtil.hmset(MqttConstant.MQTT_REAL_FAULT,redFauMap); |
| | | } |
| | | |
| | | |
| | | |
| | | //2æ£æµå·²ç»æçæ
é |
| | | //2.1妿宿¶æ°æ®ä¸åå¨redisåå¨å代表æ
éç»æï¼åå
¥æ°æ®åº |
| | | Map<Object, Object> curFauMap = redisUtil.hmget(MqttConstant.MQTT_EQP_FAULT); |
| | | Map<Object, Object> curFauMap = redisUtil.hmget(MqttConstant.MQTT_REAL_FAULT); |
| | | curFauMap.keySet().stream() |
| | | //ç¹å«æ³¨æï¼å¤ä¸ªæ¥è¦ç±»åå
±ç¨æ¹æ³éè¦åºåç±»å |
| | | .filter(key -> !realFauMap.containsKey(key) && ((DryFaultRecord)curFauMap.get(key)).getFaultType() == faultType) |
| | | .filter(key -> !realFauMap.containsKey(key) && ((DryFaultRecordVo)curFauMap.get(key)).getFaultType() == faultType) |
| | | .forEach(key -> { |
| | | System.err.println((faultType == 1 ? "ç±»åï¼æ
é" : "ç±»åï¼æ¥è¦") + DateUtils.formatDateTime()+"åå
¥æ°æ®åº"); |
| | | DryFaultRecord record = (DryFaultRecord)redFauMap.get(key); |
| | | record.setEndTime(new Date()); |
| | | faultRecordService.save(record); |
| | | redisUtil.hdel(MqttConstant.MQTT_EQP_FAULT,key); |
| | | DryFaultRecordVo vo = (DryFaultRecordVo)redFauMap.get(key); |
| | | vo.setECount(vo.getECount()+1); |
| | | if(redisUtil.hget(MqttConstant.MQTT_REAL_FAULT,key.toString())!=null){ |
| | | //æ´æ°æ¬¡æ° |
| | | redisUtil.hset(MqttConstant.MQTT_REAL_FAULT,key.toString(),vo); |
| | | System.err.println("æ¥è¦æ¬¡æ°æ´æ°ï¼key-"+key); |
| | | } |
| | | |
| | | result.add(record); |
| | | |
| | | if(vo.getECount()>=3){ |
| | | vo.setEndTime(new Date()); |
| | | //TODO ç»æè¶
è¿æä¸ªæ¶é´åºé´å¤å®ä¸ºéè¯¯æ°æ® |
| | | faultRecordService.save(vo); |
| | | redisUtil.hdel(MqttConstant.MQTT_REAL_FAULT,key); |
| | | result.add(vo); |
| | | System.err.println((faultType == 1 ? "ç±»åï¼æ
é" : "ç±»åï¼æ¥è¦") + DateUtils.formatDateTime()+"åå
¥æ°æ®åº"); |
| | | } |
| | | }); |
| | | |
| | | |
| | | |
| | | return result; |
| | | } |
| | | |