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("+", locks.getId());
|
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("+", locks.getId());
|
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);
|
mqttLogMapper.insert(log);
|
return log;
|
}
|
}
|