jiaosong
2023-11-08 a0684138890219d59d8f394fd4808b321fae6f4b
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
package com.doumee.service.business.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.doumee.core.constants.Constants;
import com.doumee.core.mqtt.config.MqttConfig;
import com.doumee.core.mqtt.service.MqttToolService;
import com.doumee.dao.business.MqttLogMapper;
import com.doumee.dao.business.model.Locks;
import com.doumee.dao.business.model.MqttLog;
import com.doumee.service.business.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.Date;
 
/**
 *  与硬件对接服务
 * @author 江蹄蹄
 * @date 2023/10/09 18:06
 */
@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private MqttToolService mqttToolService;
    @Autowired
    private MqttLogMapper mqttLogMapper;
    @Autowired
    private MqttConfig mqttConfig;
    /**
     * 发起开锁指令
     * @param locks
     * @return
     */
    @Override
    public MqttLog openLock(Locks locks) {
        String topic =  Constants.MqttTopic.pub_openLock.replace("{siteId}", locks.getSiteId()).replace("{lockId}", locks.getCode()+"");
        int result = mqttToolService.pubMessage("{}",topic);
        MqttLog mqttLog = createPushLog(topic,result,"请求开锁_"+locks.getId());
        return mqttLog;
    }
    /**
     * 实时查询锁信息
     * @param locks
     * @return
     */
    @Override
    public MqttLog getLockInfo(Locks locks) {
        String topic =  Constants.MqttTopic.pub_getLockInfo.replace("{siteId}", locks.getSiteId()).replace("{lockId}", locks.getCode()+"");
        int result = mqttToolService.pubMessage("{}",topic);
        MqttLog mqttLog = createPushLog(topic,result,"实时查询锁信息_"+locks.getId());
        return mqttLog;
    }
    @Override
    @Async
    public void testPush(String topic, String json){
        int result = mqttToolService.pubMessage(json,topic);
    }
 
    private MqttLog createPushLog(String topic, int result,String info) {
        MqttLog log = new MqttLog();
        log.setId(Constants.getUUID());
        log.setCreateDate(new Date());
        log.setResult(result);
        log.setTopic(topic);
        log.setClientid(mqttConfig.getClientid());
        log.setHostInfo(mqttConfig.getHost());
        log.setInfo(JSONObject.toJSONString(mqttConfig));
        log.setType(Constants.ONE);
        log.setMsg("");
        log.setInfo(info);
        log.setIsdeleted(Constants.ZERO);
        mqttLogMapper.insert(log);
        return  log;
    }
}