|
@@ -0,0 +1,298 @@
|
|
|
|
+package com.huimv.eartag2.api.netty;
|
|
|
|
+
|
|
|
|
+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.huimv.eartag2.api.config.WebSocket;
|
|
|
|
+import com.huimv.eartag2.api.pojo.EartagDeviceRegister;
|
|
|
|
+import com.huimv.eartag2.api.pojo.EartagEartagRegister2;
|
|
|
|
+import com.huimv.eartag2.api.pojo.PliersDemo;
|
|
|
|
+import com.huimv.eartag2.api.pojo.StationLib;
|
|
|
|
+import com.huimv.eartag2.api.service.IEartagDeviceRegisterService;
|
|
|
|
+import com.huimv.eartag2.api.service.IEartagEartagRegister2Service;
|
|
|
|
+import com.huimv.eartag2.api.service.IPliersDemoService;
|
|
|
|
+import com.huimv.eartag2.api.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.http.ResponseEntity;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
+
|
|
|
|
+import java.text.ParseException;
|
|
|
|
+import java.util.Date;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @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 IEartagEartagRegister2Service eartagRegisterService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private IEartagDeviceRegisterService deviceRegisterService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IStationLibService stationLibService;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private IPliersDemoService pliersDemoService;
|
|
|
|
+
|
|
|
|
+ @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();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ 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+119.3.44.183+10023+8+end".getBytes()));
|
|
|
|
+ }
|
|
|
|
+ if ("7".equals(split[1])){
|
|
|
|
+ System.out.println("1");
|
|
|
|
+ ctx.writeAndFlush(Unpooled.copiedBuffer("hm+7+8+end".getBytes()));
|
|
|
|
+ String device = split[2];
|
|
|
|
+ String eartag = split[3];
|
|
|
|
+ EartagDeviceRegister deviceRegister = deviceRegisterService.getOne(new QueryWrapper<EartagDeviceRegister>().eq("chip_id", device));
|
|
|
|
+ System.out.println("2");
|
|
|
|
+ if(ObjectUtil.isNotEmpty(deviceRegister)){
|
|
|
|
+ Integer upgrade = deviceRegister.getUpgrade();
|
|
|
|
+ if (upgrade ==1){
|
|
|
|
+ saveMoneyData(split,deviceRegister.getChipId());
|
|
|
|
+ }else {
|
|
|
|
+ saveData(split);
|
|
|
|
+ }
|
|
|
|
+ EartagEartagRegister2 eartagRegister = eartagRegisterService.getOne(new QueryWrapper<EartagEartagRegister2>().eq("earmark", eartag));
|
|
|
|
+ if (ObjectUtil.isNotEmpty(eartagRegister)){
|
|
|
|
+ eartagRegister.setEarmark(eartag);
|
|
|
|
+ eartagRegister.setFirstDevice(device);
|
|
|
|
+ eartagRegister.setRegisterTime(new Date());
|
|
|
|
+ eartagRegister.setRegisterType(2);
|
|
|
|
+ eartagRegister.setFarmId(deviceRegister.getFarmId());
|
|
|
|
+ eartagRegister.setCreateDate(new Date());
|
|
|
|
+ eartagRegisterService.updateById(eartagRegister);
|
|
|
|
+ }else {
|
|
|
|
+ EartagEartagRegister2 eartagEartagRegister = new EartagEartagRegister2();
|
|
|
|
+ eartagEartagRegister.setEarmark(eartag);
|
|
|
|
+ eartagEartagRegister.setFirstDevice(device);
|
|
|
|
+ eartagEartagRegister.setRegisterTime(new Date());
|
|
|
|
+ eartagEartagRegister.setRegisterType(2);
|
|
|
|
+ eartagEartagRegister.setFarmId(deviceRegister.getFarmId());
|
|
|
|
+ eartagEartagRegister.setCreateDate(new Date());
|
|
|
|
+ eartagRegisterService.save(eartagEartagRegister);
|
|
|
|
+ }
|
|
|
|
+ WebSocket.sendMessage("true");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ private final String URL = "http://apilocate.amap.com/position";
|
|
|
|
+ private final String KEY = "9ccbad3db267cd800084179fe58c5ce9";
|
|
|
|
+
|
|
|
|
+ private void saveMoneyData(String[] split,String imei ) {
|
|
|
|
+ String eartag = split[3];
|
|
|
|
+ String device = split[2];
|
|
|
|
+ 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 ="";
|
|
|
|
+ JSONObject body= new JSONObject();
|
|
|
|
+ if (ObjectUtil.isEmpty(stationLib)){
|
|
|
|
+ try {
|
|
|
|
+ ResponseEntity<JSONObject> forEntity = restTemplate.getForEntity(URL+"?accesstype=0&imei="+imei+"&cdma=0&bts=460,0,"+lac+","+ci+",-30&output=json&key="+KEY+"&network=GSM", JSONObject.class);
|
|
|
|
+ System.out.println("forEntity" + forEntity);
|
|
|
|
+ body = forEntity.getBody();
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ System.out.println(e);
|
|
|
|
+ }
|
|
|
|
+ if (ObjectUtil.isNotEmpty(body)){
|
|
|
|
+ Object infocode = body.get("infocode");
|
|
|
|
+ if ("10000".equals(infocode)){
|
|
|
|
+ JSONObject result = (JSONObject)body.get("result");
|
|
|
|
+ address = (String) result.get("desc");
|
|
|
|
+ String radius = (String) result.get("radius");
|
|
|
|
+ String locations = (String) result.get("location");
|
|
|
|
+ String[] location = locations.split(",");
|
|
|
|
+
|
|
|
|
+ lat = location[1];
|
|
|
|
+ lon = location[0];
|
|
|
|
+
|
|
|
|
+ StationLib stationLib1 = new StationLib();
|
|
|
|
+ stationLib1.setAddress(address);
|
|
|
|
+ stationLib1.setCi(ci);
|
|
|
|
+ stationLib1.setLac(lac);
|
|
|
|
+ stationLib1.setLat(lat);
|
|
|
|
+ stationLib1.setLon(lon);
|
|
|
|
+ stationLib1.setRadius(Integer.parseInt(radius));
|
|
|
|
+ stationLibService.save(stationLib1);
|
|
|
|
+ }else {
|
|
|
|
+ StationLib id = stationLibService.getOne(new QueryWrapper<StationLib>().orderByDesc("id").last("limit 1"));
|
|
|
|
+ lat = id.getLat();
|
|
|
|
+ lon = id.getLon();
|
|
|
|
+ address = id.getAddress();
|
|
|
|
+ System.out.println(body);
|
|
|
|
+ System.out.println("高德接口出错,取上一个记录接口");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }else {
|
|
|
|
+ lat = stationLib.getLat();
|
|
|
|
+ lon = stationLib.getLon();
|
|
|
|
+ address = stationLib.getAddress();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ int countEarmark = (int)pliersDemoService.count(new QueryWrapper<PliersDemo>().eq("earmark", eartag));
|
|
|
|
+ PliersDemo pliersDemo = new PliersDemo();
|
|
|
|
+ pliersDemo.setEarmark(eartag);
|
|
|
|
+ pliersDemo.setPliersId(device);
|
|
|
|
+ pliersDemo.setUpdateTime(new Date());
|
|
|
|
+ pliersDemo.setUsegeTimes(1+countEarmark);
|
|
|
|
+
|
|
|
|
+ pliersDemo.setLat(lat);
|
|
|
|
+ pliersDemo.setLon(lon);
|
|
|
|
+ pliersDemo.setAddress(address);
|
|
|
|
+ pliersDemoService.save(pliersDemo);
|
|
|
|
+ System.out.println("存储成功");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void saveData( String[] split ) {
|
|
|
|
+
|
|
|
|
+ //耳标记录,加上webSoket
|
|
|
|
+ //hm+7+868977051335099+122083123610011+22450+184902913+0+end
|
|
|
|
+ String eartag = split[3];
|
|
|
|
+ String lac = split[4];
|
|
|
|
+ String ci = split[5];
|
|
|
|
+ String pliersId = split[2];
|
|
|
|
+ StationLib stationLib = stationLibService.getOne(new QueryWrapper<StationLib>().eq("ci", ci).eq("lac", lac));
|
|
|
|
+ String lat ="";
|
|
|
|
+ String lon ="";
|
|
|
|
+ String address ="";
|
|
|
|
+
|
|
|
|
+ JSONObject body= new JSONObject();
|
|
|
|
+ if (ObjectUtil.isEmpty(stationLib)){
|
|
|
|
+ try {
|
|
|
|
+ ResponseEntity<JSONObject> forEntity = restTemplate.getForEntity("http://api.cellocation.com:82/cell/?mcc=460&mnc=1&lac="+lac+"&ci="+ci+"&output=json", JSONObject.class);
|
|
|
|
+ System.out.println("forEntity"+forEntity);
|
|
|
|
+ body = forEntity.getBody();
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ System.out.println(e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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");
|
|
|
|
+ if (errcode == 0){
|
|
|
|
+ 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);
|
|
|
|
+ }
|
|
|
|
+ if (errcode == 10001){
|
|
|
|
+ StationLib id = stationLibService.getOne(new QueryWrapper<StationLib>().orderByDesc("id").last("limit 1"));
|
|
|
|
+ lat = id.getLat();
|
|
|
|
+ lon = id.getLon();
|
|
|
|
+ address = id.getAddress();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }else {
|
|
|
|
+ lat = stationLib.getLat();
|
|
|
|
+ lon = stationLib.getLon();
|
|
|
|
+ address = stationLib.getAddress();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ int countEarmark = (int)pliersDemoService.count(new QueryWrapper<PliersDemo>().eq("earmark", eartag));
|
|
|
|
+ PliersDemo pliersDemo = new PliersDemo();
|
|
|
|
+ pliersDemo.setEarmark(eartag);
|
|
|
|
+ pliersDemo.setPliersId(pliersId);
|
|
|
|
+ pliersDemo.setUpdateTime(new Date());
|
|
|
|
+ pliersDemo.setUsegeTimes(1+countEarmark);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ pliersDemo.setLat(lat);
|
|
|
|
+ pliersDemo.setLon(lon);
|
|
|
|
+ pliersDemo.setAddress(address);
|
|
|
|
+ pliersDemoService.save(pliersDemo);
|
|
|
|
+
|
|
|
|
+// List<PliersDemo> id = pliersDemoService.list(new QueryWrapper<PliersDemo>().eq("pliers_id",pliersId).orderByDesc("id").last("limit 50"));
|
|
|
|
+// String resultJson= new ObjectMapper().writeValueAsString(true);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+//hm+7+868977051335099+122083123610011+0+end
|
|
|
|
+ //hm+6+868977051335099+0+end
|
|
|
|
+
|
|
|
|
+}
|