-
Notifications
You must be signed in to change notification settings - Fork 4
/
gardena.class.inc.php
executable file
·245 lines (189 loc) · 11.4 KB
/
gardena.class.inc.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
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
<?php
/**
* http://www.dxsdata.com/2016/07/php-class-for-gardena-smart-system-api/
* Ref. http://www.roboter-forum.com/showthread.php?16777-Gardena-Smart-System-Analyse
*/
class gardena
{
var $user_id, $token, $locations;
var $devices = array();
const LOGINURL = "https://smart.gardena.com/sg-1/sessions";
const LOCATIONSURL = "https://smart.gardena.com/sg-1/locations/?user_id=";
const DEVICESURL = "https://smart.gardena.com/sg-1/devices?locationId=";
const CMDURL = "https://smart.gardena.com/sg-1/devices/|DEVICEID|/abilities/mower/command?locationId=";
var $CMD_MOWER_PARK_UNTIL_NEXT_TIMER = array("name" => "park_until_next_timer");
var $CMD_MOWER_PARK_UNTIL_FURTHER_NOTICE = array("name" => "park_until_further_notice");
var $CMD_MOWER_START_RESUME_SCHEDULE = array("name" => "start_resume_schedule");
var $CMD_MOWER_START_24HOURS = array("name" => "start_override_timer", "parameters" => array("duration" => 1440));
var $CMD_MOWER_START_3DAYS = array("name" => "start_override_timer", "parameters" => array("duration" => 4320));
var $CMD_SENSOR_REFRESH_TEMPERATURE = array("name" => "measure_ambient_temperature");
var $CMD_SENSOR_REFRESH_LIGHT = array("name" => "measure_light");
var $CMD_SENSOR_REFRESH_HUMIDITY = array("name" => "measure_humidity");
var $CMD_WATERINGCOMPUTER_START_30MIN = array("name" => "manual_override", "parameters" => array("duration" => 30));
var $CMD_WATERINGCOMPUTER_STOP = array("name" => "cancel_override");
const CATEGORY_MOWER = "mower";
const CATEGORY_GATEWAY = "gateway";
const CATEGORY_SENSOR = "sensor";
const CATEGORY_WATERINGCOMPUTER = "watering_computer";
const PROPERTY_STATUS = "status";
const PROPERTY_BATTERYLEVEL = "level";
const PROPERTY_TEMPERATURE = "temperature";
const PROPERTY_SOIL_HUMIDITY = "humidity";
const PROPERTY_LIGHTLEVEL = "light";
const PROPERTY_VALVE_OPEN = "valve_open";
const ABILITY_CONNECTIONSTATE = "radio";
const ABILITY_BATTERY = "battery";
const ABILITY_TEMPERATURE = "ambient_temperature";
const ABILITY_SOIL_HUMIDITY = "humidity";
const ABILITY_LIGHT = "light";
const ABILITY_OUTLET = "outlet";
function gardena($user, $pw)
{
$data = array(
"sessions" => array(
"email" => "$user", "password" => "$pw")
);
$data_string = json_encode($data);
$ch = curl_init(self::LOGINURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$data = json_decode($result);
$this -> token = $data -> sessions -> token;
$this -> user_id = $data -> sessions -> user_id;
$this -> loadLocations();
$this -> loadDevices();
}
function loadLocations()
{
$url = self::LOCATIONSURL . $this -> user_id;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'X-Session:' . $this -> token)
);
$this -> locations = json_decode(curl_exec($ch)) -> locations;
}
function loadDevices()
{
foreach($this->locations as $location)
{
$url = self::DEVICESURL . $location -> id;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'X-Session:' . $this -> token)
);
$this -> devices[$location -> id] = json_decode(curl_exec($ch)) -> devices;
}
}
/**
* Finds the first occurrence of a certain category type.
* Example: You want to find your only mower, having one or more gardens.
*
* @param constant $category
*/
function getFirstDeviceOfCategory($category)
{
foreach($this -> devices as $locationId => $devices)
{
foreach($devices as $device)
if ($device -> category == $category)
return $device;
}
}
function getDeviceLocation($device)
{
foreach($this -> locations as $location)
foreach($location -> devices as $d)
if ($d == $device -> id)
return $location;
}
function sendCommand($device, $command)
{
$location = $this -> getDeviceLocation($device);
$url = str_replace("|DEVICEID|", $device -> id, self::CMDURL) . $location -> id;
$data_string = json_encode($command);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'X-Session:' . $this -> token,
'Content-Length: ' . strlen($data_string)
));
$result = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == "204") //success
return true;
return json_encode($result);
}
function getMowerState($device)
{
return $this->getPropertyData($device, $this::CATEGORY_MOWER, $this::PROPERTY_STATUS) -> value;
}
function getDeviceStatusReportFriendly($device)
{
$result = "";
foreach ($device -> status_report_history as $entry)
{
$result .= $entry -> timestamp . " | " . $entry -> source . " | " . $entry -> message . "<br>";
}
return $result;
}
function getAbilityData($device, $abilityName)
{
foreach($device -> abilities as $ability)
if ($ability -> name == $abilityName)
return $ability;
}
function getPropertyData($device, $abilityName, $propertyName)
{
$ability = $this->getAbilityData($device, $abilityName);
foreach($ability -> properties as $property)
if ($property -> name == $propertyName)
return $property;
}
/**
* Note "quality 80" seems to be quite the highest possible value (measured with external antenna and 2-3 meters distance)
*
* @param mixed $device
*/
function getConnectionDataFriendly($device)
{
$ability = $this->getAbilityData($device, $this::ABILITY_CONNECTIONSTATE);
$properties = array('quality', 'connection_status', 'state');
foreach ($properties as $property)
{
$p = $this->getPropertyData($device, $ability -> name, $property);
echo $property . ": " . $p -> value . " | " . $p -> timestamp . "<br>";
}
}
function getSensorDataFriendly($device)
{
echo "battery: ".$this->getPropertyData($device, $this::ABILITY_BATTERY, $this::PROPERTY_BATTERYLEVEL) -> value . " %<br>";
echo "humidity: ".$this->getPropertyData($device, $this::ABILITY_SOIL_HUMIDITY, $this::PROPERTY_SOIL_HUMIDITY) -> value . " %<br>";
echo "ambient temperature: ".$this->getPropertyData($device, $this::ABILITY_TEMPERATURE, $this::PROPERTY_TEMPERATURE) -> value . " °C<br>";
echo "light: ".$this->getPropertyData($device, $this::ABILITY_LIGHT, $this::PROPERTY_LIGHTLEVEL) -> value . " lx<br>";
}
function getWateringComputerDataFriendly($device)
{
echo "battery: ".$this->getPropertyData($device, $this::ABILITY_BATTERY, $this::PROPERTY_BATTERYLEVEL) -> value . " %<br>";
$open = $this->getPropertyData($device, $this::ABILITY_OUTLET, $this::PROPERTY_VALVE_OPEN) -> value;
//var_dump($open);
echo "outlet valve open: " . ($open ? "yes" : "no") . "<br>";
//2do: further properties
//var_dump($this->getPropertyData($device, $this::ABILITY_OUTLET, $this::PROPERTY_VALVE_OPEN) -> value);
}
}
?>