package net.maku.iot.service.impl;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.maku.framework.common.exception.ServerException;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.iot.convert.IotDeviceConvert;
import net.maku.iot.dao.IotDeviceDao;
import net.maku.iot.entity.IotDeviceEntity;
import net.maku.iot.enums.*;
import net.maku.iot.communication.dto.DeviceCommandResponseDTO;
import net.maku.iot.communication.dto.DevicePropertyDTO;
import net.maku.iot.communication.mqtt.handler.DeviceCommandResponseHandler;
import net.maku.iot.communication.mqtt.handler.DevicePropertyChangeHandler;
import net.maku.iot.query.IotDeviceQuery;
import net.maku.iot.communication.service.BaseCommunication;
import net.maku.iot.communication.service.CommunicationServiceFactory;
import net.maku.iot.service.IotDeviceEventLogService;
import net.maku.iot.service.IotDeviceService;
import net.maku.iot.vo.DeviceCommandResponseAttributeDataVO;
import net.maku.iot.vo.DeviceCommandVO;
import net.maku.iot.vo.DeviceReportAttributeDataVO;
import net.maku.iot.vo.IotDeviceVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
/**
* 设备服务类
*
* @author LSF [email protected]
*/
@Service
@Slf4j
@AllArgsConstructor
public class IotDeviceServiceImpl extends BaseServiceImpl<IotDeviceDao, IotDeviceEntity>
implements IotDeviceService, DevicePropertyChangeHandler, DeviceCommandResponseHandler {
private final CommunicationServiceFactory communicationService;
private final IotDeviceEventLogService deviceEventLogService;
@Override
public PageResult<IotDeviceVO> page(IotDeviceQuery query) {
IPage<IotDeviceEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
return new PageResult<>(IotDeviceConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
}
private LambdaQueryWrapper<IotDeviceEntity> getWrapper(IotDeviceQuery query) {
LambdaQueryWrapper<IotDeviceEntity> wrapper = Wrappers.lambdaQuery();
wrapper.eq(query.getStatus() != null, IotDeviceEntity::getStatus, query.getStatus());
wrapper.eq(query.getType() != null, IotDeviceEntity::getType, query.getType());
wrapper.eq(query.getRunningStatus() != null, IotDeviceEntity::getRunningStatus, query.getRunningStatus());
return wrapper;
}
@Override
public void save(IotDeviceVO vo) {
IotDeviceEntity entity = IotDeviceConvert.INSTANCE.convert(vo);
baseMapper.insert(entity);
}
@Override
public void update(IotDeviceVO vo) {
IotDeviceEntity entity = IotDeviceConvert.INSTANCE.convert(vo);
updateById(entity);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(List<Long> idList) {
removeByIds(idList);
}
@Override
public BaseCommunication getSendService(IotDeviceEntity device) {
if (device != null) {
return getSendService(device.getProtocolType());
}
return null;
}
@Override
public BaseCommunication getSendService(String protocolType) {
return communicationService.getProtocol(protocolType);
}
@Override
public BaseCommunication getSendService(Long deviceId) {
IotDeviceEntity device = getById(deviceId);
if (device != null) {
return getSendService(device.getProtocolType());
}
return null;
}
@Override
@Transactional(rollbackFor = Exception.class)
public DeviceCommandResponseDTO syncSendCommand(DeviceCommandVO vo) {
IotDeviceEntity device = getById(vo.getDeviceId());
Assert.notNull(device, "未注册的设备:{}", vo.getDeviceId());
DeviceCommandEnum commandEnum = DeviceCommandEnum.parse(vo.getCommand());
try {
return getSendService(device).syncSendCommand(getById(vo.getDeviceId()), commandEnum, vo.getPayload());
} catch (ServerException e) {
if (DeviceCommandEnum.parse(vo.getCommand()).getEventType() != null
&& StrUtil.contains(e.getMessage(), DeviceServiceEnum.COMMAND_ID.getValue())) {
//指令异常事件记录
String commandId = e.getMessage().substring(e.getMessage().indexOf("<") + 1, e.getMessage().indexOf(">"));
deviceEventLogService.createAndSaveDeviceEvent(
vo.getDeviceId(), device.getTenantId(),
commandEnum.getEventType(), commandId, vo.getPayload());
}
throw e;
}
}
@Override
public DeviceCommandResponseDTO syncSendCommandDebug(DeviceCommandVO vo) {
IotDeviceEntity device = getById(vo.getDeviceId());
DeviceCommandEnum commandEnum = DeviceCommandEnum.parse(vo.getCommand());
return getSendService(device).syncSendCommandDebug(device, commandEnum, vo.getPayload());
}
@Override
@Transactional(rollbackFor = Exception.class)
public void asyncSendCommand(DeviceCommandVO vo) {
IotDeviceEntity device = getById(vo.getDeviceId());
getSendService(device).asyncSendCommand(device, DeviceCommandEnum.parse(vo.getCommand()), vo.getPayload());
}
@Override
public void simulateDeviceReportAttributeData(DeviceReportAttributeDataVO vo) {
IotDeviceEntity device = getById(vo.getDeviceId());
vo.setDeviceId(vo.getDeviceId());
getSendService(device).simulateDeviceReportAttributeData(device, JSONUtil.toJsonStr(vo));
}
@Override
public void simulateDeviceCommandResponseAttributeData(DeviceCommandResponseAttributeDataVO vo) {
IotDeviceEntity device = getById(vo.getDeviceId());
vo.setDeviceId(vo.getDeviceId());
getSendService(device).simulateDeviceCommandResponseAttributeData(device, JSONUtil.toJsonStr(vo));
}
/**
* 设备状态上报处理
*
* @param topic
* @param deviceProperty
*/
@Override
public void handle(String topic, DevicePropertyDTO deviceProperty) {
DeviceTopicEnum.DeviceTopicContext topicContext = null;
try {
topicContext = DeviceTopicEnum.parseContext(topic);
} catch (Exception e) {
log.warn("无效设备主题:{},忽略设备状态上报消息:{}", topic, deviceProperty);
return;
}
if (log.isDebugEnabled()) {
log.warn("处理设备状态上报消息,Topic:{}, TopicContext:{}", topic, topicContext);
}
Long deviceId = topicContext.getClient().getDeviceId();
IotDeviceEntity device = super.getById(deviceId);
if (device == null) {
log.warn("无效设备id:{},忽略设备状态上报消息:{}", deviceId, deviceProperty);
return;
}
switch (deviceProperty.getPropertyType()) {
case RUNNING_STATUS:
handleRunningStatus(device, deviceProperty, topicContext);
break;
case APP_VERSION:
handleAppVersion(device, deviceProperty, topicContext);
break;
case BATTERY_PERCENT:
handleBatteryPercent(device, deviceProperty, topicContext);
break;
case TEMPERATURE:
handleTemperature(device, deviceProperty, topicContext);
break;
default:
log.warn("未知设备属性类型:{},忽略设备状态上报消息:{}", devic

Java程序员-张凯
- 粉丝: 1w+
最新资源
- (源码)基于Go语言框架的订单管理系统.zip
- 浙江省高校一级计算机等级考试理论部分参考题总汇.doc
- 人工智能时代下的计算机网络安全的风险控制策略研究.docx
- 算法分析与设计d讲.doc
- VB酒店服务管理系统.doc
- VB图书管理完整论文.doc
- 探析信息发展下的计算机网络与经济的关系.docx
- 单片机控制的花样彩灯设计.doc
- Linux攻略DNS服务器安装配置方法详细介绍.doc
- 氨合成催化剂类翻英技术文件翻译网站及中英对照.doc
- 【传统网络营销】网站推广现状分析及推广方法介绍.doc
- (源码)基于Arduino微控制器的VNT15发动机控制器项目.zip
- 论述5G无线通信场景需求与技术演进.docx
- 项目管理进度跟踪表(DOC格式).doc
- 基于大数据的高校教务管理平台设计.docx
- 室内高精度融合定位在工业物联网的应用.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


