lws: fix for malfunctioning si7021 sensor

This commit is contained in:
Evgeny Zinoviev 2022-11-21 03:51:37 +03:00
parent 80bca2085f
commit 35eec7f812
5 changed files with 25 additions and 8 deletions

View File

@ -1,17 +1,19 @@
<?php
class Si7021dClient extends MySimpleSocketClient {
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) {
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);
}
@ -28,4 +30,12 @@ class Si7021dClient extends MySimpleSocketClient {
$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;
}
}

View File

@ -2,6 +2,9 @@
class config {
const TEMPHUMD_NO_TEMP = 1 << 0;
const TEMPHUMD_NO_HUM = 1 << 1;
public static function get(string $key) {
global $config;
return is_callable($config[$key]) ? $config[$key]() : $config[$key];

View File

@ -15,9 +15,9 @@ return [
'pump_host' => '192.168.1.2',
'pump_port' => 8307,
'si7021d_servers' => [
'temphumd_servers' => [
// fill here, example:
'hall' => ['192.168.1.3', 8306, 'Big Hall'],
'hall' => ['192.168.1.3', 8306, 'Big Hall'/*, optional: config::TEMPHUMD_NO_HUM */],
],
// modem names (array keys) must match ipset names and

View File

@ -17,8 +17,8 @@ class MiscHandler extends RequestHandler
global $config;
$clients = [];
foreach ($config['si7021d_servers'] as $key => $params) {
$cl = new Si7021dClient(...$params);
foreach ($config['temphumd_servers'] as $key => $params) {
$cl = new TemphumdClient(...$params);
$clients[$key] = $cl;
$cl->readSensor();

View File

@ -6,6 +6,10 @@
{% for key, sensor in sensors %}
<h6 class="text-primary{% if not loop.first %} mt-4{% endif %}">{{ sensor.name }}</h6>
<span class="text-secondary">Температура:</span> <b>{{ sensor.temp }}</b> °C<br>
<span class="text-secondary">Влажность:</span> <b>{{ sensor.humidity }}</b>%
{% if sensor.hasTemperature() %}
<span class="text-secondary">Температура:</span> <b>{{ sensor.temp }}</b> °C<br>
{% endif %}
{% if sensor.hasHumidity() %}
<span class="text-secondary">Влажность:</span> <b>{{ sensor.humidity }}</b>%
{% endif %}
{% endfor %}