Browse Source

气象站注册

crazycat 2 năm trước cách đây
mục cha
commit
6289cfde44

+ 1 - 0
src/main/java/com/welampiot/common/InterfaceResultEnum.java

@@ -6,6 +6,7 @@ public enum InterfaceResultEnum {
     PARAM_FAIL("0001","参数异常","Parameter abnormality","Параметры аномалии"),
     TOKEN_FAIL("0004","登录信息已过期,请重新登录","Parameter abnormality","Параметры аномалии"),
     LACK_PARAM_ERROR("0007","缺少必要参数","",""),
+    DEVICE_NO_SUPPORT("0018","设备不支持","",""),
     USERNAME_PASSWORD_PARAM_ERROR("0101","用户名或密码错误","",""),
     SEND_CMD_TIME_OUT("0014","下发命令超时,请稍后重试!","",""),
     LACK_LAMP_NUMBER_ERROR("0201","请填写灯控编号","",""),

+ 161 - 0
src/main/java/com/welampiot/controller/WeatherController.java

@@ -1,9 +1,14 @@
 package com.welampiot.controller;
 
 import com.welampiot.common.BaseResult;
+import com.welampiot.common.DevInfoEnum;
 import com.welampiot.common.InterfaceResultEnum;
 import com.welampiot.dto.EnvmonitorDTO;
+import com.welampiot.dto.WeatherRS485DevInfoDTO;
+import com.welampiot.dto.WifiDTO;
 import com.welampiot.service.EnvmonitorService;
+import com.welampiot.service.WeatherRS485DevInfoService;
+import com.welampiot.service.WifiService;
 import com.welampiot.utils.ToolUtils;
 import com.welampiot.vo.EnvmonitorVO;
 import org.springframework.beans.BeanUtils;
@@ -14,6 +19,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.servlet.http.HttpServletRequest;
+import java.util.List;
 
 /**
  * ClassName: WeatherController
@@ -32,6 +38,10 @@ public class WeatherController {
     private ToolUtils toolUtils;
     @Autowired
     private EnvmonitorService envmonitorService;
+    @Autowired
+    private WifiService wifiService;
+    @Autowired
+    private WeatherRS485DevInfoService weatherRS485DevInfoService;
 
     /**
      * 设置plc气象站开、关灯时间
@@ -78,4 +88,155 @@ public class WeatherController {
         BeanUtils.copyProperties(envmonitorDTO,envmonitorVO);
         return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version,envmonitorVO);
     }
+
+
+    /**
+     * 气象站注册
+     * @param request 气象站id
+     * @return
+     */
+    @RequestMapping(value = "/registerDev", method = RequestMethod.POST)
+    public BaseResult<?> registerDev(HttpServletRequest request) {
+        Integer version = (Integer) toolUtils.getRequestContent(request,"version",1);
+        Integer id = (Integer) toolUtils.getRequestContent(request,"id",1);
+        if (id == 0) return toolUtils.response(InterfaceResultEnum.LACK_PARAM_ERROR,version);
+        EnvmonitorDTO envmonitorDTO = envmonitorService.getEnvmonitorMpDataById(id);
+        Integer lampPoleId = envmonitorDTO.getLampPoleId();
+        WifiDTO wifiDTO = new WifiDTO();
+        wifiDTO.setLampPoleId(lampPoleId);
+        WifiDTO detailByDTO = wifiService.getDetailByDTO(wifiDTO);
+        Integer model = detailByDTO.getModel();
+        String top = "";
+        String chuanKou = "";
+        String ra = "";
+        if (model == 0){
+            top = "WEGW";
+        } else if (model == 1 || model == 5) {
+            top = "WEGW2";
+        }else {
+            top = "WEGW3";
+
+            String s = "00" + envmonitorDTO.getSerialPort();
+            chuanKou = s.substring(s.length()-2);
+            System.out.println(chuanKou);
+        }
+
+        if (envmonitorDTO.getDevType() == 0){
+            DevInfoEnum[] values = DevInfoEnum.values();
+            String s1 = "00000000" + Integer.toHexString(9600);
+            if (model != 0) ra = s1.substring(s1.length()-8);
+            List<WeatherRS485DevInfoDTO> listByWeatherId = weatherRS485DevInfoService.getListByWeatherId(envmonitorDTO.getId());
+            for (WeatherRS485DevInfoDTO dto :listByWeatherId) {
+                String cmd = "";
+                String s2 = "0" + Integer.toHexString(dto.getAddress());
+                String address = s2.substring(s2.length()-2);
+                for (DevInfoEnum e :values) {
+                    if (e.getId() == dto.getDevId()){
+                        cmd = e.getCmd();
+                        break;
+                    }
+                }
+                cmd = address+cmd;
+                cmd += toolUtils.getCRC(cmd);
+                String cmdT = cmd;
+
+                cmd = ra + cmd;
+                String s3 = "0" + Integer.toHexString(cmd.length() / 2);
+                String len = s3.substring(s3.length()-2);
+                String sendData = chuanKou+address+len+cmd;
+                String sendTopic = "/"+top+"/WriteIn/"+detailByDTO.getNum();
+                String backTopic = "/"+top+"/WriteOut/"+detailByDTO.getNum();
+                sendData = "11{\"D2\":\""+sendData+"\"}";
+                String s = toolUtils.sendMqttCmd(sendTopic, sendData, backTopic,10);
+                if (s != null){
+                    sendTopic = "/"+top+"/TransIn/"+detailByDTO.getNum();
+                    backTopic = "/"+top+"/TransOut/"+detailByDTO.getNum();
+                    sendData = "0101"+chuanKou+cmdT;
+                    s = toolUtils.sendMqttCmd(sendTopic, toolUtils.hexString2Bytes(sendData), backTopic,10);
+                    if (s != null){
+                        String string = "02000000000000"+chuanKou+toolUtils.bytes2HexString(s.getBytes()).substring(4);
+                        sendTopic = "/"+top+"/DataReport/"+detailByDTO.getNum();
+                        backTopic = "/"+top+"/DataReport/"+detailByDTO.getNum();
+                        toolUtils.sendMqttCmd(sendTopic, toolUtils.hexString2Bytes(string), backTopic,0);
+                    }
+                }else {
+                    return toolUtils.response(InterfaceResultEnum.SEND_CMD_TIME_OUT,version);
+                }
+            }
+        }else {
+            Integer rate = envmonitorDTO.getRate();
+
+            if (model != 0) {
+                String s = "";
+                if (rate == 0){
+                    s = "00000000" + Integer.toHexString(1200);
+                } else if (rate == 1) {
+                    s = "00000000" + Integer.toHexString(2400);
+                } else if (rate == 2) {
+                    s = "00000000" + Integer.toHexString(4800);
+                } else if (rate == 3) {
+                    s = "00000000" + Integer.toHexString(9600);
+                } else if (rate == 4) {
+                    s = "00000000" + Integer.toHexString(19200);
+                } else if (rate == 5) {
+                    s = "00000000" + Integer.toHexString(38400);
+                } else if (rate == 6) {
+                    s = "00000000" + Integer.toHexString(38400);
+                }
+                if (ra.length() == 0) ra = "00000000"+Integer.toHexString(9600);
+                ra = s.substring(s.length()-8);
+            }
+            String cmd = "";
+            String address = "";
+            if (envmonitorDTO.getDevType() == 1){
+                cmd = "010301f4000c";
+                address = "01";
+            }else if (envmonitorDTO.getDevType() == 2){
+                cmd = "010301f40008";
+                address = "01";
+            }else if (envmonitorDTO.getDevType() == 3){
+                cmd = "ff0300010012";
+                address = "ff";
+            }else if (envmonitorDTO.getDevType() == 5){
+                cmd = "300300000010";
+                address = "30";
+            }else if (envmonitorDTO.getDevType() == 7){
+                cmd = "01030000000E";
+                address = "01";
+            }else if (envmonitorDTO.getDevType() == 9){
+                return toolUtils.response(InterfaceResultEnum.DEVICE_NO_SUPPORT,version);
+            }else{
+                cmd = "010301f4000c";
+                address = "01";
+            }
+            cmd += toolUtils.getCRC(cmd);
+            String cmdT = cmd;
+            cmd = ra+cmd;
+            String s1 = "0" + Integer.toHexString(cmd.length() / 2);
+            String len = s1.substring(s1.length()-2);
+
+            String sendData = chuanKou+address+len+cmd;
+            String sendTopic = "/"+top+"/WriteIn/"+detailByDTO.getNum();
+            String backTopic = "/"+top+"/WriteOut/"+detailByDTO.getNum();
+            sendData = "01{\"D2\":\""+sendData+"\"}";
+            String s = toolUtils.sendMqttCmd(sendTopic, sendData, backTopic);
+            if (s != null){
+                sendTopic = "/"+top+"/TransIn/"+detailByDTO.getNum();
+                backTopic = "/"+top+"/TransOut/"+detailByDTO.getNum();
+                sendData = "0101"+chuanKou+cmdT;
+
+                s = toolUtils.sendMqttCmd(sendTopic, toolUtils.hexString2Bytes(sendData), backTopic);
+                if (s != null){
+                    String string = "02000000000000"+chuanKou+toolUtils.bytes2HexString(s.getBytes()).substring(4);
+                    System.out.println(string);
+                    sendTopic = "/"+top+"/DataReport/"+detailByDTO.getNum();
+                    backTopic = "/"+top+"/DataReport/"+detailByDTO.getNum();
+                    toolUtils.sendMqttCmd(sendTopic, toolUtils.hexString2Bytes(string), backTopic,0);
+                }
+            }else {
+                return toolUtils.response(InterfaceResultEnum.SEND_CMD_TIME_OUT,version);
+            }
+        }
+        return toolUtils.response(InterfaceResultEnum.OPERATION_SUCCESS,version);
+    }
 }

+ 4 - 0
src/main/java/com/welampiot/dao/WeatherRS485DevInfoDao.java

@@ -1,6 +1,9 @@
 package com.welampiot.dao;
 
 import com.welampiot.dto.WeatherRS485DevInfoDTO;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.List;
 
 /**
  * ClassName: WeatherRS485DevInfoDao
@@ -13,4 +16,5 @@ import com.welampiot.dto.WeatherRS485DevInfoDTO;
  */
 public interface WeatherRS485DevInfoDao {
     void addWeatherRS485DevInfoData(WeatherRS485DevInfoDTO dto);
+    List<WeatherRS485DevInfoDTO> getListByWeatherId(@Param("weatherId") int weatherId);
 }

+ 3 - 0
src/main/java/com/welampiot/service/WeatherRS485DevInfoService.java

@@ -2,6 +2,8 @@ package com.welampiot.service;
 
 import com.welampiot.dto.WeatherRS485DevInfoDTO;
 
+import java.util.List;
+
 /**
  * ClassName: WeatherRS485DevInfoService
  * Package: com.welampiot.service
@@ -13,4 +15,5 @@ import com.welampiot.dto.WeatherRS485DevInfoDTO;
  */
 public interface WeatherRS485DevInfoService {
     void addWeatherRS485DevInfoData(WeatherRS485DevInfoDTO dto);
+    List<WeatherRS485DevInfoDTO> getListByWeatherId(int weatherId);
 }

+ 7 - 0
src/main/java/com/welampiot/service/impl/WeatherRS485DevInfoServiceImpl.java

@@ -6,6 +6,8 @@ import com.welampiot.service.WeatherRS485DevInfoService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.List;
+
 /**
  * ClassName: WeatherRS485DevInfoServiceImpl
  * Package: com.welampiot.service.impl
@@ -24,4 +26,9 @@ public class WeatherRS485DevInfoServiceImpl implements WeatherRS485DevInfoServic
     public void addWeatherRS485DevInfoData(WeatherRS485DevInfoDTO dto) {
         weatherRS485DevInfoDao.addWeatherRS485DevInfoData(dto);
     }
+
+    @Override
+    public List<WeatherRS485DevInfoDTO> getListByWeatherId(int weatherId) {
+        return weatherRS485DevInfoDao.getListByWeatherId(weatherId);
+    }
 }

+ 30 - 1
src/main/java/com/welampiot/utils/ToolUtils.java

@@ -325,7 +325,7 @@ public class ToolUtils {
      * @return 16进制字符串
      * @throws
      */
-    public static String bytes2HexString(byte[] b) {
+    public String bytes2HexString(byte[] b) {
         StringBuffer result = new StringBuffer();
         String hex;
         for (int i = 0; i < b.length; i++) {
@@ -398,6 +398,35 @@ public class ToolUtils {
         // 交换高低位,低位在前高位在后
         return result.substring(2, 4)+result.substring(0, 2);
     }
+    public String getCRC(String cmd) {
+        byte[] bytes = this.hexString2Bytes(cmd);
+        // CRC寄存器全为1
+        int CRC = 0x0000ffff;
+        // 多项式校验值
+        int POLYNOMIAL = 0x0000a001;
+        int i, j;
+        for (i = 0; i < bytes.length; i++) {
+            CRC ^= ((int) bytes[i] & 0x000000ff);
+            for (j = 0; j < 8; j++) {
+                if ((CRC & 0x00000001) != 0) {
+                    CRC >>= 1;
+                    CRC ^= POLYNOMIAL;
+                } else {
+                    CRC >>= 1;
+                }
+            }
+        }
+        // 结果转换为16进制
+        String result = Integer.toHexString(CRC).toUpperCase();
+        if (result.length() != 4) {
+            StringBuffer sb = new StringBuffer("0000");
+            result = sb.replace(4 - result.length(), 4, result).toString();
+        }
+        //高位在前地位在后
+        //return result.substring(2, 4) + " " + result.substring(0, 2);
+        // 交换高低位,低位在前高位在后
+        return result.substring(2, 4)+result.substring(0, 2);
+    }
 
     public Object getRequestContent(HttpServletRequest request,String key){
         return request.getParameter(key);

+ 4 - 1
src/main/resources/mapper/WeatherRS485DevInfoMapper.xml

@@ -6,5 +6,8 @@
         insert into weather_rs485_dev_info(address,devId)
         values (#{address},#{devId})
     </insert>
-
+    
+    <select id="getListByWeatherId" parameterType="int" resultType="com.welampiot.dto.WeatherRS485DevInfoDTO">
+        select address,devId,rate from weather_rs485_dev_info where weatherId = #{weatherId}
+    </select>
 </mapper>