diff --git a/class/db.php b/class/db.php
index d41bb0a..b381501 100644
--- a/class/db.php
+++ b/class/db.php
@@ -50,18 +50,12 @@ class db extends mysqli
{
// calculate a min/max value for storing data
- $var = $properties2log[$propertyTree];
- if (is_callable($var))
+ $minMax = $properties2log[$propertyTree];
+ if (is_callable($minMax))
{
- $var = $var($value);
+ $minMax = $minMax($value);
}
- //if (!is_numeric($var))
- //{
- $minMax = (float)$value + $var;
- //}else
- //{
- // $minMax = $var;
- //}
+
echo "minMax = " .$minMax . EOL;
//echo "oldValue = " . $oldValue . EOL;
//echo "Value = " . $value . EOL;
diff --git a/class/main.php b/class/main.php
index 9894cd0..954f257 100644
--- a/class/main.php
+++ b/class/main.php
@@ -33,8 +33,9 @@ class device
public $payload;
public $availability;
public $toConfirm;
+ public $triggerDevice;
- private function __construct()
+ public function __construct()
{
$this->availability = array("value" => null, "functions" => array());
}
diff --git a/config/devices_constants.php b/config/devices_constants.php
index 53a87bb..bc1e13a 100644
--- a/config/devices_constants.php
+++ b/config/devices_constants.php
@@ -22,7 +22,12 @@ $deviceTable = array(
"0x00124b002226e384" => "RDC_SALON_BAIE",
"0x842e14fffe8c53a5" => "ETAGE_CUISINE_PLAN_TRAVAIL_INTER",
"0x680ae2fffe403f22" => "ETAGE_CUISINE_PLAN_TRAVAIL_ECLAIRAGE",
- "0xbc33acfffe65621b" => "ETAGE_CUISINE_PLAN_TRAVAIL_SPOT"
+ "0xbc33acfffe65621b" => "ETAGE_CUISINE_PLAN_TRAVAIL_SPOT",
+ "0x086bd7fffe5aeab6" => "RDC_CHAMBRE_AMBIANCE",
+ "0xbc33acfffe6561e4" => "RDC_CHAMBRE_ECLAIRAGE",
+ "0x00124b00234484ad" => "RDC_CHAMBRE_MVMT",
+ "0x04cf8cdf3c7b6056" => "RDC_CHAMBRE_LUMINOSITE",
+ "0x00158d0006c0447c" => "RDC_CHAMBRE_ARMOIRE_GAUCHE"
);
foreach($deviceTable as $device => $name)
diff --git a/config/properties2log.php b/config/properties2log.php
index 7bd8852..03154c6 100644
--- a/config/properties2log.php
+++ b/config/properties2log.php
@@ -27,8 +27,8 @@ $properties2log = array(
"dewptf" => 0.5,
"windchillf" => 0.5,
"winddir" => 40,
- "windspeedmph" => 20,
- "windgustmph" => 20,
+ "windspeedmph" => 5,
+ "windgustmph" => 5,
"rainin" => null,
//"dailyrainin" => null,
//"weeklyrainin" => null,
diff --git a/db_functions.php b/db_functions.php
index e030e09..0c5cc90 100644
--- a/db_functions.php
+++ b/db_functions.php
@@ -199,7 +199,7 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
$device->{$key} = array("value" => null);
$device->$key["functions"] = array();
}
- echo $key . ' ===> oldvalue = ' . $oldValue . " value = " . $value . EOL;
+ //echo $key . ' ===> oldvalue = ' . $oldValue . " value = " . $value . EOL;
if ($oldValue !== $value)
{
$device->$key["value"] = $value;
@@ -207,8 +207,6 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
//$changed[$fn]["value"] = $value;
logger(INFO, sprintf(_("Device %s property %s, %s"), $fn, $propertyTree . $key, bool2string($value)));
$mohaDB->logProperty($parentDevice, $propertyTree . $key, $value, $oldValue);
-
- }
if (!empty($device->$key["functions"]))
{
logger(DEBUG,_("executing notifications functions"));
@@ -217,6 +215,8 @@ function iterateDevice($topic, $fn, $parentDevice, &$device, $payloadArray, $pro
$function($device, $key, $value);
}
}
+ }
+
//}
}
}
diff --git a/events.php b/events.php
index 0bd6df4..a00cec9 100644
--- a/events.php
+++ b/events.php
@@ -159,6 +159,14 @@ function setDelay(&$device, $delay, $unit="second", $property, $value, $replace=
$loglevel = $oldLevel;
}
+function removeEvent($device, $property , $value)
+{
+ if (($eventKey = searchEvent($device, $property , $value)) === true)
+ {
+ deleteEvent($eventKey);
+ }
+
+}
function searchEvent($device, $property , $value)
{
global $events;
@@ -188,7 +196,6 @@ function deleteEvent($eventKey)
//try to delete an flase event
logger(WARNING, _("Try to delete event with key = ") . var_dump($eventKey));
}
-
}
diff --git a/hooks/scripts/etage_plan_travail_eclairage b/hooks/scripts/etage_plan_travail_eclairage
index 65c4a10..ea56c45 100644
--- a/hooks/scripts/etage_plan_travail_eclairage
+++ b/hooks/scripts/etage_plan_travail_eclairage
@@ -6,15 +6,7 @@ class etage_plan_travail_eclairage extends hook
public $active = true; //enable/disable hook (true => enabled)
public $delay = 3; // amount of time in $timeunit
public $timeUnit = "hour"; // unit of time for delay, second, minute, hour, day, week, month, year
- protected $devicelist = array("ETAGE_CUISINE_PLAN_TRAVAIL_ECLAIRAGE" => array("action", false));
-
- // if you need some initialisation when creating the object, uncomment andput your code here.
- // If existing, This function is called by __construct
- /*protected function init()
- {
- // Your code here
- }
- */
+ protected $devicelist = array("ETAGE_CUISINE_PLAN_TRAVAIL_ECLAIRAGE" => array("state", false));
// callback fonction. Is called with these 3 parameters
// $device -> calling device
@@ -47,7 +39,6 @@ class etage_plan_travail_eclairage extends hook
}else
{
logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName));
-
}
//echo 'delaystate = ' . var_dump($delayState);
if ($delayState !== false) setDelay($device, $this->delay, $this->timeUnit, "state", $delayState, true);
diff --git a/hooks/scripts/rdc_chambre_eclairage.php b/hooks/scripts/rdc_chambre_eclairage.php
index acc4cbf..fb768cd 100644
--- a/hooks/scripts/rdc_chambre_eclairage.php
+++ b/hooks/scripts/rdc_chambre_eclairage.php
@@ -3,17 +3,17 @@
class rdc_chambre_eclairage extends hook
{
public $hookName = "rdc_chambre_eclairage";
- public $active = false; //enable/disable hook (true => enabled)
-
- protected $devicelist = array();
-
- // if you need some initialisation when creating the object, uncomment andput your code here.
- // If existing, This function is called by __construct
- /*protected function init()
- {
- // Your code here
- }
- */
+ public $active = false; //enable/disable hook (true => enabled)
+ public $delay = 5; // amount of time in $timeunit
+ public $timeUnit = "minute"; // unit of time for delay, second, minute, hour, day, week, month, year
+ public $luminance_min = 60;
+ public $luminance_max = 3000;
+ protected $devicelist = array(
+ "RDC_CHAMBRE_AMBIANCE" => array("state", false),
+ "RDC_CHAMBRE_ECLAIRAGE" => array("state_l1", false),
+ "RDC_CHAMBRE_MVMT" => array("occupancy", false),
+ "RDC_CHAMBRE_ARMOIRE_GAUCHE" => array("contact", false)
+ );
// callback fonction. Is called with these 3 parameters
// $device -> calling device
@@ -21,9 +21,33 @@ class rdc_chambre_eclairage extends hook
// $value -> value of the property
public function callBack(&$device, $property, $value)
{
- // here your code
+ global $devices, $indexDevices;
+ $lux = $indexDevices[RDC_CHAMBRE_LUMINOSITE]->illuminance_lux;
+ if ($property == "occupancy" and $value == "ON")
+ {
+ $this->send(RDC_CHAMBRE_LUMINOSITE, "ON", "OFF", AUTO);
+ }
+ logger (INFO, sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value));
+ }
- logger (INFO,sprintf(_("%s: notification received from MQTT from %s => parameter: %s value: %s"), $this->hookName, $device->friendlyName, $property, $value));
+ private function send($device, $state, $delayState = false, $method = MANUAL)
+ {
+ global $devices, $indexDevices;
+ $device = &$indexDevices[$device];
+ $msg = array("state" => $state);
+ if ($device->state["value"] != $state)
+ {
+ logger(INFO, sprintf(_("publishing message: %s to %s"), json_encode($msg), $device->friendlyName));
+ $device->payload = $msg;
+ $device->set();
+ $device->method = $method;
+ }else
+ {
+ logger(INFO, sprintf(_("not publishing message: %s to %s, already set"), json_encode($msg), $device->friendlyName));
+
+ }
+ //echo 'delaystate = ' . var_dump($delayState);
+ if ($delayState !== false) setDelay($device, $this->delay, $this->timeUnit, "state", $delayState, true);
}
}
diff --git a/hooks/scripts/rdc_salon_eclairage.php b/hooks/scripts/rdc_salon_eclairage.php
index 43be3fb..6833f27 100644
--- a/hooks/scripts/rdc_salon_eclairage.php
+++ b/hooks/scripts/rdc_salon_eclairage.php
@@ -11,7 +11,7 @@ class rdc_salon_eclairage extends hook
RDC_SALON_LUMINOSITE => array("illuminance_lux", false)
);
- public $delay = 5; // amount of time in $timeunit
+ public $delay = 3; // amount of time in $timeunit
public $timeUnit = "minute"; // unit of time for delay, second, minute, hour, day, week, month, year
public $luminance_min = 60;
public $luminance_max = 3000;
@@ -28,19 +28,29 @@ class rdc_salon_eclairage extends hook
//print_r($indexDevices[RDC_SALON_LUMINOSITE]);
if ($value == ON and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min)
{
- $this->send("ON", "OFF", AUTO);
+ $this->send("ON", null, AUTO);
+ removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF");
+ }
+ if ($value == OFF)
+ {
+ if ($indexDevices(RDC_SALON_MVMT)->occupancy == OFF and $indexDevices(RDC_SALON_MVMT2)->occupancy == OFF)
+ {
+ setDelay($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], $this->delay, $this->timeUnit, "state", "OFF", true);
+ //$this->send("ON", "OFF", AUTO);
+ }
}
break;
case "contact":
if ($value == false and $indexDevices[RDC_SALON_LUMINOSITE]->illuminance_lux["value"] <= $this->luminance_min)
{
- $this->send("ON", "OFF", AUTO);
+ //$this->send("ON", "OFF", AUTO);
}
break;
case "illuminance_lux":
if ($value >= $this->luminance_max)
{
- $this->send("OFF");
+ //$this->send("OFF", null, AUTO);
+ removeEvent($indexDevices[RDC_SALON_ECLAIRAGE_PANNEAU], "state", "OFF");
}
break;
}
diff --git a/moha.php b/moha.php
index a319972..a364add 100644
--- a/moha.php
+++ b/moha.php
@@ -1,21 +1,19 @@
"batterie");
$listPropertiesKeys = array("property");
//global variables
-$logLevel = INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
+$logLevel = ALL; // INFO | NOTICE | WARNING | ERROR | ALERT; //ALL;
$notificationLevel = ALERT | ERROR;
$topics = array(); // list of topics
$mids = array(); // list of message IDs
@@ -61,7 +59,7 @@ function notify($message)
function logger($level, $log, $notif = true)
{
global $logFh, $logLevel, $notificationLevel, $logLevels;
- $logString = date("c") . ' ' . $logLevels[$level] . " : $log";
+ $logString = date("c") . ' ' . $logLevels[$level] . " " . __FILE__ . ":" . __LINE__ . " : " . $log;
if ($level & $logLevel)
{
fwrite($logFh, $logString . EOL);
@@ -112,7 +110,7 @@ function listHooks($dir, &$hookList)
function endMoha()
{
- global $devices, $topics, $nSubscribed ,$client, $logFh, $connected;
+ global $testMode, $devices, $topics, $nSubscribed ,$client, $logFh, $connected;
$x = 0;
storeDB($devices, "/usr/share/moha/moha.db");
if($testMode) file_put_contents("moha.devices", print_r($devices, true));
@@ -149,6 +147,7 @@ require $configDir . "/properties2log.php";
require "mqtt_functions.php";
require "events.php";
require "db_functions.php";
+require "webserver.php";
//logger(DEBUG, _('assigning variable $client to mosquitto class "client"'), false);
//$client = new Mosquitto\Client();
@@ -263,6 +262,7 @@ while (true)
logger(DEBUG,_("All hooks initialized"));
}
checkEvents();
+ askWebServer($read);
}
}
diff --git a/topics_callbacks/pws2mqtt.php b/topics_callbacks/pws2mqtt.php
index cf5dd24..c12fbd8 100644
--- a/topics_callbacks/pws2mqtt.php
+++ b/topics_callbacks/pws2mqtt.php
@@ -8,9 +8,10 @@ $topics["pws2mqtt"]->callback = function($topic, $message)
logger(INFO, sprintf(_("Incoming notification of device %s => friendly name : %s"), $topic[0], $topic[1]));
$device = & $devices[$topic[0]];
$payloadArray = json_decode($message->payload);
+
if (!isset($device[$fn]))
{
- logger(LOG_WARNING, $logFh, "init of " . $fn .EOL);
+ logger(WARNING, $logFh, "init of " . $fn .EOL);
$device[$fn] = array();
$device[$fn]["device"] = new device;
$device[$fn]["device"]->type = $payloadArray->type;
@@ -19,6 +20,24 @@ $topics["pws2mqtt"]->callback = function($topic, $message)
$indexDevices[$device[$fn]["device"]->ieeeAddress] = & $device[$fn]["device"];
}
$device = & $device[$fn];
+ foreach ($payloadArray as $property => $value)
+ {
+ $str = substr($property, -1);
+ if ($str == "f")
+ {
+ $newProperty = rtrim($property, "f") . "c";
+ $payloadArray->{$newProperty} = farenheit2celsius($value);
+ }elseif ($str == "h")
+ {
+ $newProperty = substr($property, 0, -3) . "kmh";
+ $payloadArray->{$newProperty} = mph2kmh($value);
+ }
+ if ($property == "baromin")
+ {
+ $newProperty = "barominmb";
+ $payloadArray->{"barominmb"} = millibars($value);
+ }
+ }
changeDevice($topic[0], $fn, $device["device"], $payloadArray);
}
?>
diff --git a/utils.php b/utils.php
index 0af4226..471c00d 100644
--- a/utils.php
+++ b/utils.php
@@ -23,12 +23,17 @@ function now()
function farenheit2celsius($value)
{
- return ($value - 32 / 1.8);
+ return round((((float)$value - 32) / 1.8),2);
+}
+
+function mph2kmh($value)
+{
+ return round(((float)$value * 1.60934),2);
}
function millibars($value)
{
- return ($value * 0.029530);
+ return round(((float)$value * 33.86),2);
}
function mktopic($device)
diff --git a/webserver.php b/webserver.php
new file mode 100644
index 0000000..a59e456
--- /dev/null
+++ b/webserver.php
@@ -0,0 +1,101 @@
+";
+ }
+ fwrite($spawn, $response);
+ break;
+ case "set":
+ logger(DEBUG, "set reached");
+ $response = "";
+ fwrite($spawn, $response);
+ break;
+ case "print":
+ logger(DEBUG, "print reached");
+ $var = $GLOBALS[$argList["object"]];
+ if (isset($argList["topic"]))
+ {
+ $topic = $argList["topic"];
+ }
+ if (isset($argList["address"]))
+ {
+ $var = $var[$argList["address"]];
+ }elseif (isset($argList["fn"]))
+ {
+ if(!empty($topic))
+ {
+ $var = $var[$topic];
+ $path = explode("/", $argList["fn"]);
+ foreach($path as $tmp)
+ {
+ $var = $var[$tmp];
+ }
+ }else
+ {
+ logger(ERROR, _("topic is not defining: add &topic=zigbee2mqtt to the resquest"));
+ }
+ }
+ $error = error_get_last();
+ if($error !== null)
+ {
+ $response = "" . $error["message"] . " file: " . $error["file"] . " line: " . $error["line"] . "";
+ }
+ $response = "" . print_r($var, true) . "";
+ fwrite($spawn, $response);
+ break;
+ default:
+ logger(DEBUG, "not understanding command");
+ fwrite($spawn, "not understanding command");
+ }
+ }
+ }
+ }
+ }
+}
+?>