|  |  |  | 
|---|
|  |  |  | package com.doumee.service.business.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.doumee.core.constants.Constants; | 
|---|
|  |  |  | import com.doumee.core.mqtt.service.MqttPushCallback; | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | *  与硬件对接服务 | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class DeviceServiceImpl implements DeviceService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MqttToolService mqttToolService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MqttLogMapper mqttLogMapper; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private MqttConfig mqttConfig; | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 发起开锁指令 | 
|---|
|  |  |  | * @param locks | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @PostConstruct | 
|---|
|  |  |  | public void startSubcribe() { | 
|---|
|  |  |  | mqttToolService.subscribe(new String[]{ Constants.MqttTopic.openLock, Constants.MqttTopic.closeLock}); | 
|---|
|  |  |  | 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 | 
|---|
|  |  |  | public boolean openLock(Locks locks) { | 
|---|
|  |  |  | mqttToolService.pubMessage(locks.getName(), Constants.MqttTopic.openLock); | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | @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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|