aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Bottomley <James.Bottomley@HansenPartnership.com>2019-01-04 15:30:43 -0800
committerJames Bottomley <James.Bottomley@HansenPartnership.com>2019-01-04 15:43:50 -0800
commit82ae648a1fccca9db914e1c0c35aa6679283d902 (patch)
tree1fadc6c81580441364f5a3428700894768077b53
parent93a00391140f342d8a86c5b4c07c28b46f5e49fa (diff)
downloadasterisk-aastra-82ae648a1fccca9db914e1c0c35aa6679283d902.tar.gz
lighting: add new function
Add functionality to control homeseer based lighting using json Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
-rw-r--r--Makefile10
-rw-r--r--etc/lighting.ini22
-rw-r--r--include/Homeseer.class.php89
-rw-r--r--include/Lighting.class.php76
-rw-r--r--lighting.php6
5 files changed, 203 insertions, 0 deletions
diff --git a/Makefile b/Makefile
index db85bfe..ad35476 100644
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,7 @@ prefix := /usr/local
php_prefix := $(prefix)/php
web_prefix := $(prefix)/var/www/asterisk-aastra
doc_prefix := $(prefix)/usr/share/doc/asterisk-aastra
+etc_prefix := /etc/asterisk
base-files = \
@@ -60,6 +61,10 @@ voicemail-files = \
voicemail-doc-files = \
etc/extensions-voicemail.conf
+lighting-files = \
+ include/Lighting.class.php \
+ include/Homeseer.class.php \
+
packages := blacklist actionuri
clean:
@@ -98,3 +103,8 @@ install-voicemail: install-php install-web install-doc
install -m 0644 $(voicemail-files) $(php_prefix)
install -m 0644 voicemail.php $(web_prefix)
install -m 0644 $(voicemail-doc-files) $(doc_prefix)
+
+install-lighting: install-php install-web install-doc
+ install -m 0644 $(lighting-files) $(php_prefix)
+ install -m 0644 lighting.php $(web_prefix)
+ install -m 0644 etc/lighting.ini $(etc_prefix)
diff --git a/etc/lighting.ini b/etc/lighting.ini
new file mode 100644
index 0000000..01153fa
--- /dev/null
+++ b/etc/lighting.ini
@@ -0,0 +1,22 @@
+; ini file to tell the lighting class how to configure
+[homeseer]
+; You must configure url, user and password below. It is recommended you
+; set up a separate user identity in the homeseer for this script
+;
+; url = <url of the homeseer device>
+; user = <username of control user>
+; password = <password of control user>
+;
+; Now set up a map which contains the reference numbers of all the
+; individual lights
+; map[My Office] = 69
+;
+; Multiply controlled lights may appear in multi
+; multi[] = Standing Lamps
+; Standing Lamps[] = 55
+; Standing Lamps[] = 44
+;
+; Now each extension needs to be told what lights they can show
+; [4455]
+; lamps[] = My Office
+; lamps[] = Standing Lamps
diff --git a/include/Homeseer.class.php b/include/Homeseer.class.php
new file mode 100644
index 0000000..a2c403a
--- /dev/null
+++ b/include/Homeseer.class.php
@@ -0,0 +1,89 @@
+<?php
+
+
+class Homeseer {
+ private $baseurl;
+ private $user;
+ private $passwd;
+ private $ch;
+ private $map;
+
+ function __construct() {
+ $this->ch = curl_init();
+ curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
+ curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
+ }
+
+ private function get_url($url) {
+ curl_setopt($this->ch, CURLOPT_URL,
+ $this->baseurl.$url);
+ $json = curl_exec($this->ch);
+ $err = curl_error($this->ch);
+ if ($err) {
+ throw new Exception($err);
+ }
+ return $json;
+ }
+
+ private function get_ref($dev) {
+ $ref = $this->map[$dev];
+
+ if (!isset($ref))
+ throw new Exception('Unknown Light: '.$dev);
+
+ if (is_array($ref))
+ return $ref;
+
+ return [ $ref ];
+ }
+
+ function set_config($conf) {
+ if (!isset($conf['url']))
+ throw new Exception('No url set in config');
+ if (!isset($conf['user']))
+ throw new Exception('No user set in config');
+ if (!isset($conf['password']))
+ throw new Exception('No password set in config');
+ if (!isset($conf['map']))
+ throw new Exception('No map set in config');
+ $this->baseurl = $conf['url'];
+ curl_setopt($this->ch, CURLOPT_USERPWD, $conf['user'].":".$conf['password']);
+ $this->map = $conf['map'];
+ if (isset($conf['multi'])) {
+ foreach ($conf['multi'] as $m) {
+ if (!isset($conf[$m]))
+ throw new Exception ('Homeseer config is missing multi '.$m.' definition');
+ $this->map[$m] = $conf[$m];
+ }
+ }
+ }
+
+ private function get_status_ref($ref) {
+ $json = $this->get_url('/JSON?request=getstatus&ref='.$ref);
+ $obj = json_decode($json);
+ $val = $obj->{'Devices'}[0]->{'value'};
+ if (!isset($val))
+ throw new Exception('Failed to get homeseer data for '.$dev);
+ return $val;
+ }
+
+ function get_status($dev) {
+ $ref = $this->get_ref($dev);
+ $val = 0;
+ foreach ($ref as $r) {
+ $val += $this->get_status_ref($r);
+ }
+ return $val;
+ }
+
+ function set_status($dev, $val) {
+ $ref = $this->get_ref($dev);
+ foreach ($ref as $r) {
+ $this->get_url('/JSON?request=controldevicebyvalue&ref='.$r."&value=".$val);
+ }
+ }
+
+ function is_mapped($dev) {
+ return isset($this->map[$dev]);
+ }
+}
diff --git a/include/Lighting.class.php b/include/Lighting.class.php
new file mode 100644
index 0000000..a645a2d
--- /dev/null
+++ b/include/Lighting.class.php
@@ -0,0 +1,76 @@
+<?php
+require_once('BaseAastra.class.php');
+require_once('VoicemailListManager.class.php');
+require_once('AastraIPPhoneExecute.class.php');
+require_once('Homeseer.class.php');
+
+class Lighting extends BaseAastra {
+
+ public $hs;
+
+ const on = 'Icon:CircleGreen';
+ const off = 'Icon:CircleBlue';
+ const conffile = '/etc/asterisk/lighting.ini';
+
+ private $lights;
+ private $allowed;
+
+ function __construct() {
+ parent::__construct();
+ $this->title = 'Lighting Control';
+ $this->hs = new Homeseer();
+ $this->lights = array();
+ $conf = parse_ini_file(self::conffile, TRUE);
+ $this->hs->set_config($conf['homeseer']);
+ $this->lights = $conf[$this->user]['lights'];
+ $this->allowed = array_flip($this->lights);
+ }
+
+ function get_icon($val) {
+ $onoff = $this->hs->get_status($val);
+
+ if ($onoff) {
+ return self::on;
+ }
+ return self::off;
+ }
+
+ private function _render() {
+ $this->displayObject(new TextScreen());
+
+ for ($i = 0; $i < sizeof($this->lights); $i++) {
+ $val = $this->lights[$i];
+ if (!$this->hs->is_mapped($val))
+ throw new Exception('Undefined light '.$val);
+ $this->do->addSoftkey($i+1, $val, $this->url.'?action=toggle&dev='.$val, $this->get_icon($val));
+ }
+ }
+
+ function start() {
+ $this->_render();
+ }
+
+ function toggle() {
+ $dev = $_GET['dev'];
+ if (!isset($this->allowed[$dev])) {
+ throw new Exception('Unknown Device '.$dev);
+ }
+ $onoff = $this->hs->get_status($dev);
+
+ if ($onoff == 0) {
+ $this->hs->set_status($dev, 99);
+ $this->hs->set_status($dev, 255);
+ } else {
+ $this->hs->set_status($dev, 0);
+ }
+
+ $this->_render();
+ }
+
+ function init() {
+ parent::init();
+
+ $this->oldstat[69] = 99;
+ }
+}
+?>
diff --git a/lighting.php b/lighting.php
new file mode 100644
index 0000000..c783ebe
--- /dev/null
+++ b/lighting.php
@@ -0,0 +1,6 @@
+<?php
+ require_once('asterisk-aastra/Lighting.class.php');
+ $c = new Lighting();
+
+ $c->init();
+?>