www/drop_switches.php

Wed, 01 May 2024 14:38:37 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 01 May 2024 14:38:37 +0200
changeset 715
f5d85af156ab
parent 708
13555c27b592
permissions
-rw-r--r--

Added device_present() function to easy update device present from one-wire and simulator devices. When a simulator temperature sensor present is changed, the device table is changed too. Controlling simulator relays is now for each simulator. The simulator runs under the state machine. If something changed in the running simulator, all data is broadcasted over websocket. Completed the web editor.

<?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));
}

$answer = send_cmd("DEVICES JSON");

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

    /*
     * Build reply with only temperature sensors.
     */
    $sensors[] = array(
	'uuid' => '',
	'name' => 'Not Assigned'
    );

    $rows = json_decode($arr[1], true);

    foreach($rows as $item) {
	if ($item['direction'] == "OUT_BIN") {
	    $sensors[] = array(
		'uuid' => $item['uuid'],
		'name' => $item['address']." ".$item['comment']
	    );
	}
    }
    header("Content-type: application/json");
    exit(json_encode($sensors));
}

header("Content-type: application/json");
echo '{}';

?>

mercurial