|
@@ -0,0 +1,233 @@
|
|
|
|
+package com.huimv.env.device.listener;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.huimv.env.common.dao.entity.EnvAmmoniaEntity;
|
|
|
|
+import com.huimv.env.common.dao.entity.EnvDeviceRegisterEntity;
|
|
|
|
+import com.huimv.env.common.dao.entity.EnvHumiEntity;
|
|
|
|
+import com.huimv.env.common.dao.entity.EnvTempEntity;
|
|
|
|
+import com.huimv.env.common.dao.repo.EnvAmmoniaEntityRepo;
|
|
|
|
+import com.huimv.env.common.dao.repo.EnvHumiEntityRepo;
|
|
|
|
+import com.huimv.env.common.dao.repo.EnvTempEntityRepo;
|
|
|
|
+import com.huimv.env.common.service.IDeviceRegisterService;
|
|
|
|
+import com.huimv.env.common.service.ISensorRegisterService;
|
|
|
|
+import com.huimv.env.common.utils.Const;
|
|
|
|
+import com.huimv.env.common.utils.DateUtil;
|
|
|
|
+import com.huimv.env.common.utils.MathUtil;
|
|
|
|
+import com.huimv.env.device.producer.Producer;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
|
|
|
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.math.RoundingMode;
|
|
|
|
+import java.sql.Date;
|
|
|
|
+import java.sql.Timestamp;
|
|
|
|
+import java.text.ParseException;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Project : huimv.shiwan
|
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
|
+ * @Description : TODO
|
|
|
|
+ * @Version : 1.0
|
|
|
|
+ * @Author : ZhuoNing
|
|
|
|
+ * @Create : 2020-12-25
|
|
|
|
+ **/
|
|
|
|
+@Component
|
|
|
|
+@Slf4j
|
|
|
|
+public class DeviceListener {
|
|
|
|
+ @Autowired
|
|
|
|
+ private EnvTempEntityRepo envTempEntityRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EnvHumiEntityRepo envHumiEntityRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private EnvAmmoniaEntityRepo envAmmoniaEntityRepo;
|
|
|
|
+ @Autowired
|
|
|
|
+ private IDeviceRegisterService deviceRegisterService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ISensorRegisterService sensorRegisterService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private MathUtil mathUtil;
|
|
|
|
+ @Autowired
|
|
|
|
+ private DateUtil dateUtil;
|
|
|
|
+ @Autowired
|
|
|
|
+ private Producer producer;
|
|
|
|
+
|
|
|
|
+ @RabbitListener(queues = Const.QUEUE_TEMP)
|
|
|
|
+ @RabbitHandler
|
|
|
|
+ public void processTemp(Map RawMap) throws ParseException {
|
|
|
|
+ System.out.println(">>>>>>>>>>>>>>接收温度数据 RawMap>>" + RawMap.toString());
|
|
|
|
+ String askText = RawMap.get("askText").toString();
|
|
|
|
+ JSONObject dataJo = handleAskText(askText);
|
|
|
|
+ dataJo.put("dataUnit", "°");
|
|
|
|
+ System.out.println("温度数据=" + dataJo);
|
|
|
|
+ java.sql.Date todayDate = new java.sql.Date(new java.util.Date().getTime());
|
|
|
|
+ Timestamp nowTimestamp = new Timestamp(new java.util.Date().getTime());
|
|
|
|
+ String deviceCode = dataJo.getString("deviceCode");
|
|
|
|
+ //根据设备编码获取设备注册信息
|
|
|
|
+ EnvDeviceRegisterEntity envDeviceRegisterEntity = deviceRegisterService.getDeviceRegisterByDeviceCode(deviceCode);
|
|
|
|
+ if (envDeviceRegisterEntity == null) {
|
|
|
|
+ log.error("该设备[" + dataJo.getString("deviceCode") + "]未注册.");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ String farmCode = envDeviceRegisterEntity.getFarmCode();
|
|
|
|
+ String temp = new BigDecimal(mathUtil.countTemp(dataJo.getIntValue("value"))).setScale(1, RoundingMode.UP) + dataJo.getString("dataUnit");
|
|
|
|
+ int sensorSort = 1;
|
|
|
|
+ //更新传感器数据
|
|
|
|
+// updateSensorRegister(deviceCode, dataJo.getInteger("sensorSn"), nowTimestamp, todayDate, temp, farmCode,sensorSort);
|
|
|
|
+ sendSensorToMQ(deviceCode, dataJo.getInteger("sensorSn"), nowTimestamp, todayDate, temp, farmCode,sensorSort);
|
|
|
|
+
|
|
|
|
+ //保存温度流水数据
|
|
|
|
+ saveTempFlow(dataJo, envDeviceRegisterEntity, nowTimestamp, todayDate, farmCode);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void sendSensorToMQ(String deviceCode, Integer sensorSn, Timestamp nowTimestamp, Date todayDate, String value, String farmCode, int sensorSort) {
|
|
|
|
+ JSONObject sensorJo = new JSONObject();
|
|
|
|
+ sensorJo.put("deviceCode",deviceCode);
|
|
|
|
+ sensorJo.put("sensorSn",sensorSn);
|
|
|
|
+ sensorJo.put("timestamp",nowTimestamp);
|
|
|
|
+ sensorJo.put("todayDate",todayDate);
|
|
|
|
+ sensorJo.put("value",value);
|
|
|
|
+ sensorJo.put("farmCode",farmCode);
|
|
|
|
+ sensorJo.put("sensorSort",sensorSort);
|
|
|
|
+ producer.sendSensorAskToMQ(sensorJo);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //更新传感器数据
|
|
|
|
+ private void updateSensorRegister(String deviceCode, int sensorSn, Timestamp nowTimestamp, Date todayDate, String value, String farmCode, int sensorSort) {
|
|
|
|
+ if (sensorRegisterService.isExistSensorRegister(deviceCode, sensorSn, farmCode,sensorSort)) {
|
|
|
|
+ System.out.println("更新...");
|
|
|
|
+ //{更新传感器注册信息}
|
|
|
|
+ sensorRegisterService.updateSensorRegister(deviceCode, sensorSn, nowTimestamp, todayDate, value, farmCode,sensorSort);
|
|
|
|
+ } else {
|
|
|
|
+ System.out.println("新建...");
|
|
|
|
+ //{保存传感器注册信息}
|
|
|
|
+ sensorRegisterService.saveSensorRegister(deviceCode, sensorSn, nowTimestamp, todayDate, value, farmCode,sensorSort);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //保存温度流水数据
|
|
|
|
+ private void saveTempFlow(JSONObject dataJo, EnvDeviceRegisterEntity envDeviceRegisterEntity, Timestamp nowTimestamp, Date todayDate, String farmCode) {
|
|
|
|
+ EnvTempEntity envTempEntity = new EnvTempEntity();
|
|
|
|
+ envTempEntity.setDeviceCode(dataJo.getString("deviceCode"));
|
|
|
|
+ envTempEntity.setSensorSn(dataJo.getInteger("sensorSn"));
|
|
|
|
+ envTempEntity.setTemp(new BigDecimal(mathUtil.countTemp(dataJo.getIntValue("value"))));
|
|
|
|
+ envTempEntity.setAskTime(dateUtil.handleAskTime(dataJo.getString("askTime")));
|
|
|
|
+ envTempEntity.setAddTime(nowTimestamp);
|
|
|
|
+ envTempEntity.setAddDate(todayDate);
|
|
|
|
+ envTempEntity.setFarmCode(farmCode);
|
|
|
|
+ envTempEntity.setPigpenId(envDeviceRegisterEntity.getPigpenId());
|
|
|
|
+ envTempEntity.setUnitId(envDeviceRegisterEntity.getUnitId());
|
|
|
|
+ envTempEntity.setDataUnit(dataJo.getString("dataUnit"));
|
|
|
|
+ envTempEntityRepo.saveAndFlush(envTempEntity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RabbitListener(queues = Const.QUEUE_HUMI)
|
|
|
|
+ @RabbitHandler
|
|
|
|
+ public void processHumi(Map RawMap) throws ParseException {
|
|
|
|
+ System.out.println(">>>>>>>>>>>>>>接收湿度数据 RawMap>>" + RawMap.toString());
|
|
|
|
+ String askText = RawMap.get("askText").toString();
|
|
|
|
+ JSONObject dataJo = handleAskText(askText);
|
|
|
|
+ dataJo.put("dataUnit", "%");
|
|
|
|
+ System.out.println("湿度数据=" + dataJo);
|
|
|
|
+ String todayDateText = new DateUtil().getTodayDateText();
|
|
|
|
+ java.sql.Date todayDate = new java.sql.Date(new java.util.Date().getTime());
|
|
|
|
+ Timestamp nowTimestamp = new Timestamp(new java.util.Date().getTime());
|
|
|
|
+ String deviceCode = dataJo.getString("deviceCode");
|
|
|
|
+ //根据设备编码获取设备注册信息
|
|
|
|
+ EnvDeviceRegisterEntity envDeviceRegisterEntity = deviceRegisterService.getDeviceRegisterByDeviceCode(deviceCode);
|
|
|
|
+ if (envDeviceRegisterEntity == null) {
|
|
|
|
+ log.error("该设备[" + dataJo.getString("deviceCode") + "]未注册.");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ String farmCode = envDeviceRegisterEntity.getFarmCode();
|
|
|
|
+ String humi = new BigDecimal(mathUtil.countTemp(dataJo.getIntValue("value"))).setScale(1, RoundingMode.UP) + dataJo.getString("dataUnit");
|
|
|
|
+ int sensorSort = 2;
|
|
|
|
+ //更新传感器数据
|
|
|
|
+// updateSensorRegister(deviceCode, dataJo.getInteger("sensorSn"), nowTimestamp, todayDate, humi, farmCode, sensorSort);
|
|
|
|
+ sendSensorToMQ(deviceCode, dataJo.getInteger("sensorSn"), nowTimestamp, todayDate, humi, farmCode, sensorSort);
|
|
|
|
+ //保存湿度流水数据
|
|
|
|
+ saveHumiFlow(dataJo, envDeviceRegisterEntity, nowTimestamp, todayDate, farmCode);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //保存湿度流水数据
|
|
|
|
+ private void saveHumiFlow(JSONObject dataJo, EnvDeviceRegisterEntity envDeviceRegisterEntity, Timestamp nowTimestamp, Date todayDate, String farmCode) {
|
|
|
|
+ EnvHumiEntity envHumiEntity = new EnvHumiEntity();
|
|
|
|
+ envHumiEntity.setDeviceCode(dataJo.getString("deviceCode"));
|
|
|
|
+ envHumiEntity.setSensorSn(dataJo.getInteger("sensorSn"));
|
|
|
|
+ envHumiEntity.setHumi(new BigDecimal(mathUtil.countHumi(dataJo.getIntValue("value"))));
|
|
|
|
+ envHumiEntity.setAskTime(dateUtil.handleAskTime(dataJo.getString("askTime")));
|
|
|
|
+ envHumiEntity.setAddTime(nowTimestamp);
|
|
|
|
+ envHumiEntity.setAddDate(todayDate);
|
|
|
|
+ envHumiEntity.setFarmCode(farmCode);
|
|
|
|
+ envHumiEntity.setPigpenId(envDeviceRegisterEntity.getPigpenId());
|
|
|
|
+ envHumiEntity.setUnitId(envDeviceRegisterEntity.getUnitId());
|
|
|
|
+ envHumiEntity.setDataUnit(dataJo.getString("dataUnit"));
|
|
|
|
+ envHumiEntityRepo.saveAndFlush(envHumiEntity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @RabbitListener(queues = Const.QUEUE_AMMONIA)
|
|
|
|
+ @RabbitHandler
|
|
|
|
+ public void processAmmonia(Map RawMap) throws ParseException {
|
|
|
|
+ System.out.println(">>>>>>>>>>>>>>接收氨气数据 RawMap>>" + RawMap.toString());
|
|
|
|
+ String askText = RawMap.get("askText").toString();
|
|
|
|
+ JSONObject dataJo = handleAskText(askText);
|
|
|
|
+ dataJo.put("dataUnit", "ppm");
|
|
|
|
+ System.out.println("氨气数据=" + dataJo);
|
|
|
|
+ String todayDateText = new DateUtil().getTodayDateText();
|
|
|
|
+ java.sql.Date todayDate = new java.sql.Date(new java.util.Date().getTime());
|
|
|
|
+ Timestamp nowTimestamp = new Timestamp(new java.util.Date().getTime());
|
|
|
|
+ String deviceCode = dataJo.getString("deviceCode");
|
|
|
|
+ //根据设备编码获取设备注册信息
|
|
|
|
+ EnvDeviceRegisterEntity envDeviceRegisterEntity = deviceRegisterService.getDeviceRegisterByDeviceCode(dataJo.getString("deviceCode"));
|
|
|
|
+ if (envDeviceRegisterEntity == null) {
|
|
|
|
+ log.error("该设备[" + dataJo.getString("deviceCode") + "]未注册.");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ String farmCode = envDeviceRegisterEntity.getFarmCode();
|
|
|
|
+ String anmonia = dataJo.getString("value") + dataJo.getString("dataUnit");
|
|
|
|
+ int sensorSort = 3;
|
|
|
|
+ //更新传感器数据
|
|
|
|
+// updateSensorRegister(deviceCode, dataJo.getInteger("sensorSn"), nowTimestamp, todayDate, anmonia, farmCode, sensorSort);
|
|
|
|
+ sendSensorToMQ(deviceCode, dataJo.getInteger("sensorSn"), nowTimestamp, todayDate, anmonia, farmCode, sensorSort);
|
|
|
|
+ //保存氨气流水数据
|
|
|
|
+ saveAmmoniaFlow(dataJo, envDeviceRegisterEntity, nowTimestamp, todayDate, farmCode);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //保存氨气流水数据
|
|
|
|
+ private void saveAmmoniaFlow(JSONObject dataJo, EnvDeviceRegisterEntity envDeviceRegisterEntity, Timestamp nowTimestamp, Date todayDate, String farmCode) {
|
|
|
|
+ EnvAmmoniaEntity envAmmoniaEntity = new EnvAmmoniaEntity();
|
|
|
|
+ envAmmoniaEntity.setDeviceCode(dataJo.getString("deviceCode"));
|
|
|
|
+ envAmmoniaEntity.setSensorSn(dataJo.getInteger("sensorSn"));
|
|
|
|
+ envAmmoniaEntity.setAmmonia(new BigDecimal(dataJo.getIntValue("value")));
|
|
|
|
+ envAmmoniaEntity.setAskTime(dateUtil.handleAskTime(dataJo.getString("askTime")));
|
|
|
|
+ envAmmoniaEntity.setAddTime(nowTimestamp);
|
|
|
|
+ envAmmoniaEntity.setAddDate(todayDate);
|
|
|
|
+ envAmmoniaEntity.setFarmCode(farmCode);
|
|
|
|
+ envAmmoniaEntity.setPigpenId(envDeviceRegisterEntity.getPigpenId());
|
|
|
|
+ envAmmoniaEntity.setUnitId(envDeviceRegisterEntity.getUnitId());
|
|
|
|
+ envAmmoniaEntity.setDataUnit(dataJo.getString("dataUnit"));
|
|
|
|
+ envAmmoniaEntityRepo.saveAndFlush(envAmmoniaEntity);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+ private JSONObject handleAskText(String askText) {
|
|
|
|
+ String[] dataArray = askText.split("\\+");
|
|
|
|
+ return buildDataObj(dataArray);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //
|
|
|
|
+ public JSONObject buildDataObj(String[] dataArray) {
|
|
|
|
+ JSONObject dataJo = new JSONObject();
|
|
|
|
+ dataJo.put("deviceCode", dataArray[1]);
|
|
|
|
+ dataJo.put("cmd", dataArray[2]);
|
|
|
|
+ dataJo.put("sensorSn", dataArray[3]);
|
|
|
|
+ dataJo.put("value", dataArray[4]);
|
|
|
|
+ dataJo.put("askTime", dataArray[5]);
|
|
|
|
+ return dataJo;
|
|
|
|
+ }
|
|
|
|
+}
|