import json
import threading
import app.api.thread_func as thread_func
import app.api.stack_func as stack_func
from . import api
from flask import request, jsonify
from app.config import HttpRequestMethod
from .response import Response, DeviceDataHeader
from flask import current_app


@api.route('/admin/add', methods=["GET", "POST"])
def add():
    if request.method == "POST":
        a = request.json.get("a")
        b = request.json.get("b")

        resp = Response(code=200, msg='ok', success=True)
        print(resp.json)

        return jsonify({"code": 200, "data": float(a)+float(b)})
    else:
        a = request.args.get("a")
        b = request.args.get("b")
        return jsonify({"code": 200, "data": float(a)+float(b)})


@api.route('/admin/push_data', methods=['GET', 'POST'])
def push_data():
    if request.method == HttpRequestMethod.GET:
        pass
    elif request.method == HttpRequestMethod.POST:
        json_data = request.json
        _type = json_data["type"]
        _cmd = json_data["cmd"]
        if _type == "OGC":  # 油气浓度
            if _cmd == "oil_gas_concentration_value":   # 实时油气浓度值上传
                stack_func.set_stack(_type, json_data)
                # print(stack_func.get_stack())
                thread = threading.Thread(target=thread_func.gas_concentration, args=(current_app._get_current_object(), json_data["data"]))
                thread.start()
        elif _type == "PS":  # 压力
            if _cmd == "pressure_value":  # 压力
                stack_func.set_stack(_type, json_data)
                # print(stack_func.get_stack())
                thread = threading.Thread(target=thread_func.pressure,
                                          args=(current_app._get_current_object(), json_data["data"]))
                thread.start()
        elif _type == "ScramButton":  # 急停按钮
            if _cmd == "switch_value":  # 开关值
                stack_func.set_stack(_type, json_data)
                # print(stack_func.get_stack())
                thread = threading.Thread(target=thread_func.switch,
                                          args=(current_app._get_current_object(), json_data["data"]))
                thread.start()
        elif _type == "Video":  # 视频分析
            if _cmd == "video_alarm_data":  # 事件信息
                stack_func.set_stack(_type, json_data)
                # print(stack_func.get_stack())
                thread = threading.Thread(target=thread_func.video,
                                          args=(current_app._get_current_object(), json_data["data"]))
                thread.start()
        elif _type == "Radar":  # 雷达
            if _cmd == "target_info_param":  # 事件信息
                stack_func.set_stack(_type, json_data)
                # print(stack_func.get_stack())
                thread = threading.Thread(target=thread_func.radar,
                                          args=(current_app._get_current_object(), json_data["data"]))
                thread.start()
        elif _type == "PCM":  # 用电管理
            if _cmd == "power_consumption_param":  # 事件信息
                stack_func.set_stack(_type, json_data)
                # print(stack_func.get_stack())
                thread = threading.Thread(target=thread_func.pcm,
                                          args=(current_app._get_current_object(), json_data["data"]))
                thread.start()

    else:
        pass
    return Response(code=200, msg='ok', success=True).json