Commit 8166b0b3 by YG8999

设备网关代码修改

parent e7d48a52
......@@ -16,10 +16,16 @@ import share.common.annotation.Log;
import share.common.core.controller.BaseController;
import share.common.core.domain.AjaxResult;
import share.common.enums.BusinessType;
import share.common.utils.SecurityUtils;
import share.framework.mqtt.MqttGatewayComponent;
import share.system.domain.Device;
import share.system.domain.vo.DeviceParamVo;
import share.system.domain.vo.MqttxVo;
import share.system.service.DeviceLogService;
import share.system.service.DeviceService;
import share.common.utils.poi.ExcelUtil;
import share.common.core.page.TableDataInfo;
import share.system.service.MqttxService;
/**
* 设备信息Controller
......@@ -33,6 +39,12 @@ public class DeviceController extends BaseController
{
@Autowired
private DeviceService deviceService;
@Autowired
private MqttxService mqttxService;
@Autowired
private DeviceLogService deviceLogService;
@Autowired
private MqttGatewayComponent mqttGatewayComponent;
/**
* 查询设备信息列表
......@@ -101,4 +113,23 @@ public class DeviceController extends BaseController
{
return toAjax(deviceService.deleteDeviceByIds(ids));
}
/**
* 开门、关门、取电、断电
*/
@PreAuthorize("@ss.hasPermi('system:device:edit')")
@Log(title = "设备信息", businessType = BusinessType.UPDATE)
@PostMapping(value = "/openOrClose")
public AjaxResult openOrClose(@RequestBody DeviceParamVo deviceParam)
{
// 获取mqtt的topic、payload
MqttxVo mqttxVo = mqttxService.openOrCloseDevice(deviceParam.getDevId(),
SecurityUtils.getUsername(), deviceParam.getOpType());
// 发送mqtt消息
mqttGatewayComponent.sendToMqtt(mqttxVo.getTopic(), 0, mqttxVo.getPayload());
// 写日志记录
int result = deviceLogService.addDeviceLog(mqttxVo);
return toAjax(result);
}
}
......@@ -101,4 +101,6 @@ public class DeviceGatewayController extends BaseController
{
return toAjax(deviceGatewayService.deleteDeviceGatewayByIds(ids));
}
}
......@@ -58,6 +58,16 @@ public class MqttConstants {
public static final String MQTT_DESCRIBE_DEVICE_ELECTRICITY_INTAKE = "取电";
/**
* mqtt消息描述: 清除网关锁关门
*/
public static final String MQTT_DESCRIBE_DEVICE_CLOSE = "关门";
/**
* mqtt消息描述: 清除网关锁断电
*/
public static final String MQTT_DESCRIBE_DEVICE_ELECTRICITY_CLOSE = "断电";
/**
* mqtt消息描述: 远程下发密码
*/
public static final String MQTT_DESCRIBE_DEVICE_PASSWORD = "远程下发密码";
......
......@@ -43,7 +43,7 @@ public enum MqttReportType {
if (StrUtil.isNotBlank(code)) {
for (MqttReportType type : MqttReportType.values()) {
if (type.code.compareTo(code)==0) {
return type.name;
return type.topic;
}
}
}
......
......@@ -23,7 +23,6 @@ public interface MqttGatewayComponent {
* @param topic topic
* @see
*/
@Deprecated
void sendToMqtt(String payload, @Header(MqttHeaders.TOPIC) String topic);
/**
......
......@@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;
import share.system.service.MqttxService;
/**
* 消息处理器
......@@ -18,6 +19,8 @@ import org.springframework.stereotype.Service;
@Service
public class MqttTopicHandler {
@Autowired
private MqttxService mqttxService;
/**
* 这一步主要是对消息的有效荷载进行非空校验以及日志打印
......@@ -37,8 +40,8 @@ public class MqttTopicHandler {
private void dispose(String payload, String topic) {
if (!ObjectUtil.isNull(payload)){
try {
mqttxService.mqttReport(topic, payload);
log.info("来自topic:{}的消息处理完毕!", topic);
log.info("消息内容:{}", payload);
} catch (Exception e) {
log.error("来自topic:{}的消息处理异常!原因:{}", topic, e);
}
......
......@@ -68,12 +68,6 @@ public class SnowFlakeUtil {
return Singleton.get(SnowFlakeUtil.class, new Object[]{1L, 1L}).nextId();
}
public static void main(String[] args) {
for(int i = 0; i < 10; ++i) {
System.out.println(getDefaultSnowFlakeId());
System.out.println(getDefaultSnowFlakeId().toString().length());
}
}
}
package share.system.domain.vo;
import lombok.Data;
import share.common.core.domain.BaseEntity;
/**
* @className: share.system.domain.vo.DeviceParamVo
* @description: 设备参数
* @author: lwj
* @create: 2023-11-07 16:10
*/
@Data
public class DeviceParamVo extends BaseEntity {
/** 设备mac */
private String devMac;
/** 设备id */
private String devId;
/** 操作类型:10:开门,20:取电,30:锁门,40:断电 */
private String opType;
/** 操作用户 */
private String phone;
}
......@@ -3,6 +3,7 @@ package share.system.service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService;
import share.system.domain.DeviceLog;
import share.system.domain.vo.MqttxVo;
/**
* 设备操作日志Service接口
......@@ -59,4 +60,13 @@ public interface DeviceLogService extends IService<DeviceLog>
* @return 结果
*/
public int deleteDeviceLogById(Long id);
/**
* 新增设备操作日志
*
* @param mqttxVo 设备操作信息
* @return 结果
*/
int addDeviceLog(MqttxVo mqttxVo);
}
......@@ -28,10 +28,11 @@ public interface MqttxService {
/**
* mqtt 上报接口
* @param mqttx 上报数据对象
* @param topic 上报数据主题
* @param payload 上报数据参数
* @return
*/
boolean mqttReport(MqttxVo mqttx);
boolean mqttReport(String topic, String payload);
/**
* 开锁/关锁、取电/断电
......
......@@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import share.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import share.common.utils.SecurityUtils;
import share.system.domain.vo.MqttxVo;
import share.system.mapper.DeviceLogMapper;
import share.system.domain.DeviceLog;
import share.system.service.DeviceLogService;
......@@ -94,4 +96,19 @@ public class DeviceLogServiceImpl extends ServiceImpl<DeviceLogMapper, DeviceLog
{
return deviceLogMapper.deleteDeviceLogById(id);
}
@Override
public int addDeviceLog(MqttxVo mqttxVo) {
DeviceLog deviceLog = new DeviceLog();
deviceLog.setDevId(mqttxVo.getDevId());
deviceLog.setDevMac(mqttxVo.getDevMac());
deviceLog.setTopic(mqttxVo.getTopic());
deviceLog.setPayload(mqttxVo.getPayload());
deviceLog.setSeq(mqttxVo.getSeq());
deviceLog.setMqttType(mqttxVo.getMqttType());
deviceLog.setMqttDescribe(mqttxVo.getMqttDescribe());
deviceLog.setCreateTime(DateUtils.getNowDate());
deviceLog.setCreateBy(SecurityUtils.getUsername());
return deviceLogMapper.insertDeviceLog(deviceLog);
}
}
......@@ -122,16 +122,16 @@ public class MqttxServiceImpl implements MqttxService {
/**
* mqtt 上报接口
* @param mqttx 上报数据对象
* @param topic 上报数据主题
* @param payload 上报数据参数
* @return
*/
@Override
public boolean mqttReport(MqttxVo mqttx) {
public boolean mqttReport(String topic, String payload) {
boolean isSuccess = false;
String topic = mqttx.getTopic();
if (topic.endsWith(MqttReportType.getTopicStr("batch_report"))) {
// 修改设备状态、电量、信号值
isSuccess = this.updateDevice(mqttx);
isSuccess = this.updateDevice(topic, payload);
}
return isSuccess;
}
......@@ -149,11 +149,23 @@ public class MqttxServiceImpl implements MqttxService {
switch (opType) {
case "10":
// 开锁
mqttxVo = this.openOrCloseDeviceInit(devId,phone, MqttOpenType.OPEN.getCode(),MqttConstants.MQTT_DESCRIBE_DEVICE_OPEN);
mqttxVo = this.openOrCloseDeviceInit(devId,phone, MqttOpenType.OPEN.getCode(),
MqttConstants.MQTT_DESCRIBE_DEVICE_OPEN);
break;
case "20":
// 取电
mqttxVo = this.openOrCloseDeviceInit(devId,phone, MqttOpenType.OPEN.getCode(),MqttConstants.MQTT_DESCRIBE_DEVICE_ELECTRICITY_INTAKE);
mqttxVo = this.openOrCloseDeviceInit(devId,phone, MqttOpenType.OPEN.getCode(),
MqttConstants.MQTT_DESCRIBE_DEVICE_ELECTRICITY_INTAKE);
break;
case "30":
// 取电
mqttxVo = this.openOrCloseDeviceInit(devId,phone, MqttOpenType.CLOSE.getCode(),
MqttConstants.MQTT_DESCRIBE_DEVICE_CLOSE);
break;
case "40":
// 取电
mqttxVo = this.openOrCloseDeviceInit(devId,phone, MqttOpenType.CLOSE.getCode(),
MqttConstants.MQTT_DESCRIBE_DEVICE_ELECTRICITY_CLOSE);
break;
default:
break;
......@@ -171,10 +183,11 @@ public class MqttxServiceImpl implements MqttxService {
private MqttxVo openOrCloseDeviceInit(String devId, String phone, String openType, String mqttDescribe) {
Device device = deviceMapper.selectDeviceByDevId(devId);
if (device != null) {
DeviceGateway deviceGateway = new DeviceGateway();
deviceGateway.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(deviceGateway);
DeviceGateway gatewayParam = new DeviceGateway();
gatewayParam.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(gatewayParam);
if (deviceGateways.size() > 0) {
DeviceGateway deviceGateway = deviceGateways.get(0);
String topic = StrUtil.format(MqttConstants.MQTT_TOPIC_ACTION_EXECUTE, deviceGateway.getGroup());
String seq = IdUtil.simpleUUID().toUpperCase();
JSONArray params = JSONUtil.createArray();
......@@ -189,7 +202,8 @@ public class MqttxServiceImpl implements MqttxService {
.put("devId", deviceGateway.getGroup())
.put("devPsw", deviceGateway.getDevPsw())
.put("devVer", deviceGateway.getDevVer())
.put("devType", deviceGateway.getDevType()).put("ac", MqttConstants.MQTT_AC_REQ)
.put("devType", deviceGateway.getDevType())
.put("ac", MqttConstants.MQTT_AC_REQ)
.put("at", DateUtil.date().getTime())
.put("seq", seq).put("op", MqttOpType.MQTT_OP_OPENCLOSELOCK.getCode())
.put("mode", MqttConstants.MQTT_MODE_S2D)
......@@ -198,8 +212,8 @@ public class MqttxServiceImpl implements MqttxService {
MqttxVo mqttxVo = new MqttxVo();
mqttxVo.setTopic(topic);
mqttxVo.setPayload(JSONUtil.toJsonStr(payload));
mqttxVo.setDevMac(deviceGateway.getDevMac());
mqttxVo.setDevId(deviceGateway.getDevId());
mqttxVo.setDevMac(device.getDevMac());
mqttxVo.setDevId(device.getDevId());
mqttxVo.setSeq(seq);
mqttxVo.setMqttType(MqttConstants.MQTT_TYPE_1);
mqttxVo.setMqttDescribe(mqttDescribe);
......@@ -254,10 +268,11 @@ public class MqttxServiceImpl implements MqttxService {
String phone, String opType, String mqttDescribe) {
Device device = deviceMapper.selectDeviceByDevId(devId);
if (device != null) {
DeviceGateway deviceGateway = new DeviceGateway();
deviceGateway.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(deviceGateway);
DeviceGateway gatewayParam = new DeviceGateway();
gatewayParam.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(gatewayParam);
if (deviceGateways.size() > 0) {
DeviceGateway deviceGateway = deviceGateways.get(0);
String topic = StrUtil.format(MqttConstants.MQTT_DEVICE_SET_PASSWORD, deviceGateway.getGroup());
String seq = IdUtil.simpleUUID().toUpperCase();
JSONArray params = JSONUtil.createArray();
......@@ -282,8 +297,8 @@ public class MqttxServiceImpl implements MqttxService {
MqttxVo mqttxVo = new MqttxVo();
mqttxVo.setTopic(topic);
mqttxVo.setPayload(JSONUtil.toJsonStr(payload));
mqttxVo.setDevMac(deviceGateway.getDevMac());
mqttxVo.setDevId(deviceGateway.getDevId());
mqttxVo.setDevMac(device.getDevMac());
mqttxVo.setDevId(device.getDevId());
mqttxVo.setSeq(seq);
mqttxVo.setMqttType(MqttConstants.MQTT_TYPE_1);
mqttxVo.setMqttDescribe(mqttDescribe);
......@@ -338,10 +353,11 @@ public class MqttxServiceImpl implements MqttxService {
String phone, String opType, String mqttDescribe) {
Device device = deviceMapper.selectDeviceByDevId(devId);
if (device != null) {
DeviceGateway deviceGateway = new DeviceGateway();
deviceGateway.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(deviceGateway);
DeviceGateway gatewayParam = new DeviceGateway();
gatewayParam.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(gatewayParam);
if (deviceGateways.size() > 0) {
DeviceGateway deviceGateway = deviceGateways.get(0);
String topic = StrUtil.format(MqttConstants.MQTT_DEVICE_SET_PASSWORD, deviceGateway.getGroup());
String seq = IdUtil.simpleUUID().toUpperCase();
JSONArray params = JSONUtil.createArray();
......@@ -366,8 +382,8 @@ public class MqttxServiceImpl implements MqttxService {
MqttxVo mqttxVo = new MqttxVo();
mqttxVo.setTopic(topic);
mqttxVo.setPayload(JSONUtil.toJsonStr(payload));
mqttxVo.setDevMac(deviceGateway.getDevMac());
mqttxVo.setDevId(deviceGateway.getDevId());
mqttxVo.setDevMac(device.getDevMac());
mqttxVo.setDevId(device.getDevId());
mqttxVo.setSeq(seq);
mqttxVo.setMqttType(MqttConstants.MQTT_TYPE_1);
mqttxVo.setMqttDescribe(mqttDescribe);
......@@ -411,10 +427,11 @@ public class MqttxServiceImpl implements MqttxService {
String mqttDescribe) {
Device device = deviceMapper.selectDeviceByDevId(devId);
if (device != null) {
DeviceGateway deviceGateway = new DeviceGateway();
deviceGateway.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(deviceGateway);
DeviceGateway gatewayParam = new DeviceGateway();
gatewayParam.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(gatewayParam);
if (deviceGateways.size() > 0) {
DeviceGateway deviceGateway = deviceGateways.get(0);
String topic = StrUtil.format(MqttConstants.MQTT_TOPIC_PROPERTY_SET, deviceGateway.getGroup());
String seq = IdUtil.simpleUUID().toUpperCase();
JSONArray params = JSONUtil.createArray();
......@@ -437,8 +454,8 @@ public class MqttxServiceImpl implements MqttxService {
MqttxVo mqttxVo = new MqttxVo();
mqttxVo.setTopic(topic);
mqttxVo.setPayload(JSONUtil.toJsonStr(payload));
mqttxVo.setDevMac(deviceGateway.getDevMac());
mqttxVo.setDevId(deviceGateway.getDevId());
mqttxVo.setDevMac(device.getDevMac());
mqttxVo.setDevId(device.getDevId());
mqttxVo.setSeq(seq);
mqttxVo.setMqttType(MqttConstants.MQTT_TYPE_1);
mqttxVo.setMqttDescribe(mqttDescribe);
......@@ -463,10 +480,11 @@ public class MqttxServiceImpl implements MqttxService {
String startTime, String endTime, String number) {
Device device = deviceMapper.selectDeviceByDevId(devId);
if (device != null) {
DeviceGateway deviceGateway = new DeviceGateway();
deviceGateway.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(deviceGateway);
DeviceGateway gatewayParam = new DeviceGateway();
gatewayParam.setGroup(device.getGroup());
List<DeviceGateway> deviceGateways = deviceGatewayMapper.selectDeviceGatewayList(gatewayParam);
if (deviceGateways.size() > 0) {
DeviceGateway deviceGateway = deviceGateways.get(0);
String topic = StrUtil.format(MqttConstants.MQTT_TOPIC_ACTION_EXECUTE, deviceGateway.getGroup());
String seq = IdUtil.simpleUUID().toUpperCase();
JSONArray params = JSONUtil.createArray();
......@@ -492,8 +510,8 @@ public class MqttxServiceImpl implements MqttxService {
MqttxVo mqttxVo = new MqttxVo();
mqttxVo.setTopic(topic);
mqttxVo.setPayload(JSONUtil.toJsonStr(payload));
mqttxVo.setDevMac(deviceGateway.getDevMac());
mqttxVo.setDevId(deviceGateway.getDevId());
mqttxVo.setDevMac(device.getDevMac());
mqttxVo.setDevId(device.getDevId());
mqttxVo.setSeq(seq);
mqttxVo.setMqttType(MqttConstants.MQTT_TYPE_1);
mqttxVo.setMqttDescribe(MqttConstants.MQTT_DESCRIBE_ACTION_EXECUTE);
......@@ -506,11 +524,11 @@ public class MqttxServiceImpl implements MqttxService {
/**
* 修改设备状态、电量
* @param mqttx
* @param topic
* @param payload
* @return
*/
private boolean updateDevice(MqttxVo mqttx) {
String payload = mqttx.getPayload();
private boolean updateDevice(String topic, String payload) {
JSONObject json = JSONUtil.parseObj(payload);
if (json.size() > 0) {
JSONArray array = json.getJSONArray("params");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment