<?php

require_once 'classes/ConDB.php';
require_once 'classes/getErrorMsg.php';

header('Content-type: application/json');
if (isset($_REQUEST['connect_submit'])) {
    $db = new ConDB();

	$request = $_REQUEST;

    
    extract($request);
    $inloop = false;
    if ($device_id == '') {
        $msg = new getErrorMsg('7');
        $inloop = true;
    }

    if ($inloop) {
        $json = array('statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
        echo json_encode($json);
        return false;
          
    } else {

        $checkDevQry = "select id from device_table where device_id = '" . $device_id . "' and active = '1'";

        $checkDevRes = mysql_query($checkDevQry, $db->conn);

		if (mysql_num_rows($checkDevRes) == 1) {
            $devRow = mysql_fetch_assoc($checkDevRes);
                $update_dev_qry = "update device_table set connection_state = '1' where device_id='" . $device_id ."'";

                mysql_query($update_dev_qry, $db->conn);
                $update_rows = mysql_affected_rows();
                if ($update_rows >= 0) {
                    $msg = new getErrorMsg('0');
                    $json = array('DeviceId' => $devRow['device_id'], 'statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
                    echo json_encode($json);
                    return false;
                } else {
                    $msg = new getErrorMsg('29');
                    $json = array('statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
                    echo json_encode($json);
                    return false;
                }
        } else {
            $msg = new getErrorMsg('12');
            $json = array('statusMessage' => $msg->errMsg, 'statusNumber' => $msg->errNo);
            echo json_encode($json);
            return false;
        }
    }
}
?>