first commit
This commit is contained in:
253
moha.php
Normal file
253
moha.php
Normal file
@@ -0,0 +1,253 @@
|
||||
<?php
|
||||
|
||||
//Constants
|
||||
define( "EOL", "\n");
|
||||
define("Z2M", "zigbee2mqtt");
|
||||
|
||||
// log levels
|
||||
/*define( "DEBUG", 0);
|
||||
define( "INFO", 1);
|
||||
define( "WARNING", 3);
|
||||
define( "ERROR", 4);*/
|
||||
$logLevel = LOG_DEBUG;
|
||||
|
||||
|
||||
declare(ticks = 1);
|
||||
|
||||
$listProperties = array( "powerSource" => "batterie" );
|
||||
|
||||
class Message
|
||||
{
|
||||
public $id;
|
||||
public $state = false;
|
||||
public $msg;
|
||||
}
|
||||
|
||||
class topic {
|
||||
public $mid;
|
||||
public $status;
|
||||
public $info;
|
||||
public $devices;
|
||||
public $groups;
|
||||
public $extensions;
|
||||
public $config;
|
||||
}
|
||||
|
||||
class device
|
||||
{
|
||||
public $topic;
|
||||
public $device;
|
||||
public $json;
|
||||
public $ieeeAddress;
|
||||
public $friendlyName;
|
||||
public $type;
|
||||
public $powerSource;
|
||||
public $description;
|
||||
public $functions;
|
||||
public $payload;
|
||||
|
||||
public function set($event)
|
||||
{
|
||||
publish($this, $this->payload, "set", $event);
|
||||
}
|
||||
}
|
||||
|
||||
class event
|
||||
{
|
||||
public $ieeeAddress;
|
||||
public $topic;
|
||||
public $param;
|
||||
public $value;
|
||||
public $published;
|
||||
public $dateTimeEvent;
|
||||
public $recurrenceDay;
|
||||
public $recurrenceMonth;
|
||||
public $recurrenceYear;
|
||||
public $recurrenceWeek;
|
||||
public $recurrenceHours;
|
||||
public $recurrenceMinutes;
|
||||
public $time;
|
||||
public $day;
|
||||
public $week;
|
||||
public $month;
|
||||
public $exceptionInterval;
|
||||
}
|
||||
|
||||
class interval
|
||||
{
|
||||
public $startDate;
|
||||
public $endDate;
|
||||
}
|
||||
|
||||
class notificationMethod
|
||||
{
|
||||
public $url;
|
||||
public $msg;
|
||||
|
||||
function __construct($url)
|
||||
{
|
||||
$this->url = $url;
|
||||
}
|
||||
|
||||
public function send($text=null)
|
||||
{
|
||||
if (empty($text)) $text= $this->msg;
|
||||
if (!empty($msg))
|
||||
{
|
||||
$opts = array(
|
||||
'http'=>array(
|
||||
'method'=>"GET",
|
||||
'header'=>"Accept-language: fr\r\n"
|
||||
)
|
||||
);
|
||||
$context = stream_context_create($opts);
|
||||
|
||||
/* Envoi une requête HTTP vers $url
|
||||
avec les en-têtes additionnels ci-dessus */
|
||||
$fp = fopen($url . "/" . $msg, 'r', false, $context);
|
||||
$response = stream_get_contents($fp, -1, 0);
|
||||
fclose($fp);
|
||||
}else
|
||||
{
|
||||
// TODO log_error("notificationMethod : $msg is null");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//global variables
|
||||
$topics = array(); // list of topics
|
||||
$mids = array(); // list of message IDs
|
||||
$logFh = null; // filehandle of log file
|
||||
$devices = array(); // array of device objetcs
|
||||
$changed = array(); // list of changed devices
|
||||
$notificationMethods = array(); // array of notification methods objects
|
||||
$indexDevices = array(); // index devices by ieee_address
|
||||
$dbInit = false; // flag to indicate that desvices db is initialized
|
||||
$connected = false; // connected to MQTT server
|
||||
$nSubscribed = 0; // Number of topics subsribed
|
||||
$included = false; // flag indicate scripts are loaded
|
||||
$events = array(); // list of event objects
|
||||
|
||||
// topics definition
|
||||
$topics["zigbee2mqtt"] = new topic;
|
||||
|
||||
// gettext
|
||||
bindtextdomain("moha", "./locale");
|
||||
textdomain("moha");
|
||||
|
||||
//signal handling
|
||||
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
|
||||
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
|
||||
pcntl_signal(SIGINT, 'signalHandler');
|
||||
|
||||
|
||||
|
||||
$client = new Mosquitto\Client();
|
||||
if (!init()) exit(1);
|
||||
$client->onConnect('connectResponse');
|
||||
$client->onDisconnect('disconnectResponse');
|
||||
$client->onSubscribe('subscribeResponse');
|
||||
$client->onMessage('message');
|
||||
$client->onLog('logger');
|
||||
$client->onPublish('publishResponse');
|
||||
|
||||
$include 'mqtt_functions.php';
|
||||
|
||||
foreach($topics as $name => $topic)
|
||||
{
|
||||
//echo $name;
|
||||
$topic->mid = $client->subscribe($name . "/#", 2);
|
||||
$mids[$topic->mid] = $name;
|
||||
$topic->status = false;
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
$client->loop();
|
||||
if ($dbInit and ! $included)
|
||||
{
|
||||
$hooks = loadHooks("./scripts");
|
||||
if (!empty($hooks))
|
||||
{
|
||||
foreach ($hooks as $hook)
|
||||
{
|
||||
include $hook;
|
||||
}
|
||||
}
|
||||
}elseif($dbInit and $included)
|
||||
{
|
||||
checkEvents();
|
||||
}
|
||||
}
|
||||
|
||||
endMoha();
|
||||
|
||||
function init()
|
||||
{
|
||||
global $logFh, $client;
|
||||
date_default_timezone_set('Europe/Paris');
|
||||
$client->connect("192.168.1.253", 1883, 5);
|
||||
if (! $logFh = fopen("moha.log", "w") )
|
||||
{
|
||||
echo _("error opening log file");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function loadHooks($dir)
|
||||
{
|
||||
global $included;
|
||||
$hookList = array();
|
||||
$files = scandir($dir);
|
||||
//print_r($files);
|
||||
foreach ($files as $file)
|
||||
{
|
||||
//echo " $file" . EOL;
|
||||
if ($file != "." and $file != ".." and strpos($file, "~", -2) === false)
|
||||
{
|
||||
//echo "not . or .." . EOL;
|
||||
if (is_dir($file))
|
||||
{
|
||||
//echo "directory" . EOL;
|
||||
listHooks($dir . '/' . $file);
|
||||
}else
|
||||
{
|
||||
//echo "file" . EOL;
|
||||
$hookList[] = $dir . "/" . $file;
|
||||
}
|
||||
}
|
||||
}
|
||||
//print_r($hookList);
|
||||
$included = true;
|
||||
return $hookList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
function endMoha()
|
||||
{
|
||||
global $topics, $nSubscribed ,$client;
|
||||
$x = 0;
|
||||
foreach($topics as $topic => $object)
|
||||
{
|
||||
if ($object->status)
|
||||
{
|
||||
$client->unsubscribe($topic);
|
||||
}
|
||||
}
|
||||
|
||||
while ($nSubscribed != 0)
|
||||
{
|
||||
//echo $nSubscribed;0x00124b0022ebac5c
|
||||
if ( $x++ > 30) exit (0);
|
||||
$client->loop();
|
||||
}
|
||||
$client->disconnect();
|
||||
fclose($logFh);
|
||||
}
|
||||
|
||||
|
||||
|
||||
?>
|
Reference in New Issue
Block a user