Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

$buffer_length = strlen("PLUM 8888 12345678-90ab-cdef-1234-567890abcdef 8443");//Set the buffer length to what we expect to receive from a lightpad.

$amqp_port = 2708;
$stream_port = 2708;

//-----------------------Plum Website Config-----------------------//
$user_agent_header = 'User-Agent';//User agent header.
Expand All @@ -75,4 +75,4 @@
$plum_room_url = $plum_url.'getRoom'; //Url of plum to obtain array of room info.
$plum_logical_load_url = $plum_url.'getLogicalLoad'; //Url of plum to obtain array of logical load info.
$plum_lightpad_url = $plum_url.'getLightpad'; //Url of plum to obtain array of lightpad info.
?>
?>
18 changes: 9 additions & 9 deletions plum_lightpad_stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
function lightpad_motion($lightpad_information, $signal)//Triggered when motion is detected by a switch.
{
echo "Motion Detected: [".$lightpad_information['house_name']." - ".$lightpad_information['room_name']." - ".$lightpad_information['logical_load_name']."]\n";

//Example - If motion detected on downstairs or theatre switches, toggle the lights.
//if($lightpad_information['logical_load_name'] == 'Downstairs' || $lightpad_information['logical_load_name'] == 'Theatre')
//{
Expand Down Expand Up @@ -49,7 +49,7 @@ function lightpad_power($lightpad_information, $watts)//Triggered when the watt
foreach($lightpad_config as $lightpad_id => $lightpad_info)//For every lightpad in the config.
{
$sockets[$lightpad_id] = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);//Create a new TCP socket.

if ($sockets[$lightpad_id] === false)//Check to make sure it was created correctly.
{
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";//Spit out an error if not.
Expand All @@ -58,8 +58,8 @@ function lightpad_power($lightpad_information, $watts)//Triggered when the watt
{
echo "Socket For: ".$lightpad_info->ip." Created.\n";//Or claim success.
}
$connect_result = socket_connect($sockets[$lightpad_id], $lightpad_info->ip, $amqp_port);//Connect the socket to the amqp port.

$connect_result = socket_connect($sockets[$lightpad_id], $lightpad_info->ip, $stream_port);//Connect the socket to the stream port.
if ($connect_result === false)//Check to make sure it connected.
{
echo "socket_connect() failed.\nReason: ($connect_result) " . socket_strerror(socket_last_error($socket)) . "\n";//Spit out an error if not.
Expand All @@ -68,7 +68,7 @@ function lightpad_power($lightpad_information, $watts)//Triggered when the watt
{
echo "Socket For: ".$lightpad_info->ip." Connected.\n";//Or claim success.
}

socket_set_nonblock($sockets[$lightpad_id]);//Set non blocking of the socket so we can read without waiting when something does change.
}

Expand All @@ -80,7 +80,7 @@ function lightpad_power($lightpad_information, $watts)//Triggered when the watt
$except = null;
$timeout = null;
$buffer = 2048;//Leave room for a lot of messages, max is usually at most 3.

if(socket_select($read, $write, $except, $timeout) > 0);//Monitor all the connections to lightpads and wait until one has sent us a message.
{
foreach($sockets as $lightpad_id => $socket)//Check every lightpad.
Expand All @@ -89,11 +89,11 @@ function lightpad_power($lightpad_information, $watts)//Triggered when the watt
if($message)//If there is a message...
{
$message = str_replace("\n", '', $message);//Remove newlines.
$message = str_replace("\r", '', $message);//Remove carriage returns.
$message = str_replace("\r", '', $message);//Remove carriage returns.
$updates = explode(".", $message);//Set up an array and split on .
array_pop($updates);//Get rid of the last empty entry in the array.
$lightpad_information = lightpad_information($lightpad_id);//Pull all the information for this lightpad from the config.

foreach($updates as $key => $update)//For every message from the lightpad
{
$data = json_decode($update);//Turn it into an object.
Expand Down Expand Up @@ -128,4 +128,4 @@ function lightpad_power($lightpad_information, $watts)//Triggered when the watt
socket_close($socket);
echo "OK.\n\n";
}
?>
?>