|
@@ -4,8 +4,9 @@ import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
-//import com.huimv.env.common.service.IRawDataService;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.huimv.env.common.entity.JinghongWater;
|
|
|
+import com.huimv.env.common.service.IJinghongWaterService;
|
|
|
import com.huimv.env.common.utils.RegexUtil;
|
|
|
import com.huimv.env.input.producer.Producer;
|
|
|
//import com.huimv.env.input.service.ICommandProcessorService;
|
|
@@ -26,7 +27,9 @@ import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.nio.charset.Charset;
|
|
|
import java.text.ParseException;
|
|
|
+import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.regex.Matcher;
|
|
@@ -102,22 +105,65 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
* @Date : 2022/3/28
|
|
|
* @Time : 17:36
|
|
|
*/
|
|
|
+ @Autowired
|
|
|
+ private IJinghongWaterService jinghongWaterService;
|
|
|
+
|
|
|
private void handleClientAskCmd(String clientAskText, ChannelHandlerContext ctx) throws ParseException, IOException {
|
|
|
|
|
|
- askTextSb.delete(0, askTextSb.length());
|
|
|
-
|
|
|
- System.out.println("接收到消息"+clientAskText);
|
|
|
- if (clientAskText.length() > 10) {
|
|
|
- HashMap<String, String> paramsMap = new HashMap<>();
|
|
|
- JSONObject jsonObject = new JSONObject();
|
|
|
- jsonObject.put("result", clientAskText);
|
|
|
- System.out.println(jsonObject);
|
|
|
- paramsMap.put("Content-Type", "application/json;charset=utf-8");
|
|
|
- String post = HttpRequest.post("https://huatong.ifarmcloud.com/huatongApi/env-nh3-n/send")
|
|
|
- .headerMap(paramsMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
|
|
|
- System.out.println(post);
|
|
|
+ System.out.println("接收到消息: " + clientAskText + " 时间: " + System.currentTimeMillis());
|
|
|
+
|
|
|
+ // 假设clientAskText是一个十六进制编码的字符串,需要转换为ByteBuf
|
|
|
+ if (clientAskText.length() == 15){
|
|
|
+ JinghongWater jinghongWater = new JinghongWater();
|
|
|
+ jinghongWater.setCreateTime(new Date());
|
|
|
+ jinghongWater.setDeviceId(clientAskText);
|
|
|
+ jinghongWater.setDeviceName(getDevice(clientAskText));
|
|
|
+ jinghongWaterService.save(jinghongWater);
|
|
|
+ ByteBuf bufff = Unpooled.buffer();
|
|
|
+ if ("861213058846640".equals(clientAskText)){
|
|
|
+ bufff.writeBytes(hexString2Bytes("01030004000285CA"));
|
|
|
+ ctx.writeAndFlush(bufff);
|
|
|
+ }else {
|
|
|
+ bufff.writeBytes(hexString2Bytes("010410180002F50C"));
|
|
|
+ ctx.writeAndFlush(bufff);
|
|
|
+ }
|
|
|
+
|
|
|
+ }else {
|
|
|
+ JinghongWater id = jinghongWaterService.getOne(new QueryWrapper<JinghongWater>().orderByDesc("id").last("limit 1"));
|
|
|
+
|
|
|
+ // 获取GBK字符集
|
|
|
+ Charset gbkCharset = Charset.forName("GBK");
|
|
|
+ // 将字符串转换为GBK编码的字节数组
|
|
|
+ byte[] gbkBytes = (clientAskText).getBytes(gbkCharset);
|
|
|
+ System.out.println("22222" );
|
|
|
+ // 打印GBK编码的字节数组(以十六进制格式展示)
|
|
|
+ String raw = bytesToHex(gbkBytes);
|
|
|
+ System.out.println("GBK编码的字节数组(十六进制): " + raw);
|
|
|
+ id.setRaw(raw);
|
|
|
+ //0104040000243f213c
|
|
|
+ if(raw.startsWith("010304")){
|
|
|
+ id.setValueA(getFloat(raw.substring(6, 14)));
|
|
|
+ }else {
|
|
|
+ int i = Integer.parseInt(raw.substring(6, 14), 16);
|
|
|
+ id.setValueA(i+"");
|
|
|
+ }
|
|
|
+ jinghongWaterService.updateById(id);
|
|
|
+ ctx.writeAndFlush("");
|
|
|
}
|
|
|
- ctx.writeAndFlush(Unpooled.copiedBuffer("rok".getBytes()));
|
|
|
+
|
|
|
+// askTextSb.delete(0, askTextSb.length());
|
|
|
+ // if (clientAskText.length() > 10) {
|
|
|
+// HashMap<String, String> paramsMap = new HashMap<>();
|
|
|
+// JSONObject jsonObject = new JSONObject();
|
|
|
+// jsonObject.put("result", clientAskText);
|
|
|
+// System.out.println(jsonObject);
|
|
|
+// paramsMap.put("Content-Type", "application/json;charset=utf-8");
|
|
|
+// String post = HttpRequest.post("https://huatong.ifarmcloud.com/huatongApi/env-nh3-n/send")
|
|
|
+// .headerMap(paramsMap, false).body(jsonObject.toJSONString()).timeout(30 * 1000).execute().body();
|
|
|
+// System.out.println(post);
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
// clientAskText = clientAskText.replaceAll("\r", "").replaceAll("\n", "");
|
|
|
//// {处理非正常命令}
|
|
@@ -136,6 +182,79 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
// }
|
|
|
}
|
|
|
|
|
|
+ private String getFloat(String hexString ) {
|
|
|
+
|
|
|
+
|
|
|
+ byte[] bytes = new byte[4];
|
|
|
+ for (int i = 0; i < 4; i++) {
|
|
|
+ // 每两个字符表示一个字节
|
|
|
+ String byteString = hexString.substring(i * 2, (i + 1) * 2);
|
|
|
+ // 将16进制字符串转换为整数,并强制转换为byte
|
|
|
+ bytes[i] = (byte) Integer.parseInt(byteString, 16);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用ByteBuffer(或者你可以直接操作字节数组)
|
|
|
+ // 但由于我们只需要一个int来表示float,所以这里直接组合字节为int
|
|
|
+ int intValue = ((bytes[0] & 0xFF) << 24) |
|
|
|
+ ((bytes[1] & 0xFF) << 16) |
|
|
|
+ ((bytes[2] & 0xFF) << 8) |
|
|
|
+ (bytes[3] & 0xFF);
|
|
|
+
|
|
|
+ // 将int值转换为float值
|
|
|
+ float floatValue = Float.intBitsToFloat(intValue);
|
|
|
+ return floatValue+"";
|
|
|
+ // 输出结果
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getDevice(String deviceId){
|
|
|
+ switch (deviceId){
|
|
|
+ case "861213058846764":
|
|
|
+ return "庙贝";
|
|
|
+ case "861213058872158":
|
|
|
+ return "芙江";
|
|
|
+ case "861213058861409":
|
|
|
+ return "污水电磁流量计";
|
|
|
+ case "861213058829596":
|
|
|
+ return "凤行";
|
|
|
+ case "861213058846640":
|
|
|
+ return "凤行2";
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String bytesToHex(byte[] bytes) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (byte b : bytes) {
|
|
|
+ sb.append(String.format("%02x", b));
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ public static String bytes2HexString(byte[] b) {
|
|
|
+ String r = "";
|
|
|
+ for (int i = 0; i < b.length; i++) {
|
|
|
+ String hex = Integer.toHexString(b[i] & 0xFF);
|
|
|
+ if (hex.length() == 1) {
|
|
|
+ hex = '0' + hex;
|
|
|
+ }
|
|
|
+ r += hex.toUpperCase() + " ";
|
|
|
+ }
|
|
|
+ return r;
|
|
|
+ }
|
|
|
+ public static byte[] hexString2Bytes(String src) {
|
|
|
+
|
|
|
+ int l = src.length() / 2;
|
|
|
+
|
|
|
+ byte[] ret = new byte[l];
|
|
|
+
|
|
|
+ for (int i = 0; i < l; i++) {
|
|
|
+
|
|
|
+ ret[i] = (byte) Integer.valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+
|
|
|
+ }
|
|
|
/**
|
|
|
* @Method : askCmdActuator
|
|
|
* @Description :
|
|
@@ -403,238 +522,7 @@ public class EnvInputServerHandler extends ChannelInboundHandlerAdapter {
|
|
|
// }
|
|
|
// //TODO
|
|
|
// String answerText = "hm+23+0+8+end";
|
|
|
-// log.info(">>CO2 等级上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送湿度请求到消息队列
|
|
|
-// producer.sendCo2(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void uploadGainDayAgePeizhiuStatus(String[] dataArray,String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// try {
|
|
|
-// String originalValue =cmdProService.getDayAgePeiZhi(dataArray,idCode);
|
|
|
-// String answerText ="hm+91+1+"+originalValue+"+0+end";
|
|
|
-// if (StringUtils.isNotBlank(originalValue)){
|
|
|
-// answerText = "hm+91+0+"+originalValue+"+0+end";
|
|
|
-// log.info(">>获取日龄配置上传请求-应答数据>>" + answerText);
|
|
|
-// }else {
|
|
|
-// log.info("该设备没有设置日龄配置>>" );
|
|
|
-// }
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// }catch (Exception e){
|
|
|
-// System.out.println(e);
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void uploadGainTongFengPeizhiuStatus(String[] dataArray,String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// try {
|
|
|
-// String originalValue = cmdProService.getFengPeiZhi(dataArray);
|
|
|
-// String answerText ="hm+92+1"+originalValue+"+0+end";
|
|
|
-// if (StringUtils.isNotBlank(originalValue)) {
|
|
|
-// answerText = "hm+92+0+" + originalValue + "+0+end";
|
|
|
-// log.info(">>获取通风等级配置上传请求-应答数据>>" + answerText);
|
|
|
-// } else {
|
|
|
-// log.info("该设备没有设置获取通风等级配置>>");
|
|
|
-// }
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// }catch (Exception e){
|
|
|
-// System.out.println(e);
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-//
|
|
|
-// //风机配置
|
|
|
-// private void uploadTongFengPeizhiuStatus(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// String answerText = "hm+82+0+2+end";
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##通风配置上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// log.info(">>通风配置上传请求-应答数据>>" + answerText);
|
|
|
-// producer.sendTongFengPeiZhi(askText);
|
|
|
-// }
|
|
|
-// //日龄温度配置上传
|
|
|
-// private void uploadDayAgePeizhiuStatus(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// String answerText = "hm+81+0+2+end";
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##日龄温度上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// log.info(">>日龄温度上传请求-应答数据>>" + answerText);
|
|
|
-// producer.sendDayAgePeiZhi(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// //喷雾
|
|
|
-// private void uploadPengWuStatus(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##喷雾状态上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+63+0+2+end";
|
|
|
-// log.info(">>喷雾状态上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// producer.sendPengWuStatus(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// //湿帘
|
|
|
-// private void uploadShiLianStatus(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##湿帘状态上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+62+0+2+end";
|
|
|
-// log.info(">>湿帘状态上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //湿帘状态请求到消息队列
|
|
|
-// producer.sendShiLianStatus(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// //获取远程设备编码
|
|
|
-// private void getDeviceCode(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// System.out.println("==>获取远程设备编码请求:" + askText.trim());
|
|
|
-// //{读取设备编码}
|
|
|
-// String deviceCode = cmdProService.getDeviceCodeByChipId(idCode);
|
|
|
-// log.info("获取远程设备编码请求,芯片id>>" + idCode + " ,deviceCode>>" + deviceCode);
|
|
|
-// if (deviceCode == null) {
|
|
|
-// log.error("该设备未注册,已舍弃请求.");
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+1+0+" + deviceCode + "+0+end";
|
|
|
-// log.info("<<获取远程设备编码请求-应答数据:" + answerText);
|
|
|
-//// //{应答指令}
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// cmdProService.updateStatus(idCode);
|
|
|
-// }
|
|
|
-// //时间同步请求
|
|
|
-// private void getServerTime(String askText, String deviceCode, ChannelHandlerContext ctx) throws ParseException {
|
|
|
-// System.out.println("==>时间同步请求:" + askText);
|
|
|
-// String answerText = "hm+2+" + dateUtil.getNowText() + "+4+end";
|
|
|
-// log.info(">>时间同步请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// }
|
|
|
-//
|
|
|
-// //日龄
|
|
|
-// private void uploadDayAge(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##日龄上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+3+0+4+end";
|
|
|
-// log.info(">>日龄上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送湿度请求到消息队列
|
|
|
-// producer.sendDayAge(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void uploadFeng(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##通风等级上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+4+0+4+end";
|
|
|
-// log.info(">>通风等级上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送湿度请求到消息队列
|
|
|
-// producer.sendFeng(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void uploadOutTemp(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##舍外温度上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+5+0+4+end";
|
|
|
-// log.info(">>舍外温度上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送湿度请求到消息队列
|
|
|
-// producer.sendOutTemp(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void uploadInTemp(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##舍内温度上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+6+0+4+end";
|
|
|
-// log.info(">>舍内温度上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送湿度请求到消息队列
|
|
|
-// producer.sendInTemp(askText);
|
|
|
-// }
|
|
|
-// private void uploadTargetnTemp(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##目标温度上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+7+0+4+end";
|
|
|
-// log.info(">>目标温度上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送湿度请求到消息队列
|
|
|
-// producer.senTargetnTemp(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void uploadTemp(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##温度上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+21+0+7+end";
|
|
|
-// log.info(">>温度上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送湿度请求到消息队列
|
|
|
-// producer.sendTemp(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void uploadHumi(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##湿度度上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+22+0+8+end";
|
|
|
-// log.info(">>湿度上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送湿度请求到消息队列
|
|
|
-// producer.sendHumi(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// private void uploadWarning(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##预警上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+41+0+2+end";
|
|
|
-// log.info(">>预警上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送湿度请求到消息队列
|
|
|
-// producer.sendWarning(askText);
|
|
|
-// }
|
|
|
-// private void uploadFengStatus(String askText, String idCode, ChannelHandlerContext ctx) {
|
|
|
-// if (!cmdProService.isEffectiveDevice(idCode)) {
|
|
|
-// System.out.println("##风机状态上传请求-未注册设备 idCode=" + idCode);
|
|
|
-// return;
|
|
|
-// }
|
|
|
-// String answerText = "hm+61+0+2+end";
|
|
|
-// log.info(">>风机状态上传请求-应答数据>>" + answerText);
|
|
|
-// answerCmd(answerText, ctx);
|
|
|
-// //发送风机状态请求到消息队列
|
|
|
-// producer.uploadFengStatus(askText);
|
|
|
-// }
|
|
|
-//
|
|
|
-// //应答
|
|
|
-// public void answerCmd(String answerText, ChannelHandlerContext ctx) {
|
|
|
-// ctx.writeAndFlush(Unpooled.copiedBuffer(answerText.getBytes()));
|
|
|
-// }
|
|
|
-//
|
|
|
-// //检查无效耳标
|
|
|
-// public boolean checkValidEarmark(String earmark) {
|
|
|
-// if (earmark.trim().equalsIgnoreCase("ffffffffffffffff") || earmark.trim().equalsIgnoreCase("0000000000000000")) {
|
|
|
-// return true;
|
|
|
-// } else {
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
+
|
|
|
// //拆分粘包数据
|
|
|
public JSONArray parseAskCmdPackage(String text) {
|
|
|
String key = "end";
|