| | |
| | | |
| | | TCP_FZZY_V3("TCP_FZZY_V3", "粮情-风正致远控制柜协议V3"), |
| | | GRAIN_FZZY_IGDS_V40("GRAIN_FZZY_IGDS_V40", "粮情-风正致远系统V40"), |
| | | GRAIN_FZZY_BHZN_WEB("GRAIN_FZZY_BHZN_WEB", "粮情-FZZY-邦海智能网口协议"), |
| | | GRAIN_FZZY_ZLDZ_WEB("GRAIN_FZZY_ZLDZ_WEB", "粮情-FZZY-正来电子网口协议"), |
| | | GRAIN_FZZY_BHZN_WEB("GRAIN_FZZY_BHZN_WEB", "粮情-FZZY-BHZN网口协议"), |
| | | GRAIN_FZZY_ZLDZ_WEB("GRAIN_FZZY_ZLDZ_WEB", "粮情-FZZY-ZLDZ网口协议"), |
| | | GRAIN_YOUXIAN0_2023("GRAIN_YOUXIAN0_2023", "粮情-游仙主库协议"), |
| | | GRAIN_YOUXIAN1_2023("GRAIN_YOUXIAN1_2023", "粮情-游仙分库协议"), |
| | | DEVICE_WEIGHT_HTTP("DEVICE_WEIGHT_HTTP", "地磅-HTTP协议"), |
| | |
| | | |
| | | THDto thDto = this.getTH(message); |
| | | if (null != thDto) { |
| | | log.debug("-------THDto--={}", thDto); |
| | | log.info("-------THDto--={}", thDto); |
| | | } |
| | | //清空 |
| | | String key = "GRAIN_" + message.getAddr(); |
| | |
| | | try { |
| | | THDto th = new THDto(); |
| | | String data = message.getContent(); |
| | | String houseNo = data.substring(0, 4); |
| | | int depotId = BytesUtil.hexToInt(BytesUtil.tran_LH(houseNo)); |
| | | String houseNo = data.substring(0, 2); |
| | | int depotId = BytesUtil.hexToInt(houseNo); |
| | | String t = data.substring(4, 8); |
| | | String h = data.substring(8, 12); |
| | | double humy; |
| | |
| | | if (ServiceUtils.FUNCTION_66.equalsIgnoreCase(funId)) { |
| | | |
| | | log.info("---------开始解析粮情信息---------"); |
| | | this.analysisGrainStep1(device, msgId, strMsg); |
| | | try{ |
| | | this.analysisGrainStep1(device, msgId, strMsg); |
| | | }catch (Exception e){ |
| | | log.error(e.getMessage(),e); |
| | | } |
| | | |
| | | log.info("---------解析粮情信息结束---------"); |
| | | } |
| | | |
| | | //温湿度返回 |
| | | if (ServiceUtils.FUNCTION_68.equalsIgnoreCase(funId)) { |
| | | log.info("---------开始解析仓温湿度信息---------"); |
| | | this.analysisGrainTh(device, strMsg); |
| | | try{ |
| | | this.analysisGrainTh(device, strMsg); |
| | | }catch (Exception e){ |
| | | log.error(e.getMessage(),e); |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | private void analysisGrainTh(GatewayDevice device, String strMsg) { |
| | | THDto th = new THDto(); |
| | | try{ |
| | | THDto th = new THDto(); |
| | | |
| | | //TODO----->>> 待解析调整,先用外部气象信息 |
| | | //系统气象站信息 |
| | | WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); |
| | | th.setTempIn(Double.valueOf(weather.getTem()) - 1); |
| | | th.setHumidityIn(Double.valueOf(weather.getHumidity()) - 1); |
| | | //TODO----->>> 待解析调整,先用外部气象信息 |
| | | //7E 00 01 01 00 06 00 00 A0 FF FF 68 1A 05 CC 16 3A 62 36 7E |
| | | //系统气象站信息 |
| | | // WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); |
| | | // th.setTempIn(Double.valueOf(weather.getTem()) - 2); |
| | | // th.setHumidityIn(Double.valueOf(weather.getHumidity()) - 10); |
| | | double t,h; |
| | | String temp = strMsg.substring(30,32); |
| | | t = BytesUtil.hexToInt(temp)/2; |
| | | log.info("温度:{}",t); |
| | | temp = strMsg.substring(32,34); |
| | | h = BytesUtil.hexToInt(temp); |
| | | log.info("湿度:{}",h); |
| | | th.setTempIn(t); |
| | | th.setHumidityIn(h); |
| | | this.add2ThMap(device.getDepotIdSys(), th); |
| | | }catch (Exception e){ |
| | | log.error(e.getMessage(),e); |
| | | } |
| | | |
| | | this.add2ThMap(device.getDepotIdSys(), th); |
| | | } |
| | | |
| | | |
| | |
| | | * @param msgId 命令ID |
| | | */ |
| | | private void analysisGrainStep1(GatewayDevice device, int msgId, String strMsg) { |
| | | |
| | | String[] attCable = device.getCableRule().split("-"); |
| | | int cableZ = Integer.valueOf(attCable[0]); |
| | | int cableY = Integer.valueOf(attCable[1]); |
| | | int cableX = Integer.valueOf(attCable[2]); |
| | | log.info("z={},x={},y={}",cableZ,cableX,cableY); |
| | | //获取请求信息 |
| | | BaseReqData reqData = ProtocolUtils.getSyncReq(device.getDepotIdSys()); |
| | | if (null == reqData) { |
| | | log.error("---------没有获取到请求信息,不执行解析------{}", device.getDeviceName()); |
| | | return; |
| | | } |
| | | |
| | | //只保留粮情信息 |
| | | int start = 22 * 2; |
| | | int start = 15 * 2; |
| | | strMsg = strMsg.substring(start); |
| | | |
| | | |
| | | //密钥和点数 |
| | | String kyeNumHex = strMsg.substring(2, 4); |
| | | String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt(kyeNumHex)); |
| | | |
| | | String key = "00000" + kyeNumBin.substring(0, 3); |
| | | int keyValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key)); |
| | | key = "0000" + kyeNumBin.substring(4); |
| | | int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key)); |
| | | |
| | | //02 A4 BB BA BA B4 |
| | | start = 2 * 2; |
| | | String tempHex; |
| | | GrainRoot grainRoot = new GrainRoot(); |
| | | grainRoot.setKey(buildGrainRootKey(device.getDeviceSn(), msgId)); |
| | | grainRoot.setNum(msgId); |
| | | double point = 0; |
| | | for (int i = 0; i < numValue; i++) { |
| | | start = start + i * 2; |
| | | tempHex = strMsg.substring(start, start + 2); |
| | | //实际温度=密钥*密钥*37(溢出为无符号字节)再异或加密后的温度/2。 |
| | | point = this.getGrainTemp(keyValue, tempHex); |
| | | log.debug("--------解析后的温度点----{}---{}", tempHex, point); |
| | | grainRoot.getPoints().add(point); |
| | | start = 2 * 2; |
| | | } |
| | | |
| | | String tempStr = ""; |
| | | for (int j = 0;j<cableY;j++){ |
| | | tempStr = strMsg.substring(12 * j,12 * j +12); |
| | | //密钥和点数 02 A4 BB BA BA B4 |
| | | String kyeNumHex = tempStr.substring(2, 4); |
| | | String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt(kyeNumHex)); |
| | | |
| | | String key = "00000" + kyeNumBin.substring(0, 3); |
| | | int keyValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key)); |
| | | key = "0000" + kyeNumBin.substring(4); |
| | | int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key)); |
| | | |
| | | //02 A4 BB BA BA B4 |
| | | start = 2 * 2; |
| | | String tempHex; |
| | | |
| | | double point = 0; |
| | | for (int i = 0; i < numValue; i++) { |
| | | start = start + i * 2; |
| | | tempHex = tempStr.substring(start, start + 2); |
| | | //实际温度=密钥*密钥*37(溢出为无符号字节)再异或加密后的温度/2。 |
| | | point = this.getGrainTemp(keyValue, tempHex); |
| | | log.info("--------解析后的温度点----{}---{}", tempHex, point); |
| | | grainRoot.getPoints().add(point); |
| | | start = 2 * 2; |
| | | } |
| | | } |
| | | |
| | | this.add2GrainMap(grainRoot); |
| | | |
| | | //判断是不是最后一包数据,如果是最后一包执行解析 |
| | | String[] attCable = device.getCableRule().split("-"); |
| | | int cableZ = Integer.valueOf(attCable[0]); |
| | | int cableY = Integer.valueOf(attCable[1]); |
| | | int cableX = Integer.valueOf(attCable[2]); |
| | | |
| | | if (grainRoot.getNum() == cableX) { |
| | | analysisGrainStep2(reqData, cableZ, cableY, cableX); |
| | | } |
| | |
| | | root = this.getGrainRoot(buildGrainRootKey(reqData.getDevice().getDeviceSn(), i)); |
| | | |
| | | if (null == root || null == root.getPoints()) { |
| | | log.error("-----------解析获取所有粮情检测点失败,取消执行---------{}", reqData.getDevice().getDeviceName()); |
| | | log.error("-----------解析获取所有粮情检测点失败,取消执行---------{}---{}", reqData.getDevice().getDeviceName(),i); |
| | | return; |
| | | } |
| | | points.addAll(root.getPoints()); |
| | |
| | | |
| | | private String host; |
| | | private int port; |
| | | public Channel defaultChannel; |
| | | public static Channel defaultChannel; |
| | | |
| | | |
| | | public ClientEngine(String host, int port) { |
| | |
| | | @Override |
| | | public void run() { |
| | | try { |
| | | |
| | | startClient(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | } |
| | | |
| | | public void startClient() throws Exception { |
| | | if(defaultChannel != null){ |
| | | log.info("-----IP={},连接存在,直接使用",host); |
| | | return; |
| | | } |
| | | EventLoopGroup group = new OioEventLoopGroup(); |
| | | Bootstrap b = new Bootstrap(); |
| | | //默认长连接 |
| | |
| | | @Override |
| | | public void operationComplete(ChannelFuture arg0) throws Exception { |
| | | if (channelFuture.isSuccess()) { |
| | | log.info("-----IP={},连接成功"); |
| | | log.info("-----IP={},连接成功",host); |
| | | } else { |
| | | log.info("-----IP={},连接失败,自动关闭线程"); |
| | | log.info("-----IP={},连接失败,自动关闭线程",host); |
| | | channelFuture.cause().printStackTrace(); |
| | | group.shutdownGracefully(); // 关闭线程组 |
| | | } |
| | |
| | | } |
| | | |
| | | |
| | | public Channel getChannel() { |
| | | public static Channel getChannel() { |
| | | return defaultChannel; |
| | | } |
| | | |
| | |
| | | analysisService = SpringUtil.getBean(AnalysisService.class); |
| | | } |
| | | |
| | | analysisService.analysis(socketAddress.getAddress(), socketAddress.getPort(), strMsg); |
| | | try{ |
| | | analysisService.analysis(socketAddress.getAddress(), socketAddress.getPort(), strMsg); |
| | | }catch (Exception e){ |
| | | log.error(e.getMessage(),e); |
| | | } |
| | | |
| | | } |
| | | |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public BaseResp syncGrain(BaseReqData reqData) { |
| | | public synchronized BaseResp syncGrain(BaseReqData reqData) { |
| | | |
| | | BaseResp resp = new BaseResp(); |
| | | |
| | |
| | | log.error("----------------系统未获取到下行连接设备信息,无法执行---------"); |
| | | return resp; |
| | | } |
| | | |
| | | try { |
| | | this.syncGrainTh(reqData); |
| | | //Step 请求信息放入内存 |
| | | ProtocolUtils.addSyncReq2Map(device.getDepotIdSys(), reqData); |
| | | |
| | |
| | | |
| | | int start = 0, length = cableY; |
| | | // 生成粮情信息 |
| | | String hexStr = ""; |
| | | String hexStr = ""; |
| | | InvokeResult message; |
| | | for (int i = 1; i <= cableX; i++) { |
| | | if (1 == i) { |
| | |
| | | } else { |
| | | start = (i - 1) * cableY + 1; |
| | | } |
| | | |
| | | hexStr = this.buildGrainCmd(device, i, start, length); |
| | | |
| | | hexStr = buildGrainCmd(device, i, start, length); |
| | | // 发送命令 TODO----->>>暂时调整为每次创建一个新连接 |
| | | //Channel channel = ClientEngine.getChannel(device.getIp()); |
| | | Channel channel = null; |
| | |
| | | resp.setCode(500); |
| | | resp.setMsg("平台------>>>>控制柜:发送粮情检测命令-失败:" + message.getMessage()); |
| | | } |
| | | Thread.sleep(500); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("粮情检测异常:{}", e); |
| | |
| | | |
| | | @Override |
| | | public BaseResp syncGrainTh(BaseReqData reqData) { |
| | | return new BaseResp(); |
| | | |
| | | BaseResp resp = new BaseResp(); |
| | | |
| | | GatewayDevice device = reqData.getDevice(); |
| | | |
| | | if (null == device) { |
| | | resp.setCode(500); |
| | | resp.setMsg("系统未获取到下行连接设备信息,无法执行"); |
| | | log.error("----------------系统未获取到下行连接设备信息,无法执行---------"); |
| | | return resp; |
| | | } |
| | | |
| | | try { |
| | | //Step 请求信息放入内存 |
| | | ProtocolUtils.addSyncReq2Map(device.getDepotIdSys(), reqData); |
| | | InvokeResult message; |
| | | Channel channel = null; |
| | | if (null == channel) { |
| | | ClientEngine clientEngine = new ClientEngine(device.getIp(), device.getPort()); |
| | | clientEngine.start(); |
| | | Thread.sleep(300); |
| | | channel = clientEngine.getChannel(); |
| | | } |
| | | String hexStr = this.buildTHCmd(device); |
| | | message = ClientEngine.send2(hexStr, channel); |
| | | log.error("平台------>>>>主控:发送温湿度检测命令-{}---{}", message,hexStr); |
| | | // 封装返回信息 |
| | | if (!InvokeResult.SUCCESS.getCode().equals(message.getCode())) { |
| | | log.error("平台------>>>>主控:发送温湿度检测命令-失败{}", message.getMessage()); |
| | | resp.setCode(500); |
| | | resp.setMsg("平台------>>>>主控:发送温湿度检测命令-失败:" + message.getMessage()); |
| | | } |
| | | Thread.sleep(500); |
| | | } catch (Exception e) { |
| | | log.error("温湿度检测异常:{}", e); |
| | | resp.setCode(500); |
| | | resp.setMsg("平台------>>>>控制柜:发送温湿度检测命令:" + e.getMessage()); |
| | | return resp; |
| | | } |
| | | return resp; |
| | | } |
| | | |
| | | /** |
| | |
| | | deviceSn = BytesUtil.intToHexStr1(Integer.valueOf(deviceSn)); |
| | | content = content.replace("{id}", deviceSn); |
| | | content = content.replace("{id}", deviceSn); |
| | | |
| | | content = content.replace("{id}", deviceSn); |
| | | //命令ID |
| | | String msgIdHex = BytesUtil.intToHexStr1(cur); |
| | | content = content.replace("{msgId}", msgIdHex); |
| | |
| | | |
| | | return start + content + crcCode + end; |
| | | } |
| | | private String buildTHCmd(GatewayDevice device) { |
| | | |
| | | String start = "7e"; |
| | | //测温命令--7e 01 00 00 01 06 00 02 00 01 a0 ff ff 68 1a 05 88 5c 7e |
| | | String content = "{id}0000{id}{msgId}000200{id}a0ffff{funId}{start}{length}"; |
| | | |
| | | //开始封装消息体-主机ID |
| | | String deviceSn = device.getDeviceSn(); |
| | | deviceSn = BytesUtil.intToHexStr1(Integer.valueOf(deviceSn)); |
| | | content = content.replace("{id}", deviceSn); |
| | | content = content.replace("{id}", deviceSn); |
| | | content = content.replace("{id}", deviceSn); |
| | | //命令ID |
| | | String msgIdHex = BytesUtil.intToHexStr1(20); |
| | | content = content.replace("{msgId}", msgIdHex); |
| | | |
| | | //命令类型 |
| | | content = content.replace("{funId}", ServiceUtils.FUNCTION_68); |
| | | |
| | | //开始根号 |
| | | String startCurHex = BytesUtil.intToHexStr1(0); |
| | | content = content.replace("{start}", startCurHex); |
| | | |
| | | //截取长度 |
| | | String lenHex = BytesUtil.intToHexStr1(0); |
| | | content = content.replace("{length}", lenHex); |
| | | |
| | | //校验码 |
| | | String crcCode = this.getCRC(content); |
| | | |
| | | String end = "7e"; |
| | | |
| | | return start + content + crcCode + end; |
| | | } |
| | | /** |
| | | * 参考结果:http://www.ip33.com/crc.html |
| | | * <p> |
| | |
| | | * @param content |
| | | * @return |
| | | */ |
| | | private String getCRC(String content) { |
| | | private static String getCRC(String content) { |
| | | byte[] bytes = HexStringToBytes(content);//16进制字符串转成16进制字符串数组 |
| | | int i = CRC16_XMODEM(bytes);//进行CRC—XMODEM校验得到十进制校验数 |
| | | String CRC = Integer.toHexString(i);//10进制转16进制 |
| | | |
| | | if(CRC.length() < 4){ |
| | | CRC = "0"+CRC; |
| | | } |
| | | if(CRC.length() < 4){ |
| | | CRC = "0"+CRC; |
| | | } |
| | | //调整高位在右,地位在左侧 |
| | | CRC = tran_LH(CRC); |
| | | |
| | | return CRC; |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | //System.out.println(getCRC("010000010400020001a0ffff661005")); |
| | | //System.out.println(getCRC("010000010500020001a0ffff661505")); |
| | | String strMsg = "7E00010100010000A0FFFF66FF05BE01635B696FCE02A38ABAB5CE03E3003131CE044383B7B6CE0583497276D9FA"; |
| | | int start = 15 * 2; |
| | | strMsg = strMsg.substring(start); |
| | | System.out.println(strMsg); |
| | | String kyeNumBin = BytesUtil.toBinary8String(BytesUtil.hexToInt("A3")); |
| | | System.out.println(kyeNumBin); |
| | | String key = "00000" + kyeNumBin.substring(0, 3); |
| | | int keyValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key)); |
| | | key = "0000" + kyeNumBin.substring(4); |
| | | int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key)); |
| | | System.out.println((keyValue + "")); |
| | | System.out.println((numValue + "")); |
| | | |
| | | } |
| | | |
| | | public static String tran_LH(String info) { |
| | | return info.substring(2) + info.substring(0, 2); |
| | | } |