204 lines
5.0 KiB
PHP
204 lines
5.0 KiB
PHP
<?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;
|
|
}
|
|
?>
|