2022-01-15 10:52:03 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Documentation, License etc.
|
|
|
|
*
|
|
|
|
* @package linky2mqtt
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(ticks = 1);
|
|
|
|
|
|
|
|
$connected = false;
|
|
|
|
$logFh = null; // filehandle of log file
|
|
|
|
$curlErr = 0; // Number of errors returned by curl
|
2022-01-15 12:54:35 +01:00
|
|
|
$lastToken = array();
|
|
|
|
|
|
|
|
require $configDir . "/config.php";
|
|
|
|
require $configDir . "/taglist.php";
|
2022-01-15 10:52:03 +01:00
|
|
|
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);
|
|
|
|
dio_tcsetattr($bbSerialPort, array(
|
|
|
|
'baud' => $baudRate,
|
|
|
|
'bits' => $bits,
|
|
|
|
'stop' => $stopBit,
|
2022-01-15 12:54:35 +01:00
|
|
|
'parity' => $parity
|
2022-01-15 10:52:03 +01:00
|
|
|
));
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
2022-01-15 12:54:35 +01:00
|
|
|
|
|
|
|
// put each line of teleinformation stream in an array
|
2022-01-15 10:52:03 +01:00
|
|
|
$array = explode("\n", $string . $data);
|
|
|
|
//echo "LastToken " .EOL;print_r($lastToken);echo EOL;
|
|
|
|
|
2022-01-15 12:54:35 +01:00
|
|
|
// loop to analyze each line of teleinformation stream
|
2022-01-15 10:52:03 +01:00
|
|
|
foreach ($array as $key => $string)
|
|
|
|
{
|
|
|
|
$lastKey = array_key_last($array);
|
|
|
|
$token = array();
|
2022-01-15 12:54:35 +01:00
|
|
|
|
|
|
|
// make an array with each line of teleinfo stream
|
|
|
|
// row 1 is taglist
|
|
|
|
// row 2 is horodatage or data
|
|
|
|
// row 3 is data if row 2 is horodatage or cheksum if row 2 is data
|
|
|
|
// row 4 is check sum if row3 is data
|
|
|
|
|
2022-01-15 10:52:03 +01:00
|
|
|
$token[] = strtok($string, "\t");
|
|
|
|
while($token[] = strtok("\t"))
|
|
|
|
{}
|
2022-01-15 12:54:35 +01:00
|
|
|
|
|
|
|
//test if the precedent last tag is completed or not
|
2022-01-15 10:52:03 +01:00
|
|
|
if ($key === $lastKey)
|
|
|
|
{
|
2022-01-15 10:59:53 +01:00
|
|
|
//print("repere 1");
|
|
|
|
//var_dump($token);
|
2022-01-15 10:52:03 +01:00
|
|
|
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);
|
2022-01-15 12:54:35 +01:00
|
|
|
|
|
|
|
// checksum check
|
2022-01-15 10:52:03 +01:00
|
|
|
if (checkCRC($string, end($token)))
|
|
|
|
{
|
2022-01-15 12:54:35 +01:00
|
|
|
//add tag to list of tag to publish
|
2022-01-15 10:52:03 +01:00
|
|
|
array_pop($token);
|
|
|
|
mkLinkyDatas($token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sleep(1);
|
|
|
|
$client->loop();
|
|
|
|
}
|
|
|
|
|
|
|
|
function mkLinkyDatas($data)
|
|
|
|
{
|
|
|
|
static $started = false;
|
|
|
|
static $linkyDatas;
|
|
|
|
logger(DEBUG, _("function mkLinkyDatas"));
|
|
|
|
if (!$started)
|
|
|
|
{
|
|
|
|
logger(DEBUG, _("Array not started"));
|
|
|
|
if ($data[0] == "ADSC")
|
|
|
|
{
|
|
|
|
logger(DEBUG, _("ADSC found, i start making array"));
|
|
|
|
$started = true;
|
2022-01-15 12:54:35 +01:00
|
|
|
$linkyDatas = '{"friendlyName":"' . $friendlyName . '",';
|
2022-01-15 10:52:03 +01:00
|
|
|
json_add($data, $linkyDatas);
|
|
|
|
}
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
logger(DEBUG, _("Array already started"));
|
|
|
|
if ($data[0] == "ADSC")
|
|
|
|
{
|
|
|
|
$linkyDatas .= "}";
|
|
|
|
publish($linkyDatas);
|
|
|
|
logger(DEBUG, _("# Starting new array #"));
|
2022-01-15 12:54:35 +01:00
|
|
|
$linkyDatas = '{"friendlyName":"' . $friendlyName . '",'; //Reinitialisation of array
|
2022-01-15 10:52:03 +01:00
|
|
|
json_add($data, $linkyDatas);
|
|
|
|
}else
|
|
|
|
{
|
|
|
|
logger(DEBUG, _("Adding data to array"));
|
|
|
|
$linkyDatas.= ",";
|
|
|
|
json_add($data, $linkyDatas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
?>
|