UserDeviceController.java 4.34 KB
package cn.com.glsx.smart.modules.controller;

import cn.com.glsx.smart.common.exception.BusinessException;
import cn.com.glsx.smart.common.exception.ResultEnum;
import cn.com.glsx.smart.modules.entity.SmartUserDevice;
import cn.com.glsx.smart.modules.service.SmartUserDeviceService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.glsx.plat.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import java.util.List;

/**
 * <p>Description: 用户设备业务
 * <p>Project: chejia-smart
 *
 * @author wbh
 * @date 2021-04-23
 */
@Slf4j
@RestController
@RequestMapping("/userDevice")
public class UserDeviceController {
    @Autowired
    private SmartUserDeviceService userDeviceService;

    /**
     * 获取用户设备
     *
     * @param req 请求
     * @return 结果
     */
    @RequestMapping(value = "/getList", method = {RequestMethod.GET, RequestMethod.POST})
    public List<SmartUserDevice> getList(SmartUserDevice req) {
        log.info("$$$$ /userDevice/getList param: {}", JSON.toJSONString(req));
        if (req.getUserId() == null) {
            throw BusinessException.create(ResultEnum.PARAM_EMPTY, "userId");
        }
        return userDeviceService.getList(req);
    }

    /**
     * 根据 deviceId 查询设备详情
     *
     * @param req 请求
     * @return 结果
     */
    @RequestMapping(value = "/getByDeviceId", method = {RequestMethod.GET, RequestMethod.POST})
    public SmartUserDevice getByDeviceId(SmartUserDevice req) {
        log.info("$$$$ /userDevice/getByDeviceId param: {}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getDeviceId())) {
            throw BusinessException.create(ResultEnum.PARAM_EMPTY, "deviceId");
        }
        return userDeviceService.getByDeviceId(req);
    }

    /**
     * 保存用户设备
     *
     * @param req 请求
     * @return 结果
     */
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public boolean save(@RequestBody SmartUserDevice req) {
        log.info("$$$$ /userDevice/save param: {}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getDeviceId()) || req.getChannel() == null) {
            throw BusinessException.create(ResultEnum.PARAM_EMPTY, "deviceId, channel");
        }
        return userDeviceService.save(req) > 0;
    }

    /**
     * 删除用户设备
     *
     * @param req 请求
     * @return 结果
     */
    @RequestMapping(value = "/deleteDevice", method = {RequestMethod.POST})
    public boolean deleteDevice(@RequestBody SmartUserDevice req) {
        log.info("$$$$ /userDevice/deleteDevice param: {}", JSON.toJSONString(req));
        if (StringUtils.isBlank(req.getDeviceId()) || req.getChannel() == null) {
            throw BusinessException.create(ResultEnum.PARAM_EMPTY, "deviceId, channel");
        }
        return userDeviceService.deleteDevice(req) > 0;
    }

    /**
     * 获取用户 bindKey
     *
     * @param token 用户 token
     * @return 结果
     */
    @RequestMapping(value = "/getUserBindKey", method = {RequestMethod.GET, RequestMethod.POST})
    public String getUserBindKey(String token) {
        log.info("$$$$ /userDevice/getUserBindKey param: {}", token);
        if (StringUtils.isBlank(token)) {
            throw BusinessException.create(ResultEnum.PARAM_EMPTY, "token");
        }
        return userDeviceService.getUserBindKey(token);
    }

    /**
     * 下发指令
     *
     * @param req 请求
     * @return 结果
     */
    @RequestMapping(value = "/sendCommand", method = {RequestMethod.POST})
    public String sendCommand(@RequestBody JSONObject req) {
        log.info("$$$$ /userDevice/sendCommand param: {}", req.toJSONString());
        return userDeviceService.sendCommand(req);
    }

    /**
     * 设备解绑
     *
     * @param req 请求
     * @return 结果
     */
    @RequestMapping(value = "/unbind", method = {RequestMethod.POST})
    public String unbind(@RequestBody JSONObject req) {
        log.info("$$$$ /userDevice/unbind param: {}", req.toJSONString());
        return userDeviceService.unbind(req);
    }
}