<?php

/**
 * Created by PhpStorm.
 * User: cgarcia
 * Date: 19/10/2017
 * Time: 11:23
 */
require_once "toDoXMLClass.php";
require_once "pwneo.php";

function formatCSVMeasures($row, $tz){
    $stdout = fopen('php://output', 'w');
//    var_export($row);
    switch ($row[2]) {
        case 'LOCKED':
            $row[2] = 'STATUS';
            $row[3] = $row[3]?'LOCKED':'UNLOCKED';
            break;
        case 'F_OFF':
            $row[2] = 'OFFSET';
            $row[3]= round($row[3], 2);
            break;
        case 'C/N':
            $row[3]= round($row[3], 2);
            break;
        case 'MER':
            $row[3]= round($row[3], 2);
            break;
        case 'POWER':
            $row[3]= round($row[3], 2);
            break;
        case 'LEVEL':
            $row[3]= round($row[3], 2);
            break;
        case 'LM':
            $row[3]= round($row[3], 2);
            break;
    }

    date_default_timezone_set($tz);
    $row[5] = date("Y-m-d H:i:s",$row[5]);
    fputcsv($stdout, $row, ',');
}

class controlClass{

    private $params;

    function __construct( $prmts )
    {
        $this->params = array();
        foreach ( $prmts as $n => $v)
        {
            $this->params[$n] = $v;
        }
    }

    public function actionToDO()
    {
        if ( isset($this->params['action']) )
        {
            switch ( $this->params['action'] ) {
                case '10000':
                    $toDoXMLClass = new toDoXMLClass($this->params['path']);
                    $xml=$toDoXMLClass->getXMLFile();
                    echo $xml;
                    break;
                case '10001':
//                    header("Content-Length: 1");
                    $toDoXMLClass = new toDoXMLClass($this->params['path']);
                    $ok=$toDoXMLClass->saveFile($_FILES['f']['tmp_name']);
                    echo $ok?'1':'0';
                    break;
                case '10002':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=null;
                    $db->echoJsonMonitorings(null);
                    break;
                case '10003':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=$this->params['limit'];
                    $db->echoJsonMeasures($this->params['i'],$this->params['startDate'],$this->params['endDate'],$this->params['monId']);
                    break;
                case '10004':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=null;
                    $db->echoJsonMonitoringChannels(null,$this->params['monId']);
                    break;
                case '10005':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=$this->params['limit'];
                    $db->echoJsonMeasures($this->params['i'],$this->params['startDate'],$this->params['endDate'],$this->params['monId'],$this->params['chfreq'],$this->params['chpol']);
                    break;
                case '10006':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=null;
                    $db->echoJsonMagnitudeSettings(null,$this->params['monId'],$this->params['signaltype']);
                    break;
                case '10007':
                    $arrayMagnitudes=explode(",", $this->params['magnitude']);
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=$this->params['limit'];
                    $db->echoJsonMeasures($this->params['i'],$this->params['startDate'],$this->params['endDate'],$this->params['monId'],NULL,NULL,$arrayMagnitudes);
                    break;
                case '10008':
                    $arrayMagnitudes=explode(",", $this->params['magnitude']);
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=$this->params['limit'];
                    $db->echoJsonMeasures($this->params['i'],$this->params['startDate'],$this->params['endDate'],$this->params['monId'],$this->params['chfreq'],$this->params['chpol'],$arrayMagnitudes);
                    break;
                case '10009':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=null;
                    $db->echoJsonMonitorEvents(null,$this->params['startDate'],$this->params['endDate'],$this->params['monId']);
                    break;
                case '10010':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=null;
                    $db->echoJsonMonitorEvents(null,$this->params['startDate'],$this->params['endDate'],$this->params['monId'],$this->params['chfreq'],$this->params['chpol']);
                    break;
                case '10011':
                    $arrayMagnitudes=explode(",", $this->params['magnitude']);
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=null;
                    $db->echoJsonMonitorEvents(null,$this->params['startDate'],$this->params['endDate'],$this->params['monId'],NULL,NULL,$arrayMagnitudes);
                    break;
                case '10012':
                    $arrayMagnitudes=explode(",", $this->params['magnitude']);
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=null;
                    $db->echoJsonMonitorEvents(null,$this->params['startDate'],$this->params['endDate'],$this->params['monId'],$this->params['chfreq'],$this->params['chpol'],$arrayMagnitudes);
                    break;
                case '10013':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $count=$db->countMeasures($this->params['startDate'],$this->params['endDate'],$this->params['monId']);
                    echo json_encode($count);
                    break;
                case '10014':
                    header('Content-Type: text/csv');
                    header("Content-Disposition: attachment; filename=measures.csv");
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $stdout = fopen('php://output', 'w');
                    $headerCSV=array('FREQUENCY (kHz)','SIGNAL','MEASURE','VALUE','UNITS','TIME');
                    fputcsv($stdout, $headerCSV, ',');
                    $cols=array('CHFREQKHZ','SIGNALTYPE','MAGNITUDE','VALUE','UNITS','TIME',);
                    $db->echoFormattedMeasures('formatCSVMeasures', $cols , $this->params['startDate'],$this->params['endDate'],$this->params['monId'],NULL,NULL,NULL, $this->params['tz']);
                    break;
                case '10015':
                    header('Content-Type: text/csv');
                    header("Content-Disposition: attachment; filename=measures.csv");
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $stdout = fopen('php://output', 'w');
                    $headerCSV=array('FREQUENCY (kHz)','SIGNAL','MEASURE','VALUE','UNITS','TIME');
                    fputcsv($stdout, $headerCSV, ',');
                    $cols=array('CHFREQKHZ','SIGNALTYPE','MAGNITUDE','VALUE','UNITS','TIME',);
                    $db->echoFormattedMeasures('formatCSVMeasures', $cols , $this->params['startDate'],$this->params['endDate'],$this->params['monId'],$this->params['chfreq'],$this->params['chpol'],NULL,$this->params['tz']);
                    break;
                case '10016':
                    header('Content-Type: text/csv');
                    header("Content-Disposition: attachment; filename=measures.csv");
                    $arrayMagnitudes=explode(",", $this->params['magnitude']);
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $stdout = fopen('php://output', 'w');
                    $headerCSV=array('FREQUENCY (kHz)','SIGNAL','MEASURE','VALUE','UNITS','TIME');
                    fputcsv($stdout, $headerCSV, ',');
                    $cols=array('CHFREQKHZ','SIGNALTYPE','MAGNITUDE','VALUE','UNITS','TIME',);
                    $db->echoFormattedMeasures('formatCSVMeasures', $cols , $this->params['startDate'],$this->params['endDate'],$this->params['monId'],NULL,NULL,$arrayMagnitudes,$this->params['tz']);
                    break;
                case '10017':
                    header('Content-Type: text/csv');
                    header("Content-Disposition: attachment; filename=measures.csv");
                    $arrayMagnitudes=explode(",", $this->params['magnitude']);
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $stdout = fopen('php://output', 'w');
                    $headerCSV=array('FREQUENCY (kHz)','SIGNAL','MEASURE','VALUE','UNITS','TIME');
                    fputcsv($stdout, $headerCSV, ',');
                    $cols=array('CHFREQKHZ','SIGNALTYPE','MAGNITUDE','VALUE','UNITS','TIME',);
                    $db->echoFormattedMeasures('formatCSVMeasures', $cols , $this->params['startDate'],$this->params['endDate'],$this->params['monId'],$this->params['chfreq'],$this->params['chpol'],$arrayMagnitudes,$this->params['tz']);
                    break;
                case '10018':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $count=$db->countMeasures($this->params['startDate'],$this->params['endDate'],$this->params['monId'],$this->params['chfreq'],$this->params['chpol']);
                    echo json_encode($count);
                    break;
                case '10019':
                    $arrayMagnitudes=explode(",", $this->params['magnitude']);
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $count=$db->countMeasures($this->params['startDate'],$this->params['endDate'],$this->params['monId'],NULL,NULL,$arrayMagnitudes);
                    echo json_encode($count);
                    break;
                case '10020':
                    $arrayMagnitudes=explode(",", $this->params['magnitude']);
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $count=$db->countMeasures($this->params['startDate'],$this->params['endDate'],$this->params['monId'],$this->params['chfreq'],$this->params['chpol'],$arrayMagnitudes);
                    echo json_encode($count);
                    break;
                case '10021':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=null;
                    $db->echoJsonSystemEvents(null,$this->params['startDate'],$this->params['endDate']);
                    break;
                case '10022':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->select_limit=null;
                    $db->echoJsonNotificationDestinations(null,"EMAIL");
                    break;
                case '10023':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READWRITE,10000);
                    $db->select_limit=null;
                    $db->startTransaction();
                    $db->insertNotificationDestination("EMAIL",$this->params['email'],"");
                    $db->closeTransaction(true);
                    break;
                case '10024':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READWRITE,10000);
                    $db->select_limit=null;
                    $arrayIds = array();
                    array_push($arrayIds,$this->params['id']);
                    $db->startTransaction();
                    $db->deleteNotificationDestination($arrayIds);
                    $db->closeTransaction(true);
                    break;
                case '10025':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READWRITE,10000);
                    $db->select_limit=null;
                    $db->echoJsonSpectrumMonitorings("",$this->params['dbPath']);
                    break;
                case '10026':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READWRITE,10000);
                    $db->select_limit=null;
                    $db->echoJsonSpectrumMonitoringGroups();
                    break;
                case '10027':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READWRITE,10000);
                    $db->select_limit=null;
                    $db->echoJsonSpectraltaskfromGroup($this->params['groupId']);
                    break;
                case '10028':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READWRITE,10000);
                    $db->select_limit=$this->params['limit'];
                    $db->echoJsonSpectrumEvents($this->params['i'],$this->params['dbPath'],$this->params['SPMonId'],$this->params['startDate'],$this->params['endDate']);
                    break;
                case '10029':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READWRITE,10000);
                    $db->select_limit=100;
                    $db->echoJsonSpectrumPoints(0,$this->params['dbPath'],$this->params['SPMonId'],$this->params['startDate'],$this->params['endDate']);
                    break;
                case '10030':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $count=$db->countSpectrumEvents($this->params['dbPath'],$this->params['startDate'],$this->params['endDate'],$this->params['SPMonId']);
                    echo json_encode($count);
                    break;
                case '10031':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->echoJsonTaskScheduling();
                    break;
                case '10032':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->selectSpectralMonitoringTaskbyId($this->params['taskId']);
                    break;
                case '10033':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->selectMonitoringTaskbyId($this->params['taskId']);
                    break;
                case '10034':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->echoJsonCaptureTaskbyId($this->params['taskId']);
                    break;
                case '10035':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->echoJsonDataloggerTaskbyId($this->params['taskId']);
                    break;
                case '10036':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,10000);
                    $db->echoJsonTSRecordTaskbyId($this->params['taskId']);
                    break;
                case '10037':
                    $toDoXMLClass = new toDoXMLClass($this->params['path']);
                    $zip=$toDoXMLClass->getZipFile();
                    echo $zip;
                    break;
                case '10038':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,100000);
                    $db->echoJsonAudioRecordTaskbyId($this->params['taskId']);
                    break;
                case '10039':
                    $db = new PwneoDb("/USR/VAR/pwdb_data.sqlite", SQLITE3_OPEN_READONLY,100000);
                    $db->echoJsonEtiRecordTaskbyId($this->params['taskId']);
                    break;
                default:
                    echo "Action not correct, value: ".$this->params['action'];
                    break;
            }
        }
    }
}
?>