|
@@ -0,0 +1,124 @@
|
|
|
|
+package com.huimv.datacollection.service.impl;
|
|
|
|
+
|
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.huimv.common.exception.RRException;
|
|
|
|
+import com.huimv.common.utils.MathUtil;
|
|
|
|
+import com.huimv.common.utils.R;
|
|
|
|
+import com.huimv.datacollection.dao.DeviceUpDataDao;
|
|
|
|
+import com.huimv.datacollection.entity.DeviceEntity;
|
|
|
|
+import com.huimv.datacollection.entity.DeviceUpDataEntity;
|
|
|
|
+import com.huimv.datacollection.entity.WarningInfoEntity;
|
|
|
|
+import com.huimv.datacollection.feign.ManagementFeignService;
|
|
|
|
+import com.huimv.datacollection.service.DeviceUpDataService;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * <p>
|
|
|
|
+ * 设备数据上传和报警
|
|
|
|
+ * </p>
|
|
|
|
+ *
|
|
|
|
+ * @author yinhao
|
|
|
|
+ * @date 2021/7/5 16:12
|
|
|
|
+ */
|
|
|
|
+@Service("deviceUpDataServiceImpl")
|
|
|
|
+public class DeviceUpDataServiceImpl extends ServiceImpl<DeviceUpDataDao, DeviceUpDataEntity> implements DeviceUpDataService {
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ ManagementFeignService managementFeignService;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void saveDeviceUpData(JSONObject jsonData) {
|
|
|
|
+ String productId = jsonData.getString("productId");
|
|
|
|
+ String deviceId = jsonData.getString("deviceId");
|
|
|
|
+ long timestamp = jsonData.getLong("timestamp");
|
|
|
|
+ String upDataDate = DateUtil.date(timestamp).toString();
|
|
|
|
+ String serviceId = jsonData.getString("serviceId");
|
|
|
|
+ String imsi = jsonData.getString("IMSI");
|
|
|
|
+ String imei = jsonData.getString("IMEI");
|
|
|
|
+ String deviceType = jsonData.getString("deviceType");
|
|
|
|
+ String messageType = jsonData.getString("messageType");
|
|
|
|
+ String protocol = jsonData.getString("protocol");
|
|
|
|
+ String tenantId = jsonData.getString("tenantId");
|
|
|
|
+ JSONObject businessDataJo = jsonData.getJSONObject("payload");
|
|
|
|
+ int dataVer = businessDataJo.getInteger("VER");
|
|
|
|
+ int dataTime = businessDataJo.getInteger("TIME");
|
|
|
|
+ int dataTemp = businessDataJo.getInteger("TEMP");
|
|
|
|
+ int dataStc = businessDataJo.getInteger("STC");
|
|
|
|
+ int dataRssi = businessDataJo.getInteger("RSSI");
|
|
|
|
+ int dataHumi = businessDataJo.getInteger("HUMI");
|
|
|
|
+ int dataBat = businessDataJo.getInteger("BAT");
|
|
|
|
+ DeviceUpDataEntity deviceUpdataEntity = new DeviceUpDataEntity();
|
|
|
|
+ deviceUpdataEntity.setProductId(productId);
|
|
|
|
+ deviceUpdataEntity.setDeviceId(deviceId);
|
|
|
|
+ deviceUpdataEntity.setTimestamp(upDataDate);
|
|
|
|
+ deviceUpdataEntity.setServiceId(serviceId);
|
|
|
|
+ deviceUpdataEntity.setImei(imei);
|
|
|
|
+ deviceUpdataEntity.setImsi(imsi);
|
|
|
|
+ deviceUpdataEntity.setDeviceType(deviceType);
|
|
|
|
+ deviceUpdataEntity.setMessageType(messageType);
|
|
|
|
+ deviceUpdataEntity.setProtocol(protocol);
|
|
|
|
+ deviceUpdataEntity.setDataVer(dataVer);
|
|
|
|
+ deviceUpdataEntity.setDataTime(dataTime);
|
|
|
|
+ deviceUpdataEntity.setDataTemp(dataTemp);
|
|
|
|
+ deviceUpdataEntity.setDataStc(dataStc);
|
|
|
|
+ deviceUpdataEntity.setDataRssi(dataRssi);
|
|
|
|
+ deviceUpdataEntity.setDataHumi(dataHumi);
|
|
|
|
+ deviceUpdataEntity.setDataBat(dataBat);
|
|
|
|
+ deviceUpdataEntity.setTenantId(tenantId);
|
|
|
|
+ double dataTempConvert = MathUtil.formatDecimalNumber(dataTemp * 0.1, 1);
|
|
|
|
+ deviceUpdataEntity.setDataTempConvert(dataTempConvert);
|
|
|
|
+ double dataHumiConvert = MathUtil.formatDecimalNumber(dataHumi * 0.1, 1);
|
|
|
|
+ deviceUpdataEntity.setDataHumiConvert(dataHumiConvert);
|
|
|
|
+ deviceUpdataEntity.setDataBatConvert(MathUtil.formatDecimalNumber(dataBat * 0.01, 1));
|
|
|
|
+ save(deviceUpdataEntity);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ Map<String, Double> map = managementFeignService.selectByType((short) 1);
|
|
|
|
+ Double highTemp = map.get("高温");
|
|
|
|
+ double highTempThreshold = highTemp == null ? 40.0 : highTemp;
|
|
|
|
+ Double lowTemp = map.get("低温");
|
|
|
|
+ double lowTempThreshold = lowTemp == null ? 15.0 : lowTemp;
|
|
|
|
+
|
|
|
|
+ if (dataTempConvert > highTempThreshold) {
|
|
|
|
+ insertWarningInfo(deviceId, dataTempConvert, "高温");
|
|
|
|
+ } else if (dataTempConvert < lowTempThreshold) {
|
|
|
|
+ insertWarningInfo(deviceId, dataTempConvert, "低温");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ Double highHumidity = map.get("湿度过高");
|
|
|
|
+ double highHumidityThreshold = highHumidity == null ? 80.0 : highHumidity;
|
|
|
|
+ Double lowHumidity = map.get("湿度过低");
|
|
|
|
+ double lowHumidityThreshold = lowHumidity == null ? 30.0 : lowHumidity;
|
|
|
|
+ if (dataHumiConvert > highHumidityThreshold) {
|
|
|
|
+ insertWarningInfo(deviceId,dataHumiConvert,"湿度过高");
|
|
|
|
+ } else if (dataHumiConvert < lowHumidityThreshold) {
|
|
|
|
+ insertWarningInfo(deviceId,dataHumiConvert,"湿度过低");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void insertWarningInfo(String deviceId, double nowValue, String warningType) {
|
|
|
|
+ DeviceEntity deviceInfo = (DeviceEntity) managementFeignService.info(deviceId).get("device");
|
|
|
|
+ WarningInfoEntity warningInfoEntity = new WarningInfoEntity();
|
|
|
|
+ DateTime date = DateUtil.date();
|
|
|
|
+ warningInfoEntity.setNowDate(date);
|
|
|
|
+ warningInfoEntity.setFarmId(deviceInfo.getFarmId());
|
|
|
|
+ warningInfoEntity.setPigstyId(deviceInfo.getPigstyId());
|
|
|
|
+ warningInfoEntity.setWarningType(warningType);
|
|
|
|
+ warningInfoEntity.setBelongTo((short) 1);
|
|
|
|
+ warningInfoEntity.setNowValue(nowValue);
|
|
|
|
+ warningInfoEntity.setWarningTime(date);
|
|
|
|
+
|
|
|
|
+ managementFeignService.save(warningInfoEntity);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|