forked from angrychimp/php-myq-liftmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
use_MyQ_api.php
executable file
·133 lines (106 loc) · 2.89 KB
/
use_MyQ_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/php
<?php
function get_help() {
global $argv;
$program = basename($argv[0]);
print "Send commands to MyQ enabled devices on your network.\n";
print "usage: {$program} <open|close|on|off|status|help> [name=\"device name\"|id=#]\n";
print "\tDefaults to showing the status results.\n\n";
print "\tUse open/close for garage door units.\n";
print "\tUse on/off for lamp/light modues.\n";
}
function gmt_to_local_time($date) {
if(is_numeric($date))
$date = date("Y-m-d H:i:s",$date);
$dt = new DateTime($date, new DateTimeZone('UTC'));
$dt->setTimezone(new DateTimeZone('America/Chicago'));
return $dt->format('Y-m-d H:i:s');
}
$action = 'status';
$doorname = 'light';
$config_file = "config.ini";
function report($MyQ,$name=false) {
#print_r($MyQ->refresh());exit;
foreach($MyQ->refresh()->_myDevices as $device_type => $atom) {
foreach($atom as $id => $thisone) {
#print_r($thisone);
if($device_type == 'GarageDoorOpener')
$device_type = str_pad("Garage",8);
elseif($device_type == 'LampModule')
$device_type = str_pad("Light",8);
else
$device_type = str_pad($device_type,8);
if( (($name) && ($name == $thisone['desc']))
||
(($name) && ($name == $thisone['MyQDeviceId']))
||
(!$name)
) {
$time = round(($thisone['deviceState']['timestamp']) / 1000);
$time = gmt_to_local_time(intval($time));
$time = date("M d, g:i a",strtotime($time));
$myDeviceId = "ID:" . str_pad($thisone['MyQDeviceId'],8);
$desc = str_pad("({$thisone['desc']})",18);
$deviceState = str_pad(ucfirst($thisone['deviceState']['state']),8);
print "\t{$device_type} - {$myDeviceId} - {$desc} - {$deviceState} - {$time}\n";
}
}
#print_r($MyQ->_headers);
}
};
require_once('MyQ.php');
if(!file_exists($config_file)) {
die("Error finding: {$config_file}\n");
}
$conf = parse_ini_file($config_file);
if(empty($conf['username'])) {
die("Error: missing config.ini variable: username\n");
}
if(empty($conf['password'])) {
die("Error: missing config.ini variable: password\n");
}
$name = false;
foreach($argv as $thisone) {
if(strstr($thisone,'name=')) {
$name = str_replace("name=","",$thisone);
} elseif(strstr($thisone,'id=')) {
$name = str_replace("id=","",$thisone);
} else {
if(substr($thisone,0,2) == '--')
$thisone = substr($thisone,2);
elseif(substr($thisone,0,1) == '-')
$thisone = substr($thisone,1);
switch($thisone) {
case 'status':
case 'open':
case 'close':
case 'on':
case 'off':
case 'of':
$action = $thisone;
break;
case "h":
case "help":
case "help":
case "?":
die(get_help());
}
}
}
$MyQ = new MyQ();
$MyQ->login($conf['username'], $conf['password']);
try {
switch($action) {
case 'on':
case 'off':
case 'open':
case 'close':
$MyQ->$action($name);
break;
default:
$RC = report($MyQ,$name);
break;
}
} catch(Exception $e) {
die($e->getMessage() . "\n");
}