|
|
|
package com.zhonglai.luhui.http.service.controller;
|
|
|
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.dto.ServerDto;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.dto.business.BusinessDto;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.dto.business.BusinessDtoClassNew;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.factory.BusinessAgreement;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.factory.BusinessAgreementFactory;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.service.CacheService;
|
|
|
|
import com.zhonglai.luhui.device.analysis.comm.service.DataPersistenceService;
|
|
|
|
import com.zhonglai.luhui.device.domain.IotDevice;
|
|
|
|
import com.zhonglai.luhui.device.service.IIotDeviceService;
|
|
|
|
import com.zhonglai.luhui.http.service.util.HttpServletRequestUtil;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.web.bind.annotation.ModelAttribute;
|
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
@Api(tags = "设备操作")
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/device")
|
|
|
|
public class DeviceServiceController {
|
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(DeviceServiceController.class);
|
|
|
|
|
|
|
|
private static String authKey = "key";
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private IIotDeviceService deviceService ;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private BusinessAgreementFactory businessAgreementFactory;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CacheService cacheService; //数据缓存
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private DataPersistenceService dataPersistenceService; //数据持久化
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加校验
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
@ModelAttribute
|
|
|
|
public void preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
|
|
|
{
|
|
|
|
String key = request.getParameter(authKey);
|
|
|
|
if(StringUtils.isNoneEmpty(key))
|
|
|
|
{
|
|
|
|
response.setStatus(403);
|
|
|
|
throw new ServiceException("验证失败");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("更新指定设备的全部数据")
|
|
|
|
@RequestMapping(value = "putAllData/{deviceid}/{messageid}")
|
|
|
|
public AjaxResult putDeviceAllData(@PathVariable String deviceid,@PathVariable String messageid,HttpServletRequest request) throws Exception {
|
|
|
|
String str = HttpServletRequestUtil.getAllParametersAsJSON(request);
|
|
|
|
|
|
|
|
if(StringUtils.isEmpty(str))
|
|
|
|
{
|
|
|
|
return AjaxResult.error("数据为空");
|
|
|
|
}
|
|
|
|
|
|
|
|
String imei = deviceid.split("_")[0];
|
|
|
|
IotDevice iotDevice = deviceService.selectIotDeviceByClient_id(imei);
|
|
|
|
Topic topic = new Topic();
|
|
|
|
topic.setRoleid(iotDevice.getProduct_id()+"");
|
|
|
|
topic.setUsername(iotDevice.getMqtt_username());
|
|
|
|
topic.setClientid(iotDevice.getClient_id());
|
|
|
|
topic.setTopicType("PUT");
|
|
|
|
topic.setMessageid(messageid);
|
|
|
|
topic.setPayloadtype("json");
|
|
|
|
|
|
|
|
//转化为协议对象
|
|
|
|
BusinessDto businessDto = BusinessDtoClassNew.newBean(topic.getPayloadtype(),str.getBytes()).analyticalModel(iotDevice.getThings_model_value());
|
|
|
|
|
|
|
|
BusinessAgreement businessAgreement = businessAgreementFactory.createBusinessAgreement(topic);
|
|
|
|
//解析为业务对象
|
|
|
|
ServerDto dto = businessAgreement.analysis(topic,businessAgreement.toData(businessDto));
|
|
|
|
if(null == dto)
|
|
|
|
{
|
|
|
|
return AjaxResult.error("没有业务解析方法");
|
|
|
|
}
|
|
|
|
log.info("{} 解析到的dto【{}】",dto);
|
|
|
|
|
|
|
|
//缓存数据
|
|
|
|
cacheService.updateCache(topic,dto);
|
|
|
|
|
|
|
|
//数据持久化
|
|
|
|
dataPersistenceService.persistence(topic,dto);
|
|
|
|
return AjaxResult.success();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("更新指定设备的部分数据")
|
|
|
|
@RequestMapping(value = "putPartialData/{deviceid}")
|
|
|
|
public AjaxResult putDevicePartialData(@PathVariable String deviceid)
|
|
|
|
{
|
|
|
|
return AjaxResult.success();
|
|
|
|
}
|
|
|
|
} |
|
|
|
//package com.zhonglai.luhui.http.service.controller;
|
|
|
|
//
|
|
|
|
//import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
//import com.ruoyi.common.exception.ServiceException;
|
|
|
|
//import com.ruoyi.common.utils.StringUtils;
|
|
|
|
//import com.zhonglai.luhui.device.analysis.comm.dto.ServerDto;
|
|
|
|
//import com.zhonglai.luhui.device.analysis.comm.dto.business.BusinessDto;
|
|
|
|
//import com.zhonglai.luhui.device.analysis.comm.dto.business.BusinessDtoClassNew;
|
|
|
|
//import com.zhonglai.luhui.device.analysis.comm.factory.BusinessAgreement;
|
|
|
|
//import com.zhonglai.luhui.device.analysis.comm.factory.BusinessAgreementFactory;
|
|
|
|
//import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
|
|
|
|
//import com.zhonglai.luhui.device.analysis.comm.service.CacheService;
|
|
|
|
//import com.zhonglai.luhui.device.analysis.comm.service.DataPersistenceService;
|
|
|
|
//import com.zhonglai.luhui.device.domain.IotDevice;
|
|
|
|
//import com.zhonglai.luhui.device.service.IIotDeviceService;
|
|
|
|
//import com.zhonglai.luhui.http.service.util.HttpServletRequestUtil;
|
|
|
|
//import io.swagger.annotations.Api;
|
|
|
|
//import io.swagger.annotations.ApiOperation;
|
|
|
|
//import org.slf4j.Logger;
|
|
|
|
//import org.slf4j.LoggerFactory;
|
|
|
|
//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
//import org.springframework.web.bind.annotation.ModelAttribute;
|
|
|
|
//import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
//import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
//import org.springframework.web.bind.annotation.RestController;
|
|
|
|
//
|
|
|
|
//import javax.servlet.http.HttpServletRequest;
|
|
|
|
//import javax.servlet.http.HttpServletResponse;
|
|
|
|
//
|
|
|
|
//@Api(tags = "设备操作")
|
|
|
|
//@RestController
|
|
|
|
//@RequestMapping("/device")
|
|
|
|
//public class DeviceServiceController {
|
|
|
|
//
|
|
|
|
// private static final Logger log = LoggerFactory.getLogger(DeviceServiceController.class);
|
|
|
|
//
|
|
|
|
// private static String authKey = "key";
|
|
|
|
//
|
|
|
|
// @Autowired
|
|
|
|
// private IIotDeviceService deviceService ;
|
|
|
|
//
|
|
|
|
// @Autowired
|
|
|
|
// private BusinessAgreementFactory businessAgreementFactory;
|
|
|
|
//
|
|
|
|
// @Autowired
|
|
|
|
// private CacheService cacheService; //数据缓存
|
|
|
|
//
|
|
|
|
// @Autowired
|
|
|
|
// private DataPersistenceService dataPersistenceService; //数据持久化
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * 添加校验
|
|
|
|
// * @return
|
|
|
|
// */
|
|
|
|
// @ModelAttribute
|
|
|
|
// public void preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
|
|
|
// {
|
|
|
|
// String key = request.getParameter(authKey);
|
|
|
|
// if(StringUtils.isNoneEmpty(key))
|
|
|
|
// {
|
|
|
|
// response.setStatus(403);
|
|
|
|
// throw new ServiceException("验证失败");
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @ApiOperation("更新指定设备的全部数据")
|
|
|
|
// @RequestMapping(value = "putAllData/{deviceid}/{messageid}")
|
|
|
|
// public AjaxResult putDeviceAllData(@PathVariable String deviceid,@PathVariable String messageid,HttpServletRequest request) throws Exception {
|
|
|
|
// String str = HttpServletRequestUtil.getAllParametersAsJSON(request);
|
|
|
|
//
|
|
|
|
// if(StringUtils.isEmpty(str))
|
|
|
|
// {
|
|
|
|
// return AjaxResult.error("数据为空");
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// String imei = deviceid.split("_")[0];
|
|
|
|
// IotDevice iotDevice = deviceService.selectIotDeviceByClient_id(imei);
|
|
|
|
// Topic topic = new Topic();
|
|
|
|
// topic.setRoleid(iotDevice.getProduct_id()+"");
|
|
|
|
// topic.setUsername(iotDevice.getMqtt_username());
|
|
|
|
// topic.setClientid(iotDevice.getClient_id());
|
|
|
|
// topic.setTopicType("PUT");
|
|
|
|
// topic.setMessageid(messageid);
|
|
|
|
// topic.setPayloadtype("json");
|
|
|
|
//
|
|
|
|
// //转化为协议对象
|
|
|
|
// BusinessDto businessDto = BusinessDtoClassNew.newBean(topic.getPayloadtype(),str.getBytes()).analyticalModel(iotDevice.getThings_model_value());
|
|
|
|
//
|
|
|
|
// BusinessAgreement businessAgreement = businessAgreementFactory.createBusinessAgreement(topic);
|
|
|
|
// //解析为业务对象
|
|
|
|
// ServerDto dto = businessAgreement.analysis(topic,businessAgreement.toData(businessDto));
|
|
|
|
// if(null == dto)
|
|
|
|
// {
|
|
|
|
// return AjaxResult.error("没有业务解析方法");
|
|
|
|
// }
|
|
|
|
// log.info("{} 解析到的dto【{}】",dto);
|
|
|
|
//
|
|
|
|
// //缓存数据
|
|
|
|
// cacheService.updateCache(topic,dto);
|
|
|
|
//
|
|
|
|
// //数据持久化
|
|
|
|
// dataPersistenceService.persistence(topic,dto);
|
|
|
|
// return AjaxResult.success();
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// @ApiOperation("更新指定设备的部分数据")
|
|
|
|
// @RequestMapping(value = "putPartialData/{deviceid}")
|
|
|
|
// public AjaxResult putDevicePartialData(@PathVariable String deviceid)
|
|
|
|
// {
|
|
|
|
// return AjaxResult.success();
|
|
|
|
// }
|
|
|
|
//} |
...
|
...
|
|