|
@@ -0,0 +1,196 @@
|
|
|
+package com.huimv.demo.eartag;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.huimv.demo.eartag.conifg.WebSocket;
|
|
|
+import com.huimv.demo.eartag.entity.PliersDemo;
|
|
|
+import com.huimv.demo.eartag.entity.StationLib;
|
|
|
+import com.huimv.demo.eartag.service.IPliersDemoService;
|
|
|
+import com.huimv.demo.eartag.service.IStationLibService;
|
|
|
+import io.netty.buffer.ByteBuf;
|
|
|
+import io.netty.buffer.Unpooled;
|
|
|
+import io.netty.channel.ChannelHandler;
|
|
|
+import io.netty.channel.ChannelHandlerContext;
|
|
|
+import io.netty.channel.ChannelInboundHandlerAdapter;
|
|
|
+import io.netty.util.CharsetUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import javax.sound.midi.Soundbank;
|
|
|
+import java.text.ParseException;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@ChannelHandler.Sharable
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class EartagServerHandler2 extends ChannelInboundHandlerAdapter {
|
|
|
+ private StringBuilder askTextSb = null;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IPliersDemoService pliersDemoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IStationLibService stationLibService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate ;
|
|
|
+ //
|
|
|
+ public void appendClientAsk(String text){
|
|
|
+ if(this.askTextSb == null){
|
|
|
+ askTextSb = new StringBuilder();
|
|
|
+ }
|
|
|
+ askTextSb.append(text);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
|
|
+ ByteBuf data = (ByteBuf) msg;
|
|
|
+ String clientAskText = data.toString(CharsetUtil.UTF_8);
|
|
|
+// System.out.println((++num)+"次, 客户端消息clientAskText>>"+clientAskText);
|
|
|
+ //保存实例内的客户端请求
|
|
|
+ appendClientAsk(clientAskText);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
|
|
+// ctx.writeAndFlush("111");
|
|
|
+// System.out.println("EchoServerHandle channelReadComplete");
|
|
|
+ if(askTextSb.toString().indexOf("end") != -1){
|
|
|
+// System.out.println("askTextSb.内容()>>"+askTextSb.toString());
|
|
|
+// System.out.println("askTextSb.内容长度>>"+askTextSb.length());
|
|
|
+// System.out.println("输入完成.");
|
|
|
+ // 处理客户端消息
|
|
|
+ handleClientEartagMessage(askTextSb.toString(),ctx);
|
|
|
+ //清空重置;
|
|
|
+ askTextSb.delete(0,askTextSb.length());
|
|
|
+// System.out.println("清空sb实例. 长度>>"+askTextSb.length());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
|
|
+// System.out.println("cause.getMessage()>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+cause.getMessage());
|
|
|
+ if(cause.getMessage().indexOf("Connection reset") != -1){
|
|
|
+ log.info("相关采集器设备正在重启:"+cause.toString());
|
|
|
+ }
|
|
|
+// cause.printStackTrace();
|
|
|
+ ctx.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @Method : handleClientMessage
|
|
|
+ * @Description : 处理请求小心
|
|
|
+ * @Params : [clientAskText, ctx]
|
|
|
+ * @Return : void
|
|
|
+ *
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Date : 2022/3/28
|
|
|
+ * @Time : 17:36
|
|
|
+ */
|
|
|
+ private void handleClientEartagMessage(String clientAskText, ChannelHandlerContext ctx) throws ParseException, JsonProcessingException {
|
|
|
+ clientAskText = clientAskText.replaceAll("\r","").replaceAll("\n","");
|
|
|
+ System.out.println("clientAskText--------------->" +clientAskText);
|
|
|
+//hm+7+868977051335099+122083123610011+0+end
|
|
|
+
|
|
|
+ String[] split = clientAskText.split("\\+");
|
|
|
+
|
|
|
+
|
|
|
+ if ("6".equals(split[1])){
|
|
|
+
|
|
|
+ System.out.println("pliersDemo------------->" +clientAskText);
|
|
|
+ ctx.writeAndFlush(Unpooled.copiedBuffer("hm+6+0+115.238.57.190+8013+8+end".getBytes()));
|
|
|
+ }
|
|
|
+
|
|
|
+ if ("7".equals(split[1])){
|
|
|
+
|
|
|
+ String eartag = split[3];
|
|
|
+ //hm+7+868977051335099+122083123610011+22450+184902913+0+end
|
|
|
+ String lac = split[4];
|
|
|
+ String ci = split[5];
|
|
|
+ StationLib stationLib = stationLibService.getOne(new QueryWrapper<StationLib>().eq("ci", ci).eq("lac", lac));
|
|
|
+ String lat ="";
|
|
|
+ String lon ="";
|
|
|
+ String address ="";
|
|
|
+
|
|
|
+ if (ObjectUtil.isEmpty(stationLib)){
|
|
|
+ ResponseEntity<JSONObject> forEntity = restTemplate.getForEntity("http://api.cellocation.com:82/cell/?mcc=460&mnc=1&lac="+lac+"&ci="+ci+"&output=json", JSONObject.class);
|
|
|
+ JSONObject body = forEntity.getBody();
|
|
|
+ lat = (String)body.get("lat");
|
|
|
+ lon = (String)body.get("lon");
|
|
|
+ address = (String)body.get("address");
|
|
|
+ String radius = (String)body.get("radius");
|
|
|
+ Integer errcode = (Integer)body.get("errcode");
|
|
|
+
|
|
|
+ StationLib stationLib1 = new StationLib();
|
|
|
+ stationLib1.setAddress(address);
|
|
|
+ stationLib1.setCi(ci);
|
|
|
+ stationLib1.setErrcode(errcode+"");
|
|
|
+ stationLib1.setLac(lac);
|
|
|
+ stationLib1.setLat(lat);
|
|
|
+ stationLib1.setLon(lon);
|
|
|
+ stationLib1.setRadius(Integer.parseInt(radius));
|
|
|
+
|
|
|
+ stationLibService.save(stationLib1);
|
|
|
+
|
|
|
+
|
|
|
+ }else {
|
|
|
+ lat = stationLib.getLat();
|
|
|
+ lon = stationLib.getLon();
|
|
|
+ address = stationLib.getAddress();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ int countEarmark = pliersDemoService.count(new QueryWrapper<PliersDemo>().eq("earmark", eartag));
|
|
|
+ PliersDemo pliersDemo = new PliersDemo();
|
|
|
+ pliersDemo.setEarmark(eartag);
|
|
|
+ pliersDemo.setPliersId(split[2]);
|
|
|
+ pliersDemo.setUpdateTime(new Date());
|
|
|
+ pliersDemo.setUsegeTimes(countEarmark+1);
|
|
|
+
|
|
|
+
|
|
|
+ pliersDemo.setLat(lat);
|
|
|
+ pliersDemo.setLon(lon);
|
|
|
+ pliersDemo.setAddress(address);
|
|
|
+
|
|
|
+
|
|
|
+ pliersDemoService.save(pliersDemo);
|
|
|
+
|
|
|
+
|
|
|
+ List<PliersDemo> id = pliersDemoService.list(new QueryWrapper<PliersDemo>().orderByDesc("id").last("limit 50"));
|
|
|
+ String resultJson= new ObjectMapper().writeValueAsString(id);
|
|
|
+ WebSocket.sendMessage(resultJson);
|
|
|
+ ctx.writeAndFlush(Unpooled.copiedBuffer("hm+7+8+end".getBytes()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+//hm+7+868977051335099+122083123610011+0+end
|
|
|
+ //hm+6+868977051335099+0+end
|
|
|
+
|
|
|
+
|
|
|
+}
|