<?php


function open_socket()
{
    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

    if (!($sock === false)) {
        if (socket_connect($sock, "localhost", 6554)) {
            socket_set_option($sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 15, 'usec' => 0));
        } else {
            socket_close($sock);
        }
    }
    return $sock;
}


/**
 * @param string $command to send to the server.
 * @return string with the complete reply from the
 *         server. This can be a multiline reply.
 */
function send_cmd($command)
{
    $sock = open_socket();
    if ($sock == false) {
        return "";
    }
    socket_write($sock, $command . "\r\n", 4096);

    $answer = "";
    while (1) {
        $line = socket_read($sock, 4096);
        if ($line === '')
            break;
        $answer .= $line;
    }
    socket_close($sock);

    return $answer;
}


function startsWith($haystack, $needle)
{
    return !strncmp($haystack, $needle, strlen($needle));
}


$response = array(
   'error' => false,
   'msg' => 'Ok',
);


if (isset($_POST['update'])) {

    $cmd  = "SIMULATOR PUT " . $_POST['uuid'] . "\r\n";
    $cmd .= "NAME," . $_POST['name'] . "\r\n";
    $cmd .= "VOLUME_AIR," . $_POST['volume_air'] . "\r\n";
    $cmd .= "VOLUME_BEER," . $_POST['volume_beer'] . "\r\n";
    $cmd .= "ROOM_TEMPERATURE," . $_POST['room_temperature'] . "\r\n";
    $cmd .= "ROOM_HUMIDITY," . $_POST['room_humidity'] . "\r\n";
    $cmd .= "AIR_PRESENT," . $_POST['air_present'] . "\r\n";
    $cmd .= "BEER_PRESENT," . $_POST['beer_present'] . "\r\n";
    $cmd .= "BEER_PRESENT2," . $_POST['beer_present2'] . "\r\n";
    $cmd .= "CHILLER_PRESENT," . $_POST['chiller_present'] . "\r\n";
    $cmd .= "COOLER_TEMP," . $_POST['cooler_temp'] . "\r\n";
    $cmd .= "COOLER_TIME," . $_POST['cooler_time'] . "\r\n";
    $cmd .= "COOLER_SIZE," . $_POST['cooler_size'] . "\r\n";
    $cmd .= "COOLER_PRESENT," . $_POST['cooler_present'] . "\r\n";
    $cmd .= "COOLER_POWER," . $_POST['cooler_power'] . "\r\n";
    $cmd .= "HEATER_TEMP," . $_POST['heater_temp'] . "\r\n";
    $cmd .= "HEATER_TIME," . $_POST['heater_time'] . "\r\n";
    $cmd .= "HEATER_SIZE," . $_POST['heater_size'] . "\r\n";
    $cmd .= "HEATER_PRESENT," . $_POST['heater_present'] . "\r\n";
    $cmd .= "HEATER_POWER," . $_POST['heater_power'] . "\r\n";
    $cmd .= "FAN_PRESENT," . $_POST['fan_present'] . "\r\n";
    $cmd .= "FAN_POWER," . $_POST['fan_power'] . "\r\n";
    $cmd .= "LIGHT_PRESENT," . $_POST['light_present'] . "\r\n";
    $cmd .= "LIGHT_POWER," . $_POST['light_power'] . "\r\n";
    $cmd .= "DOOR_PRESENT," . $_POST['door_present'] . "\r\n";
    $cmd .= "DOOR_VALUE," . $_POST['door_value'] . "\r\n";
    $cmd .= "PSU_PRESENT," . $_POST['psu_present'] . "\r\n";
    $cmd .= "PSU_VALUE," . $_POST['psu_value'] . "\r\n";
    $cmd .= "FRIGO_ISOLATION," . $_POST['frigo_isolation'] . "\r\n";
    $cmd .= ".";
    $answer = send_cmd($cmd);
    $arr = explode("\r\n", $answer);
    if (! startsWith($arr[0], "219")) {
	$response['error'] = true;
	$response['msg'] = $arr[0];
    }
    exit(json_encode($response));

} else if (isset($_POST['add'])) {

    $answer = send_cmd("SIMULATOR ADD " . $_POST['name']);
    $arr = explode("\r\n", $answer);
    if (! startsWith($arr[0], "211")) {
	$response['error'] = true;
	$response['msg'] = $arr[0];
    }
    exit(json_encode($response));

} else if (isset($_POST['del'])) {

    $answer = send_cmd("SIMULATOR DEL " . $_POST['uuid']);
    $arr = explode("\r\n", $answer);
    if (! startsWith($arr[0], "211")) {
        $response['error'] = true;
        $response['msg'] = $arr[0];
    }
    exit(json_encode($response));

} else {

    $answer = send_cmd("SIMULATOR JSON");
    header("Content-type: application/json");

    $arr = explode("\r\n", $answer);
    if (startsWith($arr[0], "212")) {
    	echo $arr[1];
    } else {
        echo '{}';
    }
}

?>
