summaryrefslogtreecommitdiff
path: root/localwebsite/classes/TemphumdClient.php
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2022-11-21 03:51:37 +0300
committerEvgeny Zinoviev <me@ch1p.io>2022-11-21 03:51:37 +0300
commit35eec7f8126801a02008d405624927099ca53e5e (patch)
tree6bf89eaeb7674866b9f9918d4e22511d68481bd1 /localwebsite/classes/TemphumdClient.php
parent80bca2085f1cc177ea5060b6f1f23cab19e9f410 (diff)
lws: fix for malfunctioning si7021 sensor
Diffstat (limited to 'localwebsite/classes/TemphumdClient.php')
-rw-r--r--localwebsite/classes/TemphumdClient.php41
1 files changed, 41 insertions, 0 deletions
diff --git a/localwebsite/classes/TemphumdClient.php b/localwebsite/classes/TemphumdClient.php
new file mode 100644
index 0000000..07e5a3e
--- /dev/null
+++ b/localwebsite/classes/TemphumdClient.php
@@ -0,0 +1,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;
+ }
+
+} \ No newline at end of file