|
@@ -1,13 +1,11 @@
|
|
|
package com.zy.omp.service;
|
|
|
|
|
|
import com.zy.omp.common.Constant;
|
|
|
+import com.zy.omp.model.DeviceSet;
|
|
|
import com.zy.omp.pojo.dto.MqttMsgDto;
|
|
|
import com.zy.omp.config.mqtt.MqttGateway;
|
|
|
import com.zy.omp.model.Device;
|
|
|
-import com.zy.omp.model.SetBase;
|
|
|
import com.zy.omp.model.News;
|
|
|
-import com.zy.omp.pojo.io.wechat.SetIO;
|
|
|
-import com.zy.omp.utils.BeanUtil;
|
|
|
import com.zy.omp.websocket.WebSocketServer;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
@@ -25,9 +23,9 @@ import java.util.Map;
|
|
|
public class SetService {
|
|
|
|
|
|
@Resource
|
|
|
- private SetBaseService setBaseService;
|
|
|
+ private DeviceSetService deviceSetService;
|
|
|
@Resource
|
|
|
- private NewsService setNewsService;
|
|
|
+ private NewsService newsService;
|
|
|
@Resource
|
|
|
private MqttGateway mqttGateway;
|
|
|
@Resource
|
|
@@ -36,78 +34,76 @@ public class SetService {
|
|
|
private WebSocketServer webSocketServer;
|
|
|
|
|
|
/**
|
|
|
- * 设置设备
|
|
|
- *
|
|
|
- * @param code 设置指令
|
|
|
- * @param io 设置信息
|
|
|
+ * 存储语音播报
|
|
|
*/
|
|
|
- public void set(String code, SetIO io) {
|
|
|
- MqttMsgDto msgDto = getMqttMsgDtoByCode(code, io);
|
|
|
+ public void saveNews(News news) {
|
|
|
+ MqttMsgDto msgDto = new MqttMsgDto(Constant.M_CODE_NEWS, news.getOpenNum());
|
|
|
+ msgDto.set("newstype", news.getNewsType()).set("newstime", news.getNewsTime()).set("news", news.getNews());
|
|
|
+ String topic = Constant.getTopic(news.getOpenNum());
|
|
|
// 1. 向设备发送设置指令
|
|
|
- mqttGateway.sendMsgToMqtt(msgDto.toJson(), io.getTopic());
|
|
|
+ mqttGateway.sendMsgToMqtt(msgDto.toJson(), topic);
|
|
|
// 2. 设置信息保存至数据库
|
|
|
- saveToDataBase(code, io);
|
|
|
+ newsService.save(news);
|
|
|
// 3. 存储消息日志
|
|
|
- mqttLogService.saveSendLog(io.getTopic(), msgDto.toJson());
|
|
|
+ mqttLogService.saveSendLog(topic, msgDto.toJson());
|
|
|
// 4. 消息转发前端webSocket
|
|
|
- String deviceId = io.getTopic().split("/")[1];
|
|
|
- webSocketServer.massMessage(deviceId, msgDto.toJson());
|
|
|
+ webSocketServer.massMessage(news.getOpenNum(), msgDto.toJson());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 存储至数据库
|
|
|
+ * 设置设备
|
|
|
+ *
|
|
|
+ * @param code 设置指令
|
|
|
+ * @param param 设置信息
|
|
|
*/
|
|
|
- private void saveToDataBase(String code, SetIO io) {
|
|
|
- //发送语音播报单独处理
|
|
|
- if (code.equals(Constant.M_CODE_NEWS)) {
|
|
|
- News entity = BeanUtil.cast(io, News.class);
|
|
|
- setNewsService.save(entity);
|
|
|
- } else {
|
|
|
- SetBase entity = BeanUtil.cast(io, SetBase.class);
|
|
|
- setBaseService.updateByDeviceId(entity);
|
|
|
- }
|
|
|
+ public void set(String code, DeviceSet param) {
|
|
|
+ MqttMsgDto msgDto = getMqttMsgDtoByCode(code, param);
|
|
|
+ String topic = Constant.getTopic(param.getOpenNum());
|
|
|
+ // 1. 向设备发送设置指令
|
|
|
+ mqttGateway.sendMsgToMqtt(msgDto.toJson(), topic);
|
|
|
+ // 2. 设置信息保存至数据库
|
|
|
+ deviceSetService.updateById(param);
|
|
|
+ // 3. 存储消息日志
|
|
|
+ mqttLogService.saveSendLog(topic, msgDto.toJson());
|
|
|
+ // 4. 消息转发前端webSocket
|
|
|
+// webSocketServer.massMessage(param.getOpenNum(), msgDto.toJson());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 组装需要发送的模板消息
|
|
|
*
|
|
|
- * @param code 协议中 M 值
|
|
|
- * @param io 设置信息
|
|
|
+ * @param code 协议中 M 值
|
|
|
+ * @param param 设置信息
|
|
|
* @return 模板消息对象
|
|
|
*/
|
|
|
- private MqttMsgDto getMqttMsgDtoByCode(String code, SetIO io) {
|
|
|
- MqttMsgDto result = new MqttMsgDto(code, io.getDeviceId());
|
|
|
+ private MqttMsgDto getMqttMsgDtoByCode(String code, DeviceSet param) {
|
|
|
+ MqttMsgDto result = new MqttMsgDto(code, param.getOpenNum());
|
|
|
switch (code) {
|
|
|
//自动接听
|
|
|
case Constant.M_CODE_AUTO_ANSWER: {
|
|
|
- result.set("autoanswer", io.getAutoAnswer());
|
|
|
+ result.set("autoanswer", param.getAutoAnswer());
|
|
|
break;
|
|
|
}
|
|
|
//音量
|
|
|
case Constant.M_CODE_VOLUME: {
|
|
|
- result.set("msgvol", io.getMsgVol()).set("phonevol", io.getPhoneVol()).set("msgtype", 1);
|
|
|
+ result.set("msgvol", param.getMsgVol()).set("phonevol", param.getPhoneVol()).set("msgtype", 1);
|
|
|
break;
|
|
|
}
|
|
|
//定位频率
|
|
|
case Constant.M_CODE_GPS_RATE: {
|
|
|
- result.set("GPS_rate", io.getGpsRate());
|
|
|
- break;
|
|
|
- }
|
|
|
- //语音播报
|
|
|
- case Constant.M_CODE_NEWS: {
|
|
|
- result.set("newstype", io.getNewsType()).set("newstime", io.getNewsTime()).set("news", io.getNews());
|
|
|
+ result.set("GPS_rate", param.getGpsRate());
|
|
|
break;
|
|
|
}
|
|
|
//SOS紧急呼叫
|
|
|
case Constant.M_CODE_SOS: {
|
|
|
- result.set("key0_name", io.getKey0Name()).set("key0_phone", io.getKey0Phone())
|
|
|
- .set("key1_name", io.getKey1Name()).set("key1_phone", io.getKey1Phone())
|
|
|
- .set("key2_name", io.getKey2Name()).set("key2_phone", io.getKey2Phone());
|
|
|
+ result.set("key0_name", param.getKey0Name()).set("key0_phone", param.getKey0Phone())
|
|
|
+ .set("key1_name", param.getKey1Name()).set("key1_phone", param.getKey1Phone())
|
|
|
+ .set("key2_name", param.getKey2Name()).set("key2_phone", param.getKey2Phone());
|
|
|
break;
|
|
|
}
|
|
|
//持续定位
|
|
|
case Constant.M_CODE_CONTINUE_GPS: {
|
|
|
- result.set("highfreq", io.getHighFreq());
|
|
|
+ result.set("highfreq", param.getHighFreq());
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -116,16 +112,15 @@ public class SetService {
|
|
|
|
|
|
/**
|
|
|
* 服务器返回授权码
|
|
|
- * 老人机
|
|
|
*/
|
|
|
- public void returnCode_OMP(Device device) {
|
|
|
+ public void returnCode(Device device) {
|
|
|
//发送模板消息给设备
|
|
|
MqttMsgDto msgDto = new MqttMsgDto();
|
|
|
msgDto.setM(Constant.M_CODE_RETURN_CODE);
|
|
|
msgDto.setR(Constant.TOPIC_REGISTER_CLIENT);
|
|
|
msgDto.setT(Constant.TOPIC_REGISTER_SERVER);
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
- data.put("regnum", device.getNum());
|
|
|
+ data.put("regnum", device.getOpenNum());
|
|
|
data.put("deviceid", device.getClientId());
|
|
|
data.put("password", device.getPassword());
|
|
|
msgDto.setData(data);
|