/**
 * 零碳展厅设备商数据推送接口（无需登录）
 */

import asyncHandler from 'express-async-handler';
import * as deviceBiz from '../biz/device';
import * as regionBiz from '../biz/region';
import * as runningBiz from '../biz/running';
import { eccReqParamater } from '../util/verificationParam';
import { checkUser } from '../middleware/user';

export function setRouter(httpServer) {
    /** 注册/更新设备信息 */
    httpServer.post('/api/qdm/device/register', asyncHandler(registerDevice));

    /** 设备商推送实时数据 */
    httpServer.post('/api/qdm/device/push', asyncHandler(deviceDataPush));

    /** 设备商推送故障数据 */
    httpServer.post('/api/qdm/device/fault/push', asyncHandler(deviceFaultPush));

    /** 获取区域列表 */
    httpServer.post('/api/qdm/region/regionList', asyncHandler(getRegionList));

    /** 运行分析 */
    httpServer.post('/api/qdm/run/analysis', asyncHandler(getRunAnalysis));
    /** 运行分析 - 楼栋 */
    httpServer.post('/api/qdm/run/analysis/building', asyncHandler(getRunAnalysisByBuilding));
    /** 运行分析 - 楼层 */
    httpServer.post('/api/qdm/run/analysis/Floor', asyncHandler(getRunAnalysisByFloor));
    /** 空调运行弹框 */
    httpServer.post('/api/qdm/run/monitor/acPop', asyncHandler(getAcRunningPop));
    /** 空调开关 */
    httpServer.post('/api/qdm/run/monitor/acRun', asyncHandler(controlAcRunning));
    /** 运行分析 - 环境质量弹窗 */
    httpServer.post('/api/qdm/run/analysis/envPop', asyncHandler(getRunAnalysisEnvPop));

    /** 启动数据集成 */
    httpServer.post('/api/qdm/run/start/schedule', asyncHandler(startSchedule));
    /** 停止数据集成 */
    httpServer.post('/api/qdm/run/stop/schedule', asyncHandler(stopSchedule));

    /** IAQ-获取环境监测设备 */ 
    httpServer.post('/api/qdm/device/envDevice', asyncHandler(getEnvironmentDevices));
    /** IAQ-获取环境监测数据 */
    httpServer.post('/api/qdm/device/environment', asyncHandler(getEnvironmentData));
}

/**
 * 注册或更新设备信息
 */
 async function registerDevice(req, res) {
    let reqConf = {deviceId:'String', roomId:'String', deviceType:'String', deviceName:'String', controlParams:'Object'};
    const NotMustHaveKeys = [];
    let { deviceId, roomId, deviceType, deviceName, controlParams } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
    const result = await deviceBiz.registerOrUpdateDevice(deviceId, roomId, deviceType, deviceName, controlParams);
    res.success(result);
}

/**
 * 设备商推送数据
 */
async function deviceDataPush(req, res) {
    let reqConf = {deviceId:'String', data:'Object', deviceTime:'String'};
    const NotMustHaveKeys = [];
    let { deviceId, data, deviceTime } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);

    const result = await deviceBiz.handleDevicePush(deviceId, data, deviceTime);
    res.success(result);
}

/**
 * 设备商推送故障数据
 */
async function deviceFaultPush(req, res) {
    let reqConf = {deviceId:'String', faultType:'String', faultLevel:'String', faultDescription:'String', occurredTime:'String'};
    const NotMustHaveKeys = ["faultDescription"];
    let { deviceId, faultType, faultLevel, faultDescription, occurredTime } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);

    const result = await deviceBiz.handleDeviceFaultPush(deviceId, faultType, faultLevel, faultDescription, occurredTime);
    res.success(result);
}

/**
 * 获取区域列表接口，供前端下拉选择区域类型和区域名称
 * @param req 
 * @param res 
 */
async function getRegionList(req, res) {
    let reqConf = {regionGroup:'String'};
    const NotMustHaveKeys = [];
    let { regionGroup } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
    const result = await regionBiz.getRegionAllList(regionGroup);
    res.success(result);
}

/**
 * 运行分析
 */
 async function getRunAnalysis(req, res) {
    const { } = req.body;
    const result = await runningBiz.getRunAnalysis("","");
    res.success(result);
}

/**
 * 运行分析-楼栋
 */
 async function getRunAnalysisByBuilding(req, res) {
    let reqConf = {regionGroup:'String'};
    const NotMustHaveKeys = [];
    let { regionGroup } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
    const result = await runningBiz.getRunAnalysis(regionGroup, "");
    res.success(result);
}

/**
 * 运行分析-楼层
 */
 async function getRunAnalysisByFloor(req, res) {
    let reqConf = {regionGroup:'String', regionType:'String'};
    const NotMustHaveKeys = [];
    let { regionGroup, regionType } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
    const result = await runningBiz.getRunAnalysis(regionGroup, regionType);
    res.success(result);
}

/**
 * 空调运行弹框
 */
 async function getAcRunningPop(req, res) {
    let reqConf = {regionKey:'Number', deviceId:'String'};
    const NotMustHaveKeys = [];
    let { regionKey, deviceId } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
    const result = await runningBiz.getAcRunningPop(regionKey, deviceId);
    res.success(result);
}

/**
 * 控制空调开关
 */
async function controlAcRunning(req, res) {
    let reqConf = {deviceId:'String', mode:'String', power:'String', setTemp:'String', maxTemp:'String', minTemp:'String', autoControl:'String', fanSpeed:'String'};
    const NotMustHaveKeys = [ 'maxTemp', 'minTemp', 'autoControl', 'fanSpeed' ];
    let { deviceId, mode, power, setTemp, maxTemp, minTemp, autoControl, fanSpeed } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
    let params = { deviceId, mode, power, setTemp, maxTemp, minTemp, autoControl, fanSpeed };
    const result = await runningBiz.controlAcRunning(params);
    res.success(result);
}

/**
 * 启动定时任务
 */
async function startSchedule(req, res) {
    const result = await runningBiz.startScheduleTask();
    res.success(result);
}
/**
 * 停止定时任务
 */
async function stopSchedule(req, res) {
    const result = await runningBiz.stopScheduleTask();
    res.success(result);
}

/**
 * 获取环境设备
 */
async function getEnvironmentDevices(req, res) {
    let reqConf = {};
    let { } = eccReqParamater(reqConf, req.body);

    const result = await deviceBiz.getEnvironmentDevices();
    res.success(result);
}

/**
 * 获取环境监测数据
 */
async function getEnvironmentData(req, res) {
    let reqConf = {regionGroup:'String', regionType:'String', regionKey:'Number', deviceId:'Array'};
    const NotMustHaveKeys = ["regionGroup", "regionType", "regionKey", "deviceId"];
    let { regionGroup, regionType, regionKey, deviceId } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);

    const result = await deviceBiz.getEnvironmentData(regionGroup, regionType, regionKey, deviceId);
    res.success(result);
}

/**
 * 运行分析 - 环境质量弹窗
 */
async function getRunAnalysisEnvPop(req, res) {
    let reqConf = { regionGroup: 'String', regionType: 'String' };
    const NotMustHaveKeys = ["regionGroup", "regionType"];
    let { regionGroup, regionType } = eccReqParamater(reqConf, req.body, NotMustHaveKeys);
    const result = await deviceBiz.getEnvQualityPop(regionGroup, regionType);
    res.success(result);
}










