|
|
@@ -3,6 +3,8 @@ package com.tuoren.forward.config.amqp;
|
|
|
import java.net.URI;
|
|
|
import java.util.Date;
|
|
|
import java.util.Hashtable;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
@@ -35,16 +37,21 @@ import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.tuoren.forward.config.mqtt.MqttUtil;
|
|
|
import com.tuoren.forward.constant.CommonConstant;
|
|
|
import com.tuoren.forward.entity.Device;
|
|
|
+import com.tuoren.forward.entity.Model;
|
|
|
import com.tuoren.forward.entity.SatasticDay;
|
|
|
import com.tuoren.forward.entity.StationLocation;
|
|
|
import com.tuoren.forward.entity.User;
|
|
|
+import com.tuoren.forward.entity.dto.ModelDto;
|
|
|
import com.tuoren.forward.mapper.DeviceMapper;
|
|
|
+import com.tuoren.forward.mapper.ModelMapper;
|
|
|
import com.tuoren.forward.mapper.SatasticDayMapper;
|
|
|
import com.tuoren.forward.mapper.StationLocationMapper;
|
|
|
import com.tuoren.forward.mapper.UserMapper;
|
|
|
+import com.tuoren.forward.service.StationLocationService;
|
|
|
import com.tuoren.forward.util.RemoteUtil;
|
|
|
import com.tuoren.forward.util.UUIDUtil;
|
|
|
|
|
|
+import cn.hutool.core.util.ReUtil;
|
|
|
import lombok.Setter;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
@@ -90,7 +97,13 @@ public class AmqpClient2 {
|
|
|
MongoTemplate mongoTemplate;
|
|
|
|
|
|
@Autowired
|
|
|
- StationLocationMapper stationLocationMapper;
|
|
|
+ StationLocationMapper stationLocationMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ StationLocationService stationLocationService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ ModelMapper modelMapper;
|
|
|
|
|
|
//业务处理异步线程池,线程池参数可以根据您的业务特点调整,或者您也可以用其他异步方式处理接收到的消息。
|
|
|
private final static ExecutorService executorService = new ThreadPoolExecutor(
|
|
|
@@ -172,12 +185,23 @@ public class AmqpClient2 {
|
|
|
}
|
|
|
byte[] body = message.getBody(byte[].class);
|
|
|
String content = new String(body);
|
|
|
- log.info("下发数据内容1:"+content);
|
|
|
+ log.info("下发数据内容2:"+content);
|
|
|
JSONObject json = JSONObject.parseObject(content);
|
|
|
repackJson(json);
|
|
|
String deviceName = json.getString("deviceName");
|
|
|
- String productKey = json.getString("productKey");
|
|
|
+// String productKey = json.getString("productKey");
|
|
|
+ String productKey = "1dbfd476b7nm2";
|
|
|
JSONObject items = json.getJSONObject("items");
|
|
|
+ aliyunTranslate(items);
|
|
|
+ String valiStr =modelValidate(productKey,items);
|
|
|
+ String reason="";
|
|
|
+ String code ="success";
|
|
|
+ if(!StringUtils.isEmpty(valiStr)) {
|
|
|
+ log.info("不符合产品模型:"+valiStr);
|
|
|
+ reason = "不符合产品模型:"+valiStr;
|
|
|
+ code ="fail";
|
|
|
+ }
|
|
|
+
|
|
|
JSONObject localJson = null;
|
|
|
|
|
|
String hospitalCode = !items.containsKey("userId") ? "" : items.getString("userId");
|
|
|
@@ -191,82 +215,100 @@ public class AmqpClient2 {
|
|
|
hospitalCode = remoteUtil.getHospitalCode(deviceName);
|
|
|
if(StringUtils.isEmpty(hospitalCode)) {
|
|
|
log.info("设备没有对应医院:"+deviceName);
|
|
|
- return;
|
|
|
+ // return;
|
|
|
}
|
|
|
}
|
|
|
- User owner = userMapper.selectByCode(hospitalCode);
|
|
|
- if(owner == null) {
|
|
|
- log.info("不存在该医院:"+hospitalCode);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
+ String tenentId = "";
|
|
|
+ if(!StringUtils.isEmpty(hospitalCode)) {
|
|
|
+ User owner = userMapper.selectByCode(hospitalCode);
|
|
|
+ if(owner == null) {
|
|
|
+ log.info("不存在该医院:"+hospitalCode);
|
|
|
+ //return;
|
|
|
+ }else {
|
|
|
+ tenentId = owner.getId();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
String ci = !items.containsKey("ci") ? "" : items.getString("ci");
|
|
|
String lac = !items.containsKey("lac") ? "" : items.getString("lac");
|
|
|
if(!"".equals(ci) && !"".equals(lac)) {
|
|
|
String mnc = !items.containsKey("mnc") ? "00" : items.getString("mnc");
|
|
|
localJson = getLocation(mnc,lac,ci);
|
|
|
- items.put("location", localJson);
|
|
|
+// items.put("location", localJson);
|
|
|
+ items.putAll(localJson);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
JSONObject mongoJson = new JSONObject();
|
|
|
Device exist = deviceMapper.selectByMac(deviceName);
|
|
|
+ Device record = new Device();
|
|
|
if(exist == null) {
|
|
|
- Device record = new Device();
|
|
|
String deviceId = UUIDUtil.get32UUID();
|
|
|
record.setId(deviceId);
|
|
|
record.setCreatetime(date);
|
|
|
record.setModifytime(date);
|
|
|
record.setMac(deviceName);
|
|
|
record.setProductId(productKey);
|
|
|
- record.setTenantId(owner.getId());
|
|
|
+ record.setTenantId(tenentId);
|
|
|
if(localJson != null) {
|
|
|
record.setLat(localJson.getString("lat"));
|
|
|
record.setLng(localJson.getString("lng"));
|
|
|
record.setAddress(localJson.getString("address"));
|
|
|
record.setRadius(localJson.getString("radius"));
|
|
|
}
|
|
|
- record.setData(items.toString());
|
|
|
+ record.setData(packItemsTime(items,json.getLong("gmtCreate")).toString());
|
|
|
deviceMapper.insertSelective(record);
|
|
|
mongoJson.put("deviceId", deviceId);
|
|
|
}else {
|
|
|
- Device record = new Device();
|
|
|
+ record = exist;
|
|
|
record.setId(exist.getId());
|
|
|
record.setModifytime(date);
|
|
|
- record.setTenantId(owner.getId());
|
|
|
+ record.setProductId(productKey);
|
|
|
+ record.setTenantId(tenentId);
|
|
|
if(localJson != null) {
|
|
|
record.setLat(localJson.getString("lat"));
|
|
|
record.setLng(localJson.getString("lng"));
|
|
|
record.setAddress(localJson.getString("address"));
|
|
|
record.setRadius(localJson.getString("radius"));
|
|
|
}
|
|
|
- record.setData( items.toString());
|
|
|
+ JSONObject newItems = packItemsTime(items, json.getLong("gmtCreate"));
|
|
|
+ String lastData = record.getData();
|
|
|
+ if(!StringUtils.isEmpty(lastData)) {
|
|
|
+ JSONObject lastItems = JSONObject.parseObject(lastData);
|
|
|
+ lastItems.putAll(newItems);
|
|
|
+ newItems = lastItems;
|
|
|
+ }
|
|
|
+ record.setData( newItems.toString());
|
|
|
deviceMapper.updateByPrimaryKeySelective2(record);
|
|
|
mongoJson.put("deviceId", exist.getId());
|
|
|
}
|
|
|
mongoJson.put("deviceMac", deviceName);
|
|
|
mongoJson.put("time", json.getLong("gmtCreate"));
|
|
|
- mongoJson.put("tenantId", owner.getId());
|
|
|
+ mongoJson.put("tenantId", tenentId);
|
|
|
mongoJson.put("items", items);
|
|
|
+ mongoJson.put("content", content);
|
|
|
+ mongoJson.put("reason", reason);
|
|
|
+ mongoJson.put("code", code);
|
|
|
mongoTemplate.insert(mongoJson, CommonConstant.MONGO_COLLECTION_DEVICE_RECORD);
|
|
|
|
|
|
String sataType = "forward";
|
|
|
SatasticDay existSata = satasticDayMapper.selectSataTodayByType(sataType);
|
|
|
if(existSata == null) {
|
|
|
- SatasticDay record = new SatasticDay();
|
|
|
- record.setId(UUIDUtil.get32UUID());
|
|
|
- record.setType(sataType);
|
|
|
- record.setNum(1);
|
|
|
- record.setCreatedate(date);
|
|
|
- satasticDayMapper.insert(record);
|
|
|
+ SatasticDay recordSta = new SatasticDay();
|
|
|
+ recordSta.setId(UUIDUtil.get32UUID());
|
|
|
+ recordSta.setType(sataType);
|
|
|
+ recordSta.setNum(1);
|
|
|
+ recordSta.setCreatedate(date);
|
|
|
+ satasticDayMapper.insert(recordSta);
|
|
|
}else {
|
|
|
existSata.setNum(existSata.getNum()+1);
|
|
|
satasticDayMapper.updateByPrimaryKey(existSata);
|
|
|
}
|
|
|
|
|
|
- if(mqttUtil != null) {
|
|
|
- log.info("mqtt publish1:"+ json.toString());
|
|
|
- mqttUtil.pub(CommonConstant.MQTT_PUBLISH_PREFIX+hospitalCode, json.toString());
|
|
|
+ if(mqttUtil != null && !StringUtils.isEmpty(hospitalCode)) {
|
|
|
+ JSONObject mqttJson = JSONObject.from(record);
|
|
|
+ mqttJson.put("items", items);
|
|
|
+ log.info("mqtt publish2:"+ mqttJson.toString());
|
|
|
+ mqttUtil.pub(CommonConstant.MQTT_PUBLISH_PREFIX+hospitalCode, mqttJson.toString());
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
log.error("processMessage occurs error ", e);
|
|
|
@@ -281,11 +323,11 @@ public class AmqpClient2 {
|
|
|
String location = remoteUtil.localtion(mnc, lac, ci);
|
|
|
if(!StringUtils.isEmpty(location)) {
|
|
|
localJson = JSONObject.parseObject(location);
|
|
|
- String newPoint = remoteUtil.getBaiduPoint(localJson.getString("lng"),localJson.getString("lat"));
|
|
|
+ String newPoint = remoteUtil.getBaiduPoint2(localJson.getString("lng"),localJson.getString("lat"));
|
|
|
if(!StringUtils.isEmpty(newPoint)) {
|
|
|
JSONObject pointJson = JSONObject.parseObject(newPoint);
|
|
|
- localJson.put("lng", pointJson.getString("x"));
|
|
|
- localJson.put("lat", pointJson.getString("y"));
|
|
|
+ localJson.put("lng", pointJson.getDouble("x"));
|
|
|
+ localJson.put("lat", pointJson.getDouble("y"));
|
|
|
}
|
|
|
staLocation = new StationLocation();
|
|
|
staLocation.setId(UUIDUtil.get32UUID());
|
|
|
@@ -298,17 +340,135 @@ public class AmqpClient2 {
|
|
|
staLocation.setRadius(localJson.getString("radius"));
|
|
|
staLocation.setCreatetime(date);
|
|
|
staLocation.setModifytime(date);
|
|
|
- stationLocationMapper.insert(staLocation);
|
|
|
+ stationLocationService.add(staLocation);
|
|
|
}
|
|
|
}else {
|
|
|
localJson = new JSONObject();
|
|
|
- localJson.put("lng", staLocation.getLng());
|
|
|
- localJson.put("lat", staLocation.getLat());
|
|
|
+ localJson.put("lng", Double.parseDouble(staLocation.getLng()));
|
|
|
+ localJson.put("lat", Double.parseDouble(staLocation.getLat()));
|
|
|
localJson.put("address", staLocation.getAddress());
|
|
|
- localJson.put("radius", staLocation.getRadius());
|
|
|
+ localJson.put("radius", Integer.parseInt(staLocation.getRadius()));
|
|
|
}
|
|
|
return localJson;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @title 验证产品模型
|
|
|
+ * @param productId
|
|
|
+ * @param items
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String modelValidate(String productId,JSONObject items) {
|
|
|
+ ModelDto dto = new ModelDto();
|
|
|
+ dto.setProductId(productId);
|
|
|
+ List<Model> models = modelMapper.select(dto);
|
|
|
+ List<Model> cmodels = modelMapper.selectCommon(new ModelDto());
|
|
|
+ models.addAll(cmodels);
|
|
|
+ JSONObject newItems = new JSONObject(items);
|
|
|
+ for(String key : newItems.keySet()) {
|
|
|
+ Optional<Model> result = models.stream().filter(m->m.getTitle().equals(key)).findAny();
|
|
|
+ if(!result.isPresent()) {
|
|
|
+ items.remove(key);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ * Model m = result.get(); String resultStr =
|
|
|
+ * validateOne(m,items.getString(key)); if(!StringUtils.isEmpty(resultStr)) {
|
|
|
+ * return resultStr; }
|
|
|
+ */
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String validateOne(Model m, String value) {
|
|
|
+ String title = m.getTitle();
|
|
|
+ JSONObject define = JSONObject.parseObject(m.getDefine());
|
|
|
+ JSONObject range = define.getJSONObject("range");
|
|
|
+ if (range == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ switch (m.getType()) {
|
|
|
+ case "int": {
|
|
|
+ Integer min = range.containsKey("min") ? range.getInteger("min") : null;
|
|
|
+ Integer max = range.containsKey("max") ? range.getInteger("max") : null;
|
|
|
+ //if (define.getBoolean("isArray")) {
|
|
|
+ if (false) {
|
|
|
+ if (!StringUtils.isEmpty(value)) {
|
|
|
+ String[] arr = value.split(",");
|
|
|
+ for (String s : arr) {
|
|
|
+ if (ReUtil.isMatch("^-?\\d*$", s)) {
|
|
|
+ int v = Integer.parseInt(s);
|
|
|
+ if ((min != null && min > v) || (max != null && max < v)) {
|
|
|
+ return "字段:" + title + " 数值超出范围";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (ReUtil.isMatch("^-?\\d*$", value)) {
|
|
|
+ int v = Integer.parseInt(value);
|
|
|
+ if ((min != null && min > v) || (max != null && max < v)) {
|
|
|
+ return "字段:" + title + " 数值超出范围";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "float": {
|
|
|
+ Double min = range.containsKey("min") ? range.getDouble("min") : null;
|
|
|
+ Double max = range.containsKey("max") ? range.getDouble("max") : null;
|
|
|
+ //if (define.getBoolean("isArray")) {
|
|
|
+ if (false) {
|
|
|
+ if (!StringUtils.isEmpty(value)) {
|
|
|
+ String[] arr = value.split(",");
|
|
|
+ for (String s : arr) {
|
|
|
+ if (ReUtil.isMatch("^-?\\d+([.]?\\d+)?$", s)) {
|
|
|
+ Double v = Double.parseDouble(s);
|
|
|
+ if ((min != null && min > v) || (max != null && max < v)) {
|
|
|
+ return "字段:" + title + " 数值超出范围";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (ReUtil.isMatch("^-?\\d+([.]?\\d+)?$", value)) {
|
|
|
+ Double v = Double.parseDouble(value);
|
|
|
+ if ((min != null && min > v) || (max != null && max < v)) {
|
|
|
+ return "字段:" + title + " 数值超出范围";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "String": {
|
|
|
+ Integer len = range.containsKey("len") ? range.getInteger("len") : null;
|
|
|
+// if (define.getBoolean("isArray")) {
|
|
|
+ if (false) {
|
|
|
+ String[] arr = value.split(",");
|
|
|
+ for (String s : arr) {
|
|
|
+ if (len != null && len < s.length()) {
|
|
|
+ return "字段:" + title + " 数值超出范围";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (len != null && len < value.length()) {
|
|
|
+ return "字段:" + title + " 数值超出范围";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ case "enum": {
|
|
|
+ //if (define.getBoolean("isArray")) {
|
|
|
+ if (false) {
|
|
|
+
|
|
|
+ } else if (!range.containsKey(Integer.parseInt(value))) {
|
|
|
+ return "字段:" + title + " 数值超出范围";
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
private static JmsConnectionListener jmsConnectionListener = new JmsConnectionListener() {
|
|
|
/**
|
|
|
* 连接成功建立。
|
|
|
@@ -375,4 +535,43 @@ public class AmqpClient2 {
|
|
|
}
|
|
|
json.replace("items", items);
|
|
|
}
|
|
|
+
|
|
|
+ public static void aliyunTranslate(JSONObject items) {
|
|
|
+ items.put("networkType", 2);
|
|
|
+ items.put("infusionId", items.get("classification"));
|
|
|
+ items.put("ultimateDose", items.get("maxDose"));
|
|
|
+ items.put("continueDose", items.get("flow"));
|
|
|
+ items.put("appendDose", items.get("singleDose"));
|
|
|
+ items.put("finishDose", items.get("finishedDose"));
|
|
|
+ items.put("validTimes", items.get("pcaValid"));
|
|
|
+ items.put("invalidTimes", items.get("pcaInvalid"));
|
|
|
+ items.put("alarm", new int[] {items.getInteger("alarmStatus")});
|
|
|
+ if(items.getInteger("warnLowBattery") == 1) {
|
|
|
+ items.put("forcast", new int[]{3});
|
|
|
+ }
|
|
|
+ if(items.getInteger("warnAnalgesicPoor") == 1) {
|
|
|
+ items.put("forcast", new int[]{2});
|
|
|
+ }
|
|
|
+ if(items.getInteger("warnWillFinished") == 1) {
|
|
|
+ items.put("forcast", new int[]{1});
|
|
|
+ }
|
|
|
+ if(!items.containsKey("forcast")) {
|
|
|
+ items.put("forcast", new int[]{0});
|
|
|
+ }
|
|
|
+
|
|
|
+ items.put("electric", items.get("electricQuantity"));
|
|
|
+
|
|
|
+ items.put("finishDose", items.get("finishedDose"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONObject packItemsTime(JSONObject items, Long timestamp) {
|
|
|
+ JSONObject newItems = new JSONObject();
|
|
|
+ for(String key:items.keySet()) {
|
|
|
+ JSONObject temp = new JSONObject();
|
|
|
+ temp.put("value", items.get(key));
|
|
|
+ temp.put("time", timestamp);
|
|
|
+ newItems.put(key, temp);
|
|
|
+ }
|
|
|
+ return newItems;
|
|
|
+ }
|
|
|
}
|