master
Dirk Heilig 2024-12-09 20:00:39 +01:00
parent fed0c7124a
commit 4c844d91b1
1 changed files with 17 additions and 4 deletions

21
run.php
View File

@ -26,6 +26,10 @@ $doorOpenEnergy = null;
$doorOpenTime = null;
$lastOutput = 0;
function l(){
#echo implode(" ", func_get_args()) . "\n";
}
$c = new Client();
if (getenv("MQTT_USER") && getenv("MQTT_PASS")) {
@ -41,6 +45,7 @@ $c->subscribe(ENERGY_METER_TOPIC, 2);
$c->subscribe(DOOR_TOPIC, 2);
$c->onMessage(function (Message $message) {
l("got message", $message->topic, $message->payload);
switch ($message->topic) {
case ENERGY_METER_TOPIC:
energy($message->payload);
@ -48,6 +53,8 @@ $c->onMessage(function (Message $message) {
case DOOR_TOPIC:
door($message->payload);
break;
default:
l("unknown topic", $message->topic);
}
});
@ -61,9 +68,7 @@ while (true) {
function door($newStatus)
{
global $doorStatus, $energyCounter, $doorOpenEnergy, $doorOpenTime;
if (is_null($energyCounter)) {
return;
}
$newStatus = (bool) $newStatus;
if ($newStatus === $doorStatus) {
return;
@ -77,8 +82,13 @@ function door($newStatus)
}
function energy($value)
{
global $energyCounter;
global $energyCounter,$doorOpenEnergy;
$energyCounter = (float) $value;
if(is_null($doorOpenEnergy)){
$doorOpenEnergy = $energyCounter;
}
output();
}
@ -98,13 +108,16 @@ function output()
strval($myUsage),
2
);
l("cost", $myCost);
$c->publish(OUTPUT_TOPIC_PREFIX . "/price_float", strval($myCost), 2);
$c->publish(
OUTPUT_TOPIC_PREFIX . "/price_string",
number_format($myCost, 2, ",", "."),
2
);
} else {
l("nope");
$c->publish(OUTPUT_TOPIC_PREFIX . "/used_energy_float", "", 2);
$c->publish(OUTPUT_TOPIC_PREFIX . "/price_float", "", 2);
$c->publish(OUTPUT_TOPIC_PREFIX . "/price_string", "", 2);