#!/usr/bin/python3
import cgitb
import cgi
import json
import sqlite3
from db import *
from globalSettings import *
from global_func import *
print("Content-type:text/html\r\n\r\n")


cgitb.enable()

device = DeviceName

conn = sqlite3.connect(DBPath)
conn.text_factory = str
c = conn.cursor()

if (GUI == true):
    form = cgi.FieldStorage()
    request_Type = form.getvalue('requestType')


def start_calibration():
    if CCM:
        cmd_Id = get_ccm_command_id()
        cmd="test_response"
        name="calibration_test"
        action="start"
        message="set,element=calibration_test,element_name={0},args=2,action={1},uuid={2}".format(name,action,cmd_Id)
        send_ccm_msg(message) 
        response = get_ccm_response(cmd_Id)
        delete_ccm_response(cmd_Id)
        #response = json.JSONEncoder().encode({"status": "success", "description": "CCM state: OK, License state: OK"})
        print(response)
    else:
        print((json.JSONEncoder().encode(
        {"status": "success", "description": "success "})))


if(request_Type == 'start_calibration'):
    start_calibration()

if(request_Type == "read_calibration"):
    print((json.JSONEncoder().encode(
        {"status": "success", "description": "success "})))
