package com.imdroid.beidou.controller;

import com.alibaba.fastjson.JSONObject;
import com.imdroid.common.util.NumberUtils;
import com.imdroid.secapi.dto.GnssDevice;
import com.imdroid.secapi.dto.GnssStatus;
import com.imdroid.secapi.dto.GnssStatusJoin;
import com.imdroid.secapi.dto.GnssStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.format.DateTimeFormatter;

@Controller
public class DeviceSummaryController extends BasicController{

    @Autowired
    private GnssStatusMapper gnssStatusMapper;

    final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**** 推送页面  *****/
    @RequestMapping(value={"/device_summary","/d"})
    public String gnssDevSummary() {
        return "/page/gnss_dev_summary";
    }


    /**** 推送数据  *****/
    @RequestMapping("/device_summary/query")
    @ResponseBody
    public JSONObject query(@RequestParam String deviceId) {
        JSONObject jsonObject = new JSONObject();
        GnssStatusJoin gnssStatus = gnssStatusMapper.queryByDeviceId(deviceId);
        if(gnssStatus==null){
            jsonObject.put("code",0);
            jsonObject.put("msg","无此设备");
        }
        else{
            jsonObject.put("code",1);
            jsonObject.put("deviceState",gnssStatus.getState()==GnssStatus.STATE_OFFLINE?"0":"1");
            jsonObject.put("deviceType",gnssStatus.getDevicetype()==GnssDevice.TYPE_ROVER?"监测站":"基准站");
            jsonObject.put("onlineTime",gnssStatus.getUpdatetime().format(dateFormatter));
            jsonObject.put("voltage",NumberUtils.scaleTwo((double)gnssStatus.getVoltage()/1000));
            jsonObject.put("satNum",gnssStatus.getSatelliteinuse()>=12?"多于12":gnssStatus.getSatelliteinuse());
            jsonObject.put("rxBytes",gnssStatus.getB562bytes()+gnssStatus.getD3xxbytes());
            jsonObject.put("rssi",gnssStatus.getRssi());
        }
        return jsonObject;
    }

}
