<?php
    // header('Content-Type: application/json');
    // header("Access-Control-Allow-Origin: *");

    header('Content-Type: application/json');
    header("Access-Control-Allow-Origin: *");
    // $message = $_GET["v"];  // get internal sensor id from js

    if(!($sock = socket_create(AF_UNIX, SOCK_STREAM, 0)))
    {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Couldn't create socket: [$errorcode] $errormsg \n");
    }

    echo "Socket created";

    if(!socket_connect($sock , '/var/www/html/server.sock'))
    {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);
        die("Could not connect: [$errorcode] $errormsg \n");
    }

    echo "Connection established \n";

    // $message = $argv[1];
    // $message = $value;


    // echo "Message send successfully \n";

    // Now receive reply from server
    if(socket_recv( $sock , $buf , 1024, MSG_WAITALL ) === FALSE)
    {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);
        die("Could not receive data: [$errorcode] $errormsg \n");
    }

    echo "Message received \n";


    print json_encode($buf);
    // print the received message
    // var_dump($buf);

    socket_close($sock);

    // $dataWindow = array();