فهرست منبع

新增 润浩水肥机对接

zhaiyifei 8 ماه پیش
والد
کامیت
28db4a1d65

+ 6 - 0
src/main/java/com/yunfeiyun/agmp/iots/device/service/IRunHaoSfDevice.java

@@ -0,0 +1,6 @@
+package com.yunfeiyun.agmp.iots.device.service;
+
+import com.yunfeiyun.agmp.iots.device.common.Device;
+
+/** 润浩水肥机 */
+public interface IRunHaoSfDevice extends Device {}

+ 296 - 0
src/main/java/com/yunfeiyun/agmp/iots/device/serviceImp/RunHaoSfDeviceImpl.java

@@ -0,0 +1,296 @@
+package com.yunfeiyun.agmp.iots.device.serviceImp;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.yunfeiyun.agmp.common.utils.DateUtils;
+import com.yunfeiyun.agmp.common.utils.StringUtils;
+import com.yunfeiyun.agmp.iot.common.constant.devicetype.ServiceNameConst;
+import com.yunfeiyun.agmp.iot.common.domain.IotDevice;
+import com.yunfeiyun.agmp.iot.common.model.cmd.CmdModel;
+import com.yunfeiyun.agmp.iots.core.manager.MqttManager;
+import com.yunfeiyun.agmp.iots.device.common.DeviceAbstractImpl;
+import com.yunfeiyun.agmp.iots.device.service.IRunHaoSfDevice;
+import com.yunfeiyun.agmp.iots.service.*;
+import com.yunfeiyun.agmp.iots.service.impl.IotDeviceAddressService;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.http.util.TextUtils;
+import org.eclipse.paho.client.mqttv3.MqttException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/** 润浩水肥机 */
+@Component(ServiceNameConst.SERVICE_RUNHAO_SF)
+@Slf4j
+public class RunHaoSfDeviceImpl extends DeviceAbstractImpl implements IRunHaoSfDevice {
+
+    @Autowired
+    private MqttManager mqttManager;
+
+    @Autowired
+    private IIotDeviceService iIotDeviceService;
+
+    @Autowired
+    private IIotDeviceconfigService iIotDeviceconfigService;
+
+    @Autowired
+    private IIotDevicelasteddataService iIotDevicelasteddataService;
+    @Autowired
+    private IIotYfScddataService iIotYfScddataService;
+
+    @Autowired
+    private IIotCmdlogService iIotCmdlogService;
+
+    @Autowired
+    private IotDeviceAddressService iotDeviceAddressService;
+
+
+    private void publish(IotDevice iotDevice, String mqttMsgContent) {
+//        String devCode = iotDevice.getDevCode();
+//        List<String> topicList = new ArrayList<>();
+//        topicList.add(IotMqttConstant.YFScdTopic.TOPIC_SCD_CMD_PREFIX + devCode);
+//        topicList.add(IotMqttConstant.YFScdTopic.TOPIC_SCD_2_CMD_PREFIX + devCode);
+//        if(Objects.equals(iotDevice.getDevtypeBid(), IotDeviceDictConst.TYPE_YF_FXSSCD)){
+//            //新款风吸式杀虫灯
+//            topicList.add(IotMqttConstant.YFScdTopic.TOPIC_FXSSCD_CMD_PREFIX + devCode);
+//        }
+//        for(String topic:topicList){
+//            try{
+//                mqttManager.publishMsg(iotDevice.getDevconnBid(), topic, mqttMsgContent);
+//                log.info("【YFSCD】发送指令完毕!connectionId:{},topic :{} mqttMsgContent: {}",iotDevice.getDevconnBid(),topic, mqttMsgContent);
+//            }catch (Exception e){
+//                log.error("【YFSCD】发送指令失败!connectionId:{},topic :{} mqttMsgContent: {}",iotDevice.getDevconnBid(),topic, mqttMsgContent);
+//            }
+//        }
+    }
+
+    /**
+     * 下发指令
+     *
+     * @param cmdModel
+     * @return
+     */
+    @Override
+    public Object sendCmd(CmdModel cmdModel) throws Exception {
+        log.info("【杀虫灯】收到指令 任务 cmdModel={}", cmdModel);
+
+//        // 获取执行的指令
+//        CmdModel.Cmd cmdDistribution = cmdModel.getCmdDistribution();
+//        // 获取执行的方法 ,方法可以通过反射获取执行,也可以临时case 匹配
+//        String methodName = cmdModel.getCmdDistribution().getFunc();
+//
+//        String mqttMsgContent = "";
+//        String clogSendresult = "发送指令成功";
+//        switch (methodName) {
+//            case CmdDef.YfScdCmdDef.CMD_CONFIG:{
+//                JSONObject jobjParam = cmdDistribution.getJsons();
+//                Map<String, String> keyMap = new HashMap<>();
+//                keyMap.put("ds", "ds");
+//                keyMap.put("tt", "tt");
+//                keyMap.put("dat_f", "dattim");
+//                keyMap.put("ts", "ts");
+//                keyMap.put("st", "st");
+//                keyMap.put("et", "et");
+//
+//                JSONObject configObj = new JSONObject();
+//                for(Map.Entry<String, String> entry : keyMap.entrySet()) {
+//                    String key = entry.getKey();
+//                    String keyName = entry.getValue();
+//
+//                    if (jobjParam.containsKey(key)) {
+//                        Object value = jobjParam.get(key);
+//                        if (value != null) {
+//                            configObj.put(keyName, value);
+//                        }
+//                    }
+//                }
+//                JSONObject devConfig = new JSONObject();
+//                devConfig.put("cmd", "paramconf");
+//                devConfig.put("ext", configObj);
+//                mqttMsgContent = devConfig.toString();
+//                log.info("【杀虫灯】发送指令【config】 mqttMsgContent={}", mqttMsgContent);
+//                break;
+//            }
+//            case CmdDef.YfScdCmdDef.CMD_REFRESH:{
+//                JSONObject jobjParam = cmdDistribution.getJsons();
+//                mqttMsgContent = JSONUtils.toJSONString(jobjParam);
+//                log.info("【杀虫灯】发送指令【refresh】 mqttMsgContent={}", mqttMsgContent);
+//                break;
+//            }
+//            case CmdDef.YfScdCmdDef.CMD_COMMON:{
+//                JSONObject jobjParam = cmdDistribution.getJsons();
+//                mqttMsgContent = JSONUtils.toJSONString(jobjParam);
+//                log.info("【杀虫灯】发送指令【report】 mqttMsgContent={}", mqttMsgContent);
+//                break;
+//            }
+//        }
+//
+//        if(StringUtils.isNotEmpty(mqttMsgContent)){
+//            IotDevice iotDevice= iIotDeviceService.selectIotDeviceByDevBid(cmdModel.getIotDevice().getDevBid());
+//            publish(iotDevice, mqttMsgContent);
+//        }
+//
+//        cmdModel.setClogSendresult(clogSendresult);
+//        cmdModel.setClogDesc(mqttMsgContent);
+//
+//        iIotCmdlogService.insertSuccessCmdlog(cmdModel);
+        return null;
+    }
+
+    public Object cmdData(JSONObject dataJson, String topic, String connectionId, String devUpdateddate) throws Exception {
+        log.info("杀虫灯数据解析 {},topic:{}", dataJson.toString(),topic);
+
+//        IotDevice oldIotDevice = findIotDevice(topic, dataJson, connectionId);
+//        if (oldIotDevice == null) {
+//            log.error("未取到 iotDevice");
+//            return null;
+//        }
+//
+//        IotDevice iotDevice = new IotDevice();
+//        iotDevice.setTid(oldIotDevice.getTid());
+//        iotDevice.setDevtypeBid(oldIotDevice.getDevtypeBid());
+//        iotDevice.setDevBid(oldIotDevice.getDevBid());
+//        iotDevice.setDevUpdateddate(devUpdateddate);
+//        iotDevice.setDevStatus("1");//设备上线
+//        iotDevice.setDevOriginalStatus(oldIotDevice.getDevOriginalStatus());
+//        HashMap<String, String> keyMaps = new HashMap<>();
+//        keyMaps.put("dver", "devVersion");
+//        keyMaps.put("imei", "devCode");
+//        keyMaps.put("gps", "devPositiontype");
+//
+//        for (Map.Entry<String, String> entry : keyMaps.entrySet()) {
+//            String k = entry.getValue();
+//            String v = dataJson.getString(entry.getKey());
+//            if (StringUtils.isNotEmpty(v)) {
+//                ReflectUtils.invokeSetter(iotDevice, k, v);
+//            }
+//        }
+//        String lng = dataJson.getString("lng");
+//        String lat = dataJson.getString("lat");
+//        if (!Objects.equals(oldIotDevice.getDevPositionstatus(), "0") && StringUtils.isNotEmpty(lng) && StringUtils.isNotEmpty(lat)) {
+//            iotDeviceAddressService.setDeviceAddress(iotDevice, lng, lat);
+//        }
+//
+//        String[] keyArrays = {
+//                "ds",
+//                "tt",
+//                "dat_f",
+//                "clt_t",
+//                "ts",
+//                "st",
+//                "et",
+//                "stm8vs"
+//        };
+//
+//        JSONObject extConf = new JSONObject();
+//        for (String k : keyArrays) {
+//            String v = "0";
+//            if (dataJson.containsKey(k)) {
+//                v = dataJson.getString(k);
+//            }
+//            extConf.put(k, v);
+//        }
+//        extConf.put("dver", iotDevice.getDevVersion());
+//        String devConfig = JSONUtils.toJSONString(extConf);
+//
+//        // 更新设备基础信息数据库 mysql
+//        iIotDeviceService.updateIotDevice(iotDevice);
+//        // 创建或更新设备配置信息
+//        if (StringUtils.isNotEmpty(devConfig)) {
+//            iIotDeviceconfigService.createOrUpdateDevConfig(oldIotDevice, devConfig, iotDevice.getDevUpdateddate());
+//        }
+//
+//        // 更新设备数据到mongodb
+//        iIotYfScddataService.insertScddata(iotDevice, dataJson);
+//
+//        // 保存 设备最新数据 到redis
+//        iIotDevicelasteddataService.updateDeviceLastedData(oldIotDevice, String.valueOf(dataJson), devUpdateddate);
+        return null;
+    }
+
+    public void cmdOffline(JSONObject dataJson, String topic, String connectionId) throws MqttException {
+        log.debug("杀虫灯离线数据 {}", dataJson.toString());
+//        IotDevice oldIotDevice = findIotDevice(topic, dataJson, connectionId);
+//        if (oldIotDevice == null) {
+//            log.error("未取到 iotDevice");
+//            return;
+//        }
+//
+//        IotDevice newIotDevice = new IotDevice();
+//        newIotDevice.setDevBid(oldIotDevice.getDevBid());
+//        newIotDevice.setDevStatus("0");
+//        newIotDevice.setDevOfflinedate(DateUtils.dateTimeNow());
+//        newIotDevice.setTid(oldIotDevice.getTid());
+//        newIotDevice.setDevtypeBid(oldIotDevice.getDevtypeBid());
+//        newIotDevice.setDevCreateddate(oldIotDevice.getDevCreateddate());
+//        newIotDevice.setDevUpdateddate(oldIotDevice.getDevUpdateddate());
+//        newIotDevice.setDevCode(oldIotDevice.getDevCode());
+//        newIotDevice.setDevOriginalStatus(oldIotDevice.getDevOriginalStatus());
+//        iIotDeviceService.updateIotDevice(newIotDevice);
+//
+//
+//        //发送离线预警
+//        SpringUtils.getBean(WarnService.class).processWarningOfflineData(newIotDevice, dataJson);
+//
+//        /**
+//         * 下发刷新指令,检测设备是否真离线
+//         */
+//        JSONObject payload = new JSONObject();
+//        payload.put("cmd", "read");
+//        payload.put("ext", "data");
+//        String mqttMsgContent = JSONUtils.toJSONString(payload);
+//        publish(oldIotDevice, mqttMsgContent);
+//
+//        log.info("[杀虫灯] 下发刷新指令,检测设备是否真离线: " + oldIotDevice.getDevCode());
+    }
+
+    /**
+     * 接收上报数据
+     *
+     * @param topic
+     * @param dataJson
+     * @return
+     */
+    @Override
+    public Object receiveData(String topic, JSONObject dataJson,String connectionId) throws Exception {
+        log.info("杀虫灯实现类  处理收到的 设备上报数据 " + dataJson.toString());
+        // 接收设备上报数据后的处理逻辑
+        String cmd = dataJson.getString("cmd");
+        if (TextUtils.isEmpty(cmd)) {
+            log.error("未取到cmd");
+            return false;
+        }
+        JSONObject ext = dataJson.getJSONObject("ext");
+        String devUpdateddate = dataJson.getString("devUpdateddate");
+        if(StringUtils.isEmpty(devUpdateddate)){
+            devUpdateddate= DateUtils.dateTimeNow();
+        }
+        if (ext == null) {
+            log.error("未取到ext");
+            return false;
+        }
+
+        if ("data".equals(cmd)) {
+            this.cmdData(ext, topic, connectionId, devUpdateddate);
+        } else if ("offline".equals(cmd)) {
+            this.cmdOffline(ext, topic, connectionId);
+        }
+        return null;
+    }
+
+    @Override
+    public boolean isDeviceProps(JSONObject jobjMsg) {
+        return "data".equals(jobjMsg.getString("cmd"));
+    }
+
+    /**
+     * 根据topic、设备发来的消息,查询对应设备实体
+     *
+     * @param topic
+     * @param jobjMsg
+     * @return
+     */
+    @Override
+    public IotDevice findIotDevice(String topic, JSONObject jobjMsg,String connectionId) {
+        String devId = mqttManager.getDevIdByTopic(connectionId,topic);
+        return iIotDeviceService.selectIotDeviceByDevBid(devId);
+    }
+}