Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
lamps.php
<?php
$offafter=15*60;
$loop=60;
$lightstatus = array();
$lightstatus[0]='off';
$lightstatus[1]='on';
function lampset($state) {
global $lightstatus;
for ($i=13; $i<=15;$i++) {
system("snmpset -v1 -c XXXXXXXXXX NETCONTROLIP .1.3.6.1.4.1.19865.2.3.1.$i.6.0 i $state >/dev/null 2>/dev/null");
}
echo "Lamps set to ".$lightstatus[$state]."\n";
system('/usr/bin/twidge -c /home/vasil/.twidgerc update "initLab street lights turned '.$lightstatus[$state].' at `date +%H:%M`"');
}
function getstate() {
$state = array();
$state['sun']=`/usr/local/bin/sunpos -D 42.70N 23.33E `;
$state['lamps']=`snmpget -v1 -c public NETCONTROLIP .1.3.6.1.4.1.19865.2.3.1.15.6.0|awk '{print $4}'| tr -d '\n'`;
$doorjs=file_get_contents('http://DOORPI/status?key=XXXXXXXXXXX');
$doorstat=json_decode($doorjs);
$state['door']=$doorstat->latch;
if (file_exists('/tmp/lamptrigger')) {
// unlink('/tmp/lamptrigger');
// the trigger needs to be removed by cron
$state['trigger']=1;
} else {
$state['trigger']=0;
}
return $state;
}
function printstate($state) {
global $lightstatus;
echo "S ".time()." sun: ".$state['sun']." lights:".$lightstatus[$state['lamps']]." door:".$state['door']." trigger:".$state['trigger']."\n";
}
$doorstatechg=0;
$sn="/tmp/lamper";
$sock=socket_create(AF_UNIX, SOCK_DGRAM, 0);
@unlink($sn);
socket_bind($sock, $sn);
chmod($sn, 0777);
socket_set_nonblock($sock);
while(42) {
$r = array($sock);
$w = NULL;
$e = NULL;
socket_select($r, $w, $e, $loop);
socket_recvfrom($sock, $buff, 1024, 0, $src);
if (!empty($state)) $oldstate=$state;
$state = getstate();
$now=time();
printstate($state);
if (!empty($oldstate)) {
if ($state['door']!=$oldstate['door']) $doorstatechg=$now;
}
if ($state['lamps']=='1') {
// Lamps are on
if ($state['sun']=='day' && $state['trigger']==0) {
// It's daytime,the lamps are on and there is not trigger, turn them off
lampset(0);
continue;
}
if ($state['sun']=='night' && $state['door']=='locked' && $state['trigger']==0 && $now-$doorstatechg>($offafter)) {
// There's nobody in the lab (it was locked more than 15 minutes ago) and no trigger, turn the lamps off
lampset(0);
continue;
}
} else {
// Lamps are off
if ($state['trigger']==1) {
// unconditional turn on if the trigger is set
lampset(1);
continue;
}
if ($state['sun']=='night' && $state['door']=='unlocked') {
// it's night and there are people in the lab
lampset(1);
continue;
}
}
}
@miglen

This comment has been minimized.

Copy link

commented Feb 19, 2016

while(42)!! 👍

@peycho

This comment has been minimized.

Copy link

commented Apr 5, 2016

What the heck... Why PHP? ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.