Commit 45f34fdd by 111-PC\111

[+]pulsar消费服务

1 parent 398b5c05
package cn.com.glsx.consumer.client;
import cn.com.glsx.consumer.entity.DeviceVo;
import cn.com.glsx.consumer.pojo.request.scene.SceneRequest;
import cn.com.glsx.consumer.pojo.response.scene.OrderResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author zhouhaibao
* @date 2021/5/6 15:19
*/
@FeignClient(name = "${feign.server.name}",url = "${feign.server.url}")
public interface SceneClient {
@ResponseBody
@RequestMapping(value = "/scene/issueOrder", method = {RequestMethod.POST})
List<OrderResponse> issueOrder(@RequestBody SceneRequest sceneRequest);
@ResponseBody
@RequestMapping(value = "/userDevice/save", method = {RequestMethod.POST})
Boolean devBind(@RequestBody DeviceVo deviceVo);
@ResponseBody
@RequestMapping(value = "/userDevice/deleteDevice", method = {RequestMethod.POST})
Boolean devUnBind(@RequestBody DeviceVo deviceVo);
}
package cn.com.glsx.consumer.pojo.request.scene;
import lombok.Data;
/**
* @author zhouhaibao
* @date 2021/5/6 15:36
*/
@Data
public class SceneRequest {
private Long id;
public SceneRequest(Long id) {
this.id = id;
}
}
package cn.com.glsx.consumer.pojo.response.scene;
import lombok.Data;
/**
* @author zhouhaibao
* @date 2021/3/26 17:57
*/
@Data
public class OrderResponse {
private String deviceName;
private String message;
private boolean result;
private Integer code;
}
package cn.com.glsx.consumer.service;
import cn.com.glsx.consumer.client.SceneClient;
import cn.com.glsx.consumer.entity.DeviceVo;
import cn.com.glsx.consumer.utils.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
......@@ -24,11 +26,8 @@ import java.util.List;
@Slf4j
public class DeviceService {
@Value("${addUserDevUrl}")
private String addUserDevUrl;
@Value("${deleteUserDevUrl}")
private String deleteUserDevUrl;
@Autowired
private SceneClient sceneClient;
@Value("${getDeviceNameUrl}")
private String getDeviceNameUrl;
......@@ -39,13 +38,8 @@ public class DeviceService {
* @param deviceVo 入参
*/
public void devBind(DeviceVo deviceVo) {
try {
String responseBody = HttpUtil.sendPost(addUserDevUrl, JSONObject.toJSONString(deviceVo), new HashMap<>());
log.info("设备id{}保存更新结果{}", deviceVo.getDeviceId(), responseBody);
} catch (Exception e) {
e.printStackTrace();
log.info("设备id{}保存更新异常,{}", deviceVo.getDeviceId(), e.getMessage());
}
Boolean aBoolean = sceneClient.devBind(deviceVo);
log.info("设备id{}保存更新结果{}", deviceVo.getDeviceId(), aBoolean);
}
/**
......@@ -54,13 +48,8 @@ public class DeviceService {
* @param deviceVo 入参
*/
public void devUnBind(DeviceVo deviceVo) {
try {
String responseBody = HttpUtil.sendPost(deleteUserDevUrl, JSONObject.toJSONString(deviceVo), new HashMap<>());
log.info("设备id{}删除结果{}", deviceVo.getDeviceId(), responseBody);
} catch (Exception e) {
e.printStackTrace();
log.info("设备id{}删除异常,{}", deviceVo.getDeviceId(), e);
}
Boolean aBoolean = sceneClient.devUnBind(deviceVo);
log.info("设备id{}删除结果{}", deviceVo.getDeviceId(), aBoolean);
}
public String getDeviceName(String devId) {
......
package cn.com.glsx.consumer.service;
import cn.com.glsx.consumer.client.SceneClient;
import cn.com.glsx.consumer.common.Contants;
import cn.com.glsx.consumer.common.EDay;
import cn.com.glsx.consumer.common.ELogic;
import cn.com.glsx.consumer.entity.DayInfo;
import cn.com.glsx.consumer.entity.DeviceEvent;
import cn.com.glsx.consumer.entity.DeviceEventDetail;
import cn.com.glsx.consumer.entity.DeviceStatus;
import cn.com.glsx.consumer.entity.DeviceStatusDetail;
import cn.com.glsx.consumer.entity.DeviceVo;
import cn.com.glsx.consumer.entity.ScenseParamValue;
import cn.com.glsx.consumer.entity.SmartDevideStatus;
import cn.com.glsx.consumer.entity.SmartSceneData;
import cn.com.glsx.consumer.mapper.DayInfoMapper;
import cn.com.glsx.consumer.mapper.SmartDevideStatusMapper;
import cn.com.glsx.consumer.mapper.SmartSceneDataMapper;
import cn.com.glsx.consumer.pojo.request.scene.SceneRequest;
import cn.com.glsx.consumer.pojo.response.scene.OrderResponse;
import cn.com.glsx.consumer.redis.RedisService;
import cn.com.glsx.consumer.utils.DateUtils;
import cn.com.glsx.consumer.utils.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
......@@ -51,12 +53,15 @@ public class InfoService {
@Autowired
private ConditionStrategy conditionStrategy;
@Value("${issueOrderUrl}")
private String issueOrderUrl;
@Autowired
private SceneClient sceneClient;
@Autowired
private RedisService redisService;
@Autowired
private DeviceService deviceService;
/**
* 处理消息
*
......@@ -98,6 +103,7 @@ public class InfoService {
}
boolean isTrigger = conditionStrategy.isTrigger(scene_condition_time, index.getConditionRule(),
(SmartSceneData condition) -> getTriggerResult(condition));
log.info("场景id{}---{}触发条件", index.getSceneId(), isTrigger ? "符合" : "不符合");
// 温度控制
Object o = redisService.get(key);
if(!in.getFlag()) {
......@@ -108,24 +114,14 @@ public class InfoService {
log.info("场景id{}温湿度已经触发过一次", index.getSceneId());
return;
}
// 下发场景
List<OrderResponse> orderResponses = sceneClient.issueOrder(new SceneRequest(index.getSceneId()));
log.info("场景id{}下发结果{}", index.getSceneId(), orderResponses);
}else{
// 重新触发的话
redisService.delete(key);
}
}
log.info("场景id{}---{}触发条件", index.getSceneId(), isTrigger ? "符合" : "不符合");
if (isTrigger) {
// 下发场景
Map<String, String> requestMap = new HashMap<>();
requestMap.put("id", index.getSceneId() + "");
try {
String responseBody = HttpUtil.sendPost(issueOrderUrl, JSONObject.toJSONString(requestMap), new HashMap<>());
log.info("场景id{}下发结果{}", index.getSceneId(), responseBody);
} catch (Exception e) {
e.printStackTrace();
log.info("场景id{}下发指令异常,{}", index.getSceneId(), e);
}
}
});
});
......@@ -225,4 +221,45 @@ public class InfoService {
log.info("deviceOrderCode为{}的判断是{}", deviceOrderCode, valueByCode);
return valueByCode;
}
/**
* 事件消息
*
* @param data
*/
public void processEeventMessage(String data) {
DeviceEvent deviceEvent = JSONObject.parseObject(data, DeviceEvent.class);
String bizCode = deviceEvent.getBizCode();
switch (bizCode) {
case "nameUpdate":
deviceService.devBind(convervo(deviceEvent));
break;
case "bindUser":
deviceService.devBind(convervo(deviceEvent));
break;
case "delete":
deviceService.devUnBind(convervo(deviceEvent));
break;
default:
break;
}
}
private DeviceVo convervo(DeviceEvent deviceEvent) {
DeviceVo deviceVo = new DeviceVo();
DeviceEventDetail bizData = deviceEvent.getBizData();
if(deviceEvent.getBizCode().equals("bindUser")) {
String name = deviceService.getDeviceName(bizData.getDevId());
deviceVo.setDeviceName(name);
}else{
deviceVo.setDeviceName(bizData.getName());
}
deviceVo.setUid(bizData.getUid());
deviceVo.setPlatformProductId(deviceEvent.getProductKey());
deviceVo.setChannel(1);
deviceVo.setParentId(bizData.getParentDevId());
deviceVo.setType(bizData.getParentDevId()==null?2:1);
deviceVo.setDeviceId(bizData.getDevId());
return deviceVo;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!