干燥机配套车间生产管理系统/云平台服务端
bsw215583320
2024-11-07 e62ffd3aaa1ba27b63885f8a3c8610ed283245d6
jeecg-module-dry/jeecg-module-dry-biz/src/main/java/org/jeecg/modules/dry/service/impl/DryRealTimeDataServiceImpl.java
@@ -8,16 +8,23 @@
import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IoSession;
import org.apache.shiro.SecurityUtils;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.MqttConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.dry.common.CacheConstants;
import org.jeecg.modules.dry.entity.*;
import org.jeecg.modules.dry.mqtt.MqttUtil;
import org.jeecg.modules.dry.service.*;
import org.jeecg.modules.dry.socket.ServerHandler;
import org.jeecg.modules.dry.socket.SocketServerConfig;
@@ -25,9 +32,11 @@
import org.jeecg.modules.dry.vo.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.security.auth.login.LoginContext;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
@@ -63,7 +72,11 @@
    private String token;
    @Value(value = "${jeecg.mqtt.role}")
    private String role;
    @Autowired
    private MqttUtil mqttUtil;
    public String getTemporaryToken() {
        if (token == null) {
@@ -238,17 +251,23 @@
     */
    private DryOrderVo saveNewOrder(RealTimeDataVo realTimeDataVo) {
        DryOrderVo orderVo;
        // 查询药材
        DryHerbFormula herbFormula =  queryHerbByIndexTenant(realTimeDataVo);
        // 查询设备
        DryEquipment equ = queryEquipmentByCodeTenant(realTimeDataVo);
        if (equ == null) {
            log.error("未找到设备:"+realTimeDataVo.getIndex() +","+realTimeDataVo.getName() +",机台:" + realTimeDataVo.getMachineid());
            return null;
        }
        // 查询药材
        DryHerbFormula herbFormula =  queryHerbByIndexTenant(realTimeDataVo);
        // 创建新工单
        orderVo = new DryOrderVo(realTimeDataVo);
        if (herbFormula == null) {
            log.error("未找到药材:"+realTimeDataVo.getIndex() +","+realTimeDataVo.getName() +",机台:" + realTimeDataVo.getMachineid());
            return null;
        }
        // 创建新工单
        orderVo = new DryOrderVo(realTimeDataVo);
        orderVo.setHerbId(herbFormula.getId());
        orderVo.setEquId(equ.getId());
        DryOrder dryOrder = BeanUtil.toBean(orderVo, DryOrder.class);
@@ -268,8 +287,24 @@
        queryWrapper.eq(DryEquipment::getCode, realTimeDataVo.getMachineid());
        DryEquipment one = equipmentService.getOne(queryWrapper);
        if (one == null) {
            one = new DryEquipment(realTimeDataVo);
            equipmentService.save(one);
            log.error(role+"保存实时数据,未找到设备:"+realTimeDataVo.getMachineid());
//            one = new DryEquipment(realTimeDataVo);
//            equipmentService.save(one);
            if (MqttConstant.ROLE_ADMIN.equals(role)) {
                MqttMessage mqttMessage = new MqttMessage();
                mqttMessage.setQos(0);
                JSONObject object = new JSONObject();
                object.put("code", realTimeDataVo.getMachineid());
                object.put("tenantId", realTimeDataVo.getTenantid());
                mqttMessage.setPayload(object.toJSONString().getBytes());
                try {
                    mqttUtil.getMqttClient().publish(MqttConstant.SERVICE_REQ_PREFIX ,mqttMessage);
                }catch (MqttException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
        return one;
    }