|
@@ -0,0 +1,252 @@
|
|
|
+/*
|
|
|
+ * 文 件 名: GeekOpenService
|
|
|
+ * 版 权: 华设设计集团股份有限公司
|
|
|
+ * 描 述: <描述>
|
|
|
+ * 修 改 人: lvwenbin
|
|
|
+ * 修改时间: 2025/2/27
|
|
|
+ * 跟踪单号: <跟踪单号>
|
|
|
+ * 修改单号: <修改单号>
|
|
|
+ * 修改内容: <修改内容>
|
|
|
+ */
|
|
|
+package com.ruoyi.ems.handle;
|
|
|
+
|
|
|
+import com.alibaba.fastjson2.JSONObject;
|
|
|
+import com.huashe.common.exception.Assert;
|
|
|
+import com.huashe.common.exception.BusinessException;
|
|
|
+import com.huashe.common.utils.ThreadUtils;
|
|
|
+import com.ruoyi.ems.core.MessageCache;
|
|
|
+import com.ruoyi.ems.core.MqttTemplate;
|
|
|
+import com.ruoyi.ems.domain.EmsDevice;
|
|
|
+import com.ruoyi.ems.domain.EmsObjAbilityCallLog;
|
|
|
+import com.ruoyi.ems.domain.EmsObjAttrValue;
|
|
|
+import com.ruoyi.ems.model.AbilityPayload;
|
|
|
+import com.ruoyi.ems.model.MqttCacheMsg;
|
|
|
+import com.ruoyi.ems.service.IEmsDeviceService;
|
|
|
+import com.ruoyi.ems.service.IEmsObjAbilityCallLogService;
|
|
|
+import com.ruoyi.ems.service.IEmsObjAttrValueService;
|
|
|
+import com.ruoyi.ems.util.IdUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * GeekOpen 断路器服务层
|
|
|
+ * <功能详细描述>
|
|
|
+ *
|
|
|
+ * @author lvwenbin
|
|
|
+ * @version [版本号, 2025/2/27]
|
|
|
+ * @see [相关类/方法]
|
|
|
+ * @since [产品/模块版本]
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class GeekOpenCbHandler extends MqttBaseHandler {
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(GeekOpenCbHandler.class);
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ @Qualifier("mqttTemplate")
|
|
|
+ private MqttTemplate mqttTemplate;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MessageCache messageCache;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IEmsDeviceService deviceService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IEmsObjAttrValueService objAttrValueService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IEmsObjAbilityCallLogService objAbilityCallLogService;
|
|
|
+
|
|
|
+ private static final String TOPIC_PREFIX = "/device/dlq/";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 能力执行
|
|
|
+ *
|
|
|
+ * @param abilityParam 执行参数
|
|
|
+ * @return 响应
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public String call(AbilityPayload abilityParam) {
|
|
|
+ String retStr = null;
|
|
|
+
|
|
|
+ String messageId = IdUtils.generateMessageId();
|
|
|
+ String deviceCode = abilityParam.getObjCode();
|
|
|
+ String msgBody = addMsgId(abilityParam.getAbilityParam(), "messageId", messageId);
|
|
|
+
|
|
|
+ // 发送消息到MQTT服务器
|
|
|
+ long sendTime = System.currentTimeMillis();
|
|
|
+ String topic = TOPIC_PREFIX + deviceCode;
|
|
|
+ mqttTemplate.send(topic, msgBody, 2, false);
|
|
|
+
|
|
|
+ // 写入日志
|
|
|
+ EmsObjAbilityCallLog logItem = saveLog(abilityParam, sendTime, 1);
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ MqttCacheMsg cacheMsg = messageCache.getAndRemoveMqttMessage(messageId);
|
|
|
+
|
|
|
+ if (null != cacheMsg) {
|
|
|
+ if (checkResult(abilityParam, cacheMsg)) {
|
|
|
+ retStr = "执行成功!";
|
|
|
+ updateLog(logItem, cacheMsg, 1);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ retStr = "执行失败!";
|
|
|
+ updateLog(logItem, cacheMsg, 2);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ ThreadUtils.sleep(100);
|
|
|
+
|
|
|
+ if (System.currentTimeMillis() - sendTime > 60000) {
|
|
|
+ retStr = "响应超时!";
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return retStr;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Async("msgHandleExecutor")
|
|
|
+ @Override
|
|
|
+ public void msgHandle(String deviceCode, String payload) {
|
|
|
+ try {
|
|
|
+ EmsDevice device = deviceService.selectByCode(deviceCode);
|
|
|
+
|
|
|
+ if (null != device) {
|
|
|
+ JSONObject msgBody = JSONObject.parseObject(payload);
|
|
|
+ refreshStatus(device);
|
|
|
+
|
|
|
+ String messageId = msgBody.getString("messageId");
|
|
|
+
|
|
|
+ if (StringUtils.equals("auto", messageId)) {
|
|
|
+ updateAttr(device, 2, msgBody);
|
|
|
+ }
|
|
|
+ else if (StringUtils.isNotEmpty(messageId)) {
|
|
|
+ MqttCacheMsg mqttCacheMsg = new MqttCacheMsg(messageId, deviceCode, new Date(), payload);
|
|
|
+ messageCache.addMqttMessage(messageId, mqttCacheMsg);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ log.warn("接收消息,设备未注册, deviceCode:{}\nmessageBody:{}", deviceCode, payload);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新设备状态
|
|
|
+ * @param device 设备信息
|
|
|
+ */
|
|
|
+ private void refreshStatus(EmsDevice device) {
|
|
|
+ device.setDeviceStatus(1);
|
|
|
+ deviceService.updateEmsDevice(device);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateAttr(EmsDevice device, Integer objType, JSONObject jsonBody) {
|
|
|
+ if (jsonBody.containsKey("voltage")) {
|
|
|
+ EmsObjAttrValue attrValue = new EmsObjAttrValue(device.getDeviceCode(), objType, device.getDeviceModel(),
|
|
|
+ "voltage", jsonBody.getString("voltage"));
|
|
|
+ objAttrValueService.mergeObjAttrValue(attrValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (jsonBody.containsKey("current")) {
|
|
|
+ EmsObjAttrValue attrValue = new EmsObjAttrValue(device.getDeviceCode(), objType, device.getDeviceModel(),
|
|
|
+ "current", jsonBody.getString("current"));
|
|
|
+ objAttrValueService.mergeObjAttrValue(attrValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (jsonBody.containsKey("power")) {
|
|
|
+ EmsObjAttrValue attrValue = new EmsObjAttrValue(device.getDeviceCode(), objType, device.getDeviceModel(),
|
|
|
+ "power", jsonBody.getString("power"));
|
|
|
+ objAttrValueService.mergeObjAttrValue(attrValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (jsonBody.containsKey("energy")) {
|
|
|
+ EmsObjAttrValue attrValue = new EmsObjAttrValue(device.getDeviceCode(), objType, device.getDeviceModel(),
|
|
|
+ "energy", jsonBody.getString("energy"));
|
|
|
+ objAttrValueService.mergeObjAttrValue(attrValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean checkResult(AbilityPayload abilityParam, MqttCacheMsg cacheMsg) {
|
|
|
+ boolean flag = true;
|
|
|
+
|
|
|
+ try {
|
|
|
+ String sendParam = abilityParam.getAbilityParam();
|
|
|
+ String receiveParam = cacheMsg.getPayload();
|
|
|
+
|
|
|
+ JSONObject sendObject = JSONObject.parseObject(sendParam);
|
|
|
+ JSONObject receiveObject = JSONObject.parseObject(receiveParam);
|
|
|
+
|
|
|
+ String type = sendObject.getString("type");
|
|
|
+
|
|
|
+ if (StringUtils.equals(type, "event")) {
|
|
|
+ int sendKeyValue = sendObject.getIntValue("key");
|
|
|
+ int receiveKeyValue = receiveObject.getIntValue("key");
|
|
|
+ Assert.isTrue(sendKeyValue == receiveKeyValue, -1, "响应参数key校验失败!");
|
|
|
+ }
|
|
|
+ else if (StringUtils.equals(type, "setting")) {
|
|
|
+ if (sendObject.containsKey("keyLock")) {
|
|
|
+ int sendKeyLockValue = sendObject.getIntValue("keyLock");
|
|
|
+ int receiveKeyLockValue = receiveObject.getIntValue("keyLock");
|
|
|
+ Assert.isTrue(sendKeyLockValue == receiveKeyLockValue, -1, "响应参数keyLock校验失败!");
|
|
|
+ }
|
|
|
+ else if (sendObject.containsKey("timerEnable") && sendObject.containsKey("timerInterval")) {
|
|
|
+ int sendTimerEnableValue = sendObject.getIntValue("timerEnable");
|
|
|
+ int receiveTimerEnableValue = receiveObject.getIntValue("timerEnable");
|
|
|
+ Assert.isTrue(sendTimerEnableValue == receiveTimerEnableValue, -1, "响应参数timerEnable校验失败!");
|
|
|
+ int sendTimerIntervalValue = sendObject.getIntValue("keyLock");
|
|
|
+ int receiveTimerIntervalValue = receiveObject.getIntValue("timerInterval");
|
|
|
+ Assert.isTrue(sendTimerIntervalValue == receiveTimerIntervalValue, -1,
|
|
|
+ "响应参数timerInterval校验失败!");
|
|
|
+ }
|
|
|
+ else if (sendObject.containsKey("onState")) {
|
|
|
+ int sendOnStateValue = sendObject.getIntValue("onState");
|
|
|
+ int receiveOnStateValue = receiveObject.getIntValue("onState");
|
|
|
+ Assert.isTrue(sendOnStateValue == receiveOnStateValue, -1, "响应参数onState校验失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (BusinessException e) {
|
|
|
+ log.info(e.getMessage());
|
|
|
+ flag = false;
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ flag = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+ private EmsObjAbilityCallLog saveLog(AbilityPayload abilityParam, long sendTime, int callStatus) {
|
|
|
+ EmsObjAbilityCallLog objAbilityCallLog = new EmsObjAbilityCallLog();
|
|
|
+ objAbilityCallLog.setObjCode(abilityParam.getObjCode());
|
|
|
+ objAbilityCallLog.setObjType(abilityParam.getObjType());
|
|
|
+ objAbilityCallLog.setModelCode(abilityParam.getModeCode());
|
|
|
+ objAbilityCallLog.setAbilityKey(abilityParam.getAbilityKey());
|
|
|
+ objAbilityCallLog.setCallTime(new Date(sendTime));
|
|
|
+ objAbilityCallLog.setCallParam(abilityParam.getAbilityParam());
|
|
|
+ objAbilityCallLog.setCallStatus(callStatus);
|
|
|
+ objAbilityCallLogService.addLog(objAbilityCallLog);
|
|
|
+ return objAbilityCallLog;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateLog(EmsObjAbilityCallLog objAbilityCallLog, MqttCacheMsg cacheMsg, int callStatus) {
|
|
|
+ objAbilityCallLog.setResTime(cacheMsg.getReceiveTime());
|
|
|
+ objAbilityCallLog.setResParam(cacheMsg.getPayload());
|
|
|
+ objAbilityCallLog.setCallStatus(callStatus);
|
|
|
+ objAbilityCallLogService.updateLog(objAbilityCallLog);
|
|
|
+ }
|
|
|
+}
|