package com.huimv.video.dhicc.controller.BaseController;


import com.huimv.common.utils.Result;
import com.huimv.video.dhicc.entity.SysTelecom;
import com.huimv.video.dhicc.entity.TelecomDto;
import com.huimv.video.dhicc.service.ISysTelecomService;
import net.sf.json.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;

import java.text.DecimalFormat;
import java.util.Map;

/**
 * <p>
 * 前端控制器   牧场文件不同  配置文件不同 麻烦啊  不能通过简单配置来处理
 * 理论上来说可以通过复写多个tokenSingle来实现文件的选择
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-22
 */
@RestController
@RequestMapping("/device")
public class SysTelecomController {
    @Autowired
    private ISysTelecomService telecomService;

    @PostMapping("/deviceResponse")
    public String deviceResponse(@RequestBody TelecomDto telecomDto) {
        Map<String, Integer> payload = telecomDto.getPayload();

        SysTelecom telecom = new SysTelecom();
        telecom.setDeviceId(telecomDto.getDeviceId());
        telecom.setFarmId(1);
        telecom.setTimestamp(telecomDto.getTimestamp());
        Integer rssi = payload.get("RSSI");
        Integer STC = payload.get("STC");
        Integer HUMI = payload.get("HUMI");
        Integer TEMP = payload.get("TEMP");
        TEMP +=80;
        telecom.setRssi(rssi);
        telecom.setStc(STC);

        telecom.setHumi(HUMI);
        DecimalFormat df = new DecimalFormat("0.0");
        telecom.setTemp(df.format(TEMP * 0.1));

        telecomService.save(telecom);
        return "200";
    }


    @PostMapping("/listByRoom")
    public Result listByRoom(@RequestBody Map map) {
        return telecomService.listByRoom(map);
    }


}
