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