123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- package cn.minbb.iot.controller.rest;
- import cn.minbb.iot.data.ResponseResult;
- import cn.minbb.iot.model.Device;
- import cn.minbb.iot.model.User;
- import cn.minbb.iot.service.DeviceService;
- import cn.minbb.iot.service.UserService;
- import com.alibaba.fastjson.JSONObject;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @RequestMapping("device")
- public class DeviceController {
- @Autowired
- DeviceService deviceService;
- @Autowired
- UserService userService;
- @PostMapping("search")
- public ResponseResult<Device> search(@RequestBody String params) {
- JSONObject jsonObject = JSONObject.parseObject(params);
- Device device = deviceService.findOneBySerialNumber(jsonObject.getString("serial"));
- if (device == null) {
- return ResponseResult.create().success(false).message("没有查询到设备").dataNone();
- }
- return ResponseResult.create().success(true).message("OK").data(device);
- }
- @PostMapping("bind")
- public ResponseResult<Device> bind(@RequestBody String params) {
- JSONObject jsonObject = JSONObject.parseObject(params);
- Device device = JSONObject.parseObject(jsonObject.getString("device"), Device.class);
- User user = JSONObject.parseObject(jsonObject.getString("user"), User.class);
- Device deviceServer = deviceService.findOneBySerialNumber(device.getSerialNumber());
- User userServer = userService.findUserByUsername(user.getUsername());
- if (userServer != null && deviceServer != null) {
- if (deviceServer.getUserId() != null) {
- return ResponseResult.create().success(false).message("设备已绑定").dataNone();
- } else {
- deviceServer.setUserId(userServer.getId());
- return ResponseResult.create().success(true).message("OK").data(deviceService.save(deviceServer));
- }
- } else {
- return ResponseResult.create().success(false).message("参数缺失").dataNone();
- }
- }
- @PostMapping("all")
- public ResponseResult<Device> all(@RequestBody String params) {
- JSONObject jsonObject = JSONObject.parseObject(params);
- User user = JSONObject.parseObject(jsonObject.getString("user"), User.class);
- if (user == null) {
- return ResponseResult.create().success(false).message("参数缺失").dataNone();
- } else {
- return ResponseResult.create().success(true).message("OK").dataset(deviceService.findAllByUserId(user.getId()));
- }
- }
- @PostMapping("unbind")
- public ResponseResult<Device> unbind(@RequestBody String params) {
- JSONObject jsonObject = JSONObject.parseObject(params);
- Device device = JSONObject.parseObject(jsonObject.getString("device"), Device.class);
- User user = JSONObject.parseObject(jsonObject.getString("user"), User.class);
- Device deviceServer = deviceService.findOneBySerialNumber(device.getSerialNumber());
- User userServer = userService.findUserByUsername(user.getUsername());
- if (userServer != null && deviceServer != null) {
- if (deviceServer.getUserId() != null) {
- deviceServer.setUserId(null);
- deviceService.save(deviceServer);
- return ResponseResult.create().success(true).message("解绑设备" + deviceServer.getSerialNumber() + "成功").dataNone();
- } else {
- return ResponseResult.create().success(false).message("设备未绑定").dataNone();
- }
- } else {
- return ResponseResult.create().success(false).message("参数缺失").dataNone();
- }
- }
- }
|