|
@@ -0,0 +1,167 @@
|
|
|
|
+package com.huimv.location.service.impl;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.huimv.location.dao.entity.BeaconEntity;
|
|
|
|
+import com.huimv.location.dao.repo.BeaconRepo;
|
|
|
|
+import com.huimv.location.service.BeaconService;
|
|
|
|
+import com.huimv.location.utils.DataUtil;
|
|
|
|
+import com.huimv.location.utils.HttpClient;
|
|
|
|
+
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.util.List;
|
|
|
|
+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
|
|
|
|
+ **/
|
|
|
|
+@Service
|
|
|
|
+public class BeaconServiceImpl implements BeaconService {
|
|
|
|
+ @Autowired
|
|
|
|
+ private HttpClient httpClient;
|
|
|
|
+ @Autowired
|
|
|
|
+ private com.huimv.location.utils.StringUtils stringUtils;
|
|
|
|
+ @Autowired
|
|
|
|
+ private BeaconRepo beaconRepo;
|
|
|
|
+
|
|
|
|
+// private final String host_url = "http://192.168.1.17:44444";
|
|
|
|
+// private final String host_url = "http://localhost:44444";
|
|
|
|
+ @Value("${host_url}")
|
|
|
|
+ private String host_url;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public JSONArray getRemoteBeacons() throws UnsupportedEncodingException {
|
|
|
|
+ String url = host_url + "/beacons";
|
|
|
|
+ String beaconsData = httpClient.doGet(url);
|
|
|
|
+ System.out.println("输出所有信标数据>>"+beaconsData);
|
|
|
|
+ System.out.println("bytes:"+stringUtils.getBytes(beaconsData));
|
|
|
|
+ System.out.println("KB:"+stringUtils.getKBytes(beaconsData));
|
|
|
|
+// System.out.println("MB:"+stringUtils.getMBytes(outData));
|
|
|
|
+// String[] beaconsArray = beaconsData.split(",");
|
|
|
|
+// System.out.println("length:"+beaconsArray.length);
|
|
|
|
+// for(int a=0;a<beaconsArray.length;a++){
|
|
|
|
+// System.out.println(a+",beaconInfo>>"+beaconsArray[a]);
|
|
|
|
+// }
|
|
|
|
+ //
|
|
|
|
+ JSONArray beaconMacJa = getBeaconMac(beaconsData);
|
|
|
|
+// System.out.println("beaconMacJa>>"+beaconMacJa);
|
|
|
|
+ JSONObject beaconsJo = JSONObject.parseObject(beaconsData);
|
|
|
|
+// System.out.println("beaconJo>>"+beaconsJo);
|
|
|
|
+ DataUtil dateUtil = new DataUtil();
|
|
|
|
+// JSONObject beaconInfoJo = beaconsJo.getJSONObject("84c2e4d234c6");
|
|
|
|
+// System.out.println("beaconInfoJo>>"+beaconInfoJo);
|
|
|
|
+// DateUtil dateUtil = new DateUtil();
|
|
|
|
+ JSONArray beaconDataJa = new JSONArray();
|
|
|
|
+ for(int a=0;a<beaconMacJa.size();a++){
|
|
|
|
+ JSONObject beaconInfoJo = beaconsJo.getJSONObject(beaconMacJa.getString(a));
|
|
|
|
+// System.out.println(a+", beaconInfoJo>>"+beaconInfoJo);
|
|
|
|
+// long updatedAt = beaconInfoJo.getLong("updatedAt");
|
|
|
|
+// System.out.println(" updatedAt>>"+updatedAt+""+dateUtil.transferLongToDate("yyyy-MM-dd HH:mm:ss",updatedAt));
|
|
|
|
+ JSONObject beaconDataJo = new JSONObject();
|
|
|
|
+ beaconDataJo.put("mac",beaconMacJa.getString(a));
|
|
|
|
+ beaconDataJo.put("x",beaconInfoJo.getString("x"));
|
|
|
|
+ beaconDataJo.put("y",beaconInfoJo.getString("y"));
|
|
|
|
+ beaconDataJo.put("z",beaconInfoJo.getString("z"));
|
|
|
|
+ // 最后上传时间
|
|
|
|
+ JSONObject userDataTsJo = beaconInfoJo.getJSONObject("userDataTs");
|
|
|
|
+ beaconDataJo.put("up_last_time",userDataTsJo.getLong("0"));
|
|
|
|
+ beaconDataJo.put("up_last_time2",dateUtil.transferLongToDate("yyyy-MM-dd HH:mm:ss",userDataTsJo.getLong("0")));
|
|
|
|
+ // 电量
|
|
|
|
+ beaconDataJo.put("battery",beaconInfoJo.getJSONObject("userData").getJSONObject("0").getIntValue("battery"));
|
|
|
|
+ beaconDataJo.put("last_gateway",beaconInfoJo.getString("lastGateway"));
|
|
|
|
+ beaconDataJo.put("user_data_ts",beaconInfoJo.getString("userDataTs"));
|
|
|
|
+ beaconDataJo.put("user_data",beaconInfoJo.getString("userData"));
|
|
|
|
+ beaconDataJo.put("rssi",beaconInfoJo.getString("rssi"));
|
|
|
|
+ beaconDataJo.put("map_id",beaconInfoJo.getString("mapId"));
|
|
|
|
+ beaconDataJo.put("zone_id",beaconInfoJo.getString("zoneId"));
|
|
|
|
+ beaconDataJo.put("updated_at",dateUtil.transferLongToDate("yyyy-MM-dd HH:mm:ss",beaconInfoJo.getLong("updatedAt")));
|
|
|
|
+ beaconDataJa.add(beaconDataJo);
|
|
|
|
+ }
|
|
|
|
+ return beaconDataJa;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void saveBeacon() throws UnsupportedEncodingException {
|
|
|
|
+ // 获取远程耳标数据
|
|
|
|
+ JSONArray beaconJa = getRemoteBeacons();
|
|
|
|
+ for(int a=0;a<beaconJa.size();a++){
|
|
|
|
+ JSONObject beaconJo = beaconJa.getJSONObject(a);
|
|
|
|
+// System.out.println("beaconJo="+beaconJo);
|
|
|
|
+ BeaconEntity beaconEntity = new BeaconEntity();
|
|
|
|
+ beaconEntity.setMac(beaconJo.getString("mac"));
|
|
|
|
+ beaconEntity.setX(beaconJo.getString("x"));
|
|
|
|
+ beaconEntity.setY(beaconJo.getString("y"));
|
|
|
|
+ beaconEntity.setZ(beaconJo.getString("z"));
|
|
|
|
+ beaconEntity.setBattery(beaconJo.getIntValue("battery"));
|
|
|
|
+ beaconEntity.setUpLastTime(beaconJo.getLong("up_last_time"));
|
|
|
|
+ beaconEntity.setUpLastTime2(beaconJo.getString("up_last_time2"));
|
|
|
|
+ beaconEntity.setLastGateway(beaconJo.getString("last_gateway"));
|
|
|
|
+ beaconEntity.setUserDataTs(beaconJo.getString("user_data_ts"));
|
|
|
|
+ beaconEntity.setUserData(beaconJo.getString("user_data"));
|
|
|
|
+ beaconEntity.setRssi(beaconJo.getString("rssi"));
|
|
|
|
+ beaconEntity.setMapId(beaconJo.getString("map_id"));
|
|
|
|
+ beaconEntity.setZoneId(beaconJo.getString("zone_id"));
|
|
|
|
+ beaconEntity.setUpdatedAt(beaconJo.getString("updated_at"));
|
|
|
|
+ beaconRepo.saveAndFlush(beaconEntity);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @Method : getOneBeacons
|
|
|
|
+ * @Description : 读取信标数据
|
|
|
|
+ * @Params : [mac, startTime, endTime]
|
|
|
|
+ * @Return : java.util.List<com.huimv.location.dao.entity.BeaconEntity>
|
|
|
|
+ *
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Date : 2021/9/18
|
|
|
|
+ * @Time : 17:29
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public List<BeaconEntity> getOneBeacons(String mac, String startTime, String endTime) {
|
|
|
|
+ // 读取信标数据
|
|
|
|
+ return beaconRepo.getByIdAndStartTimeAndEndTime(mac,startTime,endTime);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public JSONArray getBeaconMac(String data){
|
|
|
|
+ JSONArray beaconMacJa = new JSONArray();
|
|
|
|
+ String[] dataArray = data.split("\":\\{");
|
|
|
|
+ for(int a=0;a<dataArray.length;a++){
|
|
|
|
+ String sectionData = dataArray[a].trim().replace("{","").replace("\"","");
|
|
|
|
+// System.out.println("sectionData>>"+sectionData);
|
|
|
|
+// Pattern pattern = Pattern.compile("(\\d|[a-z]){12}");
|
|
|
|
+// Pattern pattern = Pattern.compile(".*([0-9]|[a-z]{12})*");
|
|
|
|
+ //System.out.println(pattern.pattern());
|
|
|
|
+// String text3 = sectionData.trim().replace("{","").replace("\"","");
|
|
|
|
+// System.out.println("text3>>"+text3);
|
|
|
|
+// Matcher isNum = pattern.matcher(sectionData);
|
|
|
|
+// System.out.println("isNum.matches()>>"+isNum.matches());
|
|
|
|
+ if( sectionData.length() >=12)
|
|
|
|
+ {
|
|
|
|
+ String part = sectionData.substring(sectionData.length() - 12, sectionData.length());
|
|
|
|
+// System.out.println("part:" + part);
|
|
|
|
+ Pattern pattern = Pattern.compile("[a-z0-9]+");
|
|
|
|
+ Matcher isNum = pattern.matcher(part);
|
|
|
|
+// System.out.println("isNum.matches()>>"+isNum.matches());
|
|
|
|
+ if( isNum.matches() )
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+// if (mac.length() == 12) {
|
|
|
|
+ beaconMacJa.add(part);
|
|
|
|
+// String t = textArray3[a].replace("{","").replace("\"","");
|
|
|
|
+// System.out.println("t:"+t);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return beaconMacJa;
|
|
|
|
+ }
|
|
|
|
+}
|