Skip to content

Commit

Permalink
[feat]: fix execute service
Browse files Browse the repository at this point in the history
  • Loading branch information
lgt1126 committed Jan 4, 2022
1 parent ba7f61f commit 343d611
Showing 3 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.zmops.iot.web.device.controller;

import com.zmops.iot.model.response.ResponseData;
import com.zmops.iot.web.device.dto.ServiceExecuteDto;
import com.zmops.iot.web.device.service.DeviceSvrService;
import org.apache.camel.spi.AsEndpointUri;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
@@ -21,8 +22,8 @@ public class DeviceServiceController {
DeviceSvrService deviceSvrService;

@RequestMapping("/execute")
public ResponseData execute(@RequestParam("deviceId") String deviceId,@RequestParam("serviceId") Long serviceId){
deviceSvrService.execute(deviceId,serviceId);
public ResponseData execute(@Validated @RequestBody ServiceExecuteDto serviceExecuteDto) {
deviceSvrService.execute(serviceExecuteDto.getDeviceId(), serviceExecuteDto.getServiceId(), serviceExecuteDto.getServiceParams());
return ResponseData.success();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.zmops.iot.web.device.dto;

import lombok.Data;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
* @author yefei
**/
@Data
public class ServiceExecuteDto {

@NotBlank
private String deviceId;
@NotNull
private Long serviceId;

private List<ServiceParam> serviceParams;

@Data
public static class ServiceParam {
@NotBlank
private String key;
@NotBlank
private String value;
}
}
Original file line number Diff line number Diff line change
@@ -13,12 +13,14 @@
import com.zmops.iot.model.exception.ServiceException;
import com.zmops.iot.util.DefinitionsUtil;
import com.zmops.iot.util.ToolUtil;
import com.zmops.iot.web.device.dto.ServiceExecuteDto.ServiceParam;
import com.zmops.iot.web.exception.enums.BizExceptionEnum;
import io.ebean.DB;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -38,7 +40,7 @@ public class DeviceSvrService {
* @param deviceId 设备ID
* @param serviceId 服务ID
*/
public void execute(String deviceId, Long serviceId) {
public void execute(String deviceId, Long serviceId, List<ServiceParam> serviceParams) {

//封装执行参数
List<Map<String, Object>> body = new ArrayList<>();
@@ -54,14 +56,20 @@ public void execute(String deviceId, Long serviceId) {
throw new ServiceException(BizExceptionEnum.SERVICE_NOT_EXISTS);
}
serviceMap.put("name", productService.getName());

List<ProductServiceParam> paramList = DefinitionsUtil.getServiceParam(serviceId);
if (ToolUtil.isNotEmpty(paramList)) {
serviceMap.put("param", paramList.parallelStream().collect(Collectors.toMap(ProductServiceParam::getKey, ProductServiceParam::getValue)));
Map<String,String> paramStr = new HashMap<>(2);
if (ToolUtil.isNotEmpty(serviceParams)) {
paramStr = serviceParams.parallelStream().collect(Collectors.toMap(ServiceParam::getKey, ServiceParam::getValue));
serviceMap.put("param", paramStr);
} else {
List<ProductServiceParam> paramList = DefinitionsUtil.getServiceParam(serviceId);
if (ToolUtil.isNotEmpty(paramList)) {
paramStr = paramList.parallelStream().collect(Collectors.toMap(ProductServiceParam::getKey, ProductServiceParam::getValue));
serviceMap.put("param", paramStr);
}
}
serviceList.add(serviceMap);

map.put("service", serviceList);

body.add(map);

//下发命令 执行
@@ -70,8 +78,8 @@ public void execute(String deviceId, Long serviceId) {
//记录服务日志
ServiceExecuteRecord serviceExecuteRecord = new ServiceExecuteRecord();
serviceExecuteRecord.setDeviceId(deviceId);
if (ToolUtil.isNotEmpty(paramList)) {
serviceExecuteRecord.setParam(JSONObject.toJSONString(paramList.parallelStream().collect(Collectors.toMap(ProductServiceParam::getKey, ProductServiceParam::getValue))));
if (ToolUtil.isNotEmpty(paramStr)) {
serviceExecuteRecord.setParam(JSONObject.toJSONString(paramStr));
}
serviceExecuteRecord.setServiceName(DefinitionsUtil.getServiceName(serviceId));

0 comments on commit 343d611

Please sign in to comment.