|
@@ -0,0 +1,140 @@
|
|
|
+package com.huimv.business.farm.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.huimv.business.farm.service.IFarmDevice;
|
|
|
+import com.huimv.business.server.dao.entity.FarmDeviceEntity;
|
|
|
+import com.huimv.business.server.dao.repo.FarmDeviceRepo;
|
|
|
+import com.huimv.business.utils.HttpTemplete;
|
|
|
+import com.huimv.business.utils.SnowflakeIdWorker;
|
|
|
+import com.huimv.business.utils.TextUtil;
|
|
|
+import com.huimv.common.utils.Result;
|
|
|
+import com.huimv.common.utils.ResultCode;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+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.sql.Timestamp;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.Optional;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Project : huimv.shiwan
|
|
|
+ * @Package : com.huimv.biosafety.uface.controller
|
|
|
+ * @Description : TODO
|
|
|
+ * @Version : 1.0
|
|
|
+ * @Author : ZhuoNing
|
|
|
+ * @Create : 2020-12-25
|
|
|
+ **/
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class FarmDeviceImpl implements IFarmDevice {
|
|
|
+ @Autowired
|
|
|
+ private FarmDeviceRepo deviceRepo;
|
|
|
+ @Autowired
|
|
|
+ private TextUtil textUtil;
|
|
|
+ @Autowired
|
|
|
+ private HttpTemplete httpTemplete;
|
|
|
+ @Value("${dataCenter.ipAddr}")
|
|
|
+ private String DATA_CENTER_IP_ADDR;
|
|
|
+ @Value("${dataCenter.device.port}")
|
|
|
+ private String PORT;
|
|
|
+ @Value("${dataCenter.device.sync}")
|
|
|
+ private boolean dataSync;
|
|
|
+ @Value("${dataCenter.device.addService}")
|
|
|
+ private String deviceAddService;
|
|
|
+ @Value("${dataCenter.device.editService}")
|
|
|
+ private String deviceEditService;
|
|
|
+ @Value("${dataCenter.device.removeService}")
|
|
|
+ private String deviceRemoveService;
|
|
|
+ @Value("${local.farmID}")
|
|
|
+ private Integer FARM_ID;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result newDevice(String deviceData) throws UnsupportedEncodingException {
|
|
|
+ //
|
|
|
+ SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 0);
|
|
|
+ JSONObject deviceDataJo = JSON.parseObject(deviceData);
|
|
|
+ FarmDeviceEntity deviceEntity = new FarmDeviceEntity();
|
|
|
+ deviceEntity.setDeviceCode(deviceDataJo.getString("deviceCode"));
|
|
|
+ deviceEntity.setDeviceName(deviceDataJo.getString("deviceName"));
|
|
|
+ deviceEntity.setDeviceType(deviceDataJo.getString("deviceType"));
|
|
|
+ deviceEntity.setFactory(deviceDataJo.getString("factory"));
|
|
|
+ deviceEntity.setFarmId(FARM_ID);
|
|
|
+ deviceEntity.setMainParams(deviceDataJo.getString("mainParams"));
|
|
|
+ deviceEntity.setRecord(deviceDataJo.getString("record"));
|
|
|
+ deviceEntity.setWorker(deviceDataJo.getString("worker"));
|
|
|
+ deviceEntity.setState(deviceDataJo.getInteger("state"));
|
|
|
+ deviceEntity.setLastTime(new Timestamp(new Date().getTime()));
|
|
|
+ deviceEntity.setUpTime(new Timestamp(new Date().getTime()));
|
|
|
+ deviceEntity.setLockinState(1);
|
|
|
+ deviceEntity.setDataId(idWorker.newId());
|
|
|
+ FarmDeviceEntity addDeviceEntity = deviceRepo.saveAndFlush(deviceEntity);
|
|
|
+ log.info("牧场端添加设备信息>>"+addDeviceEntity);
|
|
|
+ //同步更新数据中心端设备数据
|
|
|
+ if(dataSync){
|
|
|
+ log.info("同步数据中心端设备数据。");
|
|
|
+ String serviceUrl = DATA_CENTER_IP_ADDR + PORT + deviceAddService;
|
|
|
+ log.info("serviceUrl>>"+serviceUrl);
|
|
|
+ String data = textUtil.encode(JSON.toJSONString(addDeviceEntity));
|
|
|
+ log.info("base64密文>>"+data);
|
|
|
+ //提交请求
|
|
|
+ httpTemplete.doPostSimple(serviceUrl,data);
|
|
|
+ }
|
|
|
+ return new Result(ResultCode.SUCCESS, addDeviceEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result editDevice(String deviceData) throws UnsupportedEncodingException {
|
|
|
+ //
|
|
|
+ SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 0);
|
|
|
+ JSONObject deviceDataJo = JSON.parseObject(deviceData);
|
|
|
+ //查询记录是否存在
|
|
|
+ Optional<FarmDeviceEntity> optional = deviceRepo.findById(deviceDataJo.getInteger("id"));
|
|
|
+ if(!optional.isPresent()){
|
|
|
+ String ERR_INFO_NOEXIST = "该记录不存在.";
|
|
|
+ log.error(ERR_INFO_NOEXIST);
|
|
|
+ return new Result(10001,ERR_INFO_NOEXIST,false);
|
|
|
+ }
|
|
|
+ //编辑记录
|
|
|
+ FarmDeviceEntity deviceEntity = optional.get();
|
|
|
+ deviceEntity.setDeviceCode(deviceDataJo.getString("deviceCode"));
|
|
|
+ deviceEntity.setDeviceName(deviceDataJo.getString("deviceName"));
|
|
|
+ deviceEntity.setDeviceType(deviceDataJo.getString("deviceType"));
|
|
|
+ deviceEntity.setFactory(deviceDataJo.getString("factory"));
|
|
|
+ deviceEntity.setMainParams(deviceDataJo.getString("mainParams"));
|
|
|
+ deviceEntity.setRecord(deviceDataJo.getString("record"));
|
|
|
+ deviceEntity.setWorker(deviceDataJo.getString("worker"));
|
|
|
+ deviceEntity.setState(deviceDataJo.getInteger("state"));
|
|
|
+ deviceEntity.setLastTime(new Timestamp(new Date().getTime()));
|
|
|
+ deviceEntity.setUpTime(new Timestamp(new Date().getTime()));
|
|
|
+ deviceEntity.setLockinState(1);
|
|
|
+ FarmDeviceEntity addDeviceEntity = deviceRepo.saveAndFlush(deviceEntity);
|
|
|
+ log.info("牧场端添加设备信息>>"+addDeviceEntity);
|
|
|
+ //同步更新数据中心端设备数据
|
|
|
+ if(dataSync){
|
|
|
+ log.info("同步数据中心端设备数据。");
|
|
|
+ String serviceUrl = DATA_CENTER_IP_ADDR + PORT + deviceEditService;
|
|
|
+ log.info("serviceUrl>>"+serviceUrl);
|
|
|
+ String data = textUtil.encode(JSON.toJSONString(addDeviceEntity));
|
|
|
+ log.info("base64密文>>"+data);
|
|
|
+ //提交请求
|
|
|
+ httpTemplete.doPostSimple(serviceUrl,data);
|
|
|
+ }
|
|
|
+ return new Result(ResultCode.SUCCESS, addDeviceEntity);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result removeDevice(String deviceData){
|
|
|
+
|
|
|
+ return new Result(ResultCode.SUCCESS);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result listDevice(String deviceData){
|
|
|
+
|
|
|
+ return new Result(ResultCode.SUCCESS);
|
|
|
+ }
|
|
|
+}
|