-
Notifications
You must be signed in to change notification settings - Fork 1
/
Device_NetIO230.ips.php
216 lines (193 loc) · 6.85 KB
/
Device_NetIO230.ips.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
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
<?
include_once "IPSLogger.ips.php";
// variable names for username, password and interface
define("v_USERNAME", "Username");
define("v_PASSWORD", "Password");
define("v_IP", "IP");
define("v_NETIO_SETTINGS", "Settings");
define("MAX_RETRIES", 3);
/**
* Extracts and returns the login credentials for the given netIO230 device from variables with the names defined in v_USERNAME and v_PASSWORD.
*
* @author Dominik
*/
function readSettingsFromCategory($objectID) {
$usernameID = IPS_GetVariableIDByName(v_USERNAME, $objectID);
$passwordID = IPS_GetVariableIDByName(v_PASSWORD, $objectID);
$ipID = IPS_GetVariableIDByName(v_IP, $objectID);
if($usernameID == false || $passwordID == false) {
$msg = v_USERNAME." or ".v_PASSWORD." variable missing in category ".$objectID;
IPSLogger_Err(__file__, $msg);
throw new Exception($msg);
}
if($ipID == false) {
$msg = v_IP." variable missing in category ".$objectID;
IPSLogger_Err(__file__, $msg);
throw new Exception($msg);
}
$username = GetValue($usernameID);
$password = GetValue($passwordID);
$ip = GetValue($ipID);
return array(v_USERNAME => $username, v_PASSWORD => $password, v_IP => $ip);
}
/**
* Sets the status of the given portid. PortId represents the "interface" register variable of the netio230 device.
*
* @author Dominik
*/
function NetIO_setPortStatus($portID, $status) {
$parentID = IPS_GetParent($portID);
$portVarObject = IPS_GetObject($portID);
$portName = $portVarObject["ObjectName"];
$portNumber = substr($portName, 4, strlen($portName));
$moduleID = IPS_GetObjectIDByName(v_NETIO_SETTINGS, $parentID);
if($status === null) {
$oldStatus = GetValue($portID);
IPSLogger_Wrn(__file__, "Overridding power status ".$status." with ".$oldStatus);
$status = !$oldStatus;
}
$statusInt = (int) $status;
$stateStr = "";
for($i = 1; $i <= 4; $i++) {
$stateStr .= ($portNumber == $i ? $statusInt : "u");
}
if(loginAndSendCommands($moduleID, $stateStr)) {
SetValue($portID, $status);
}
}
/**
* Logins into the netio instance and sets the given parameter.
*
* @author Dominik
*/
function loginAndSendCommands($netIORegisterVariableID, $parameter) {
$settings = readSettingsFromCategory($netIORegisterVariableID);
// check if device is available at port 80
$resp = @fsockopen($settings[v_IP], 80, $errno, $errstr, 1);
if(!$resp) {
IPSLogger_Wrn(__file__, "No response from NetIO device @ ".$settings[v_IP]);
return false;
}
$basePath = "http://".$settings[v_IP]."/tgi/control.tgi?";
$result = false;
$retries = 0;
while($result == false && $retries < MAX_RETRIES) {
$loginURL = $basePath."login=p:".$settings[v_USERNAME].":".$settings[v_PASSWORD];
$response = Sys_GetURLContent($loginURL);
$result = parseResponse($response);
$parameterURL = $basePath."p=".$parameter;
$response = Sys_GetURLContent($parameterURL);
$result &= parseResponse($response, $netIORegisterVariableID);
$logoutURL = $basePath."quit=quit";
$response = Sys_GetURLContent($logoutURL);
$result &= parseResponse($response);
if(!$result) {
usleep(500000); // 500ms
}
}
return $result;
}
function parseResponse($text, $categoryID = null) {
//IPSLogger_Wrn(__file__, $text);
$webmode = false;
if(strstr($text, "<html>")) {
$webmode = true;
$text = strip_tags($text);
}
$datasets = str_split($text, 4);
if(strstr($text,"250 OK") != false) {
// request was executed successfully
return true;
} else if($datasets[0] == "250 " || preg_match("/\d \d \d \d/", $text)) {
if($webmode) {
$portStatuses = str_replace(" ", "", $text);
} else {
$portStatuses = $datasets[1];
}
$portCount = strlen($portStatuses);
if($categoryID == null) {
throw new Exception("Missing parent category id, which contains the port variables with the name \"PortX\"");
}
$parentID = IPS_GetParent($categoryID);
for($i = 1; $i <= $portCount; $i++) {
$varName = "Port".$i;
$varID = IPS_GetVariableIDByName($varName, $parentID);
if($varID == false) {
IPSLogger_Wrn(__file__, "Found ".$portCount." port statuses, but missing variable with name ".$varName);
continue;
}
$rawPortStatus = substr($portStatuses, $i - 1, 1);
if($rawPortStatus != "1" && $rawPortStatus != "0") {
throw new Exception("Found incompatible status value: ".$rawPortStatus." [Full response:".(print_r($text, true))."]");
}
SetValue($varID, $rawPortStatus == "1");
}
return true;
} else if($datasets[0] == "504 ") {
// already logged in => ignore
return true;
} else if($datasets[0] == "100 " && $datasets[1] == "HELL") {
// succesful login
//IPSLogger_Inf(__file__, "Login successful");
return true;
} else if($datasets[0] == "110 ") {
// succesful logout
//IPSLogger_Inf(__file__, "Logout successful");
return true;
} else if($datasets[0] == "550 ") {
// succesful logout
IPSLogger_Wrn(__file__, "Parameter format wrong");
return false;
} else {
IPSLogger_Wrn(__file__, "Unknown NetIO230 response: ".print_r($text, true));
return false;
}
}
if(isset($IPS_SENDER)) {
if ($IPS_SENDER == "RunScript") {
if($action == "getStatus") {
loginAndSendCommands($source, "l");
} else if($action == "poweroff") {
NetIO_setPortStatus($IPS_VARIABLE, false);
} else if($action == "poweron") {
NetIO_setPortStatus($IPS_VARIABLE, true);
} else {
// toggle
IPSLogger_Inf(__file__, "Unknown action ".$action.". Toggleing power.");
NetIO_setPortStatus($IPS_VARIABLE, !GetValue($IPS_VARIABLE));
}
}
else if($IPS_SENDER == "WebFront") {
NetIO_setPortStatus($IPS_VARIABLE, $IPS_VALUE);
} else if($IPS_SENDER == "Execute") {
// trigger installation
include_once "IPSInstaller.ips.php";
$DevicePath = 'ROOMS.LIVING';
$DeviceName = 'IP Steckdose (Weihnachten)';
$ActionScriptId = 13545;
$CategoryId = CreateCategoryPath($DevicePath);
$CategoryIdScripts = CreateCategoryPath("Scripts");
$newIOInstanceID = CreateDummyInstance($DeviceName, $CategoryId, 2000);
$netIOScriptID = CreateScript('Device_NetIO230','Device_NetIO230.ips.php', $CategoryIdScripts, 10);
// create settings
$settingsID = CreateDummyInstance(v_NETIO_SETTINGS, $newIOInstanceID, 1);
$Order = 10;
CreateVariable(v_USERNAME, 3 /*String*/, $settingsID, $Order++, "");
CreateVariable(v_PASSWORD, 3 /*String*/, $settingsID, $Order++, "");
CreateVariable(v_IP, 3 /*String*/, $settingsID, $Order++, "");
// create "get state" event
$TimerID = CreateTimer_CyclicBySeconds('Refresh State', $settingsID, 30);
IPS_SetEventScript($TimerID, "IPS_RunScriptEx(".$netIOScriptID.", array(\"action\" => \"getStatus\", \"source\" => ".$settingsID."));");
IPS_SetPosition($TimerID, $Order++);
// create the port variables
$Order = 10;
for($i = 1; $i <= 4; $i++) {
$Name = "Port".$i;
$ControlId = CreateVariable($Name, 0 /*Boolean*/, $newIOInstanceID, $Order, '~Switch', $netIOScriptID, null, 'Power');
$Order += 10;
}
} else {
IPSLogger_Wrn(__file__, "Unhandled IPS_SENDER: ".$IPS_SENDER);
}
}
?>