Przeglądaj źródła

add 下发指令

18339543638 4 lat temu
rodzic
commit
4f2395abb7

+ 0 - 2
jetlinks-core/src/main/java/org/jetlinks/core/message/CommonDeviceMessage.java

@@ -23,8 +23,6 @@ public class CommonDeviceMessage implements DeviceMessage {
 
     private String deviceId;
 
-    private boolean mock;
-
     private Map<String, Object> headers;
 
     private long timestamp = System.currentTimeMillis();

+ 33 - 7
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/entity/DeviceDirectivesEntity.java

@@ -1,6 +1,8 @@
 package org.jetlinks.community.device.entity;
 
+import cn.hutool.json.JSONUtil;
 import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
 import io.swagger.v3.oas.annotations.media.Schema;
 import lombok.AllArgsConstructor;
 import lombok.Getter;
@@ -10,13 +12,11 @@ import org.hswebframework.ezorm.rdb.mapping.annotation.*;
 import org.hswebframework.web.api.crud.entity.GenericEntity;
 import org.hswebframework.web.dict.Dict;
 import org.hswebframework.web.dict.EnumDict;
-import org.hswebframework.web.validator.CreateGroup;
 import org.jetlinks.community.device.enums.DirectiveState;
-import org.jetlinks.core.message.DeviceMessage;
+import org.jetlinks.core.message.CommonDeviceMessage;
 import javax.persistence.Column;
 import javax.persistence.Index;
 import javax.persistence.Table;
-import javax.validation.constraints.NotBlank;
 import java.sql.JDBCType;
 
 /**
@@ -28,7 +28,6 @@ import java.sql.JDBCType;
  */
 @Getter
 @Setter
-@AllArgsConstructor
 @NoArgsConstructor
 @Table(name = "dev_device_directives",indexes = {
     @Index(name = "directives_device_id", columnList = "device_id,message_id",unique = true),
@@ -74,13 +73,15 @@ public class DeviceDirectivesEntity extends GenericEntity<String> {
     @Column(name = "reply_message")
     @ColumnType(jdbcType = JDBCType.CLOB)
     @Schema(description = "回复内容")
-    private DeviceMessage replyMessage;
+    @JsonCodec
+    private CommonDeviceMessage replyMessage;
 
     @Comment("下发指令")
     @Column(name = "send_message")
     @ColumnType(jdbcType = JDBCType.CLOB)
     @Schema(description = "下发指令")
-    private DeviceMessage sendMessage;
+    @JsonCodec
+    private CommonDeviceMessage sendMessage;
 
     @Column(name = "state",length = 16)
     @EnumCodec
@@ -93,11 +94,23 @@ public class DeviceDirectivesEntity extends GenericEntity<String> {
     )
     private DirectiveState state;
 
+    public DeviceDirectivesEntity(String productId, String deviceId, String messageId, Long sendTimestamp, MessageType messageType, String lastError, CommonDeviceMessage replyMessage, CommonDeviceMessage sendMessage, DirectiveState state) {
+        this.productId = productId;
+        this.deviceId = deviceId;
+        this.messageId = messageId;
+        this.sendTimestamp = sendTimestamp;
+        this.messageType = messageType;
+        this.lastError = lastError;
+        this.replyMessage = replyMessage;
+        this.sendMessage = sendMessage;
+        this.state = state;
+    }
+
     public static DeviceDirectivesEntity of(String productId,
                                             String deviceId,
                                             String messageId,
                                             long sendTimestamp,
-                                            MessageType messageType, DeviceMessage sendMessage) {
+                                            MessageType messageType, CommonDeviceMessage sendMessage) {
         return new DeviceDirectivesEntity(productId,deviceId,messageId,sendTimestamp,messageType,null,null,sendMessage,null);
     }
 
@@ -114,5 +127,18 @@ public class DeviceDirectivesEntity extends GenericEntity<String> {
         public String getValue() {
             return this.name();
         }
+        public org.jetlinks.core.message.MessageType convertTo(){
+            if(READ_PROPERTY.equals(this)){
+                return org.jetlinks.core.message.MessageType.READ_PROPERTY;
+            }
+            if(WRITE_PROPERTY.equals(this)){
+                return org.jetlinks.core.message.MessageType.WRITE_PROPERTY;
+            }
+            if(INVOKE_FUNCTION.equals(this)){
+                return org.jetlinks.core.message.MessageType.INVOKE_FUNCTION;
+            }
+            return null;
+        }
     }
+
 }

+ 2 - 1
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/interceptor/DeviceDirectiveSendInterceptor.java

@@ -7,6 +7,7 @@ import org.jetlinks.community.device.enums.DirectiveState;
 import org.jetlinks.community.device.service.DeviceDirectivesService;
 import org.jetlinks.core.device.DeviceOperator;
 import org.jetlinks.core.exception.DeviceOperationException;
+import org.jetlinks.core.message.CommonDeviceMessage;
 import org.jetlinks.core.message.DeviceMessage;
 import org.jetlinks.core.message.function.FunctionInvokeMessage;
 import org.jetlinks.core.message.interceptor.DeviceMessageSenderInterceptor;
@@ -34,7 +35,7 @@ public class DeviceDirectiveSendInterceptor implements DeviceMessageSenderInterc
     public Mono<DeviceMessage> preSend(DeviceOperator device, DeviceMessage message) {
         DeviceDirectivesEntity directivesEntity = new DeviceDirectivesEntity();
         directivesEntity.setState(DirectiveState.wait);
-        directivesEntity.setSendMessage(message);
+        directivesEntity.setSendMessage((CommonDeviceMessage) message);
         directivesEntity.setSendTimestamp(message.getTimestamp());
         directivesEntity.setMessageId(message.getMessageId());
         DeviceDirectivesEntity.MessageType directive = isDirective(message);

+ 27 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/web/DeviceDirectiveController.java

@@ -1,16 +1,27 @@
 package org.jetlinks.community.device.web;
 
+import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.AllArgsConstructor;
+import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.hswebframework.web.authorization.annotation.Authorize;
+import org.hswebframework.web.authorization.annotation.CreateAction;
+import org.hswebframework.web.authorization.annotation.QueryAction;
 import org.hswebframework.web.authorization.annotation.Resource;
 import org.hswebframework.web.crud.service.ReactiveCrudService;
 import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
+import org.hswebframework.web.exception.BusinessException;
 import org.jetlinks.community.device.entity.DeviceDirectivesEntity;
 import org.jetlinks.community.device.service.DeviceDirectivesService;
+import org.jetlinks.core.device.DeviceOperator;
+import org.jetlinks.core.device.DeviceRegistry;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
 
 /**
  * @author lifang
@@ -29,8 +40,24 @@ import org.springframework.web.bind.annotation.RestController;
 public class DeviceDirectiveController implements
     ReactiveServiceCrudController<DeviceDirectivesEntity, String> {
     private final DeviceDirectivesService directivesService;
+    private final DeviceRegistry deviceRegistry;
     @Override
     public ReactiveCrudService<DeviceDirectivesEntity, String> getService() {
         return directivesService;
     }
+
+    @PostMapping("/resend")
+    @CreateAction
+    @Operation(summary = "批量发送指令到设备")
+    public Mono<Boolean> sendDirectives(@RequestBody Flux<DeviceDirectivesEntity> directives){
+         return directives
+            .flatMap(directive->
+                deviceRegistry
+                    .getDevice(directive.getDeviceId())
+                    .map(DeviceOperator::messageSender)
+                    .map(send->send.send(directive.getSendMessage()))
+            )
+             .doOnError(e->Mono.error(new BusinessException("参数错误")))
+         .then(Mono.just(true));
+    }
 }

+ 12 - 0
jetlinks-manager/device-manager/src/main/java/org/jetlinks/community/device/web/request/DirectiveParam.java

@@ -0,0 +1,12 @@
+package org.jetlinks.community.device.web.request;
+
+/**
+ * @author lifang
+ * @version 1.0.0
+ * @ClassName DirectiveParams.java
+ * @Description 下发指令参数
+ * @createTime 2021年11月24日 10:53:00
+ */
+public class DirectiveParam {
+
+}