package com.huimv.environ.eco.controller;


import com.huimv.common.utils.Result;
import com.huimv.environ.eco.entity.SysTelecom;
import com.huimv.environ.eco.entity.dto.TelecomDto;
import com.huimv.environ.eco.service.ISysTelecomService;
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>
 *  前端控制器
 * </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) {
        System.out.println("yyyyyyyyyyyyyyyyyy---"+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);
    }

}
