package com.ruoyi.web.bst;

import com.ruoyi.bst.device.domain.dto.DeviceSetDTO;
import com.ruoyi.bst.device.domain.dto.DeviceWifiDTO;
import com.ruoyi.bst.device.domain.dto.DeviceWxsDTO;
import com.ruoyi.bst.device.domain.enums.DeviceTimeUnit;
import com.ruoyi.bst.device.service.DeviceIotService;
import com.ruoyi.bst.device.service.DeviceService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.iot.constants.IotConstants;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hpsf.Decimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bst/device/iot")
public class DeviceIotController extends BaseController {

    @Autowired
    private DeviceIotService deviceIotService;

    @ApiOperation("刷新设备信息")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @GetMapping("/{deviceId}/refreshIot")
    public AjaxResult syncIot(@PathVariable @ApiParam("设备id") Long deviceId,
                              @RequestParam(required = false, defaultValue = IotConstants.ONLINE_TYPE_GET) String onlineType) {
        deviceIotService.pullDeviceInfo(deviceId, IotConstants.ONLINE_TYPE_COMMAND);
        return success();
    }

    @ApiOperation("设备开关")
    @PreAuthorize("@ss.hasPermi('bst:device:switch')")
    @Log(title = "设备", businessType = BusinessType.OTHER)
    @PutMapping("/{deviceId}/switch")
    public AjaxResult switchDevice(@PathVariable Long deviceId, @RequestParam Boolean open) {
        return toAjax(deviceIotService.switchDevice(deviceId, open, "后台管理员开关设备"));
    }

    @ApiOperation("设置电量系数")
    @PutMapping("/setWxs")
    public AjaxResult setWxs(@RequestBody @Validated DeviceWxsDTO dto) {
        return toAjax(deviceIotService.setWxs(dto.getDeviceId(), dto.getWxs(), "管理员设置电量系数:" + dto.getWxs()));
    }

    @ApiOperation("设置设备反转参数")
    @PutMapping("/setSet")
    public AjaxResult setSet(@RequestBody @Validated DeviceSetDTO dto) {
        return toAjax(deviceIotService.setSet(dto.getDeviceId(), dto.getSet(), "管理员设置设备反转参数:" + dto.getSet()));
    }

    @ApiOperation("初始化设备总用电量读数")
    @PreAuthorize("@ss.hasPermi('bst:device:initTotalEle')")
    @PutMapping("/{deviceId}/initTotalEle")
    public AjaxResult initTotalEle(@PathVariable Long deviceId ) {
        return toAjax(deviceIotService.initTotalEle(deviceId));
    }


}
