blob: 07e5a3ee85517c8789782f8a9f53a52d4b84280d (
plain)
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
|
<?php
class TemphumdClient extends MySimpleSocketClient {
public string $name;
public float $temp;
public float $humidity;
public ?int $flags;
/**
* @throws Exception
*/
public function __construct(string $host, int $port, string $name, ?int $flags = null) {
parent::__construct($host, $port);
$this->name = $name;
$this->flags = $flags;
socket_set_timeout($this->sock, 3);
}
public function readSensor(): void {
$this->send('read');
$data = jsonDecode($this->recv());
$temp = round((float)$data['temp'], 3);
$hum = round((float)$data['humidity'], 3);
$this->temp = $temp;
$this->humidity = $hum;
}
public function hasTemperature(): bool {
return ($this->flags & config::TEMPHUMD_NO_TEMP) == 0;
}
public function hasHumidity(): bool {
return ($this->flags & config::TEMPHUMD_NO_HUM) == 0;
}
}
|