| | |
| | | package com.ruoyi.other.controller; |
| | | |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ruoyi.common.core.web.domain.AjaxResult; |
| | | import com.ruoyi.common.core.web.page.PageInfo; |
| | | import com.ruoyi.common.log.annotation.Log; |
| | |
| | | import com.ruoyi.other.api.domain.TDevice; |
| | | import com.ruoyi.other.query.DevicePageList; |
| | | import com.ruoyi.other.service.TDeviceService; |
| | | import com.ruoyi.other.util.EzvizOpenApiUtils; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.util.StringUtils; |
| | | import org.springframework.web.bind.annotation.*; |
| | |
| | | |
| | | @Resource |
| | | private TDeviceService deviceService; |
| | | |
| | | @Resource |
| | | private EzvizOpenApiUtils ezvizOpenApiUtils; |
| | | |
| | | @RequiresPermissions(value = {"/deviceManagement/list"}, logical = Logical.OR) |
| | | @ResponseBody |
| | |
| | | } |
| | | if (device.getOnlineStatus() == null) { |
| | | device.setOnlineStatus(1); |
| | | } |
| | | if (device.getIssueStatus() == null) { |
| | | device.setIssueStatus(0); |
| | | } |
| | | deviceService.save(device); |
| | | return AjaxResult.success(); |
| | |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | @RequiresPermissions(value = {"/deviceManagement/issue"}, logical = Logical.OR) |
| | | @ResponseBody |
| | | @PostMapping("/issueDevice/{id}") |
| | | @ApiOperation(value = "下发设备到萤石平台", tags = {"管理后台-设备管理"}) |
| | | @Log(title = "【设备管理】下发设备到萤石平台", businessType = BusinessType.UPDATE) |
| | | public AjaxResult issueDevice(@PathVariable Integer id) { |
| | | TDevice device = deviceService.getById(id); |
| | | if (device == null) { |
| | | return AjaxResult.error("设备不存在"); |
| | | } |
| | | if (Integer.valueOf(1).equals(device.getIssueStatus())) { |
| | | return AjaxResult.success("设备已下发"); |
| | | } |
| | | JSONObject ezvizResult; |
| | | try { |
| | | ezvizResult = ezvizOpenApiUtils.addDevice(device.getDeviceSerial(), device.getValidateCode()); |
| | | } catch (Exception e) { |
| | | device.setIssueStatus(2); |
| | | device.setIssueMsg("萤石设备下发失败:" + e.getMessage()); |
| | | deviceService.updateById(device); |
| | | return AjaxResult.error(device.getIssueMsg()); |
| | | } |
| | | if (!isEzvizSuccess(ezvizResult)) { |
| | | device.setIssueStatus(2); |
| | | device.setIssueMsg(ezvizErrorMessage("萤石设备下发失败", ezvizResult)); |
| | | deviceService.updateById(device); |
| | | return AjaxResult.error(device.getIssueMsg()); |
| | | } |
| | | device.setIssueStatus(1); |
| | | device.setIssueMsg("下发成功"); |
| | | deviceService.updateById(device); |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | @RequiresPermissions(value = {"/deviceManagement/del"}, logical = Logical.OR) |
| | | @ResponseBody |
| | | @DeleteMapping("/delDevice") |
| | |
| | | public AjaxResult<TDevice> delDevice(Integer[] id) { |
| | | if (id == null || id.length == 0) { |
| | | return AjaxResult.error("请选择要删除的设备"); |
| | | } |
| | | for (TDevice device : deviceService.listByIds(Arrays.asList(id))) { |
| | | if (Integer.valueOf(1).equals(device.getIssueStatus())) { |
| | | JSONObject ezvizResult; |
| | | try { |
| | | ezvizResult = ezvizOpenApiUtils.deleteDevice(device.getDeviceSerial()); |
| | | } catch (Exception e) { |
| | | return AjaxResult.error("萤石设备删除失败:" + e.getMessage()); |
| | | } |
| | | if (!isEzvizSuccess(ezvizResult)) { |
| | | return AjaxResult.error(ezvizErrorMessage("萤石设备删除失败", ezvizResult)); |
| | | } |
| | | } |
| | | } |
| | | deviceService.removeByIds(Arrays.asList(id)); |
| | | return AjaxResult.success(); |
| | |
| | | if (!StringUtils.hasText(device.getDeviceName())) { |
| | | return AjaxResult.error("设备名称不能为空"); |
| | | } |
| | | if (!StringUtils.hasText(device.getSerialNo())) { |
| | | if (!StringUtils.hasText(device.getDeviceSerial())) { |
| | | return AjaxResult.error("设备序列号不能为空"); |
| | | } |
| | | if (!StringUtils.hasText(device.getEquipmentModel())) { |
| | | return AjaxResult.error("设备型号不能为空"); |
| | | if (device.getDeviceBrand() == null) { |
| | | return AjaxResult.error("设备品牌不能为空"); |
| | | } |
| | | if (!StringUtils.hasText(device.getValidateCode())) { |
| | | return AjaxResult.error("设备验证码不能为空"); |
| | | } |
| | | if (device.getPartnerId() == null) { |
| | | return AjaxResult.error("合作商不能为空"); |
| | | } |
| | | if (deviceService.existsBySerialNo(device.getSerialNo(), excludeId)) { |
| | | if (deviceService.existsByDeviceSerial(device.getDeviceSerial(), excludeId)) { |
| | | return AjaxResult.error("设备序列号已存在"); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | private boolean isEzvizSuccess(JSONObject result) { |
| | | return result != null && "200".equals(result.getString("code")); |
| | | } |
| | | |
| | | private String ezvizErrorMessage(String defaultMessage, JSONObject result) { |
| | | if (result == null) { |
| | | return defaultMessage; |
| | | } |
| | | String message = result.getString("msg"); |
| | | return StringUtils.hasText(message) ? message : defaultMessage; |
| | | } |
| | | } |