TuyaComsumer.java
2.73 KB
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
package cn.com.glsx.consumer.modules.consumers;
import cn.com.glsx.consumer.modules.consumers.pulsar.MqConsumer;
import cn.com.glsx.consumer.modules.utils.AESBase64Utils;
import cn.com.glsx.consumer.common.constant.Contants;
import cn.com.glsx.consumer.modules.model.response.device.MessageRep;
import cn.com.glsx.consumer.modules.service.InfoService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
@Slf4j
public class TuyaComsumer implements CommandLineRunner {
@Resource
private InfoService infoService;
@Value("${pulsar.serviceUrl}")
private String serviceUrl;
@Value("${pulsar.accessId}")
private String accessId;
@Value("${pulsar.accessKey}")
private String accessKey;
@Value("${pulsar.maxRedeliverCount}")
private int maxRedeliverCount;
@Value("${pulsar.topic}")
private String topic;
@Value("${pulsar.subscriptionName}")
private String subscriptionName;
@Override
public void run(String... args) throws Exception {
MqConsumer mqConsumer = MqConsumer.build().serviceUrl(serviceUrl).accessId(accessId).accessKey(accessKey).topic(topic).subscriptionName(subscriptionName)
.maxRedeliverCount(maxRedeliverCount).messageListener(
message -> {
log.info("Message received:" + new String(message.getData()) + ",seq="
+ message.getSequenceId() + ",time=" + message.getPublishTime() + ",consumed time="
+ System.currentTimeMillis());
String jsonMessage = new String(message.getData());
MessageRep vo = JSON.parseObject(jsonMessage, MessageRep.class);
Integer protocol = vo.getProtocol();
if(protocol.equals(Contants.STATUS_APPEAR)){
String data = AESBase64Utils.decrypt(vo.getData(), accessKey.substring(8, 24));
log.info("the real message data:" + data);
infoService.processMessage(data);
}else if(protocol.equals(Contants.event_APPEAR)){
String data = AESBase64Utils.decrypt(vo.getData(), accessKey.substring(8, 24));
log.info("the real message data:" + data);
infoService.processEeventMessage(data);
}
}
);
mqConsumer.start();
}
}