| | |
| | | * @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); |
| | | } |