Browse Source

fix websocket 订阅发布模式
add 泵状态枚举

18339543638 3 năm trước cách đây
mục cha
commit
38066f441f

+ 58 - 0
coffee-admin/src/main/test/java/com/coffee/admin/BusNetpumpTest.java

@@ -0,0 +1,58 @@
+package com.coffee.admin;
+
+import com.coffee.bus.controller.BusHospitalController;
+import com.coffee.bus.controller.BusNetPumpController;
+import com.coffee.bus.entity.BusHospitalEntity;
+import com.coffee.bus.entity.BusNetPumpEntity;
+import com.coffee.bus.enums.NetPumpAlarmEnum;
+import com.coffee.bus.service.LocalBusHospitalService;
+import com.coffee.bus.service.LocalBusNetPumpService;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+import java.util.Date;
+import java.util.List;
+
+/**
+ * @author lifang
+ * @version 1.0.0
+ * @ClassName BusHospitalTest.java
+ * @Description TODO
+ * @createTime 2022年03月19日 10:27:00
+ */
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes = AdminApplication.class)
+public class BusNetpumpTest {
+    @Autowired
+    private LocalBusNetPumpService netPumpService;
+
+    @Autowired
+    private BusNetPumpController netPumpController;
+    @Test
+    public void save(){
+//        StpUtil.login();
+        BusNetPumpEntity netPumpEntity = new BusNetPumpEntity();
+        netPumpEntity.setAlarm(NetPumpAlarmEnum.ComingEnd);
+        netPumpEntity.setAlias("1");
+        netPumpEntity.setBedNo("1");
+        netPumpEntity.setDeviceId("123");
+        netPumpEntity.setTenantId("123");
+        netPumpEntity.setRegisterTime(new Date());
+        netPumpService.save(netPumpEntity);
+    }
+
+    @Test
+    public void query(){
+        List<BusNetPumpEntity> list = netPumpService.list();
+        System.out.println(list);
+    }
+
+    @Test
+    public void Delete(){
+//        boolean b = busHospitalService.removeById(1505789328745721857L);
+//        System.out.println(b);
+    }
+}

+ 6 - 3
coffee-common/src/main/java/com/coffee/common/config/websocket/DefaultMessageListener.java

@@ -8,6 +8,7 @@ import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.data.redis.connection.Message;
 import org.springframework.data.redis.connection.MessageListener;
+import org.springframework.util.SerializationUtils;
 import org.tio.core.ChannelContext;
 import org.tio.core.Tio;
 import org.tio.websocket.common.WsResponse;
@@ -30,10 +31,12 @@ public class DefaultMessageListener implements MessageListener {
     @Override
     public void onMessage(Message message, byte[] pattern) {
         if (CollectionUtil.isNotEmpty(channelContexts)) {
-            String body= new String(message.getBody());
             channelContexts.parallelStream()
-                    .filter(channelContext -> !channelContext.isClosed)
-                    .forEach(channel -> Tio.send(channel, WsResponse.fromText(JSONUtil.toJsonStr(R.success(body)),"utf-8")));
+//                    .filter(channelContext -> !channelContext.isClosed)
+                    .forEach(channel -> Tio.send(channel,
+                            WsResponse.fromText(JSONUtil.toJsonStr(R.success(
+                                    JSONUtil.parseArray(new String(message.getBody())).get(1)))
+                                    ,"utf-8")));
         }
     }
 }

+ 1 - 1
coffee-common/src/main/java/com/coffee/common/config/websocket/handler/Subscribe.java

@@ -53,7 +53,6 @@ public abstract class Subscribe implements WsHandler {
         //同一主题只订阅一次
         Set<ChannelContext> channelContexts = Optional.ofNullable(subscribeTopics.get(topic)).orElse(new HashSet<>());
         if(!subscribeTopics.containsKey(topic)){
-            channelContexts.add(channelContext);
             redisTemplate.execute(new RedisCallback<Object>() {
                 @Override
                 public Object doInRedis(RedisConnection connection) throws DataAccessException {
@@ -65,6 +64,7 @@ public abstract class Subscribe implements WsHandler {
             new Thread(()->singleThreadEventLoop.scheduleAtFixedRate(()->redisTemplate.convertAndSend(topic, MessageResponse.of(getId(),"result","主题为["+topic+"]的回复消息")),1,3, TimeUnit.SECONDS))
                     .start();
         }
+        channelContexts.add(channelContext);
         subscribeTopics.put(topic,channelContexts);
         //将主题与ws通道绑定
         Object result = Optional.ofNullable(channelContext.get(SUBSCRIBE_TOPIC)).orElse(new HashSet<>());

+ 2 - 0
coffee-common/src/main/java/com/coffee/common/entity/TenantGenericEntity.java

@@ -8,6 +8,7 @@ import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
 import lombok.Getter;
+import lombok.Setter;
 
 /**
  * @author lifang
@@ -22,6 +23,7 @@ public abstract class TenantGenericEntity<PK,TN> extends GenericEntity<PK> {
 
     @Getter
     @TableField("tenant_id")
+    @Setter
 //    @JsonFormat
 //    @JSONField(serializeUsing = ToStringSerializer.class)
 //    @JsonSerialize(using = ToStringSerializer.class,contentAs=String.class)

+ 16 - 2
coffee-system/src/main/java/com/coffee/bus/controller/BusNetPumpController.java

@@ -1,14 +1,17 @@
 package com.coffee.bus.controller;
 
 import com.baomidou.mybatisplus.core.mapper.Mapper;
-import com.coffee.bus.entity.BusDoctorEntity;
 import com.coffee.bus.entity.BusNetPumpEntity;
-import com.coffee.bus.service.LocalBusDoctorService;
+import com.coffee.bus.enums.NetPumpAlarmEnum;
+import com.coffee.bus.enums.NetPumpStatusEnum;
 import com.coffee.bus.service.LocalBusNetPumpService;
 import com.coffee.common.crud.BaseService;
 import com.coffee.common.crud.controller.BaseCrudController;
+import com.coffee.common.result.R;
 import io.swagger.annotations.Api;
+import io.swagger.v3.oas.annotations.Operation;
 import lombok.AllArgsConstructor;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -26,7 +29,18 @@ import org.springframework.web.bind.annotation.RestController;
 public class BusNetPumpController extends BaseCrudController<BusNetPumpEntity, String> {
     private final LocalBusNetPumpService netPumpService;
 
+    @GetMapping("/alarm/dict")
+    @Operation(summary = "网络泵报警信息枚举,无权限配置")
+    public R alarm(){
+        return R.success(NetPumpAlarmEnum.values());
+    }
+
 
+    @GetMapping("/status/dict")
+    @Operation(summary = "网络泵状态枚举,无权限配置, alarm字段表示该状态是否为报警状态")
+    public R status(){
+        return R.success(NetPumpStatusEnum.values());
+    }
     /**
      * 权限控制前缀
      * @return

+ 13 - 2
coffee-system/src/main/java/com/coffee/bus/entity/BusNetPumpEntity.java

@@ -4,14 +4,20 @@ import com.alibaba.fastjson.annotation.JSONField;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
+import com.coffee.bus.enums.NetPumpAlarmEnum;
+import com.coffee.bus.enums.NetPumpStatusEnum;
 import com.coffee.common.config.mybatis.DateToBigIntHandler;
 import com.coffee.common.entity.TenantGenericEntity;
+import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
 import com.fasterxml.jackson.annotation.JsonSetter;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.EnumSerializer;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 import lombok.EqualsAndHashCode;
 import lombok.experimental.Accessors;
+import org.apache.ibatis.type.EnumOrdinalTypeHandler;
 
 import java.io.Serializable;
 import java.math.BigDecimal;
@@ -29,7 +35,7 @@ import java.util.Date;
 @Data
 @EqualsAndHashCode(callSuper = false)
 @Accessors(chain = true)
-@TableName("bus_netpump")
+@TableName(value = "bus_netpump",autoResultMap = true)
 @ApiModel(value="网络泵", description="")
 public class BusNetPumpEntity extends TenantGenericEntity<String,String> {
 
@@ -92,7 +98,12 @@ public class BusNetPumpEntity extends TenantGenericEntity<String,String> {
 
 
     @ApiModelProperty(value = "镇痛泵运行状态")
-    private Integer runState;
+    @TableField(typeHandler = EnumOrdinalTypeHandler.class)
+    private NetPumpStatusEnum runState;
+
+    @ApiModelProperty(value = "报警信息")
+    @TableField(typeHandler = EnumOrdinalTypeHandler.class)
+    private NetPumpAlarmEnum alarm;
 
     @ApiModelProperty(value = "镇痛泵状态BUF")
     private Integer bufState1;

+ 40 - 0
coffee-system/src/main/java/com/coffee/bus/enums/NetPumpAlarmEnum.java

@@ -0,0 +1,40 @@
+package com.coffee.bus.enums;
+
+import com.alibaba.fastjson.annotation.JSONType;
+import com.fasterxml.jackson.annotation.*;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+
+/**
+ * @author lifang
+ * @version 1.0.0
+ * @ClassName AlarmEnum.java
+ * @Description 泵报警类型
+ * @createTime 2022年03月27日 09:49:00
+ */
+@AllArgsConstructor
+@JsonFormat(shape = JsonFormat.Shape.OBJECT)
+public enum  NetPumpAlarmEnum {
+    /**
+     * 请在添加或修改的过程中不要改变枚举的顺序!!!!!!!!!!!!!!!
+     * 如若新增,请在最后尾部新增
+     * 切记不可删除!!!!!!!!!!!!
+     */
+    UpLimit(0,"加档受限"),
+    ComingEnd(1,"即将输液完毕"),
+    LowInfusion(2,"低输注状态"),
+    LowBattery(3,"电量不足"),
+    FlowRateLimit(4,"流速已达上限"),
+    InsufficientAnalgesia(5,"镇痛不足");
+
+
+    /**
+     * 与枚举ordinal保持一致
+     */
+    @Getter
+    private Integer code;
+    @Getter
+    private String text;
+
+}

+ 51 - 0
coffee-system/src/main/java/com/coffee/bus/enums/NetPumpStatusEnum.java

@@ -0,0 +1,51 @@
+package com.coffee.bus.enums;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import io.swagger.annotations.ApiOperation;
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.Setter;
+
+/**
+ * @author lifang
+ * @version 1.0.0
+ * @ClassName AlarmEnum.java
+ * @Description 泵状态类型
+ * @createTime 2022年03月27日 09:49:00
+ */
+@AllArgsConstructor
+@JsonFormat(shape = JsonFormat.Shape.OBJECT)
+public enum NetPumpStatusEnum {
+    /**
+     * 请在添加或修改的过程中不要改变枚举的顺序!!!!!!!!!!!!!!!
+     * 如若新增,请在最后尾部新增
+     * 切记不可删除!!!!!!!!!!!!
+     */
+    Running(0,"正在运行",false),
+    Waiting(1,"待机中",false),
+    Shutdown(2,"关机",false),
+
+    //    以下为报警信息状态
+    Finished(3,"输液结束",true),
+    LowBattery(4,"低电量报警",true),
+    Jam(5,"堵塞报警",true),
+    Bubble(6,"气泡报警",true),
+    NoSignal(7,"不在服务区",true),
+    Machine(8,"机械故障",true),
+    NotFastened(9,"未扣合股涨",true);
+
+
+    /**
+     * 与枚举ordinal保持一致
+     */
+    @Getter
+    private Integer code;
+    @Getter
+    private String text;
+    /**
+     * 是否为报警状态
+     */
+    @Getter
+    private Boolean alarm;
+
+}

+ 10 - 8
coffee-system/src/main/java/com/coffee/bus/listener/event/bean/DeviceInfoEvent.java

@@ -2,7 +2,10 @@ package com.coffee.bus.listener.event.bean;
 
 import cn.hutool.extra.spring.SpringUtil;
 import com.alibaba.fastjson.JSONObject;
+import com.coffee.bus.entity.BusNetPumpEntity;
 import com.coffee.bus.listener.enums.ReportType;
+import lombok.Getter;
+import lombok.Setter;
 import org.springframework.context.ApplicationEvent;
 import java.time.Clock;
 
@@ -10,33 +13,32 @@ import java.time.Clock;
  * @author lifang
  * @version 1.0.0
  * @ClassName DeviceInfoEvent.java
- * @Description TODO
+ * @Description 所有通过第三方传过来的数据,请先解析成netpump格式类型,然后分发到系统中
+ * @see  BusNetPumpEntity
  * @createTime 2022年03月21日 16:21:00
  */
+@Setter
+@Getter
 public class DeviceInfoEvent extends ApplicationEvent {
 
-    private final ReportType type;
-
     /**
      * 设备属性值
      */
-    private final JSONObject content;
+    private final BusNetPumpEntity content;
 
     /**
      * 设备id,即泵设备号
      */
     private final String deviceId;
 
-    public DeviceInfoEvent(Object source,ReportType type,JSONObject content,String deviceId) {
+    public DeviceInfoEvent(Object source,BusNetPumpEntity content,String deviceId) {
         super(source);
-        this.type=type;
         this.content=content;
         this.deviceId=deviceId;
     }
 
-    public DeviceInfoEvent(Object source, Clock clock,ReportType type,JSONObject content,String deviceId) {
+    public DeviceInfoEvent(Object source, Clock clock,BusNetPumpEntity content,String deviceId) {
         super(source, clock);
-        this.type=type;
         this.content=content;
         this.deviceId=deviceId;
     }