-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.php
More file actions
179 lines (163 loc) · 8.69 KB
/
Copy pathmodule.php
File metadata and controls
179 lines (163 loc) · 8.69 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
<?php
declare(strict_types=1);
class UnifiClient extends IPSModuleStrict
{
private const COLOR_GREEN = 1692672;
private const COLOR_RED = 16077123;
public function GetCompatibleParents(): string
{
return json_encode([ 'type' => 'connect', 'moduleIDs' => [ '{A792D3EC-FEC5-A8E6-F792-E141097C6AB0}' ] ]);
}
public function Create(): void
{
//Never delete this line!
parent::Create();
$this->RegisterPropertyString('ID', '');
$this->RegisterPropertyInteger('Timer', 0);
$this->RegisterPropertyBoolean("MACAnzeigen", 0);
$this->RegisterPropertyBoolean("IDAnzeigen", 0);
$this->RegisterTimer('Collect Data', 0, "UNIFICL_Send(\$_IPS['TARGET'],'getClientData','');");
}
public function Destroy(): void
{
//Never delete this line!
parent::Destroy();
}
public function ApplyChanges(): void
{
//Never delete this line!
parent::ApplyChanges();
$vpos = 100;
$this->MaintainVariable( 'ClientName', $this->Translate( 'Client Name' ), 3, [ 'PRESENTATION' => VARIABLE_PRESENTATION_VALUE_PRESENTATION, 'USAGE_TYPE'=> 0 ,'ICON'=> 'circle-info'], $vpos++, 1 );
$this->MaintainVariable( 'ID', $this->Translate( 'Client ID' ), 3, [ 'PRESENTATION' => VARIABLE_PRESENTATION_VALUE_PRESENTATION, 'USAGE_TYPE'=> 0 ,'ICON'=> 'circle-info'], $vpos++, $this->ReadPropertyBoolean("IDAnzeigen") );
$this->MaintainVariable( 'ClientType', $this->Translate( 'Client Type' ), 3, [ 'PRESENTATION' => VARIABLE_PRESENTATION_VALUE_PRESENTATION, 'USAGE_TYPE'=> 0 ,'ICON'=> 'circle-info'], $vpos++, 1 );
$this->MaintainVariable( 'ClientIP', $this->Translate( 'Client IP' ), 3, [ 'PRESENTATION' => VARIABLE_PRESENTATION_VALUE_PRESENTATION, 'USAGE_TYPE'=> 0 ,'ICON'=> 'circle-info'], $vpos++, 1 );
// The date/time presentation has no ICON parameter, so the icon is set on the object.
// RegisterVariableInteger does not return the variable ID, hence GetIDForIdent afterwards.
if (@$this->GetIDForIdent("ConnectedAt") == false) {
$this->RegisterVariableInteger('ConnectedAt', $this->Translate( 'Connected At' ), [ 'PRESENTATION' => VARIABLE_PRESENTATION_DATE_TIME],$vpos++);
IPS_SetIcon($this->GetIDForIdent("ConnectedAt"),'circle-info');
} else {
$vpos++;
}
$this->MaintainVariable( 'Online', $this->Translate( 'Online' ), 0, [ 'PRESENTATION' => VARIABLE_PRESENTATION_VALUE_PRESENTATION, 'ICON'=> 'network-wired','OPTIONS'=> json_encode( [
[ 'Caption'=> $this->Translate( 'Offline' ), 'ColorActive'=> true, 'ColorValue'=> self::COLOR_RED, 'ContentColorActive'=> false, 'ContentColorValue'=> -1, 'IconActive'=> false, 'IconValue'=> '', 'Value'=> false ],
[ 'Caption'=> $this->Translate( 'Online' ), 'ColorActive'=> true, 'ColorValue'=> self::COLOR_GREEN, 'ContentColorActive'=> false, 'ContentColorValue'=> -1, 'IconActive'=> false, 'IconValue'=> '', 'Value'=> true ]
] )], $vpos++, 1 );
$this->MaintainVariable( 'UplinkDevice', $this->Translate( 'Uplink Device' ), 3, [ 'PRESENTATION' => VARIABLE_PRESENTATION_VALUE_PRESENTATION, 'USAGE_TYPE'=> 0 ,'ICON'=> 'circle-info'], $vpos++, 1 );
$this->MaintainVariable( 'MAC', $this->Translate( 'Client MAC' ), 3, [ 'PRESENTATION' => VARIABLE_PRESENTATION_VALUE_PRESENTATION, 'USAGE_TYPE'=> 0 ,'ICON'=> 'circle-info'], $vpos++, $this->ReadPropertyBoolean("MACAnzeigen") );
$TimerMS = $this->ReadPropertyInteger( 'Timer' ) * 1000;
$this->SetTimerInterval( 'Collect Data', $TimerMS );
if ( 0 == $TimerMS )
{
// instance inactive
$this->SetStatus( 104 );
} else {
// instance active
$this->SetStatus( 102 );
$this->Send('getClientData','');
}
}
public function Send(string $api, string $param1): void
{
if ($this->HasActiveParent()) {
$data=$this->SendDataToParent(json_encode(['DataID' => '{4A5538F1-1C38-198A-3144-D806E0DADF87}',
'Api' => $api,
'InstanceID' => $this->InstanceID,
'Param1' => $param1
]));
if (!$data) {
$this->SendDebug("UnifiCL", "Send Data Error: " . $api, 0);
return;
};
switch($api) {
case "getClients":
$deviceData=json_encode(unserialize($data));
$this->UpdateFormField("ID", "options", $deviceData);
$this->SetBuffer("clients", $deviceData);
break;
case "getClientData":
$array=json_decode(unserialize($data),true);
$this->SendDebug("UnifiCL", unserialize($data), 0);
# $array = json_decode($data['data'],true);
if ( isset( $array[ 'statusCode' ] ) ) {
if ($array[ 'statusCode' ]== 404) {
// instance inactive
$this->SendDebug("UnifiCL", "Device Offline: " . json_encode($array), 0);
$this->SetValue( 'Online', false );
$this->SetValue( 'UplinkDevice', '-');
} else {
// instance inactive
$this->SendDebug("UnifiCL", "Request failed: " . json_encode($array), 0);
$this->SetValue( 'Online', false );
$this->SetValue( 'UplinkDevice', '-');
$this->SetStatus( $array[ 'statusCode' ] );
}
}
if ( is_array( $array ) && isset( $array ) ) {
if ( isset( $array[ 'statusCode' ] ) ) {
$this->SetValue( 'Online', false );
} else {
$this->SetValue( 'ClientName', $array[ 'name' ] );
if ($this->ReadPropertyBoolean("IDAnzeigen")) {
$this->SetValue( 'ID', $array[ 'id' ] );
}
$this->SetValue( 'ClientType', $array[ 'type' ] );
if ( isset( $array[ 'ipAddress' ] ) ) {
$this->SetValue( 'ClientIP', $array[ 'ipAddress' ] );
$this->SetSummary($array[ 'ipAddress' ]);
}
$this->SetValue( 'ConnectedAt', strtotime( $array[ 'connectedAt' ] ) );
$this->SetValue( 'Online', true );
if ( isset( $array['uplinkDeviceId'] ) ) {
$this->Send('getDeviceName',$array['uplinkDeviceId']);
}
if ($this->ReadPropertyBoolean("MACAnzeigen")) {
$this->SetValue( 'MAC', $array[ 'macAddress' ] );
}
}
}
break;
case "getDeviceName":
$data=unserialize($data);
$this->SendDebug("UnifiCL", $data, 0);
$this->SetValue( 'UplinkDevice', $data);
break;
}
}
}
public function GetConfigurationForm(): string {
if ($this->HasActiveParent()) {
$this->Send("getClients",'');
}
$arrayStatus = array();
$arrayStatus[] = array( 'code' => 102, 'icon' => 'active', 'caption' => 'Instance is active' );
$arrayStatus[] = array( 'code' => 400, 'icon' => 'error', 'caption' => 'Instance is faulty: Bad Request' );
$arrayStatus[] = array( 'code' => 401, 'icon' => 'error', 'caption' => 'Instance is faulty: Unauthorized' );
$arrayStatus[] = array( 'code' => 403, 'icon' => 'error', 'caption' => 'Instance is faulty: Forbidden' );
$arrayStatus[] = array( 'code' => 429, 'icon' => 'error', 'caption' => 'Instance is faulty: Rate Limit' );
$arrayStatus[] = array( 'code' => 500, 'icon' => 'error', 'caption' => 'Instance is faulty: Server Error' );
$arrayStatus[] = array( 'code' => 502, 'icon' => 'error', 'caption' => 'Instance is faulty: Bad Gateway' );
$arrayElements = array();
$arrayElements[] = array( 'type' => 'Label', 'bold' => true, 'label' => $this->Translate('UniFi Client') );
$arrayElements[] = array( 'type' => 'Label', 'label' => $this->Translate('Set Timer to activate instance. The instance will then automatically collect data from the device at the specified interval.'));
$arrayElements[] = array( 'type' => 'NumberSpinner', 'name' => 'Timer', 'caption' => 'Timer (s) -> 0=Off' );
$Bufferdata = $this->GetBuffer("clients");
if ($Bufferdata=="") {
$arrayOptions[] = array( 'caption' => $this->Translate('Please load the data first'), 'value' => '' );
} else {
$arrayOptions=json_decode($Bufferdata);
}
$arrayElements[] = array( 'type' => 'Select', 'name' => 'ID', 'caption' => 'Client ID', 'options' => $arrayOptions );
unset($arrayOptions);
$arrayOptions[] = array( 'type' => 'CheckBox', 'name' => 'MACAnzeigen', 'caption' => $this->Translate('Show MAC') );
$arrayOptions[] = array( 'type' => 'CheckBox', 'name' => 'IDAnzeigen', 'caption' => $this->Translate('Show ID') );
$arrayElements[] = array( 'type' => 'RowLayout', 'items' => $arrayOptions );
$arrayActions = array();
unset($arrayOptions);
$arrayOptions[] = array( 'type' => 'Button', 'width' => '220px','label' => $this->Translate('Get Clients'), 'onClick' => 'UNIFICL_Send($id,"getClients","");' );
$arrayOptions[] = array( 'type' => 'Button', 'width' => '220px','label' => $this->Translate('Get Data'), 'onClick' => 'UNIFICL_Send($id,"getClientData","");' );
$arrayActions[] = array( 'type' => 'RowLayout', 'items' => $arrayOptions );
return JSON_encode( array( 'status' => $arrayStatus, 'elements' => $arrayElements, 'actions' => $arrayActions ) );
}
}