-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapper.php
61 lines (49 loc) · 1.56 KB
/
scrapper.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
<?php
use GuzzleHttp\Client;
use InfluxDB\Database;
use InfluxDB\Point;
require __DIR__ . '/vendor/autoload.php';
function env(string $name, $default = null)
{
$value = getenv($name);
return $value === false ? $default : $value;
}
$plugName = env('SHELLY_PLUG_NAME', 'plug1');
$plugHost = env('SHELLY_PLUG_IP');
$plugUser = env('SHELLY_PLUG_USER');
$plugPassword = env('SHELLY_PLUG_PASSWORD');
$dbHost = env('INFLUXDB_HOST', 'influxdb');
$dbPort = env('INFLUXDB_PORT', 8086);
$dbName = env('INFLUXDB_DB', 'shelly-plug');
$dbUser = env('INFLUXDB_ADMIN_USER');
$dbPassword = env('INFLUXDB_ADMIN_PASSWORD');
if (!$dbUser || !$dbPassword) {
throw new Exception('Please provide INFLUXDB_ADMIN_USER and INFLUXDB_ADMIN_PASSWORD');
}
$dbClient = new \InfluxDB\Client($dbHost, $dbPort, $dbUser, $dbPassword);
$database = $dbClient->selectDB($dbName);
if (!$database->exists()) {
$database->create();
}
$client = new Client();
$response = $client->get("http://{$plugHost}/status", [
'auth' => [$plugUser, $plugPassword]
]);
$data = json_decode($response->getBody()->getContents(), false, 512, JSON_THROW_ON_ERROR);
$meter = $data->meters[0];
$fields = [
'power' => $meter->power,
'total' => $meter->total,
'temperature' => $data->temperature,
'uptime' => $data->uptime,
'active' => $data->relays[0]->ison ? 1 : 0
];
$points = [];
foreach ($fields as $name => $value) {
$points[] = new Point(
$name,
$value,
[ 'name' => $plugName, 'ip' => $plugHost ],
);
}
$database->writePoints($points, Database::PRECISION_MILLISECONDS);