#!/usr/bin/python3
import cgi,cgitb
import sqlite3
import time
import json
from db import *
from globalSettings import *
import json
from global_func import *

DBPath = DBPath

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

cgitb.enable()
form = cgi.FieldStorage()
request_Type = form.getvalue('requestType')

def save_sweep():
    start_wavelegth = float(form.getvalue("start_wavelegth"))
    stop_wavelegth = float(form.getvalue("stop_wavelegth"))
    increment_wavelegth = float(form.getvalue("increment_wavelegth"))
    repetation_rate = float(form.getvalue("repetation_rate"))
    chirp_bandwith = float(form.getvalue("chirp_bandwith"))
    chirp_frequency = float(form.getvalue("chirp_frequency"))
    if(wavelengthRange == 'Lyra-TL-5'):
        id = 2
    else:
        id = 1
    if CCM==true:
        cmd = "update"
        cmd_id = get_ccm_command_id()
        action="update"
        name="sweep"
        message = "set,element=sweep,args=11,uuid={0},action={1},element_name={2},start_wavelength={3},stop_wavelength={4},increment_wavelength={5},repetation_rate={6},chirp_bandwith={7},chirp_frequency={8},device={9},id={10}"\
                .format(cmd_id, cmd, name, start_wavelegth, stop_wavelegth, increment_wavelegth,repetation_rate,chirp_bandwith,chirp_frequency, device,id)

        send_ccm_msg(message)
        response = get_ccm_response(cmd_id)
        print(response)
        delete_ccm_response(cmd_id)
    else:
        # c.execute("Insert into sweep (start_wave,stop_wave ,increment_wave ,repetition_rate ,chirp_bandwidth ,chirp_frequency ) values(?,?,?,?,?,?)",[start_wavelegth,stop_wavelegth,increment_wavelegth,repetation_rate,chirp_bandwith,chirp_frequency])
        # conn.commit()
        c.execute("update sweep set start_wave =? ,stop_wave = ?,increment_wave =? ,repetition_rate =?,chirp_bandwidth =?,chirp_frequency =? where id = ?",[start_wavelegth,stop_wavelegth,increment_wavelegth,repetation_rate,chirp_bandwith,chirp_frequency,id])
        # conn.commit()
        
        print(json.JSONEncoder().encode(
                    {"status": "success", "description": "Successfully Created"}))
def save_sweep1():
    start_wavelegth = float(form.getvalue("start_wavelegth"))
    stop_wavelegth = float(form.getvalue("stop_wavelegth"))
    increment_wavelegth = float(form.getvalue("increment_wavelegth"))
    repetation_rate = float(form.getvalue("repetation_rate"))
    chirp_bandwith = float(form.getvalue("chirp_bandwith"))
    chirp_frequency = float(form.getvalue("chirp_frequency"))
    if(wavelengthRange == 'Lyra-TL-5'):
        id = 4
    else:
        id = 3
    if CCM==true:
        cmd = "update"
        cmd_id = get_ccm_command_id()
        action="update"
        name="sweep"
        message = "set,element=sweep,args=11,uuid={0},action={1},element_name={2},start_wavelength={3},stop_wavelength={4},increment_wavelength={5},repetation_rate={6},chirp_bandwith={7},chirp_frequency={8},device={9},id={10}"\
                .format(cmd_id, cmd, name, start_wavelegth, stop_wavelegth, increment_wavelegth,repetation_rate,chirp_bandwith,chirp_frequency, device,id)

        send_ccm_msg(message)
        response = get_ccm_response(cmd_id)
        print(response)
        delete_ccm_response(cmd_id)
    else:
        # c.execute("Insert into sweep (start_wave,stop_wave ,increment_wave ,repetition_rate ,chirp_bandwidth ,chirp_frequency ) values(?,?,?,?,?,?)",[start_wavelegth,stop_wavelegth,increment_wavelegth,repetation_rate,chirp_bandwith,chirp_frequency])
        # conn.commit()
        c.execute("update sweep set start_wave1 =? ,stop_wave1 = ?,increment_wave1 =? ,repetition_rate1 =?,chirp_bandwidth1 =?,chirp_frequency1 =? where id = ?",[start_wavelegth,stop_wavelegth,increment_wavelegth,repetation_rate,chirp_bandwith,chirp_frequency,id])
        # conn.commit()
        
        print(json.JSONEncoder().encode(
                    {"status": "success", "description": "Successfully Created"}))
def stop_sweep():
    if CCM == true:
        cmd = "stop"
        cmd_id = get_ccm_command_id()
        action="stop"
        name="sweep"
        message = "set,element=sweep,args=11,uuid={0},action={1},element_name={2},device={3}".format(cmd_id, cmd, name, device)
        send_ccm_msg(message)
        response = get_ccm_response(cmd_id)
        print(response)
        delete_ccm_response(cmd_id)

def stop_sweep1():
    if CCM == true:
        cmd = "stop1"
        cmd_id = get_ccm_command_id()
        action="stop"
        name="sweep"
        message = "set,element=sweep,args=11,uuid={0},action={1},element_name={2},device={3}".format(cmd_id, cmd, name, device)
        send_ccm_msg(message)
        response = get_ccm_response(cmd_id)
        print(response)
        delete_ccm_response(cmd_id)
        
        
def read_sweep():
    if(wavelengthRange == 'Lyra-TL-5'):
        id = "(2,4)"
    else:
        id = "(1,3)"
    c.execute("select * from sweep where id in "+ id)
    dbData = []
    for col in c:
        
        jsonSchema = {"id": col[0],"start_wave" : col[1],"stop_wave" : col[2],"increment_wave": col[3],"repetition_rate": col[4],"chirp_bandwidth": col[5],"chirp_frequency": col[6]}
        dbData.append(jsonSchema)
    print((json.JSONEncoder().encode(dbData)))
print("Content-type:text/html\r\n\r\n")

if request_Type == 'save_sweep':
    save_sweep()
elif request_Type == 'save_sweep1':
    save_sweep1()
elif request_Type == 'stop_sweep':
    stop_sweep()
elif request_Type == 'stop_sweep1':
    stop_sweep1()
else:
    read_sweep()