jeecg-boot-base-core/src/main/java/org/jeecg/common/constant/MqttConstant.java
@@ -27,6 +27,18 @@ /**************************移动端向服务端请求指令end*******************************/ /**************************租户端向服务端发送数据start*******************************/ String TENANT_UP_PREFIX = "tenant/up"; String TENANT_UP_PREFIX_REALTIME_DATA = TENANT_UP_PREFIX + "/realTime/data"; /**************************租户端向服务端发送数据end*******************************/ /**************************服务端向移动端响应指令start*******************************/ //返回数据仅返回给请求的客户端 %s发起请求客户端id //服务端下行指令前缀(返回给移动端) jeecg-module-dry/jeecg-module-dry-api/src/main/java/org/jeecg/modules/dry/entity/DryHerbFormula.java
@@ -1,15 +1,14 @@ package org.jeecg.modules.dry.entity; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.util.Date; import java.math.BigDecimal; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableLogic; import lombok.Data; import com.fasterxml.jackson.annotation.JsonFormat; import org.jeecg.modules.dry.vo.RealTimeDataVo; import org.springframework.format.annotation.DateTimeFormat; import org.jeecgframework.poi.excel.annotation.Excel; import org.jeecg.common.aspect.annotation.Dict; @@ -127,4 +126,19 @@ @DateTimeFormat(pattern="yyyy-MM-dd") @ApiModelProperty(value = "更新时间") private Date updateTime; public DryHerbFormula(){} public DryHerbFormula(RealTimeDataVo realTimeDataVo) { this.code = realTimeDataVo.getIndex() + ""; this.delay = realTimeDataVo.getDelay(); this.name = realTimeDataVo.getName(); this.delay = realTimeDataVo.getDelay(); this.turn = realTimeDataVo.getTurntime(); this.feed = realTimeDataVo.getWeight1(); this.target = realTimeDataVo.getMoisture3(); this.windTemp = realTimeDataVo.getTemp3(); this.et = realTimeDataVo.getTime1(); this.tenantId = realTimeDataVo.getTenantid(); } } jeecg-module-dry/jeecg-module-dry-api/src/main/java/org/jeecg/modules/dry/vo/DryOrderVo.java
@@ -163,7 +163,7 @@ private String warning; /**状态**/ private Map<String, Object> eqp_state; // private Map<String, Object> eqp_state; /**设备状态**/ private String eqp_status; jeecg-module-dry/jeecg-module-dry-api/src/main/java/org/jeecg/modules/dry/vo/RealTimeDataVo.java
@@ -1,5 +1,6 @@ package org.jeecg.modules.dry.vo; import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.annotations.ApiModelProperty; import io.swagger.models.auth.In; import lombok.Data; @@ -13,6 +14,8 @@ @Data @ToString public class RealTimeDataVo { private static final long serialVersionUID = 1L; /**工单ID*/ private String orderId; @@ -91,7 +94,7 @@ private Integer ai_time; private List<DryEquipment> eqpCodes; // private List<DryEquipment> eqpCodes; /**接管状态 0:自动 1:手动 */ @@ -113,7 +116,7 @@ private Integer workorder_status; /**各部位状态**/ private Map<String, Object> eqp_state; // private Map<String, Object> eqp_state; /**设备状态**/ private String eqp_status; @@ -137,6 +140,7 @@ private String report_head_num ; //数量--16框/355.8Kg private String report_head_machine ; //设备 private String report_head_accepter; //接料人 @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd") private Date report_head_date ; //生产日期 private String report_head_leader ; //班组长 private String report_head_techer ; //工艺员 @@ -172,6 +176,7 @@ private Boolean report_clean_door ; //门窗清理 private Boolean report_clean_box ; //箱体清理 private Boolean report_clean_record ; //生产记录清理 @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd") private Date report_clean_date ; //清场日期 private String report_clean_man ; //清场人 private Boolean report_clean_confirm ; //清场确认 jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/controller/DryRealTimeDataController.java
@@ -3,20 +3,27 @@ import ai.djl.modality.Classifications; import cn.hutool.core.bean.BeanUtil; import com.alibaba.druid.support.json.JSONUtils; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; import org.jeecg.common.api.vo.Result; import org.jeecg.common.config.TenantContext; import org.jeecg.common.constant.MqttConstant; import org.jeecg.common.system.query.QueryGenerator; import org.jeecg.common.util.IpUtils; import org.jeecg.common.util.SpringContextUtils; import org.jeecg.common.util.oConvertUtils; import org.jeecg.config.mybatis.MybatisPlusSaasConfig; import org.jeecg.modules.dry.entity.*; import org.jeecg.modules.dry.mqtt.MqttConfig; import org.jeecg.modules.dry.mqtt.MqttUtil; import org.jeecg.modules.dry.service.*; //import org.jeecg.modules.dry.util.HerbUtil; @@ -59,6 +66,12 @@ // private HerbUtil herbUtil; @Autowired private MqttUtil mqttUtil; @Autowired private MqttConfig mqttConfig; @Autowired private IDryHerbInfoService herbInfoService; @Autowired @@ -78,6 +91,18 @@ @ApiOperation(value="接收实时数据Json", notes="设备实时数据上传") @PostMapping("/sendRealTimeDataJson") public Result<?> realTimeDataJson(@RequestBody RealTimeDataVo realTimeDataVo) { try { if ("user".equals(mqttConfig.getRole())){ MqttMessage mqttMessage = new MqttMessage(); mqttMessage.setQos(0); mqttMessage.setPayload(JSONObject.toJSONString(realTimeDataVo).getBytes()); mqttUtil.getMqttClient().publish(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA,mqttMessage); } } catch (MqttException e) { e.printStackTrace(); } return dryRealTimeDataService.realTimeDataHandle(realTimeDataVo); } jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttConfig.java
@@ -3,6 +3,7 @@ import cn.hutool.core.thread.ThreadUtil; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.eclipse.paho.client.mqttv3.*; import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence; @@ -20,6 +21,7 @@ /** * mqtt */ @Data @Slf4j @Configuration public class MqttConfig { @@ -92,6 +94,9 @@ //订阅移动端上行指令 mqttClient.subscribe(MqttConstant.MOBILE_UP); System.err.println("admin订阅" + MqttConstant.MOBILE_UP); // 订阅租户实时数据 mqttClient.subscribe(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA); System.out.println("admin订阅" + MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA); initClients(); break; jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/mqtt/MqttSampleCallback.java
@@ -1,6 +1,7 @@ package org.jeecg.modules.dry.mqtt; import cn.hutool.core.thread.ThreadUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; import org.eclipse.paho.client.mqttv3.MqttCallback; @@ -11,6 +12,8 @@ import org.jeecg.common.util.RedisUtil; import org.jeecg.modules.base.service.BaseCommonService; import org.jeecg.modules.dry.api.EmqxApi; import org.jeecg.modules.dry.service.IDryRealTimeDataService; import org.jeecg.modules.dry.vo.RealTimeDataVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; @@ -29,6 +32,9 @@ private BaseCommonService baseCommonService; @Autowired private RedisUtil redisUtil; @Autowired private IDryRealTimeDataService realTimeDataService; @Override @@ -84,7 +90,12 @@ // 普通用户 case "user": System.err.println("user"); try { parseUserCommand(topic, mqttMessage); } catch (Exception e) { e.printStackTrace(); } break; } @@ -111,7 +122,11 @@ if (messageJson.containsKey("timestamp")) { messageJson.put("timestamp", DateUtils.zone2Str(messageJson.get("timestamp").toString())); } // 实时数据上传太频繁且数据内容超过字段大小不记录日志 if (!topic.equals(MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA)){ baseCommonService.addLog(message, CommonConstant.LOG_TYPE_MQTT, CommonConstant.OPERATE_MQTT_1); } switch (topic) { // 查询设备在线 case MqttConstant.MOBILE_QUERY_EQU_STATU: @@ -149,6 +164,17 @@ }); break; // 接收设备实时数据 case MqttConstant.TENANT_UP_PREFIX_REALTIME_DATA: try { RealTimeDataVo vo = JSON.parseObject(message, RealTimeDataVo.class); realTimeDataService.realTimeDataHandle(vo); } catch (Exception e) { e.printStackTrace(); } break; } } jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java
@@ -97,6 +97,10 @@ // 根据租户id和工单号查询数据库是否有记录,有则返回,没有则新增一条再返回 orderVo = getOrSaveDryOrderVoDB(realTimeDataVo); } if (orderVo == null) { log.error("工单不存在,工单号:"+realTimeDataVo.getWorkorder()+",设备:" + realTimeDataVo.getMachineid() +",药材:" + realTimeDataVo.getName()); return Result.error("工单不存在"); } // 2 更新工单实时数据 // 2.1 将工单中的数据替换为最新数据 @@ -126,7 +130,7 @@ orderVo.setState_valve(realTimeDataVo.getState_valve()); orderVo.setOrderStatus(realTimeDataVo.getWorkorder_status()); orderVo.setEqp_status(realTimeDataVo.getEqp_status()); orderVo.setEqp_state(realTimeDataVo.getEqp_state()); // orderVo.setEqp_state(realTimeDataVo.getEqp_state()); orderVo.setWarning(realTimeDataVo.getEqp_warning()); orderVo.setFault(realTimeDataVo.getEqp_fault()); orderVo.setLevel(realTimeDataVo.getLevel()); @@ -232,7 +236,6 @@ * @param realTimeDataVo * @return */ @NotNull private DryOrderVo saveNewOrder(RealTimeDataVo realTimeDataVo) { DryOrderVo orderVo; // 查询药材 @@ -242,6 +245,10 @@ // 创建新工单 orderVo = new DryOrderVo(realTimeDataVo); if (herbFormula == null) { log.error("未找到药材:"+realTimeDataVo.getIndex() +","+realTimeDataVo.getName() +",机台:" + realTimeDataVo.getMachineid()); return null; } orderVo.setHerbId(herbFormula.getId()); orderVo.setEquId(equ.getId()); DryOrder dryOrder = BeanUtil.toBean(orderVo, DryOrder.class); @@ -275,12 +282,14 @@ private DryHerbFormula queryHerbByIndexTenant(RealTimeDataVo realTimeDataVo) { LambdaQueryWrapper<DryHerbFormula> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(DryHerbFormula::getTenantId, realTimeDataVo.getTenantid()); queryWrapper.eq(DryHerbFormula::getCode, realTimeDataVo.getIndex()); queryWrapper.eq(DryHerbFormula::getName, realTimeDataVo.getName()); DryHerbFormula one = dryHerbFormulaService.getOne(queryWrapper); // if (one == null) { // one = new DryHerb(realTimeDataVo); // herbService.save(one); // } if (one == null) { one = new DryHerbFormula(realTimeDataVo); DryEquipment dryEquipment = equipmentService.selectByTenantIdEquipmentId(realTimeDataVo.getTenantid() + "", realTimeDataVo.getMachineid()); one.setEqpType(dryEquipment.getType()); dryHerbFormulaService.save(one); } return one; } @@ -318,7 +327,7 @@ public Result<?> queryMachineRealTImeData(RealTimeDataVo realTimeDataVo) { TenantContext.setTenant(realTimeDataVo.getTenantid()+""); // 查询所有机台查询组装 // 查询所有机台,查询语句组装 LambdaQueryWrapper<DryEquipment> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(DryEquipment::getTenantId, realTimeDataVo.getTenantid()); queryWrapper.eq(DryEquipment::getEnable, "Y"); @@ -351,9 +360,11 @@ dList.add(50d); } } } else { // 如果没有生产,效率置0 dList.add(0d); } }); }