1
0

first stable version

This commit is contained in:
daniel Tartavel 2022-01-15 10:52:03 +01:00
parent 8d5797547a
commit e08470856c
4 changed files with 462 additions and 0 deletions

203
linky2mqtt.php Normal file
View File

@ -0,0 +1,203 @@
<?php
/**
* Documentation, License etc.
*
* @package linky2mqtt
*/
define("CONTACT_SEC", 2**0);
define("ORGANE_COUPURE", 2**1 | 2**2 | 2**3);
define("CACHE_BORNE", 2**4);
define("SURTENSION", 2**6);
define("DEPASSEMENT", 2**7);
define("PRODUCTEUR_CONSOMMATEUR", 2**8);
define("SENS_ENERGIE_ACTIVE", 2**9); // o : positive, 1 : negative
define("TARIF_FOURNITURE", 2**10 | 2**11 | 2**12 | 2**13); // 4 bits
define("TARIF_DISTRIBUTEUR", 2**14 | 2**15); // 2 bits
define("MODE_HORLOGE", 2**16); // 0 : correcte 1 : dégradé
define("MODE_LINKY", 2**17); // 0 : standard 1 : historique
define("EURIDIS", 2**19| 2**20); // 00 = désactivée 01 = activée 11 = activée avec sécurité
define("STATUS_CPL", 2**21 | 2**22);
define("SYNCHRO_CPL", 2**23);
define("TEMPO", 2**24 | 2**25);
define("TEMPO_LENDEMAIN", 2**26 | 2**27);
define("PREAVIS_POINTE", 2**28 | 2**29);
define("POINTE", 2**30 | 2**31);
declare(ticks = 1);
$friendlyName = "Linky";
$ieeeAdddress = "0xxxx";
$connected = false;
$logFh = null; // filehandle of log file
$curlErr = 0; // Number of errors returned by curl
$configDir = "./config";
$topicName = "linky2mqtt";
$mqttServerAddress = "127.0.0.1";
$mqttServerPort = "1883";
$portName = "/dev/serial/by-id/usb-FTDI_FT230X_Basic_UART_DN04S0O2-if00-port0";
$reconnectDelay = 10;
$baudRate = 9600;
$bits = 7;
$stopBit = 1;
$linky;
$exit = false; //TODO to remove
require "taglist.php";
require "mqtt_functions.php";
logger(INFO, _("Connecting to serial port: " . $portName));
$bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK );
dio_fcntl($bbSerialPort, F_SETFL, O_SYNC);
//we're on 'nix configure com from php direct io function
dio_tcsetattr($bbSerialPort, array(
'baud' => $baudRate,
'bits' => $bits,
'stop' => $stopBit,
'parity' => 1
));
while(!$bbSerialPort)
{
logger(ERROR, sprintf(_("Connection to Linky can not be established, retrying in %s seconds"), $reconnectDelay));
sleep($reconnectDelay);
}
logger(INFO, _("Connected to serial port: " . $portName));
$lastToken = array();
while(1)
{
$data = dio_read($bbSerialPort); //this is a blocking call
if ($data)
{
$string = "";
logger(DEBUG, _("Data Received: "));
// add the precedent last incomplete tag
foreach ($lastToken as $lt)
{
if (isset($lt))
{
$string .= $lt . "\t";
}
}
$array = explode("\n", $string . $data);
//echo "LastToken " .EOL;print_r($lastToken);echo EOL;
// loop to analyze tags
foreach ($array as $key => $string)
{
$lastKey = array_key_last($array);
$token = array();
$token[] = strtok($string, "\t");
while($token[] = strtok("\t"))
{}
//test if the last tag is completed or not
if ($key === $lastKey)
{
print("repere 1");
var_dump($token);
if (isset($token[0]))
{
logger(DEBUG, _("ẗoken[0] is set"));
if (array_key_exists($token[0], $taglist))
{
logger(DEBUG, _("ẗoken[0] exists in taglist"));
if (($taglist[$token[0]] & HORODATAGE) and !isset($token[3]))
{
logger(DEBUG, _("Token[3] not set"));
$lastToken = $token;
break;
}elseif (!isset($token[2]) or !isset($token[1]))
{
logger(DEBUG, _("Token[1] or Token[2] not set"));
$lastToken = $token;
break;
}
}else
{
logger(DEBUG, _("last Token not in taglist"));
$lastToken = $token;
break;
}
}else
{
logger(WARNING, _("Token[0] is not set"));
break;
}
}
array_pop($token);
if (checkCRC($string, end($token)))
{
array_pop($token);
mkLinkyDatas($token);
}
}
}
sleep(1);
/*if ($exit === false)
{
$exit = true;
}else {
exit();
}*/
$client->loop();
}
function mkLinkyDatas($data)
{
static $started = false;
static $linkyDatas;
echo $data[0] . EOL;
logger(DEBUG, _("function mkLinkyDatas"));
if (!$started)
{
logger(DEBUG, _("Array not started"));
echo "is ADSC ?" . $data[0] .EOL;
if ($data[0] == "ADSC")
{
logger(DEBUG, _("ADSC found, i start making array"));
$started = true;
//$linkyDatas[][$data[0]] = $data[1];
$linkyDatas = "{";
json_add($data, $linkyDatas);
echo $data[0] . "=>" . $data[1] .EOL;
}
}else
{
logger(DEBUG, _("Array already started"));
if ($data[0] == "ADSC")
{
$linkyDatas .= "}";
publish($linkyDatas);
logger(DEBUG, _("# Starting new array #"));
$linkyDatas = "{"; //Reinitialisation of array
//$linkyDatas[][$data[0]] = $data[1];
json_add($data, $linkyDatas);
echo $data[0] . "=>" . $data[1] .EOL;
}else
{
logger(DEBUG, _("Adding data to array"));
//$linkyDatas[][$data[0]] = $data[1];
$linkyDatas.= ",";
json_add($data, $linkyDatas);
echo $data[0] . "=>" . $data[1] . EOL;
}
}
//echo $linkyDatas[array_key_last($linkyDatas)][$data[0]] ." => " . $linkyDatas[array_key_last($linkyDatas)][$data[1]];
}
function json_add($data, &$jsonString)
{
if (count($data) == 3)
{
$jsonString .= '"' . $data[0] . '":{"' . $data[1] . '":"' . trim($data[2]) . '"}';
}else
{
$jsonString .= '"' . $data[0] . '":"' . trim($data[1]) . '"';
}
return $jsonString;
}
?>

137
mqtt_functions.php Normal file
View File

@ -0,0 +1,137 @@
<?php
require "utils.php";
$mids = array();
$client = new Mosquitto\Client();
// log levels
define( "DEBUG", $client::LOG_DEBUG); // => 16
define( "INFO", $client::LOG_INFO); // => 1
define( "NOTICE", $client::LOG_NOTICE); // => 2
define( "WARNING", $client::LOG_WARNING); // => 4
define( "ERROR", $client::LOG_ERR); // => 8
define( "ALERT", 32);
define( "ALL", DEBUG | INFO | NOTICE | WARNING | ERROR | ALERT);
$logLevel = ALL ; //INFO | WARNING | ERROR | ALERT;
//$notificationLevel = WARNING | ERROR; // TODO send notification
logger(DEBUG, _("defining callback functions"));
// defining callback functions
$client->onConnect('connectResponse');
$client->onDisconnect('disconnectResponse');
$client->onSubscribe('subscribeResponse');
$client->onUnsubscribe('unsubscribeResponse');
$client->onMessage('messageReceived');
$client->onLog('logger');
$client->onPublish('publishResponse');
logger(DEBUG, _("connecting to mqtt server"));
$client->connect($mqttServerAddress, $mqttServerPort, 30);
logger(DEBUG, _("subscribing"));
$mid = $client->subscribe($topicName . "/#", 2);
function messageReceived($message)
{
global $topicName, $logFh;
$topic = explode ("/", $message->topic);
if($topic[array_key_last($topic)] != "get" and ($topic[array_key_last($topic)]) != "set")
{
$topic = explode ("/", $message->topic, 2); // get topic name
$fnTree = explode ("/" , $topic[1]); // get friendlyname
echo $topic[0] . " => " . $topic[1] . EOL;
logger(INFO, print_r(json_decode($message->payload), true));
}
}
// payload is an array :
// $key is property => $value is value of the parameter
function publish($payload, $commande="")
{
global $mids, $friendlyName, $topicName, $client, $logFh;
//print_r($payload);
$string = $topicName . "/" . $friendlyName;
$mid = $client->publish($string, $payload , 2);
if (isset($mids[$mid]))
{
//echo "unsetting mids" .EOL;
unset ($mids[$mid]);
}else
{
//echo "setting mids" .EOL;
$mids[$mid] = true;
}
logger(LOG_INFO, $logFh, "Publishing " . $string . " with payload => " . $payload);
}
function connectResponse($r, $message)
{
global $connected;
echo sprintf(_("I got code %d and message : '%s'"), $r, $message) . EOL;
switch ($r)
{
case 0:
logger(INFO, _("Successfull connection"));
$connected = true;
break;
case 1:
logger(INFO, _("Connection refused : unacceptable protocol version"));
$connected = false;
break;
case 2:
logger(INFO, _("Connection refused : identifier rejected"));
$connected = false;
break;
case 3:
logger(INFO, _("Connection refused (broker unavailable )"));
$connected = false;
break;
}
return $connected;
}
function subscribeResponse($mid, $qosCount)
{
global $topics;
logger(INFO, _("Subscribed"));
}
function unsubscribeResponse($mid)
{
global $client;
logger(INFO, _("Unsubscribed"));
$client->disconnect();
}
function disconnectResponse($r)
{
global $connected;
if ($r != 0)
{
$str = _('Badly ');
}else
{
$str = _('Cleanly ');
}
logger(INFO, $str . _("disconnected from server"));
$connected = false;
}
function publishResponse($mid)
{
global $mids, $events;
logger(LOG_INFO, "Event with mid = " . $mid . " published by MQTT broker");
if (isset($mids[$mid]))
{
//echo "unsetting mids" . EOL;
unset ($mids[$mid]);
//print_r($mids);
}else
{
//echo "setting mids" . EOL;
$mids[$mid] = true;
}
}
?>

41
taglist.php Normal file
View File

@ -0,0 +1,41 @@
<?php
define("HORODATAGE", 1);
$taglist = array(
"ADSC" => null,
"VTIC" => null,
"DATE" => null,
"NGTF" => null,
"LTARF" => null,
"EAST" => null,
"EASF01" => null,
"EASF02" => null,
"EASF03" => null,
"EASF04" => null,
"EASF05" => null,
"EASF06" => null,
"EASF07" => null,
"EASF08" => null,
"EASF09" => null,
"EASF10" => null,
"EASD01" => null,
"EASD02" => null,
"EASD03" => null,
"EASD04" => null,
"IRMS1" => null,
"URMS1" => null,
"PREF" => null,
"PCOUP" => null,
"SINSTS" => null,
"SMAXSN" => HORODATAGE,
"SMAXSN-1" => HORODATAGE,
"UMOY1" => HORODATAGE,
"STGE" => null,
"MSG1" => null,
"PRM" => null,
"RELAIS" => null,
"NTARF" => null,
"NJOURF" => null,
"NJOURF+1" => null,
"PJOURF+1" => null,
);

81
utils.php Normal file
View File

@ -0,0 +1,81 @@
<?php
define( "EOL", "\n");
cli_set_process_title($topicName);
file_put_contents("/proc/".getmypid()."/comm",$topicName);
function checkCRC($t, $crc)
{
logger(DEBUG, _("Entering function checkCRC"));
$s1 = 0;
$t = substr($t, 0, -2);
$a = str_split($t);
foreach($a as $char)
{
$s1 = ($s1 + ord($char));
}
$s1 = ($s1 & 0x3F) + 0x20;
logger(DEBUG, sprintf(_("resultat = %x => CRC = %x"), $s1, ord($crc)));
if ($s1 == ord($crc))
{
return true;
}else
{
return false;
}
}
function now()
{
return new DateTime("now");
}
function signalHandler($signal)
{
global $bbSerialPort, $connected, $client, $logFh;
$x = 0;
dio_close($bbSerialPort);
while ($connected)
{
if ( $x++ > 60)
{
$client->disconnect();
fclose($logFh);
exit (1);
}
$client->loop();
}
sleep(4);
fclose($logFh);
exit(0);
}
function logger($level, $log, $notif = false)
{
global $logFh, $logLevel, $notificationLevel;
//echo "=====>>>> $level => $logLevel => $notificationLevel" . EOL ;
//echo $log .EOL;
//var_dump($level);
//var_dump($log);
if ($level & $logLevel)
{
fwrite($logFh, "$level : $log" . EOL);
print ("$level : $log" . EOL);
}
}
date_default_timezone_set('Europe/Paris');
if (! $logFh = fopen("/var/log/pws2mqtt.log", "w") )
{
echo _("error opening log file") . EOL;
}else
{
echo _("Log file opened") . EOL;
}
//signal handling
pcntl_signal(SIGTERM, 'signalHandler');// Termination ('kill' was called)
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
pcntl_signal(SIGINT, 'signalHandler');
?>