ソースを参照

add 设备添加功能代码
add 从阿里云同步设备的功能

龙三郎 3 年 前
コミット
8fea49f95a

+ 3 - 2
coffee-admin/src/main/resources/application.yml

@@ -71,5 +71,6 @@ mybatis-plus:
 # 阿里云对接配置
 aliyun:
   server-subscription:
-    enable: true  # 是否开启阿里云物联网服务端订阅
-
+    enable: false  # 是否开启阿里云物联网服务端订阅
+  product:
+    productKey: a1ALlsBa2ZK

+ 2 - 3
coffee-system/src/main/java/com/coffee/aliyun/AliyunConsumerGroupService.java

@@ -4,6 +4,7 @@ import cn.hutool.extra.spring.SpringUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.coffee.aliyun.utils.Constants;
+import com.coffee.aliyun.utils.EnumUtils;
 import com.coffee.aliyun.utils.Items;
 import com.coffee.bus.bean.AliIotConfig;
 import com.coffee.bus.entity.BusDeviceEntity;
@@ -135,15 +136,13 @@ public class AliyunConsumerGroupService {
                 BusDeviceEntity device = new BusDeviceEntity();
                 device.setDeviceId(deviceName);
                 String status = content.getString(STATUS);
+                device.setStatus(EnumUtils.getDeviceStatusEnum2(status));
                 if (status.equals("online")){
                     log.info(deviceName+"设备上线");
-                    device.setStatus(1);
                 }else if (status.equals("offline")){
                     log.info(deviceName+"设备下线");
-                    device.setStatus(2);
                 }else {
                     log.info(deviceName+"设备未激活");
-                    device.setStatus(0);
                 }
                 // 更新设备状态
                 deviceService.updateByDeviceId(device);

+ 1 - 1
coffee-system/src/main/java/com/coffee/aliyun/AliyunIotSubscribeClient.java

@@ -24,7 +24,7 @@ import java.util.Hashtable;
 import java.util.List;
 
 /**
- * @Author XX
+ * @Author 龙三郎
  * @Date 2021-06-17 10:53:30
  * @Version 1.0
  * @Description XXX

+ 27 - 23
coffee-system/src/main/java/com/coffee/aliyun/AliyunIotSdk.java → coffee-system/src/main/java/com/coffee/aliyun/sdk/AliyunIotSdk.java

@@ -1,4 +1,4 @@
-package com.coffee.aliyun;
+package com.coffee.aliyun.sdk;
 
 import com.alibaba.fastjson.JSON;
 import com.aliyuncs.DefaultAcsClient;
@@ -6,7 +6,12 @@ import com.aliyuncs.exceptions.ClientException;
 import com.aliyuncs.iot.model.v20180120.*;
 import com.aliyuncs.profile.DefaultProfile;
 import com.aliyuncs.profile.IClientProfile;
+import com.coffee.aliyun.PlatformAccount;
 import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -16,29 +21,26 @@ import java.util.Map;
 //import com.tuoren.common.utils.StringUtils;
 
 /**
- * @Author XX
+ * @Author 龙三郎
  * @Date 2021-06-11 08:10:58
  * @Version 1.0
  * @Description XXX
  */
-@Data
+@Configuration
+@EnableConfigurationProperties(PlatformAccount.class)
+@Slf4j
 public class AliyunIotSdk {
-    private static final String ACCESSKEY = "accessKey";
-    private static final String ACCESSSECRET = "accessSecret";
-    private static final String REFIONID = "regionId";
+
+
+    @Value("${aliyun.product.productKey:a1ALlsBa2ZK}")
+    private String productKey;
 
     private String accessKey;
     private String accessSecret;
     private String regionId;
     // client
     private DefaultAcsClient client;
-    public AliyunIotSdk(String accessKey, String accessSecret, String regionId) {
-        this.accessKey = accessKey;
-        this.accessSecret = accessSecret;
-        this.regionId = regionId;
-    }
-
-    public AliyunIotSdk(PlatformAccount platformAccount) {
+    public AliyunIotSdk(PlatformAccount platformAccount){
         this.accessKey = platformAccount.getAccessKey();
         this.accessSecret = platformAccount.getAccessSecret();
         this.regionId = platformAccount.getRegionId();
@@ -105,6 +107,9 @@ public class AliyunIotSdk {
 
         // 获取阿里云SDK客户端
         DefaultAcsClient client = this.getAliyuniotClient();
+        List<QueryDeviceResponse.DeviceInfo> list = new ArrayList<>();
+        Integer pageSize = 100,currentPage = 1;
+
         RegisterDeviceRequest request = new RegisterDeviceRequest();
         // 产品key,必需
         request.setProductKey(productKey);
@@ -113,7 +118,7 @@ public class AliyunIotSdk {
         try {
             RegisterDeviceResponse response = client.getAcsResponse(request);
             System.out.println(response.getSuccess());
-            // 获取失败直接跳出
+            // 获取失败直接跳出循环
             if (!response.getSuccess()){
                 return null;
             }
@@ -151,10 +156,9 @@ public class AliyunIotSdk {
 
     /**
      * 获取设备
-     * @param productKey
      * @return
      */
-    public List<QueryDeviceResponse.DeviceInfo> queryDevice(String productKey){
+    public List<QueryDeviceResponse.DeviceInfo> queryDevice(){
 
         // 获取阿里云SDK客户端
         DefaultAcsClient client = this.getAliyuniotClient();
@@ -292,23 +296,23 @@ public class AliyunIotSdk {
 
     /**
      * 获取设备的详细信息
-     * @param iotId
+     * @param deviceName
      */
-    public QueryDeviceDetailResponse.Data queryDeviceDetail(String iotId){
+    public QueryDeviceDetailResponse queryDeviceDetail(String deviceName){
         // 获取阿里云SDK客户端
         DefaultAcsClient client = this.getAliyuniotClient();
 
         QueryDeviceDetailRequest _request = new QueryDeviceDetailRequest();
-        _request.setIotId(iotId);
+        _request.setProductKey(productKey);
+        _request.setDeviceName(deviceName);
 
-        QueryDeviceDetailResponse.Data data = null;
+        QueryDeviceDetailResponse acsResponse = null;
         try {
-            QueryDeviceDetailResponse acsResponse = client.getAcsResponse(_request);
-            data = acsResponse.getData();
+            acsResponse = client.getAcsResponse(_request);
         } catch (ClientException e) {
             e.printStackTrace();
         }
-        return data;
+        return acsResponse;
     }
 
 

+ 1 - 1
coffee-system/src/main/java/com/coffee/aliyun/utils/Constants.java

@@ -1,7 +1,7 @@
 package com.coffee.aliyun.utils;
 
 /**
- * @Author XX
+ * @Author 龙三郎
  * @Date 2022-04-26 13:44:36
  * @Version 1.0
  * @Description XXX

+ 2 - 2
coffee-system/src/main/java/com/coffee/aliyun/utils/DeviceTypeUtils.java

@@ -16,9 +16,9 @@ public class DeviceTypeUtils {
         }else if (i == 1){
             return DeviceTypeEnum.continuous;
         }else if (i == 2){
-            return DeviceTypeEnum.intelligent;
-        }else if (i == 3){
             return DeviceTypeEnum.pulse;
+        }else if (i == 3){
+            return DeviceTypeEnum.intelligent;
         }else {
             return DeviceTypeEnum.other;
         }

+ 30 - 0
coffee-system/src/main/java/com/coffee/aliyun/utils/EnumUtils.java

@@ -0,0 +1,30 @@
+package com.coffee.aliyun.utils;
+
+import cn.hutool.core.util.StrUtil;
+import com.coffee.bus.enums.DeviceStatusEnum2;
+
+/**
+ * @Author 龙三郎
+ * @Date 2022-05-06 15:03:48
+ * @Version 1.0
+ * @Description XXX
+ */
+public class EnumUtils {
+    /**
+     * @author 龙三郎
+     * 根据阿里云返回的状态获取枚举
+     * @param status
+     * @return
+     */
+    public static DeviceStatusEnum2 getDeviceStatusEnum2(String status){
+        status = status.toLowerCase();
+        if ("online".equals(status)){
+            return DeviceStatusEnum2.ONLINE;
+        }else if ("offline".equals(status)){
+            return DeviceStatusEnum2.OFFLINE;
+        }else{
+            return DeviceStatusEnum2.NOACTIVE;
+        }
+    }
+
+}

+ 47 - 0
coffee-system/src/main/java/com/coffee/bus/controller/BusDeviceController.java

@@ -1,12 +1,16 @@
 package com.coffee.bus.controller;
 
 import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.mapper.Mapper;
 import com.coffee.bus.controller.vo.DeviceBindVo;
 import com.coffee.bus.entity.BusDeviceEntity;
+import com.coffee.bus.entity.BusHospitalEntity;
 import com.coffee.bus.enums.DeviceAlarmEnum;
+import com.coffee.bus.exception.BusinessException;
+import com.coffee.bus.exception.ErrorStatus;
 import com.coffee.bus.registry.device.DeviceRegistry;
 import com.coffee.bus.service.LocalBusDeviceService;
 import com.coffee.common.crud.BaseService;
@@ -16,10 +20,12 @@ import com.coffee.common.result.R;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.AllArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.bind.annotation.*;
 
 import javax.validation.constraints.NotNull;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * @author lifang
@@ -27,7 +33,9 @@ import java.util.List;
  * @ClassName BusHospitalController.java
  * @Description TODO
  * @createTime 2022年03月19日 09:28:00
+ * @accendant 龙三郎
  */
+@Slf4j
 @RestController
 @AllArgsConstructor
 @RequestMapping("/bus/device/info")
@@ -82,4 +90,43 @@ public class BusDeviceController extends BaseCrudController<BusDeviceEntity, Str
     public BaseService<? extends Mapper<BusDeviceEntity>, BusDeviceEntity, String> getService() {
         return deviceService;
     }
+
+    /**
+     * @author 龙三郎
+     * @param payload
+     * @return
+     */
+    @Override
+    @PostMapping("/add")
+    @ApiOperation(value = "新增单个数据,并返回新增后的数据.")
+    public R<BusDeviceEntity> add(BusDeviceEntity payload) {
+        log.info("添加设备");
+        // 设备id不能为空
+        if (StrUtil.isEmpty(payload.getDeviceId())){
+            throw new BusinessException(ErrorStatus.DEVICEID_IS_EMPTY);
+        }
+        // 根据deviceId从阿里云获取设备信息。
+        BusDeviceEntity device = deviceService.add(payload.getDeviceId());
+        if (ObjectUtil.isNull(device)){
+            return R.fail("设备添加失败");
+        }
+        return R.success(device);
+    }
+
+    /**
+     * @author 龙三郎
+     * 从阿里云同步系统所有的设备
+     * @return
+     */
+    @ApiOperation(value = "从阿里云同步设备",notes = "从阿里云平台获取设备并更新")
+    @PutMapping("/sync-device")
+    public R syncDevice() {
+        int i = deviceService.syncDevice();
+        if (i > 0){
+            return R.success(i,"同步成功");
+        }else {
+            return R.fail("同步失败");
+        }
+    }
+
 }

+ 33 - 1
coffee-system/src/main/java/com/coffee/bus/entity/BusDeviceEntity.java

@@ -1,9 +1,13 @@
 package com.coffee.bus.entity;
 
+import com.aliyuncs.iot.model.v20180120.QueryDeviceDetailResponse;
+import com.aliyuncs.iot.model.v20180120.QueryDeviceResponse;
 import com.baomidou.mybatisplus.annotation.*;
 import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
+import com.coffee.aliyun.utils.EnumUtils;
 import com.coffee.bus.bean.AliIotConfig;
 import com.coffee.bus.enums.DeviceAlarmEnum;
+import com.coffee.bus.enums.DeviceStatusEnum2;
 import com.coffee.bus.enums.DeviceTypeEnum;
 import com.coffee.bus.enums.DeviceStatusEnum;
 import com.coffee.common.entity.RecordCreationEntity;
@@ -65,5 +69,33 @@ public class BusDeviceEntity extends TenantGenericEntity<String,String> {
     private Integer isDelete;
 
     @ApiModelProperty(value = "设备在线状态,0未激活,1在线,2离线",readOnly = true)
-    private Integer status;
+    private DeviceStatusEnum2 status;
+
+    /**
+     * @author 龙三郎
+     * 根据阿里云返回的数据更新设备对象
+     * @param data 由queryDeviceDetail接口返回的数据
+     */
+    public void setFields(QueryDeviceDetailResponse.Data data) {
+        this.deviceId = data.getDeviceName();
+        this.config.setProductKey(data.getProductKey());
+        this.config.setIotId(data.getIotId());
+        this.config.setDeviceSecret(data.getDeviceSecret());
+        this.config.setDeviceName(data.getDeviceName());
+        this.setStatus(EnumUtils.getDeviceStatusEnum2(data.getStatus()));
+    }
+
+    /**
+     * @author 龙三郎
+     * 根据设备查询接口返回的数据,更新设备对象
+     * @param item
+     */
+    public void setFields(QueryDeviceResponse.DeviceInfo item) {
+        this.deviceId = item.getDeviceName();
+        this.config.setProductKey(item.getProductKey());
+        this.config.setIotId(item.getIotId());
+        this.config.setDeviceSecret(item.getDeviceSecret());
+        this.config.setDeviceName(item.getDeviceName());
+        this.setStatus(EnumUtils.getDeviceStatusEnum2(item.getDeviceStatus()));
+    }
 }

+ 1 - 25
coffee-system/src/main/java/com/coffee/bus/entity/BusDeviceRunningEntity.java

@@ -118,32 +118,8 @@ public class BusDeviceRunningEntity extends CommonDeviceParam<String,String> {
         this.setElectricQuantity(items.getInteger(PumpParams.electricQuantity));
         this.setRunState(DeviceRunStatusUtils.getRunStatus(items.getInteger(PumpParams.runStatus)));
         this.setAlarm(DeviceAlarmUtils.getAlarm(items.getInteger(PumpParams.alarmStatus)));
-//        this.deviceId = deviceName;
-
-//        this.classification = items.getString(PumpParams.classification);
-//        this.dataNumber = items.getString(PumpParams.dataNumber);
-//        this.patientCode = items.getString(PumpParams.patientCode);
-//        this.ward = items.getString(PumpParams.ward);
-//        this.bedNo = items.getString(PumpParams.bedNo);
-//        this.totalDose = items.getInteger(PumpParams.totalDose);
-//        this.inputDose = items.getBigDecimal(PumpParams.finishedDose);
-//        this.firstDose = items.getInteger(PumpParams.firstDosis);
-//        this.maxDose = items.getBigDecimal(PumpParams.maxDose);
-//        this.appendLockTime = items.getBigDecimal(PumpParams.lockTime);
-//        this.continueDose = items.getBigDecimal(PumpParams.flow);
-//        this.type = DeviceTypeUtils.getDeviceType(items.getInteger(PumpParams.pumpType));
-//        this.appendDose = items.getBigDecimal(PumpParams.singleDosis);
-//        this.pcaValidCount = items.getInteger(PumpParams.pcaValid);
-//        this.pcaInvalidCount = items.getInteger(PumpParams.pcaInvalid);
-//        this.pcaTotalCount = this.pcaInvalidCount + this.pcaValidCount;
-//        this.electricQuantity = items.getInteger(PumpParams.electricQuantity);
-
-        // 运行状态
-//        this.runState = DeviceRunStatusUtils.getRunStatus(items.getInteger(PumpParams.runStatus));
-        // 报警
-//        this.alarm = DeviceAlarmUtils.getAlarm(items.getInteger(PumpParams.alarmStatus));
-
         // 预报
+
     }
 
 

+ 0 - 23
coffee-system/src/main/java/com/coffee/bus/entity/BusInfusionHistoryEntity.java

@@ -161,29 +161,6 @@ public class BusInfusionHistoryEntity extends CommonDeviceParam<String,String> {
         this.setElectricQuantity(items.getInteger(PumpParams.electricQuantity));
         this.setRunState(DeviceRunStatusUtils.getRunStatus(items.getInteger(PumpParams.runStatus)));
         this.setAlarm(DeviceAlarmUtils.getAlarm(items.getInteger(PumpParams.alarmStatus)));
-//        this.classification = items.getString(PumpParams.classification);
-//        this.dataNumber = items.getString(PumpParams.dataNumber);
-//        this.patientCode = items.getString(PumpParams.patientCode);
-//        this.ward = items.getString(PumpParams.ward);
-//        this.bedNo = items.getString(PumpParams.bedNo);
-//        this.totalDose = items.getInteger(PumpParams.totalDose);
-//        this.inputDose = items.getBigDecimal(PumpParams.finishedDose);
-//        this.firstDose = items.getInteger(PumpParams.firstDosis);
-//        this.maxDose = items.getBigDecimal(PumpParams.maxDose);
-//        this.appendLockTime = items.getBigDecimal(PumpParams.lockTime);
-//        this.continueDose = items.getBigDecimal(PumpParams.flow);
-//        this.deviceType = DeviceTypeUtils.getDeviceType(items.getInteger(PumpParams.pumpType));
-//        this.appendDose = items.getBigDecimal(PumpParams.singleDosis);
-//        this.pcaValidCount = items.getInteger(PumpParams.pcaValid);
-//        this.pcaInvalidCount = items.getInteger(PumpParams.pcaInvalid);
-//        this.pcaTotalCount = this.pcaInvalidCount + this.pcaValidCount;
-//        this.electricQuantity = items.getInteger(PumpParams.electricQuantity);
-//
-//        // 运行状态
-//        this.runState = DeviceRunStatusUtils.getRunStatus(items.getInteger(PumpParams.runStatus));
-//        // 报警
-//        this.alarm = DeviceAlarmUtils.getAlarm(items.getInteger(PumpParams.alarmStatus));
-
         // 预报
 
     }

+ 30 - 0
coffee-system/src/main/java/com/coffee/bus/enums/DeviceStatusEnum2.java

@@ -0,0 +1,30 @@
+package com.coffee.bus.enums;
+
+import com.baomidou.mybatisplus.annotation.IEnum;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * @Author 龙三郎
+ * @Date 2022-05-06 14:54:04
+ * @Version 1.0
+ * @Description 设备网络状态
+ */
+@AllArgsConstructor
+@JsonFormat(shape = JsonFormat.Shape.OBJECT)
+public enum DeviceStatusEnum2 implements IEnum<Integer> {
+
+    NOACTIVE(0,"未激活"),
+    ONLINE(1,"在线"),
+    OFFLINE(2,"离线");
+
+
+    @Getter
+    @ApiModelProperty("状态编码")
+    private Integer value;
+    @Getter
+    @ApiModelProperty("状态内容")
+    private String text;
+}

+ 33 - 0
coffee-system/src/main/java/com/coffee/bus/exception/BusinessException.java

@@ -0,0 +1,33 @@
+package com.coffee.bus.exception;
+
+import lombok.Data;
+
+/**
+ * @Author 龙三郎
+ * @Date 2022-05-06 10:03:58
+ * @Version 1.0
+ * @Description 业务异常
+ */
+@Data
+public class BusinessException extends RuntimeException {
+    static final long serialVersionUID = -7034897190745766940L;
+
+    private Integer code;
+
+    public BusinessException(ErrorStatus errorStatus){
+        super(errorStatus.getMessage());
+        this.code = errorStatus.getCode();
+    }
+
+    public BusinessException(Integer code, String message){
+        super(message);
+        this.code = code;
+    }
+
+    public BusinessException(ErrorStatus errorStatus, String message){
+        super(message);
+        this.code = errorStatus.getCode();
+    }
+
+
+}

+ 80 - 0
coffee-system/src/main/java/com/coffee/bus/exception/ErrorStatus.java

@@ -0,0 +1,80 @@
+package com.coffee.bus.exception;
+
+
+/**
+ * @Author 龙三郎
+ * @Date 2022-05-06 10:17:13
+ * @Version 1.0
+ * @Description 错误状态码
+ */
+public enum ErrorStatus {
+    DEVICE_NOT_EXISTS(100,"设备不存在"),
+    DEVICEID_IS_EMPTY(101,"设备ID不能为空"),
+    DEVICE_NOT_MAPPING_HOSPITAL(102,"设备没有映射医院"),
+    DEVICE_NOT_MAPPING_PRODUCT(103,"设备没有映射产品"),
+    RAWPATIENTCODE_IS_EMPTY(104,"住院号不能为空"),
+    PATIENT_NOT_EXISTS(105,"患者不存在"),
+    AUTO_CREATE_CLINIC_FAILURE(106,"自动创建临床失败"),
+    CLASSIFICATION_NOT_EMPTY(107,"数据分类标识不能为空"),
+    DATANUMBER_NOT_EMPTY(108,"数据编号不能为空"),
+    AUTO_CREATE_DEVICEUSE_FAILURE(109,"自动创建设备使用失败"),
+    STATUS_NOT_EMPTY(110,"状态不能为空"),
+    DATATYPE_NOT_EMPTY(111,"数据类型不能为空"),
+    PLATFORMCODE_IS_EMPTY(112,"平台编号不能为空"),
+    PLATFORM_NOT_EXISTS(113,"平台不存在"),
+    PRODUCTCODE_IS_EMPTY(114,"产品编号不能为空"),
+    PRODUCTNAME_IS_EMPTY(115,"产品名称不能为空"),
+    PRODUCT_HAD_EXISTS(116,"产品已经存在"),
+    PRODUCT_NOT_EXISTS(117,"产品不存在"),
+    OBJECT_CHANGE_EXCEPTION(118,"对象转换异常"),
+    CLINIC_HAD_EXISTS(119,"患者正在治疗中"),
+    DEVICENAME_IS_EMPTY(120,"设备名称不能为空"),
+    HOSPITALCODE_IS_EMPTY(121,"医院编号不能为空"),
+    DEVICE_HAD_EXISTS(122,"设备已经存在"),
+    HOSPITAL_NOT_EXISTS(123,"医院不存在"),
+    PATIENT_HAD_EXISTS(124,"患者已经存在"),
+    USERNAME_IS_EMPTY(125,"用户名不能为空"),
+    ROLE_NOT_EXISTS(126,"角色不存在"),
+    REPASSWORD_NOT_EQUALS(127,"两次密码不一致"),
+    PLATFORMPRODUCTCODE_IS_EMPTY(128,"平台产品编号不能为空"),
+    PLATFORMPRODUCT_NOT_EXISTS(129,"平台产品不存在"),
+    AUTO_CREATE_PATIENT_FAILURE(130,"自动创建患者失败"),
+    RAWDATA_IS_EMPTY(131,"原始数据不能为空"),
+    DATA_CHANGE_FAILURE(132,"数据转换失败"),
+    PATIENTCODE_IS_EMPTY(133,"患者编号不能为空"),
+    MENU_IS_EXISTS(134,"菜单已经存在,或菜单编号重复"),
+    PARENTMENU_NOT_EXISTS(135,"上级菜单不存在"),
+    MENUCODE_IS_EMPTY(136,"菜单编号不能为空"),
+    MENUNAME_IS_EMPTY(137,"菜单名称不能为空"),
+    PATIENTCODEANDRAWPATIENTCODE_IS_EMPTY_TOGETHER(138,"患者编号和住院号不能同时为空"),
+    PLATFORM_ACCOUNT_NOT_EXISTS(139,"平台账号不存在"),
+    DEVICE_REGISTER_FAILURE(140,"向平台注册设备失败"),
+    DEVICE_AMOUNT_NOT_NULL(141,"设备数量不能为空"),
+    TUORENPLATDATA_ANALY_ERROR(142,"驼人平台数据解析错误"),
+    CLINICCODE_NOT_EMPTY(143,"临床编号不能为空"),
+    CLINIC_NOT_EMPTY(144,"临床不存在"),
+
+    NONE(1000,"没有错误");
+
+    private Integer code;
+    private String message;
+
+    public Integer getCode() {
+        return code;
+    }
+    public void setCode(Integer code) {
+        this.code = code;
+    }
+    public String getMessage() {
+        return message;
+    }
+    public void setMessage(String message) {
+        this.message = message;
+    }
+    ErrorStatus(Integer code, String message){
+        this.code = code;
+        this.message = message;
+    }
+
+
+}

+ 52 - 8
coffee-system/src/main/java/com/coffee/bus/service/LocalBusDeviceService.java

@@ -1,9 +1,10 @@
 package com.coffee.bus.service;
 
 import cn.hutool.core.util.StrUtil;
+import com.aliyuncs.iot.model.v20180120.QueryDeviceDetailResponse;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
-import com.coffee.bus.entity.BusClinicEntity;
+import com.coffee.aliyun.sdk.AliyunIotSdk;
+import com.coffee.bus.bean.AliIotConfig;
 import com.coffee.bus.registry.device.DeviceRegistry;
 import com.coffee.bus.entity.BusDeviceEntity;
 import com.coffee.bus.mapper.BusDeviceMapper;
@@ -14,8 +15,8 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Service;
 
-import java.util.Date;
 import java.util.Objects;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * @author lifang
@@ -35,6 +36,9 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
     @Autowired
     private BusDeviceMapper deviceMapper;
 
+    @Autowired
+    private AliyunIotSdk aliyunIotSdk;
+
     @Override
     public void validateBeforeSave(BusDeviceEntity entity) {
 
@@ -94,8 +98,8 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
      * 根据deviceId更新设备在线状态
      * @param device
      */
-    public void updateByDeviceId(BusDeviceEntity device){
-        this.update(device,new QueryWrapper<BusDeviceEntity>().lambda()
+    public boolean updateByDeviceId(BusDeviceEntity device){
+        return this.update(device,new QueryWrapper<BusDeviceEntity>().lambda()
                 .eq(BusDeviceEntity::getDeviceId,device.getDeviceId()));
     }
 
@@ -104,7 +108,7 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
      * 添加设备
      * @param entity
      */
-    public void saveByDeviceId(BusDeviceEntity entity) {
+    public boolean saveByDeviceId(BusDeviceEntity entity) {
         // 查询设备是否存在,无视逻辑删除
         BusDeviceEntity old = deviceMapper.selectOneByDeviceId(entity.getDeviceId());
         // 设备存在,且处于删除状态,首先去掉逻辑删除标志 ?? 逻辑删除后设备数据是否不再接收
@@ -114,10 +118,10 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
         // 设备存在
         if (Objects.nonNull(old)){
             // 更新设备
-            this.updateByDeviceId(entity);
+            return this.updateByDeviceId(entity);
         }else {
             // 添加设备
-            this.save(entity);
+            return this.save(entity);
         }
     }
 
@@ -132,4 +136,44 @@ public class LocalBusDeviceService extends BaseService<BusDeviceMapper, BusDevic
                 .eq(BusDeviceEntity::getDeviceId,deviceId));
         return device;
     }
+
+    /**
+     * @author 龙三郎
+     * 该方法用于从阿里云同步设备,系统暂时不允许创建非阿里云物联网平台设备。意思是系统中的设备必须存在于阿里云物联网平台。
+     * 通过deviceId从阿里云查询设备,如果设备在阿里云平台不存在,则创建失败,如果存在,则获取设备数据,插入或更新设备数据,表示创建成功。
+     * @param deviceId
+     * @return
+     */
+    public BusDeviceEntity add(String deviceId) {
+        BusDeviceEntity device = new BusDeviceEntity();
+        device.setConfig(new AliIotConfig());
+        // 从阿里云物联网查询设备
+        QueryDeviceDetailResponse response = aliyunIotSdk.queryDeviceDetail(deviceId);
+        // 设备存在
+        if (response.getSuccess()){
+            device.setFields(response.getData());
+            // 存储设备
+            this.saveByDeviceId(device);
+            return device;
+        }
+        // 不存在返回null
+        return null;
+    }
+
+    /**
+     * @author 龙三郎
+     * 从阿里云平台获取全部的设备,同步到系统数据库
+     * @return
+     */
+    public int syncDevice() {
+        AtomicReference<Integer> n = new AtomicReference<>(0);
+        aliyunIotSdk.queryDevice().forEach(item ->{
+            BusDeviceEntity device = new BusDeviceEntity();
+            device.setConfig(new AliIotConfig());
+            // 更新设备属性
+            device.setFields(item);
+            n.updateAndGet(v -> v + (this.saveByDeviceId(device)?1:0));
+        });
+        return n.get();
+    }
 }

+ 1 - 1
coffee-system/src/main/java/com/coffee/bus/websocket/listener/DeviceInfoListener.java

@@ -468,7 +468,7 @@ public class DeviceInfoListener {
 
     public static int count=1;
     public static long mark=System.currentTimeMillis();
-    @Scheduled(cron = "0/10 * * * * ?")
+//    @Scheduled(cron = "0/10 * * * * ?")
     public void send() throws InterruptedException {
 //        List<BusDeviceRunningEntity> list = deviceUsingService.list();
 //        list.forEach(pump->{