Parcourir la source

B端交班下发MQTT

qiangxuan il y a 1 mois
Parent
commit
2d83aa3277

+ 32 - 0
zgztBus/jeecg-module-sbm/src/main/java/org/jeecg/modules/actualControl/billetActual/billetActual/utils/MqttClientUtil.java

@@ -82,6 +82,38 @@ public class MqttClientUtil implements ApplicationRunner {
         }
     }
 
+    public void pushChangeShiftData(ConfigMqttMapper configMqttMapper, Map<String, Object> map, String topicInfo){
+        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        map.put("time",formatter.format(new Date()));
+        ConfigMqtt configMqtt = configMqttMapper.selectOne(new LambdaQueryWrapper<ConfigMqtt>().like(ConfigMqtt::getTopic,topicInfo).eq(ConfigMqtt::getPushOrSub,"1"));
+        MqttClient mqttClient = null;
+        try {
+            mqttClient = getMqttClient(configMqtt);
+        } catch (MqttException e) {
+            e.printStackTrace();
+        }
+        MqttTopic topic = mqttClient.getTopic(topicInfo);
+        MqttMessage message = new MqttMessage();
+        message.setPayload(JSON.toJSON(map).toString().getBytes());
+        message.setQos(0);
+        message.setRetained(true);
+        if (null == topic) {
+            log.error("topic is not exist");
+        }else{
+            MqttDeliveryToken token;//Delivery:配送
+            synchronized (topic) {//注意:这里一定要同步,否则,在多线程publish的情况下,线程会发生死锁,分析见文章最后补充
+                try {
+                    token = topic.publish(message);//也是发送到执行队列中,等待执行线程执行,将消息发送到消息中间件
+                    token.waitForCompletion(1000L);
+                } catch (MqttPersistenceException e) {
+                    e.printStackTrace();
+                } catch (MqttException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
     public Boolean testConn(ConfigMqtt configMqtt){
         MqttConnectOptions options = new MqttConnectOptions();
         options.setUserName(configMqtt.getUsername());

+ 19 - 13
zgztBus/jeecg-module-sbm/src/main/java/org/jeecg/modules/billet/billetHotsendChangeShift/controller/BilletHotsendChangeShiftController.java

@@ -156,22 +156,28 @@ public class BilletHotsendChangeShiftController extends JeecgController<BilletHo
 	@RequiresPermissions("billetHotsendChangeShift:billet_hotsend_change_shift:add")
 	@PostMapping(value = "/add")
 	public Result<String> add(@RequestBody BilletHotsendChangeShift billetHotsendChangeShiftVo) {
+		// 通过铸机号查询最新的一条记录
 		LambdaQueryWrapper<BilletHotsendChangeShift> queryWrapperCS = new LambdaQueryWrapper<>();
-		queryWrapperCS.between(BilletHotsendChangeShift::getCreateTime, DateUtils.getStartOfDay(), DateUtils.getEndOfDay());
-		List<BilletHotsendChangeShift> billetHotsendChangeShiftList = billetHotsendChangeShiftService.list(queryWrapperCS);
-
-		String nextShift = String.join(",", billetHotsendChangeShiftVo.getShift(), billetHotsendChangeShiftVo.getShiftGroup());
-		if (oConvertUtils.listIsNotEmpty(billetHotsendChangeShiftList)) {
-			boolean exists = billetHotsendChangeShiftList.stream().map(x -> String.join(",", x.getShift(), x.getShiftGroup())).anyMatch(s -> s.equals(nextShift));
-			if (exists){
-				return Result.error("当天不能重复交班");
-			}
+		queryWrapperCS.eq(BilletHotsendChangeShift::getCcmNo, billetHotsendChangeShiftVo.getCcmNo())
+				.isNotNull(BilletHotsendChangeShift::getChangeShiftTime)
+				.orderByDesc(BilletHotsendChangeShift::getCreateTime)
+				.last("limit 1");
+		BilletHotsendChangeShift billetHotsendChangeShift = billetHotsendChangeShiftService.getOne(queryWrapperCS, false);
+		if (billetHotsendChangeShift.getShift().equals(billetHotsendChangeShiftVo.getShift()) && billetHotsendChangeShift.getShiftGroup().equals(billetHotsendChangeShiftVo.getShiftGroup())){
+			return Result.error("该班别已交班,请勿重复交班!");
 		}
-		JSONObject jsonObject = billetHotsendChangeShiftService.billetHotsendChangeShiftHandle(billetHotsendChangeShiftVo);
-		if (jsonObject.containsKey("fail")){
-			return Result.OK((String) jsonObject.get("fail"));
+
+		// B端手动交班下发mqtt
+		if ("5".equals(billetHotsendChangeShiftVo.getCcmNo())){
+			billetHotsendChangeShiftService.fiveChangeShiftHandle(billetHotsendChangeShiftVo);
+		}else if ("6".equals(billetHotsendChangeShiftVo.getCcmNo())){
+			billetHotsendChangeShiftService.sixChangeShiftHandle(billetHotsendChangeShiftVo);
 		}
-		return Result.OK((String) jsonObject.get("success"));
+//		JSONObject jsonObject = billetHotsendChangeShiftService.billetHotsendChangeShiftHandle(billetHotsendChangeShiftVo);
+//		if (jsonObject.containsKey("fail")){
+//			return Result.OK((String) jsonObject.get("fail"));
+//		}
+		return Result.OK("交班成功!");
 	}
 
 	/**

+ 4 - 0
zgztBus/jeecg-module-sbm/src/main/java/org/jeecg/modules/billet/billetHotsendChangeShift/service/IBilletHotsendChangeShiftService.java

@@ -15,4 +15,8 @@ public interface IBilletHotsendChangeShiftService extends IService<BilletHotsend
     JSONObject billetHotsendChangeShiftHandle(BilletHotsendChangeShift billetHotsendChangeShiftVo);
 
     JSONObject autoChangeShiftHandle(BilletHotsendChangeShift billetHotsendChangeShiftVo, String sixShiftGroup, String sixShift);
+
+    void fiveChangeShiftHandle(BilletHotsendChangeShift billetHotsendChangeShiftVo);
+
+    void sixChangeShiftHandle(BilletHotsendChangeShift billetHotsendChangeShiftVo);
 }

+ 34 - 0
zgztBus/jeecg-module-sbm/src/main/java/org/jeecg/modules/billet/billetHotsendChangeShift/service/impl/BilletHotsendChangeShiftServiceImpl.java

@@ -11,12 +11,14 @@ import org.jeecg.common.util.DateUtils;
 import org.jeecg.common.util.oConvertUtils;
 import org.jeecg.modules.actualControl.billetActual.billetActual.entity.BilletBasicInfo;
 import org.jeecg.modules.actualControl.billetActual.billetActual.service.IBilletBasicInfoService;
+import org.jeecg.modules.actualControl.billetActual.billetActual.utils.MqttClientUtil;
 import org.jeecg.modules.billet.billetHotsendChangeShift.entity.BilletHotsendChangeShift;
 import org.jeecg.modules.billet.billetHotsendChangeShift.mapper.BilletHotsendChangeShiftMapper;
 import org.jeecg.modules.billet.billetHotsendChangeShift.service.IBilletHotsendChangeShiftService;
 import org.jeecg.modules.billet.operateLog.service.IOperateLogService;
 import org.jeecg.modules.billet.shiftConfiguration.entity.ShiftConfiguration;
 import org.jeecg.modules.billet.shiftConfiguration.service.IShiftConfigurationService;
+import org.jeecg.modules.connConfig.mapper.ConfigMqttMapper;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
@@ -47,6 +49,9 @@ public class BilletHotsendChangeShiftServiceImpl extends ServiceImpl<BilletHotse
     @Autowired
     private IShiftConfigurationService shiftConfigurationService;
 
+    @Autowired
+    private ConfigMqttMapper configMqttMapper;
+
     @Override
     @Transactional(rollbackFor = Exception.class)
     public JSONObject billetHotsendChangeShiftHandle(BilletHotsendChangeShift billetHotsendChangeShiftVo) {
@@ -296,6 +301,35 @@ public class BilletHotsendChangeShiftServiceImpl extends ServiceImpl<BilletHotse
         }
         return result;
     }
+
+    @Override
+    public void fiveChangeShiftHandle(BilletHotsendChangeShift billetHotsendChangeShiftVo) {
+        try {
+            Map<String, Object> mapSendInfo = new HashMap<>();
+            mapSendInfo.put("ccmNo", billetHotsendChangeShiftVo.getCcmNo());// 铸机号
+            mapSendInfo.put("changeShiftTime", DateUtils.date2Str(new Date(), DateUtils.datetimeFormat.get()));
+            MqttClientUtil mqttClientUtilBe = new MqttClientUtil();
+            mqttClientUtilBe.pushChangeShiftData(configMqttMapper, mapSendInfo, "syn/billet/changeShift");
+            log.info("5号机手动交班发送MQTT成功: {}", mapSendInfo);
+        } catch (Exception e) {
+            log.error("5号机手动交班发送MQTT成功异常!", e);
+        }
+    }
+
+    @Override
+    public void sixChangeShiftHandle(BilletHotsendChangeShift billetHotsendChangeShiftVo) {
+        try {
+            Map<String, Object> mapSendInfo = new HashMap<>();
+            mapSendInfo.put("ccmNo", billetHotsendChangeShiftVo.getCcmNo());
+            mapSendInfo.put("changeShiftTime", DateUtils.date2Str(new Date(), DateUtils.datetimeFormat.get()));
+            MqttClientUtil mqttClientUtilBe = new MqttClientUtil();
+            mqttClientUtilBe.pushChangeShiftData(configMqttMapper, mapSendInfo, "syn/billet/changeSixShift");
+            log.info("6号机手动交班发送MQTT成功: {}", mapSendInfo);
+        } catch (Exception e) {
+            log.error("6号机手动交班发送MQTT成功异常!", e);
+        }
+    }
+
     /**
      * 计算余数并提取相应元素
      * @param billetBasicInfoList

+ 1 - 1
zgztBus/jeecg-server-cloud/jeecg-cloud-nacos/src/main/resources/application.yml

@@ -15,7 +15,7 @@ db:
   url:
 #    '0': jdbc:mysql://192.168.1.190:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
 #  研发环境
-   '0': jdbc:mysql://123.57.213.14:33066/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
+   '0': jdbc:mysql://192.168.1.53:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
 #  本地环境
 #   '0': jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
   user: