Commit 2b28194e by tyfanchz

订阅用户设备功能

1 parent a1df91e7
......@@ -24,6 +24,7 @@ public enum ResultEnum {
DEVICE_NOT_FOUND(4001, "设备信息不存在"),
USER_NOT_FOUND(5001, "用户信息不存在"),
DEVICE_SUBSCRIBE_ERROR(5002, "用户设备订阅失败"),
ENCRYPT_EXCEP_1(1101, "crep加密异常,无法插入"),
ENCRYPT_EXCEP_2(1102, "crep加密异常,无法返回钥匙信息"),
......
......@@ -8,15 +8,21 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.HttpUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.smart.constant.Constants;
import com.glsx.smart.dao.entity.SmartUserChannel;
import com.glsx.smart.dao.entity.SmartUserDevice;
import com.glsx.smart.dao.entity.SmartUserRelation;
import com.glsx.smart.dao.mapper.SmartProductMapper;
import com.glsx.smart.dao.mapper.SmartUserChannelMapper;
import com.glsx.smart.dao.mapper.SmartUserDeviceMapper;
import com.glsx.smart.dao.mapper.SmartUserRelationMapper;
import com.glsx.smart.exception.BusinessException;
import com.glsx.smart.exception.ResultEnum;
import com.glsx.smart.redis.RedisService;
import com.glsx.smart.service.BaseService;
import com.glsx.smart.service.SmartUserDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
......@@ -39,12 +45,24 @@ public class SmartUserDeviceServiceImpl extends BaseService implements SmartUser
@Value("${user_send_command}")
private String userSendCommandApi;
@Value("${user_device_subscribe}")
private String userDeviceSubscribeApi;
@Resource
private SmartUserDeviceMapper userDeviceMapper;
@Resource
private SmartUserRelationMapper userRelationMapper;
@Resource
private SmartUserChannelMapper userChannelMapper;
@Resource
private SmartProductMapper productMapper;
@Autowired
private RedisService redisService;
@Override
public List<SmartUserDevice> getList(SmartUserDevice req) {
return userDeviceMapper.getList(req);
......@@ -82,6 +100,7 @@ public class SmartUserDeviceServiceImpl extends BaseService implements SmartUser
req.setCreateTime(now);
req.setUpdateTime(now);
rows = userDeviceMapper.insertSelective(req);
this.subscribeDevice(req);
} else {
if (StringUtils.isBlank(req.getDeviceName())) {
throw BusinessException.create(ResultEnum.PARAM_EMPTY, "deviceName");
......@@ -125,4 +144,36 @@ public class SmartUserDeviceServiceImpl extends BaseService implements SmartUser
? respObj.getString("result") : null;
return result;
}
/**
* 订阅用户设备
*
* @param req 请求
*/
private void subscribeDevice(SmartUserDevice req) {
if (Constants.USER_CHANNEL_AQARA.equals(req.getChannel())) {
// 只有绿米的设备需要订阅
if (StringUtils.isBlank(req.getPlatformProductId())) {
throw BusinessException.create(ResultEnum.PARAM_EMPTY, "platformProductId");
}
SmartUserRelation userRelation = userRelationMapper.selectByPrimaryKey(req.getUserId());
String phone = userRelation.getPhone();
String token = (String) redisService.get(phone + "_aqara");
List<String> commands = productMapper.getSupportedCommands(req.getPlatformProductId());
JSONObject reqData = new JSONObject();
reqData.put("did", req.getDeviceId());
reqData.put("attrs", commands);
JSONObject reqObj = new JSONObject();
reqObj.put("data", reqData);
reqObj.put("token", token);
String url = remoteUrl + userDeviceSubscribeApi;
log.info("$$$$ subscribeDevice url = {}, req = {}", url, reqObj.toJSONString());
String respJson = HttpUtils.post(url, reqObj.toJSONString());
log.info("$$$$ subscribeDevice resp = {}", respJson);
JSONObject respObj = JSON.parseObject(respJson);
if (!"200".equals(respObj.getString("code"))) {
throw BusinessException.create(ResultEnum.DEVICE_SUBSCRIBE_ERROR);
}
}
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!