张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//package com.thhy.engineering.modules.biz.config;
//
//import com.alibaba.fastjson.JSONObject;
//import com.thhy.engineering.modules.biz.device.mapper.SysDeviceMapper;
//import lombok.SneakyThrows;
//import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
//import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.integration.annotation.IntegrationComponentScan;
//import org.springframework.integration.annotation.ServiceActivator;
//import org.springframework.integration.channel.DirectChannel;
//import org.springframework.integration.core.MessageProducer;
//import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
//import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
//import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
//import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
//import org.springframework.integration.mqtt.support.MqttHeaders;
//import org.springframework.messaging.Message;
//import org.springframework.messaging.MessageChannel;
//import org.springframework.messaging.MessageHandler;
//import org.springframework.messaging.MessagingException;
//
//import javax.annotation.Resource;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.HashMap;
//
//
//@Configuration
//@IntegrationComponentScan
//public class MqttInboundConfiguration {
//
//    @Resource
//    private MqttConfiguration mqttProperties;
//    @Resource
//    private SysDeviceMapper sysDeviceMapper;
//
//    @Bean
//    public MessageChannel mqttInputChannel() {
//        return new DirectChannel();
//    }
//
//    @Bean
//    public MqttPahoClientFactory mqttClientFactory() {
//        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
//        String[] array = mqttProperties.getUrl().split(",");
//        MqttConnectOptions options = new MqttConnectOptions();
//        options.setServerURIs(array);
//        options.setUserName(mqttProperties.getUsername());
//        options.setPassword(mqttProperties.getPassword().toCharArray());
//        options.setKeepAliveInterval(2);
//
//        //接受离线消息
//        options.setCleanSession(false);
//        factory.setConnectionOptions(options);
//        return factory;
//    }
//
//    //配置client,监听的topic
//    @Bean
//    public MessageProducer inbound() {
//        String[] inboundTopics = mqttProperties.getTopic().split(",");
//        MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter(
//                mqttProperties.getClientId()+"_inbound",mqttClientFactory(), inboundTopics);  //对inboundTopics主题进行监听
//        adapter.setCompletionTimeout(5000);
//        adapter.setQos(1);
//        adapter.setConverter(new DefaultPahoMessageConverter());
//        adapter.setOutputChannel(mqttInputChannel());
//        return adapter;
//    }
//
//
//    //通过通道获取数据
//    @Bean
//    @ServiceActivator(inputChannel = "mqttInputChannel")  //异步处理
//    public MessageHandler handler() {
//        return new MessageHandler() {
//            @SneakyThrows
//            @Override
//            public void handleMessage(Message<?> message) throws MessagingException {
////                System.out.println("message:"+message);
//                System.out.println("----------------------");
//                System.out.println("message:"+message.getPayload());
//                System.out.println("PacketId:"+message.getHeaders().getId());
//                System.out.println("Qos:"+message.getHeaders().get(MqttHeaders.QOS));
//                String topic = (String) message.getHeaders().get(MqttHeaders.RECEIVED_TOPIC);
//                System.out.println("topic:"+topic);
//                System.out.println(message.getPayload().toString());
//                JSONObject json = JSONObject.parseObject(message.getPayload().toString());
//                System.out.println("json---"+json);
//                String type = json.get("type").toString();
//                String timestamp = json.get("timestamp").toString();
//                String worksId = json.get("worksId").toString();
//                String rfid = json.get("rfid").toString();
//                // !!! 原生数据录入 数据表中
//                HashMap<String, Object> hashMap = new HashMap<>();
//                hashMap.put("dataId","");
//                hashMap.put("dataTime",timestamp);
//                hashMap.put("worksId",worksId);
//                hashMap.put("types",type);
//                hashMap.put("moldId",rfid);
//                sysDeviceMapper.moldDataInsert(hashMap);
//                // !!! 根据 模具mn查询最新一条管片
//                String pipeId= sysDeviceMapper.moldIdSelectPipeId(rfid);
//                // !!! 判断  入窑还是出窑
//                if(type.equals("upper")){// 入
//                    Date date = new Date();
//                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    String format1 = format.format(date);
////                    sysDeviceMapper.pipeInfoUpdate(pipeId,2,format1);
//                    //1.根据读卡模具数据与管片表进行匹配更新管片表中的状态,添加入窑时间
//                    sysDeviceMapper.pipeIdUpdate(pipeId,1,format1);//入窑修改管片状态
//                    //2.记录读卡数据表 根据 SIM 号到模块查询,新增一条读卡数据: 上次读卡次数 +1为本次读卡次数
//                    Integer current_cycle_time = sysDeviceMapper.mouldMouldMn(rfid);
//                    // 模具次数 +1
//                    int i = (current_cycle_time + 1);
//                    sysDeviceMapper.mouldUpdate(rfid,i);
//                }else if(type.equals("offline")){//出
//                    Date date = new Date();
//                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    String format1 = format.format(date);
//                    //1 根据读卡模具数据与管片表进行匹配 更新管片表中的状态,添加出窑时间
//                    sysDeviceMapper.pipeInfoUpdate(pipeId,2,format1); //管片出模状态 PDA  (出模)
//                    sysDeviceMapper.pipeIdOutUpdate(pipeId,2,format1);//出窑--修改管片状态 (出窑)
//                    //2.记录读卡数据表 修改 模具时长=本次读卡时间 - 上一个模具读卡时间接受硬件读卡数据
//                    String dataTime = sysDeviceMapper.dataInTime(rfid);//该模具最新一条入窑的时间
//                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//                    long eTime = df.parse(timestamp).getTime();//出窑的时间
//                    long sTime = df.parse(dataTime).getTime();//入窑的时间
//                    //入->出   相差的秒数
//                    long diff = (eTime - sTime) / 1000;
//                    HashMap<String, Object> hashMap1 = new HashMap<>();
//                    hashMap1.put("moldId",rfid);
//                    hashMap1.put("useId","");
//                    hashMap1.put("seconds",diff);
//                    sysDeviceMapper.moldUseInsert(hashMap1);
//                    //模具状态 改为闲置
//                    sysDeviceMapper.mouldFreeUpdate(rfid);//修改 模具状态为 闲置
//                }
//            }
//            public void deliveryComplete(IMqttDeliveryToken token) {
//                System.out.println("deliveryComplete---------" + token.isComplete());
//            }
//
//        };
//    }
//
//
//}