|  | @@ -1,21 +1,23 @@
 | 
	
		
			
				|  |  |  package com.xunmei.mediator.iot.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  | -import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.toolkit.IdWorker;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
		
			
				|  |  | -import com.xunmei.common.core.utils.DateUtils;
 | 
	
		
			
				|  |  | -import com.xunmei.common.core.web.page.TableDataInfo;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.constant.Constants;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.enums.iot.SensorType;
 | 
	
		
			
				|  |  |  import com.xunmei.mediator.iot.mapper.IotDeviceStatusMapper;
 | 
	
		
			
				|  |  |  import com.xunmei.mediator.iot.service.IIotDeviceInfoService;
 | 
	
		
			
				|  |  |  import com.xunmei.mediator.iot.service.IotDeviceStatusService;
 | 
	
		
			
				|  |  |  import com.xunmei.mediator.websocket.dto.WebsocketExecuteReq;
 | 
	
		
			
				|  |  |  import com.xunmei.mediator.websocket.dto.WebsocketResult;
 | 
	
		
			
				|  |  |  import com.xunmei.mediator.websocket.enums.DeviceTypeEnum;
 | 
	
		
			
				|  |  | -import com.xunmei.mediator.websocket.enums.ProductEnums;
 | 
	
		
			
				|  |  | +import com.xunmei.system.api.domain.iot.IotDeviceInfo;
 | 
	
		
			
				|  |  |  import com.xunmei.system.api.domain.iot.IotDeviceStatus;
 | 
	
		
			
				|  |  | -import com.xunmei.system.api.enums.DeviceType;
 | 
	
		
			
				|  |  | +import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  | +import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -26,6 +28,7 @@ import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |   * @author xunmei
 | 
	
		
			
				|  |  |   * @date 2024-07-18
 | 
	
		
			
				|  |  |   */
 | 
	
		
			
				|  |  | +@Slf4j
 | 
	
		
			
				|  |  |  @Service
 | 
	
		
			
				|  |  |  public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMapper, IotDeviceStatus> implements IotDeviceStatusService {
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
	
		
			
				|  | @@ -33,46 +36,56 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private IIotDeviceInfoService iotDeviceInfoService;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public IotDeviceStatus getByDeviceId(Long deviceId) {
 | 
	
		
			
				|  |  | +        LambdaQueryWrapper<IotDeviceStatus> wrapper = new LambdaQueryWrapper<>();
 | 
	
		
			
				|  |  | +        wrapper.eq(IotDeviceStatus::getDeviceId, deviceId);
 | 
	
		
			
				|  |  | +        wrapper.last(Constants.LIMIT1);
 | 
	
		
			
				|  |  | +        return iotDeviceStatusMapper.selectOne(wrapper);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * {
 | 
	
		
			
				|  |  | -     *   "topic": "/things/FSU_TemperatureAndHumidity/XXDevice/property/post",
 | 
	
		
			
				|  |  | -     *   "id":"xxx",  // Guid,消息IoT生成。
 | 
	
		
			
				|  |  | -     *   "timestamp": "2023-01-11T18:00:00 +08:00",
 | 
	
		
			
				|  |  | -     *   "headers":{
 | 
	
		
			
				|  |  | -     *     "topicType":"",// Topic类型,相对于topicCategory的细分
 | 
	
		
			
				|  |  | -     *     "topicCategory": "",// Topic分类
 | 
	
		
			
				|  |  | -     *     "productName": "FSU_TemperatureAndHumidity",
 | 
	
		
			
				|  |  | -     *     "deviceName": "XXDevice"
 | 
	
		
			
				|  |  | -     *   },
 | 
	
		
			
				|  |  | -     *   "payload":[
 | 
	
		
			
				|  |  | -     *     {
 | 
	
		
			
				|  |  | -     *       "propertyName": "temperature",// 应用方调用的属性名称
 | 
	
		
			
				|  |  | -     *       "args": 23.6,
 | 
	
		
			
				|  |  | -     *       "dataSpec": {
 | 
	
		
			
				|  |  | -     *         "defaultValue": 0,
 | 
	
		
			
				|  |  | -     *         "max": 5000,
 | 
	
		
			
				|  |  | -     *         "min": -300,
 | 
	
		
			
				|  |  | -     *         "step": 0.01,
 | 
	
		
			
				|  |  | -     *         "unit": "℃",
 | 
	
		
			
				|  |  | -     *         "unitName": "摄氏度",
 | 
	
		
			
				|  |  | -     *         "dataType": "Int"
 | 
	
		
			
				|  |  | -     *       }
 | 
	
		
			
				|  |  | -     *     },
 | 
	
		
			
				|  |  | -     *     {
 | 
	
		
			
				|  |  | -     *       "propertyName": "humidity",// 应用方调用的属性名称
 | 
	
		
			
				|  |  | -     *       "args": 23.6,
 | 
	
		
			
				|  |  | -     *       "dataSpec": {
 | 
	
		
			
				|  |  | -     *         "defaultValue": 0,
 | 
	
		
			
				|  |  | -     *         "max": 5000,
 | 
	
		
			
				|  |  | -     *         "min": -300,
 | 
	
		
			
				|  |  | -     *         "step": 0.01,
 | 
	
		
			
				|  |  | -     *         "unit": "%RH",
 | 
	
		
			
				|  |  | -     *         "unitName": "",
 | 
	
		
			
				|  |  | -     *         "dataType": "Int"
 | 
	
		
			
				|  |  | -     *       }
 | 
	
		
			
				|  |  | -     *     }
 | 
	
		
			
				|  |  | -     *   ]
 | 
	
		
			
				|  |  | +     * "topic": "/things/FSU_TemperatureAndHumidity/XXDevice/property/post",
 | 
	
		
			
				|  |  | +     * "id":"xxx",  // Guid,消息IoT生成。
 | 
	
		
			
				|  |  | +     * "timestamp": "2023-01-11T18:00:00 +08:00",
 | 
	
		
			
				|  |  | +     * "headers":{
 | 
	
		
			
				|  |  | +     * "topicType":"",// Topic类型,相对于topicCategory的细分
 | 
	
		
			
				|  |  | +     * "topicCategory": "",// Topic分类
 | 
	
		
			
				|  |  | +     * "productName": "FSU_TemperatureAndHumidity",
 | 
	
		
			
				|  |  | +     * "deviceName": "XXDevice"
 | 
	
		
			
				|  |  | +     * },
 | 
	
		
			
				|  |  | +     * "payload":[
 | 
	
		
			
				|  |  | +     * {
 | 
	
		
			
				|  |  | +     * "propertyName": "temperature",// 应用方调用的属性名称
 | 
	
		
			
				|  |  | +     * "args": 23.6,
 | 
	
		
			
				|  |  | +     * "dataSpec": {
 | 
	
		
			
				|  |  | +     * "defaultValue": 0,
 | 
	
		
			
				|  |  | +     * "max": 5000,
 | 
	
		
			
				|  |  | +     * "min": -300,
 | 
	
		
			
				|  |  | +     * "step": 0.01,
 | 
	
		
			
				|  |  | +     * "unit": "℃",
 | 
	
		
			
				|  |  | +     * "unitName": "摄氏度",
 | 
	
		
			
				|  |  | +     * "dataType": "Int"
 | 
	
		
			
				|  |  |       * }
 | 
	
		
			
				|  |  | +     * },
 | 
	
		
			
				|  |  | +     * {
 | 
	
		
			
				|  |  | +     * "propertyName": "humidity",// 应用方调用的属性名称
 | 
	
		
			
				|  |  | +     * "args": 23.6,
 | 
	
		
			
				|  |  | +     * "dataSpec": {
 | 
	
		
			
				|  |  | +     * "defaultValue": 0,
 | 
	
		
			
				|  |  | +     * "max": 5000,
 | 
	
		
			
				|  |  | +     * "min": -300,
 | 
	
		
			
				|  |  | +     * "step": 0.01,
 | 
	
		
			
				|  |  | +     * "unit": "%RH",
 | 
	
		
			
				|  |  | +     * "unitName": "",
 | 
	
		
			
				|  |  | +     * "dataType": "Int"
 | 
	
		
			
				|  |  | +     * }
 | 
	
		
			
				|  |  | +     * }
 | 
	
		
			
				|  |  | +     * ]
 | 
	
		
			
				|  |  | +     * }
 | 
	
		
			
				|  |  | +     *
 | 
	
		
			
				|  |  |       * @param req
 | 
	
		
			
				|  |  |       * @return
 | 
	
		
			
				|  |  |       */
 | 
	
	
		
			
				|  | @@ -84,8 +97,41 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
 | 
	
		
			
				|  |  |          final String productName = req.getProductName();
 | 
	
		
			
				|  |  |          final DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.valueOf(productName);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        //iotDeviceInfoService.selectByTypeAndCode()
 | 
	
		
			
				|  |  | +        final IotDeviceInfo code = iotDeviceInfoService.selectByTypeAndCode(req.getToken(), deviceTypeEnum.getCode(), req.getProductName(), req.getDeviceName());
 | 
	
		
			
				|  |  | +        if (code == null) {
 | 
	
		
			
				|  |  | +            log.error("动环设备状态同步时,未能找到对应设备,token:{},productName:{},deviceNane:{}", req.getToken(), req.getProductName(), req.getDeviceName());
 | 
	
		
			
				|  |  | +            return new WebsocketResult();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        final JSONArray array = dealStatusData(data);
 | 
	
		
			
				|  |  | +        IotDeviceStatus status = getByDeviceId(code.getId());
 | 
	
		
			
				|  |  | +        if (status == null) {
 | 
	
		
			
				|  |  | +            status = new IotDeviceStatus();
 | 
	
		
			
				|  |  | +            BeanUtils.copyProperties(code, status, "id");
 | 
	
		
			
				|  |  | +            status.setDeviceId(code.getId());
 | 
	
		
			
				|  |  | +            status.setId(IdWorker.getId());
 | 
	
		
			
				|  |  | +            status.setUniqueCode();
 | 
	
		
			
				|  |  | +            status.setInfo(JSON.toJSONString(array));
 | 
	
		
			
				|  |  | +            save(status);
 | 
	
		
			
				|  |  | +        }else {
 | 
	
		
			
				|  |  | +            status.setInfo(JSON.toJSONString(array));
 | 
	
		
			
				|  |  | +            updateById(status);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        return null;
 | 
	
		
			
				|  |  | +        return new WebsocketResult();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +    private static JSONArray dealStatusData(JSONArray data) {
 | 
	
		
			
				|  |  | +        final JSONArray array = new JSONArray();
 | 
	
		
			
				|  |  | +        for (Object datum : data) {
 | 
	
		
			
				|  |  | +            JSONObject jsb = (JSONObject) datum;
 | 
	
		
			
				|  |  | +            JSONObject object = new JSONObject();
 | 
	
		
			
				|  |  | +            object.put("propertyName", jsb.get("propertyName"));
 | 
	
		
			
				|  |  | +            object.put("args", jsb.get("args"));
 | 
	
		
			
				|  |  | +            JSONObject dataSpec = JSON.parseObject(jsb.get("dataSpec").toString());
 | 
	
		
			
				|  |  | +            object.put("unit", dataSpec.get("unit"));
 | 
	
		
			
				|  |  | +            object.put("unitName", dataSpec.get("unitName"));
 | 
	
		
			
				|  |  | +            object.put("dataType", dataSpec.get("dataType"));
 | 
	
		
			
				|  |  | +            array.add(object);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return array;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |