package com.ard.web.controller.sample; import com.ard.sample.common.error.CommonErrorEnum; import com.ard.sample.common.model.PaginationData; import com.ard.sample.common.model.ResponseResult; import com.ard.sample.component.mqtt.model.ChannelName; import com.ard.sample.component.mqtt.model.CommonTopicReceiver; import com.ard.sample.component.mqtt.model.CommonTopicResponse; import com.ard.sample.manage.model.dto.DeviceDTO; import com.ard.sample.manage.model.dto.DeviceFirmwareUpgradeDTO; import com.ard.sample.manage.model.enums.DeviceSetPropertyEnum; import com.ard.sample.manage.model.receiver.StatusGatewayReceiver; import com.ard.sample.manage.service.IDeviceService; import com.fasterxml.jackson.databind.JsonNode; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.annotation.ServiceActivator; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Optional; /** * @author sean.zhou * @version 0.1 * @date 2021/11/15 */ @Api(tags = "设备管理") @RestController @Slf4j @RequestMapping("/dock/device") public class DeviceController { @Autowired private IDeviceService deviceService; /** * 处理上线的消息 * * @param receiver The drone information is not empty. */ @ServiceActivator(inputChannel = ChannelName.INBOUND_STATUS_ONLINE, outputChannel = ChannelName.OUTBOUND) public void deviceOnline(CommonTopicReceiver receiver) { boolean online = deviceService.deviceOnline(receiver.getData()); if (online) { // Notify pilot that the drone is online successfully. deviceService.publishStatusReply(receiver.getData().getSn(), CommonTopicResponse.builder() .tid(receiver.getTid()) .bid(receiver.getBid()) .timestamp(System.currentTimeMillis()) .method(receiver.getMethod()) .build()); } } /** * 处理离线的消息 * * @param receiver The drone information is empty. */ @ServiceActivator(inputChannel = ChannelName.INBOUND_STATUS_OFFLINE, outputChannel = ChannelName.OUTBOUND) public void deviceOffline(CommonTopicReceiver receiver) { boolean offline = deviceService.deviceOffline(receiver.getData()); if (offline) { // Notify pilot that the device is offline successfully. deviceService.publishStatusReply(receiver.getData().getSn(), CommonTopicResponse.builder() .tid(receiver.getTid()) .bid(receiver.getBid()) .timestamp(System.currentTimeMillis()) .method(receiver.getMethod()) .build()); } } /** * 获取所有在线设备列表 * * @return */ @ApiOperation("设备列表") @GetMapping("/list") public ResponseResult> getDevices() { List devicesList = deviceService.getDevicesTopoForWeb(); return ResponseResult.success(devicesList); } /** * 绑定设备 * * @param device * @param deviceSn * @return */ @ApiOperation("设备绑定") @PostMapping("/{device_sn}/binding") public ResponseResult bindDevice(@RequestBody DeviceDTO device, @PathVariable("device_sn") String deviceSn) { device.setDeviceSn(deviceSn); boolean isUpd = deviceService.bindDevice(device); return isUpd ? ResponseResult.success() : ResponseResult.error(); } /** * Obtain device information according to device sn. * * @param deviceSn * @return */ @ApiOperation("设备信息") @GetMapping("/{device_sn}") public ResponseResult getDevice(@PathVariable("device_sn") String deviceSn) { Optional deviceOpt = deviceService.getDeviceBySn(deviceSn); return deviceOpt.isEmpty() ? ResponseResult.error("device not found.") : ResponseResult.success(deviceOpt.get()); } /** * Get the binding devices list * 获取绑定设备列表 * * @param page * @param pageSize * @return */ @ApiOperation("绑定设备列表") @GetMapping("/list/bound") public ResponseResult> getBoundDevicesWithDomain(Integer domain, @RequestParam(defaultValue = "1") Long page, @RequestParam(value = "page_size", defaultValue = "50") Long pageSize, @RequestParam(required = false) Long deptId) { PaginationData devices = deviceService.getBoundDevicesWithDomain(page, pageSize, domain, deptId); return ResponseResult.success(devices); } /** * Removing the binding state of the device. * * @param deviceSn * @return */ @ApiOperation("设备解绑") @DeleteMapping("/{device_sn}/unbinding") public ResponseResult unbindingDevice(@PathVariable("device_sn") String deviceSn) { deviceService.unbindDevice(deviceSn); return ResponseResult.success(); } /** * Update device information. * * @param device * @param deviceSn * @return */ @ApiOperation("设备更新") @PutMapping("/{device_sn}") public ResponseResult updateDevice(@RequestBody DeviceDTO device, @PathVariable("device_sn") String deviceSn) { device.setDeviceSn(deviceSn); boolean isUpd = deviceService.updateDevice(device); return isUpd ? ResponseResult.success() : ResponseResult.error(); } /** * Delivers offline firmware upgrade tasks. * * @param upgradeDTOS * @return */ @ApiOperation("固件升级") @PostMapping("/ota") public ResponseResult createOtaJob(@RequestBody List upgradeDTOS) { return deviceService.createDeviceOtaJob(upgradeDTOS); } /** * Set the property parameters of the drone. * * @param dockSn * @param param * @return */ @ApiOperation("属性设置") @PutMapping("{device_sn}/property") public ResponseResult devicePropertySet(@PathVariable("device_sn") String dockSn, @RequestBody JsonNode param) { if (param.size() != 1) { return ResponseResult.error(CommonErrorEnum.ILLEGAL_ARGUMENT); } String property = param.fieldNames().next(); Optional propertyEnumOpt = DeviceSetPropertyEnum.find(property); if (propertyEnumOpt.isEmpty()) { return ResponseResult.error(CommonErrorEnum.ILLEGAL_ARGUMENT); } deviceService.devicePropertySet(dockSn, propertyEnumOpt.get(), param.get(property)); return ResponseResult.success(); } }