24282 hace 2 años
padre
commit
309af1e692

+ 37 - 129
hx-iot/src/main/java/com/fjhx/iot/listener/DtaListener.java

@@ -1,18 +1,11 @@
 package com.fjhx.iot.listener;
 
-import cn.hutool.core.convert.Convert;
 import cn.hutool.extra.spring.SpringUtil;
 import com.alibaba.fastjson.JSONObject;
 import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
 import com.fjhx.common.constant.SourceConstant;
-import com.fjhx.iot.constants.RedisConstant;
 import com.fjhx.iot.entity.tda.dto.MessageBody;
-import com.fjhx.iot.entity.tda.dto.OpcUaData;
-import com.fjhx.iot.entity.tda.dto.UpperComputerData;
-import com.fjhx.iot.entity.tda.dto.ZSJDto;
 import com.fjhx.iot.entity.tda.po.TdaConfig;
-import com.fjhx.iot.enums.ProductEnum;
-import com.fjhx.iot.enums.UpperComputerEnum;
 import com.fjhx.iot.service.tda.TdaConfigService;
 import com.fjhx.iot.utils.amqp.AmqpClient;
 import com.fjhx.iot.utils.amqp.AmqpClientOptions;
@@ -27,12 +20,9 @@ import javax.annotation.PostConstruct;
 import javax.jms.JMSException;
 import javax.jms.Message;
 import javax.jms.MessageConsumer;
-import java.math.BigDecimal;
-import java.math.RoundingMode;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.stream.Collectors;
 
 /**
  * iot数据推送监听器
@@ -107,156 +97,74 @@ public class DtaListener {
      */
     public static void handleData(Message message) {
         try {
-            MessageBody messageBody = JSONObject.parseObject(message.getBody(String.class), MessageBody.class);
+
+            String messageBodyStr = message.getBody(String.class);
+            MessageBody messageBody = JSONObject.parseObject(messageBodyStr, MessageBody.class);
             if (messageBody == null) {
                 return;
             }
-            MessageBody.NotifyData notifyData = messageBody.getNotifyData();
-            if (notifyData == null) {
-                return;
-            }
-            MessageBody.Header header = notifyData.getHeader();
-            if (header == null) {
-                return;
-            }
-            String productId = header.getProductId();
-            if (productId == null || productId.isEmpty()) {
-                return;
-            }
-            MessageBody.Body body = notifyData.getBody();
-            if (body == null) {
-                return;
-            }
-            List<MessageBody.Service> services = body.getServices();
-            if (services == null || services.size() == 0) {
-                return;
-            }
 
-            ProductEnum productEnum = ProductEnum.getEnumByProductId(productId);
-            if (productEnum != null) {
-                appointProductHandle(header.getDeviceId(), message, services);
-            } else {
-                upperComputerHandle(message, services);
+            // 事件类型
+            String event = messageBody.getEvent();
+
+            switch (event) {
+                case "report":
+                    reportHandel(messageBody);
+                    break;
+                default:
+                    log.error("未知事件类型:{}\r\nmessageBody:{}", event, messageBodyStr);
+                    return;
             }
 
-            log.error(JSONObject.toJSONString(messageBody));
+            message.acknowledge();
 
         } catch (JMSException e) {
             e.printStackTrace();
         }
+
     }
 
     /**
-     * 指定设备处理方式
-     *
-     * @param deviceId 设备id
-     * @param message  message
-     * @param services 设备消息列表
+     * 处理报告数据
      */
-    private static void appointProductHandle(String deviceId, Message message, List<MessageBody.Service> services) {
-        String redisKey = RedisConstant.DEVICE_PREFIX + deviceId;
+    private static void reportHandel(MessageBody messageBody) {
 
-        MessageBody.Service oldService = redisCache.getCacheObject(redisKey);
-        MessageBody.Service newService = services.get(0);
+        MessageBody.NotifyData notifyData = messageBody.getNotifyData();
 
-        if (oldService == null || newService.getEventTime().compareTo(oldService.getEventTime()) > 0) {
-            redisCache.setCacheObject(redisKey, newService);
+        if (notifyData == null) {
+            log.error(" notifyData == null , messageBody = {}", messageBody);
+            return;
         }
 
-        try {
-            message.acknowledge();
-        } catch (JMSException e) {
-            e.printStackTrace();
+        MessageBody.Header header = notifyData.getHeader();
+        if (header == null) {
+            log.error(" header == null , messageBody = {}", messageBody);
+            return;
         }
-    }
 
-    /**
-     * 上位机连接设备处理
-     */
-    private static void upperComputerHandle(Message message, List<MessageBody.Service> services) {
-        MessageBody.Service service = services.get(0);
-
-        UpperComputerData newData = service.getProperties().toJavaObject(UpperComputerData.class);
-
-        UpperComputerEnum upperComputerEnum = UpperComputerEnum.getUpperComputerEnum(newData.getType());
+        MessageBody.Body body = notifyData.getBody();
+        if (body == null) {
+            log.error(" body == null , messageBody = {}", messageBody);
+            return;
+        }
 
-        if (upperComputerEnum == UpperComputerEnum.ZSJ) {
-            handleZSJ(message, newData);
+        List<MessageBody.Service> services = body.getServices();
+        if (services == null || services.size() == 0) {
+            log.error(" services == null || services.size() == 0 , messageBody = {}", messageBody);
             return;
         }
 
-        String redisKey = RedisConstant.DEVICE_PREFIX + newData.getEquipmentNo();
-        UpperComputerData oldData = redisCache.getCacheObject(redisKey);
 
-        if (oldData == null || newData.getCreateTime().compareTo(oldData.getCreateTime()) > 0) {
-            redisCache.setCacheObject(redisKey, newData);
-        }
+        String productId = header.getProductId();
 
-        try {
-            message.acknowledge();
-        } catch (JMSException e) {
-            e.printStackTrace();
-        }
-    }
+        String deviceId = header.getDeviceId();
 
-    /**
-     * 注塑机解析
-     */
-    private static void handleZSJ(Message message, UpperComputerData upperComputerData) {
+        MessageBody.Service service = services.get(0);
 
-        log.error("注塑机推送数据:{}", JSONObject.toJSONString(upperComputerData));
 
-        String redisKey = RedisConstant.DEVICE_PREFIX + upperComputerData.getEquipmentNo();
-        ZSJDto oldZsjDto = redisCache.getCacheObject(redisKey);
-        if (oldZsjDto != null && upperComputerData.getCreateTime().compareTo(oldZsjDto.getDate()) > 0) {
-            return;
-        }
-        ZSJDto zsjDto = createZSJDto(upperComputerData);
-        redisCache.setCacheObject(redisKey, zsjDto);
+        System.out.println(service);
 
-        try {
-            message.acknowledge();
-        } catch (JMSException e) {
-            e.printStackTrace();
-        }
     }
 
-    private static ZSJDto createZSJDto(UpperComputerData upperComputerData) {
-        List<OpcUaData> opcUaDataList = Convert.toList(OpcUaData.class, upperComputerData.getData());
-
-        Map<String, String> collect = opcUaDataList.stream().collect(Collectors.toMap(
-                OpcUaData::getId,
-                OpcUaData::getNs
-        ));
-
-        ZSJDto zsjDto = new ZSJDto();
-        zsjDto.setDate(upperComputerData.getCreateTime());
-        zsjDto.setTmOnlineState(Convert.toInt(collect.get("tmOnlineState")));
-        zsjDto.setTmOperateMode(Convert.toInt(collect.get("tmOperateMode")));
-        zsjDto.setTmMotorState(Convert.toInt(collect.get("tmMotorState")));
-        zsjDto.setTmHeatState(Convert.toInt(collect.get("tmHeatState")));
-        zsjDto.setTmTempOilCurrent(Convert.toBigDecimal(collect.get("tmTempOil_Current")));
-
-        BigDecimal tmTempCurrent = Convert.toBigDecimal(collect.get("tmTemp1_Current"))
-                .add(Convert.toBigDecimal(collect.get("tmTemp2_Current")))
-                .add(Convert.toBigDecimal(collect.get("tmTemp3_Current")))
-                .add(Convert.toBigDecimal(collect.get("tmTemp4_Current")))
-                .add(Convert.toBigDecimal(collect.get("tmTemp5_Current")))
-                .add(Convert.toBigDecimal(collect.get("tmTemp6_Current")))
-                .add(Convert.toBigDecimal(collect.get("tmTemp7_Current")))
-                .add(Convert.toBigDecimal(collect.get("tmTemp8_Current")))
-                .add(Convert.toBigDecimal(collect.get("tmTemp9_Current")))
-                .divide(new BigDecimal("9"), 1, RoundingMode.HALF_UP);
-        zsjDto.setTmTempCurrent(tmTempCurrent);
-        zsjDto.setTmChargePressB1(Convert.toBigDecimal(collect.get("tmChargePressB1")));
-        zsjDto.setTmChargePressB2(Convert.toBigDecimal(collect.get("tmChargePressB2")));
-        zsjDto.setTmChargePressB3(Convert.toBigDecimal(collect.get("tmChargePressB3")));
-        zsjDto.setTmChargePressB4(Convert.toBigDecimal(collect.get("tmChargePressB4")));
-        zsjDto.setTmChargeSpeedB1(Convert.toBigDecimal(collect.get("tmChargeSpeedB1")));
-        zsjDto.setTmChargeSpeedB2(Convert.toBigDecimal(collect.get("tmChargeSpeedB2")));
-        zsjDto.setTmChargeSpeedB3(Convert.toBigDecimal(collect.get("tmChargeSpeedB3")));
-        zsjDto.setTmChargeSpeedB4(Convert.toBigDecimal(collect.get("tmChargeSpeedB4")));
-        return zsjDto;
-    }
 
 }

+ 13 - 6
hx-iot/src/main/java/com/fjhx/iot/service/tda/impl/TdaProductServiceImpl.java

@@ -6,12 +6,15 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.fjhx.iot.entity.tda.dto.TdaProductDto;
 import com.fjhx.iot.entity.tda.dto.TdaProductSelectDto;
 import com.fjhx.iot.entity.tda.po.TdaApplication;
+import com.fjhx.iot.entity.tda.po.TdaDevice;
 import com.fjhx.iot.entity.tda.po.TdaProduct;
 import com.fjhx.iot.entity.tda.vo.TdaProductVo;
 import com.fjhx.iot.mapper.tda.TdaProductMapper;
 import com.fjhx.iot.service.tda.TdaApplicationService;
+import com.fjhx.iot.service.tda.TdaDeviceService;
 import com.fjhx.iot.service.tda.TdaProductService;
 import com.fjhx.iot.utils.IoTDAUtil;
+import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.wrapper.IWrapper;
 import com.ruoyi.common.utils.wrapper.SqlField;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -33,6 +36,9 @@ public class TdaProductServiceImpl extends ServiceImpl<TdaProductMapper, TdaProd
     @Autowired
     private TdaApplicationService tdaApplicationService;
 
+    @Autowired
+    private TdaDeviceService tdaDeviceService;
+
     @Override
     public Page<TdaProductVo> getPage(TdaProductSelectDto dto) {
         IWrapper<TdaProduct> wrapper = getWrapper();
@@ -59,10 +65,11 @@ public class TdaProductServiceImpl extends ServiceImpl<TdaProductMapper, TdaProd
 
         // 华为ioDta添加产品
         String productId = IoTDAUtil.createProduct(tdaApplication.getTdaConfigId(), appId, tdaProductDto.getName(),
-                tdaProductDto.getDeviceType(), tdaProductDto.getManufacturerName());
+                tdaProductDto.getDeviceType(), tdaProductDto.getManufacturerName(), tdaProductDto.getProtocolType());
 
         tdaProductDto.setAppId(appId);
         tdaProductDto.setProductId(productId);
+
         this.save(tdaProductDto);
     }
 
@@ -70,11 +77,11 @@ public class TdaProductServiceImpl extends ServiceImpl<TdaProductMapper, TdaProd
     @Override
     public void delete(Long id) {
 
-        // // 查询产品是否已添加设备,已添加设备则不可删除
-        // Long count = tdaDeviceService.count(TdaDevice::getTdaProductId, id);
-        // if (count > 0) {
-        //     throw new ServiceException("产品已添加设备,无法删除");
-        // }
+        // 查询产品是否已添加设备,已添加设备则不可删除
+        long count = tdaDeviceService.count(q -> q.eq(TdaDevice::getTdaProductId, id));
+        if (count > 0) {
+            throw new ServiceException("产品已添加设备,无法删除");
+        }
 
         // 华为ioDta删除产品
         TdaProduct tdaProduct = getById(id);

+ 21 - 17
hx-iot/src/main/java/com/fjhx/iot/utils/IoTDAUtil.java

@@ -1,5 +1,6 @@
 package com.fjhx.iot.utils;
 
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.extra.spring.SpringUtil;
 import com.fjhx.iot.entity.tda.po.TdaConfig;
 import com.fjhx.iot.service.tda.TdaConfigService;
@@ -59,25 +60,11 @@ public class IoTDAUtil {
      * @param name             产品名称
      * @param deviceType       设备类型
      * @param manufacturerName 厂商名称
+     * @param protocolType     设备使用的协议类型
      * @return 产品id
      */
-    public static String createProduct(Long configId, String appId, String name, String deviceType, String manufacturerName) {
-        CreateProductRequest createProductRequest = new CreateProductRequest();
-        AddProduct addProduct = new AddProduct();
-        // 资源空间
-        addProduct.setAppId(appId);
-        // 产品名称
-        addProduct.setName(name);
-        addProduct.setDeviceType(deviceType);
-        addProduct.setManufacturerName(manufacturerName);
+    public static String createProduct(Long configId, String appId, String name, String deviceType, String manufacturerName, String protocolType) {
 
-        addProduct.setProtocolType("MQTT");
-        addProduct.setDataFormat("json");
-
-        // 添加产品属性
-        ServiceCapability serviceCapability = new ServiceCapability();
-        serviceCapability.setServiceId("Data");
-        serviceCapability.setServiceType("data");
         // 添加命名空间
         ServiceProperty serviceProperty = new ServiceProperty();
         serviceProperty.setPropertyName("DeviceData");
@@ -86,11 +73,28 @@ public class IoTDAUtil {
         serviceProperty.setMaxLength(2000000);
         serviceProperty.setStep(0.0D);
         serviceProperty.setMethod("RW");
+
+        // 添加产品属性
+        ServiceCapability serviceCapability = new ServiceCapability();
+        serviceCapability.setServiceId("Data");
+        serviceCapability.setServiceType("data");
         serviceCapability.setProperties(Collections.singletonList(serviceProperty));
-        addProduct.setServiceCapabilities(Collections.singletonList(serviceCapability));
 
+        CreateProductRequest createProductRequest = new CreateProductRequest();
+        AddProduct addProduct = new AddProduct();
+        // 资源空间
+        addProduct.setAppId(appId);
+        // 产品名称
+        addProduct.setName(name);
+        addProduct.setDeviceType(deviceType);
+        addProduct.setManufacturerName(manufacturerName);
+        addProduct.setProtocolType(ObjectUtil.defaultIfBlank(protocolType, "MQTT"));
+        addProduct.setDataFormat("json");
+        addProduct.setServiceCapabilities(Collections.singletonList(serviceCapability));
         createProductRequest.setBody(addProduct);
+
         CreateProductResponse product = getIoTDAClient(configId).createProduct(createProductRequest);
+
         return product.getProductId();
     }