<?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  = "UNIT PUT " . $_POST['uuid'] . "\r\n";
    if (isset($_POST['air_address'])) {
	$cmd .= "AIR_ADDRESS," . $_POST['air_address'] . "\r\n";
	$cmd .= "AIR_IDX," . $_POST['air_idx'] . "\r\n";
    }
    if (isset($_POST['beer_address'])) {
	$cmd .= "BEER_ADDRESS," . $_POST['beer_address'] . "\r\n";
	if (isset($_POST['beer_address2']))
	    $cmd .= "BEER_ADDRESS2," . $_POST['beer_address2'] . "\r\n";
        $cmd .= "BEER_IDX," . $_POST['beer_idx'] . "\r\n";
    }
    if (isset($_POST['chiller_address'])) {
	$cmd .= "CHILLER_ADDRESS," . $_POST['chiller_address'] . "\r\n";
        $cmd .= "CHILLER_IDX," . $_POST['chiller_idx'] . "\r\n";
    }
    if (isset($_POST['heater_address'])) {
	$cmd .= "HEATER_ADDRESS," . $_POST['heater_address'] . "\r\n";
	$cmd .= "HEATER_STATE," . $_POST['heater_state'] . "\r\n";
	$cmd .= "HEATER_DELAY," . $_POST['heater_delay'] . "\r\n";
        $cmd .= "HEATER_IDX," . $_POST['heater_idx'] . "\r\n";
    }
    if (isset($_POST['cooler_address'])) {
        $cmd .= "COOLER_ADDRESS," . $_POST['cooler_address'] . "\r\n";
        $cmd .= "COOLER_STATE," . $_POST['cooler_state'] . "\r\n";
        $cmd .= "COOLER_DELAY," . $_POST['cooler_delay'] . "\r\n";
        $cmd .= "COOLER_IDX," . $_POST['cooler_idx'] . "\r\n";
    }
    if (isset($_POST['fan_address'])) {
        $cmd .= "FAN_ADDRESS," . $_POST['fan_address'] . "\r\n";
        $cmd .= "FAN_STATE," . $_POST['fan_state'] . "\r\n";
        $cmd .= "FAN_DELAY," . $_POST['fan_delay'] . "\r\n";
        $cmd .= "FAN_IDX," . $_POST['fan_idx'] . "\r\n";
    }
    if (isset($_POST['light_address'])) {
        $cmd .= "LIGHT_ADDRESS," . $_POST['light_address'] . "\r\n";
        $cmd .= "LIGHT_STATE," . $_POST['light_state'] . "\r\n";
        $cmd .= "LIGHT_DELAY," . $_POST['light_delay'] . "\r\n";
        $cmd .= "LIGHT_IDX," . $_POST['light_idx'] . "\r\n";
    }
    if (isset($_POST['door_address'])) {
        $cmd .= "DOOR_ADDRESS," . $_POST['door_address'] . "\r\n";
        $cmd .= "DOOR_IDX," . $_POST['door_idx'] . "\r\n";
    }
    if (isset($_POST['psu_address'])) {
        $cmd .= "PSU_ADDRESS," . $_POST['psu_address'] . "\r\n";
        $cmd .= "PSU_IDX," . $_POST['psu_idx'] . "\r\n";
    }
    $cmd .= "STAGE," . $_POST['stage'] . "\r\n";
    $cmd .= "MODE," . $_POST['mode'] . "\r\n";
    $cmd .= "PIDC_IMAX," . $_POST['pidc_imax'] . "\r\n";
    $cmd .= "PIDC_PGAIN," . $_POST['pidc_p'] . "\r\n";
    $cmd .= "PIDC_IGAIN," . $_POST['pidc_i'] . "\r\n";
    $cmd .= "PIDC_DGAIN," . $_POST['pidc_d'] . "\r\n";
    $cmd .= "PIDC_IDLERANGE," . $_POST['pidc_idle'] . "\r\n";
    $cmd .= "PIDH_IMAX," . $_POST['pidh_imax'] . "\r\n";
    $cmd .= "PIDH_PGAIN," . $_POST['pidh_p'] . "\r\n";
    $cmd .= "PIDH_IGAIN," . $_POST['pidh_i'] . "\r\n";
    $cmd .= "PIDH_DGAIN," . $_POST['pidh_d'] . "\r\n";
    $cmd .= "PIDH_IDLERANGE," . $_POST['pidh_idle'] . "\r\n";
    $cmd .= "TEMP_SET_MIN," . $_POST['temp_set_min'] . "\r\n";
    $cmd .= "TEMP_SET_MAX," . $_POST['temp_set_max'] . "\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'])) {

    // Name is not used for the faked beername.
    $answer = send_cmd("UNIT 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("UNIT 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("UNIT JSON");
    header("Content-type: application/json");

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

?>
