-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTC2Proxy.php
More file actions
364 lines (310 loc) · 13.7 KB
/
TC2Proxy.php
File metadata and controls
364 lines (310 loc) · 13.7 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<?php
/** Total Connect Control */
//=== CHANGE Between Dev and Production Servers ====//
define("TC2FILE", "/home/TC2/TC2Login.txt");
define("LOGFILE", "/home/TC2/TC2.log");
define("CURL_SSL_VERIFY", TRUE); // On dev server only!
//===================================================//
// ==== CHANGE THESE IF LOGIN/PASS changes ==========//
define("USERNAME", "MyTC2Username"); // This is your TotalConnect Username. Create a new user for Home Automation
define("PASSWD", "MyTC2Password"); // This is your TotalConnect password
// ==== CHANGE THESE to Secure this script with your own Login/Passwd ==========//
define("MYUSERID", "MyProxyLogin"); //Since this script provides a blanket access to your TotalConnect account. Configure the script to use a username/passwd authentication. If the browser/automation request doesn't include this username/password, the script will disconnect.
define("MYPASSWD", "MyProxyPasswd");
//===================================================//
// Stays Static. No Need to change unless TC changes on their end
define("APPID", "14588");
define("APPVER", "3.16.5");
define("LOCALE", "en-US");
define("TC2URL", "https://rs.alarmnet.com/TC21API/TC2.asmx/");
//===================================================//
// Used as Index to Store Info in TC2File
// Used as Index to Store Info in TC2File
define("SESSIONID", "SessionID");
define("LOCATIONID", "LocationID");
define("DEVICEID", "DeviceID");
// Arguments to be passed, also Index for armArray
define("ARMAWAY", "ARMAWAY");
define("AWAYBYPASS", "AWAYBYPASS");
define("ARMSTAY", "ARMSTAY");
define("STAYBYPASS", "ARMSTAYBYPASS");
define("DISARM", "DISARM");
define("STATUS", "STATUS");
// Constants used in the Multidimensional Array: armArray
define("URLCOMMAND",0);
define("ARMCODE",1);
define("ALARMSTATE",2);
$armArray = array
(
"Command" => array("URLCommand","ArmCode","AlarmStatus"),
ARMAWAY => array("ArmSecuritySystem","0","10201"),
ARMSTAY => array("ArmSecuritySystem","1","10203"),
DISARM => array("DisarmSecuritySystem","0","10200"),
);
define("USERCODE", "-1"); // User Code to Use to Arm/Disarm system
function authenticateUser() {
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Unauthorized';
return false;
}
if ( ($_SERVER['PHP_AUTH_USER'] == MYUSERID) && ($_SERVER['PHP_AUTH_PW'] == MYPASSWD))
return true;
else {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Unauthorized';
return false;
}
}
function postToTC2($postPage, $postfields) {
$url = TC2URL . $postPage;
//print "Calling URL: " . $url . "\n";
error_log("Calling URL: " . $url . "\n",3, LOGFILE);
$ch = curl_init();
//curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, CURL_SSL_VERIFY); // On dev server only!
$result = curl_exec($ch);
return $result;
}
function validateSession($sessionID) {
$postPage = "GetSessionDetails";
$data = "SessionID=" . $sessionID . "&ApplicationID=" . APPID . "&ApplicationVersion=" . APPVER;
$result = postToTC2($postPage, $data);
$xml = simplexml_load_string($result);
$resultcode = $xml->ResultCode[0];
$resultdata = $xml->ResultData[0];
if (($resultcode == "0") && ($resultdata == "Success")) {
//print "ValidateSession: Saved SessionID : $sessionID still active. Reusing" . PHP_EOL;
return true;
}
else
if ($resultcode == "-102") {
//print "ValidateSession: Saved SessionID failed. Response: $resultdata" . PHP_EOL;
return false;
}
//print "ValidateSession: unknown error. ResultCode: $resultcode ResultData: $resultdata" . PHP_EOL;
error_log("ValidateSession: unknown error. ResultCode: $resultcode ResultData: $resultdata" . PHP_EOL, 3, LOGFILE);
return false;
}
function saveLoginInfo($loginArray) {
file_put_contents (TC2FILE, json_encode($loginArray));
}
function loadLoginInfo() {
if (!file_exists(TC2FILE)) {
error_log("loadLoginInfo: Cache file doesnt exist. Need to login(): " . TC2FILE . PHP_EOL, 3, LOGFILE);
return false;
}
if (!$loadString = file_get_contents(TC2FILE)){
//print "loadLoginInfo: Could not open file: " . TC2FILE . PHP_EOL;
error_log("loadLoginInfo: Could not open file: " . TC2FILE . PHP_EOL, 3, LOGFILE);
return false;
}
$jsonArray = json_decode($loadString,true);
$loginArray = array(
SESSIONID => $jsonArray[SESSIONID],
LOCATIONID => $jsonArray[LOCATIONID],
DEVICEID => $jsonArray[DEVICEID],
);
// echo "Size: " . count($loginArray) , PHP_EOL;;
return $loginArray;
}
function login() {
if ($loginArray = loadLoginInfo()) { // Means Login File Exists. May resuse
$sessionID = $loginArray[SESSIONID];
if (validateSession($sessionID)) {
//print "login: Saved SessionID : $sessionID still active. Reusing" . PHP_EOL;
error_log("login: Saved SessionID : $sessionID still active. Reusing" . PHP_EOL, 3, LOGFILE);
return $loginArray;
}
else
//print "login: Saved SessionID : $sessionID expired. Need to login again." . PHP_EOL;
error_log("login: Saved SessionID : $sessionID expired. Need to login again." . PHP_EOL, 3, LOGFILE);
}
//else
//print "Loading File failed. Need to login 1st. \n";
//error_log("Loading File failed. Need to login 1st." . PHP_EOL, 3, LOGFILE);
$postPage = "LoginAndGetSessionDetails";
$data = "userName=" . USERNAME . "&password=" . PASSWD . "&ApplicationID=" . APPID . "&ApplicationVersion=" . APPVER . "&LocaleCode=" . LOCALE;
$result = postToTC2($postPage, $data);
error_log("Login RESULT: $result " . PHP_EOL, 3, LOGFILE);
// echo("RESULT: $result " . PHP_EOL);
// ASHTODO: Need to figure out what to do if I have error in result
$xml = simplexml_load_string($result);
$json = json_encode($xml->Locations->LocationInfoBasic->DeviceList);
$array = json_decode($json,TRUE);
$myDeviceID = "";
foreach ($array["DeviceInfoBasic"] as $device) {
if ($device["DeviceClassID"] == "1") {
//echo $device["DeviceClassID"] . $device["DeviceName"] . $device["DeviceID"] . PHP_EOL;
$myDeviceID = $device["DeviceID"];
}
}
$loginArray = array(
SESSIONID => $xml->SessionID[0]->__toString(),
LOCATIONID => $xml->Locations->LocationInfoBasic->LocationID[0]->__toString(),
DEVICEID => $myDeviceID,
);
saveLoginInfo($loginArray);
return $loginArray;
}
function armDisarm($command, $armArray) {
$loginArray = login();
$postPage = $armArray[URLCOMMAND];
$data = "SessionID=" . $loginArray[SESSIONID] . "&LocationID=" . $loginArray[LOCATIONID] . "&DeviceID=" . $loginArray[DEVICEID] . "&UserCode=" . USERCODE;
if (($command == ARMAWAY) || ($command == ARMSTAY)) {
$data = $data . "&ArmType=" . $armArray[ARMCODE];
}
$result = postToTC2($postPage, $data);
$xml = simplexml_load_string($result);
$resultCode = $xml->ResultCode[0]->__toString();
$resultData = $xml->ResultData[0]->__toString();
//echo "ARMDISARM: ResultCode: $resultCode Data: $resultData \n";
error_log("ARMDISARM: ResultCode: $resultCode Data: $resultData" . PHP_EOL, 3, LOGFILE);
//Poll for Status Completion of the command
while (($resultCode == "4500") || ($resultCode == "4501")){ // 4500=Command Session Initiated. 4501=Command Scheduled/Working 0=Command Completed
sleep(2);
$postPage = "CheckSecurityPanelLastCommandState";
$data = "SessionID=" . $loginArray[SESSIONID] . "&LocationID=" . $loginArray[LOCATIONID] . "&DeviceID=" . $loginArray[DEVICEID] . "&CommandCode=-1";
$result = postToTC2($postPage, $data);
$xml = simplexml_load_string($result);
$resultCode = $xml->ResultCode[0]->__toString();
$resultData = $xml->ResultData[0]->__toString();
//echo "ARMDISARM: ResultCode: $resultCode Data: $resultData \n";
error_log("ARMDISARM: ResultCode: $resultCode Data: $resultData" . PHP_EOL, 3, LOGFILE);
}
if ($resultCode == "0") {
//Check Metadata that current state is ArmAway/Stay/Disarm
$postPage = "GetPanelMetaDataAndFullStatus";
$data = "SessionID=" . $loginArray[SESSIONID] . "&LocationID=" . $loginArray[LOCATIONID] . "&LastSequenceNumber=0&LastUpdatedTimestampTicks=0&PartitionID=1";
$result = postToTC2($postPage, $data);
$xml = simplexml_load_string($result);
$alarmState = $xml->PanelMetadataAndStatus->Partitions->PartitionInfo->ArmingState[0]->__toString();
//echo "AlarmState: $alarmState \n";
error_log("AlarmState: $alarmState" . PHP_EOL, 3, LOGFILE);
if ($alarmState == $armArray[ALARMSTATE]) {
// echo "Alarm Successful: $command Current State: $alarmState \n";
error_log("Alarm Successful: $command Current State: $alarmState" . PHP_EOL, 3, LOGFILE);
return true;
}
else
//echo "ERROR !!! ERROR !!! Alarm ARM/DISARM Failed: $command Current State: $alarmState \n";
error_log("ERROR !!! ERROR !!! Alarm ARM/DISARM Failed: $command Current State: $alarmState" . PHP_EOL, 3, LOGFILE);
}
else
//echo "ERROR !!! ERROR !!! Alarm ARM/DISARM Failed: $resultCode Data: $resultData \n";
error_log("ERROR !!! ERROR !!! Alarm ARM/DISARM Failed: $resultCode Data: $resultData" . PHP_EOL, 3, LOGFILE);
return false;
}
function systemStatus($armArray, $loginArray) {
$systemStatusArray = array(
'10200' => DISARM,
'10201' => ARMAWAY,
'10202' => AWAYBYPASS, // This is Actually ARMAWAY_BYPASS
'10203' => ARMSTAY,
'10204' => STAYBYPASS,
'10205' => ARMAWAY,
);
if (is_null($loginArray))
$loginArray = login();
$postPage = "GetPanelMetaDataAndFullStatus";
$data = "SessionID=" . $loginArray[SESSIONID] . "&LocationID=" . $loginArray[LOCATIONID] . "&LastSequenceNumber=0&LastUpdatedTimestampTicks=0&PartitionID=1";
$result = postToTC2($postPage, $data);
$xml = simplexml_load_string($result);
$alarmState = $xml->PanelMetadataAndStatus->Partitions->PartitionInfo->ArmingState[0]->__toString();
//echo "AlarmState: " . $alarmState . " = " . $systemStatusArray[$alarmState] . PHP_EOL;
return $systemStatusArray[$alarmState];
/*
foreach ($armArray as $command => $state)
if ($state[ALARMSTATE] == $alarmState) {
// print "My Status is: $alarmState = " . $command . PHP_EOL;
error_log("My Status is: $alarmState = " . $command . PHP_EOL, 3, LOGFILE);
return $command;
}
echo "STATUS ERROR: Unknown Status of the Panel. Status: $alarmState \n";
error_log("STATUS ERROR: Unknown Status of the Panel. Status: $alarmState" . PHP_EOL, 3, LOGFILE);
return $alarmState;
*/
}
function zoneStatus($loginArray) {
// Zone status Information is below
// 0 – Normal
// 1 – Bypassed
// 2 – Faulted
// 8 – Trouble
// 16 – Tampered
// 32 – Supervision Failed
$zoneStatusArray = array(
'0' => 'CLOSED',
'1' => 'BYPASS',
'2' => 'OPEN',
'8' => 'TROUBLE',
'16' => 'TAMPER',
'32' => 'SUPER_FAIL',
);
$postPage = "GetZonesListInStateEx";
$data = "SessionID=" . $loginArray[SESSIONID] . "&LocationID=" . $loginArray[LOCATIONID] . "&PartitionID=1&ListIdentifierID=0";
$result = postToTC2($postPage, $data);
$xml = simplexml_load_string($result);
var_dump($xml);
// error_log($xml, 0);
echo "";
$zoneStatus = "";
foreach($xml->ZoneStatus->Zones->ZoneStatusInfoEx as $zone) {
$zoneStatus .= "Zone" . $zone['ZoneID'] . "=" . $zoneStatusArray[$zone['ZoneStatus']->__toString()] . ";";
}
return $zoneStatus;
}
function status($armArray) {
// In OpenHAB, Use following Regex to Parse:
// REGEX(.*SysStatus=([a-zA-Z]+))
// REGEX(.*Zone2=([a-zA-Z_]+))
// REGEX(.*Zone46=([a-zA-Z_]+))
$loginArray = login();
$status = "SysStatus=" . systemStatus($armArray, $loginArray) . ";";
$status .= zoneStatus($loginArray);
return $status;
}
// ==========================================================================================
// Main Class starts here
// ==========================================================================================
if (!authenticateUser())
exit(false);
if (count($_GET) < 1) {
print "Usage: http://localhost/TC2Proxy?action=[ARMSTAY, ARMAWAY, DISARM, STATUS, SYSSTATUS]\n\n";
exit(false);
}
if (!isset($_GET['action'])) {
print "Usage: http://localhost/TC2Proxy?action=[ARMSTAY, ARMAWAY, DISARM, STATUS, SYSSTATUS]\n\n";
exit(false);
}
$command = $_GET['action'];
switch ($command) {
case "DISARM":
case "ARMSTAY":
case "ARMAWAY":
$success = armDisarm($command, $armArray[$command]);
print $success . PHP_EOL;
exit(0);
break;
case "STATUS":
$mystatus = status($armArray);
print $mystatus . PHP_EOL;
exit(0);
break;
case "SYSSTATUS":
$mystatus = systemStatus($armArray, null);
print $mystatus . PHP_EOL;
exit(0);
break;
default:
print "Usage: http://localhost/TC2Proxy?action=[ARMSTAY, ARMAWAY, DISARM, STATUS]\n\n";
exit(false);
}
?>