-
Notifications
You must be signed in to change notification settings - Fork 0
/
Netatmo.php
253 lines (242 loc) · 10.4 KB
/
Netatmo.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
246
247
248
249
250
251
252
253
<?php
require __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/vendor/netatmo/netatmo-api-php/src/Netatmo/autoload.php';
require_once __DIR__.'/Storage.php';
/**
* Netatmo API wrapper
*/
class Netatmo
{
private $logger;
private $client;
private $isMocked;
private $defaultDeviceName;
public $devices;
private $todayTimestamp;
private $storage;
public $hasError;
const MAX_REQUESTS_BY_MODULE = 50;
const WAITING_TIME_BEFORE_NEXT_MODULE = 10;
const COLLECT_INTERVAL = 1000;
const TOKENS_FILE = 'tokens.json';
/**
* Initialize a Netatmo wrapper
*
* @param array $config Associative array including application and account information
*/
public function __construct($config)
{
$this->logger = Logger::getLogger('Netatmo');
$this->todayTimestamp = time();
// Initialize Netatmo client
$this->hasError = false;
if (!key_exists('client_id', $config) || !key_exists('client_secret', $config) || $config['client_id'] === '' || $config['client_secret'] === '') {
$this->logger->error('Client id or secret id is not set, please update config.php');
$this->hasError = true;
return;
}
$configNetatmo = [];
$configNetatmo['client_id'] = $config['client_id'];
$configNetatmo['client_secret'] = $config['client_secret'];
$configNetatmo['scope'] = Netatmo\Common\NAScopes::SCOPE_READ_STATION;
$this->defaultDeviceName = $config['defaultDeviceName'];
$this->isMocked = $config['mock'];
if ($this->isMocked) {
$this->logger->warn('API is mocked');
} else {
$this->client = new Netatmo\Clients\NAWSApiClient($configNetatmo);
}
// Initialize database access
$this->storage = new Storage();
$this->storage->connect($config['host'], $config['port'], $config['database'], $config['retentionDuration']);
}
/**
* Retrieve previous tokens
*
* @return Authentication result
*/
public function getExistingTokens()
{
if (!$this->isMocked) {
$this->logger->debug('Try to get existing tokens');
$tokensString = @file_get_contents($this::TOKENS_FILE);
if ($tokensString === false) {
// File not found
$this->logger->debug('No tokens file found');
return false;
}
$tokens = json_decode($tokensString, true);
if (json_last_error() !== JSON_ERROR_NONE) {
// Invalid JSON
$this->logger->debug('Invalid tokens file');
return false;
}
if (!array_key_exists('refresh_token', $tokens)) {
// Missing refresh token
$this->logger->debug('Missing refresh token');
return false;
}
if (!is_null($tokens['expires_at']) && $tokens['expires_at'] < time()) {
// access_token expired.
$this->logger->debug('Access token expired');
unset($tokens['access_token']);
}
$this->client->setTokensFromStore($tokens);
$newTokens = $this->client->getAccessToken();
if (array_key_exists('refresh_token', $newTokens)) {
$this->logger->debug('Refresh token updated');
$newTokens['expires_at'] = time() + $newTokens['expires_in'] - 30;
$this->logger->trace('Update tokens: '.json_encode($newTokens));
file_put_contents($this::TOKENS_FILE, json_encode($newTokens));
}
$this->logger->debug('Using existing tokens');
}
return true;
}
/**
* Retrieve user's Weather Stations Information
*
* @return Query result
*/
public function getStations()
{
try {
$this->logger->debug('Request stations');
if (!$this->isMocked) {
$data = $this->client->getData(null, true);
} else {
$this->logger->debug('Mocked: mock/stations.json');
$data = json_decode(file_get_contents('mock/stations.json'), true);
}
$this->logger->debug('Stations received');
// $this->logger->trace('Stations: '.json_encode($data));
} catch (Netatmo\Exceptions\NAClientException $e) {
$this->logger->error('An error occured while retrieving data');
$this->logger->debug('Reason: '.$e->getMessage());
return false;
}
if (empty($data['devices'])) {
$this->logger->error('No devices affiliated to user');
return false;
}
$this->devices = $data['devices'];
$this->logger->info('Found ' . count($this->devices) . ' devices');
return true;
}
/**
* Request measures for a specific device/module from provided timestamp
*
* @param int $startTimestamp (optional) starting timestamp of requested measurements
* @param array $device associative array including information about device to get measures
* @param array $module (optional) associative array including information about module to get measures
* @return void
*/
public function getMeasures($startTimestamp, $device, $module)
{
$deviceId = $device['_id'];
$deviceName = $device['home_name'];
// Override deviceName according to configuration
if ($this->defaultDeviceName !== '') {
$this->logger->info("Override deviceName from config: $deviceName -> $this->defaultDeviceName");
$deviceName = $this->defaultDeviceName;
}
// default values for module
$moduleId = null;
$moduleName = $device['module_name'];
$moduleType = $device['type'];
if ($module) {
// if module provided, override default values
$moduleId = $module['_id'];
$moduleName = $module['module_name'];
$moduleType = $module['type'];
}
$this->logger->info("Request measures for device: $deviceName, module: $moduleName ($moduleType)");
// Requested data type depends on the module's type
switch ($moduleType) {
case 'NAMain':
//main indoor module
$type = 'temperature,Co2,humidity,noise,pressure';
break;
case 'NAModule1':
// outdoor module
$type = 'temperature,humidity';
break;
case 'NAModule2':
// wind gauge module
$type = 'WindStrength,WindAngle,GustStrength,GustAngle';
break;
case 'NAModule3':
// rain gauge module
$type = 'rain';
break;
default:
// other (including additional indoor module)
$type = 'temperature,Co2,humidity';
break;
}
$fieldKeys = explode(',', $type);
if ($startTimestamp) {
$lastTimestamp = $startTimestamp;
} else {
$lastTimestamp = time() - 24*3600*30;
// Get last fetched timestamp
try {
$last = $this->storage->get_last_fetch_date($deviceName . '-' . $moduleName, $fieldKeys[0]);
if ($last !== null) {
$lastTimestamp = $last;
}
} catch (Exception $e) {
$this->logger->error('Can not get last fetch timestamp');
// Can not access database, exit script
return;
}
}
$hasError = false;
$requestCount = 0;
// Get measures by requesting API until max requests is reached, all data are reiceved or an error occurs
do {
$requestCount++;
$measures = [];
try {
$this->logger->debug('Starting timestamp: ' . $lastTimestamp . ' (' . date('Y-m-d H:i:sP', $lastTimestamp) . ')');
if (!$this->isMocked) {
$measures = $this->client->getMeasure($deviceId, $moduleId, 'max', $type, $lastTimestamp, $this->todayTimestamp, 1024, false, true);
// file_put_contents("mock2/$moduleType.json", json_encode($measures));
} else {
$this->logger->debug("Mocked: mock/$moduleType.json");
$measures = json_decode(file_get_contents("mock/$moduleType.json"), true);
}
// $this->logger->trace('Measure: '. json_encode($measures));
} catch (Netatmo\Exceptions\NAClientException $e) {
$hasError = true;
$this->logger->error("An error occured while retrieving device $deviceName / module: $moduleName ($moduleType) measurements");
$this->logger->debug('Reason: '.$e->getMessage());
}
// Store module measures in database
$points = [];
foreach ($measures as $timestamp => $values) {
$dt = new DateTime();
$dt->setTimestamp($timestamp);
// $this->logger->trace('Handling values for ' . $dt->format('Y-m-d H:i:sP'));
$fields = [];
foreach ($values as $key => $val) {
if (array_key_exists($key, $fieldKeys)) {
// $this->logger->trace('. ' . $fieldKeys[$key] . ': ' . $val);
$fields[$fieldKeys[$key]] = (float) $val;
}
}
array_push($points, $this->storage->createPoint($deviceName . '-' . $moduleName, $timestamp, null, [], $fields));
$lastTimestamp = max($lastTimestamp, $timestamp);
// $this->logger->trace('Max timestamp is now : ' . $lastTimestamp . ' (' . date('Y-m-d H:i:sP', $lastTimestamp) . ' )');
}
try {
$this->storage->writePoints($points);
} catch (Exception $e) {
$hasError = true;
$this->logger->error("Can not write device $deviceName / module: $moduleName ($moduleType) measurements");
}
} while ($lastTimestamp <= ($this->todayTimestamp - $this::COLLECT_INTERVAL) && !$hasError && $requestCount < $this::MAX_REQUESTS_BY_MODULE);
// Wait some seconds before continue to avoid reaching user limit API
sleep($this::WAITING_TIME_BEFORE_NEXT_MODULE);
}
}