1
0

cleaning of the code

This commit is contained in:
daniel Tartavel 2022-01-15 12:54:35 +01:00
parent b7d2b8def5
commit 718c127954
7 changed files with 58 additions and 66 deletions

15
config/config.php Normal file
View File

@ -0,0 +1,15 @@
<?php
//if multiple linky then change friendlyName for each
$friendlyName = "Linky";
$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;
$parity = 1;

View File

@ -0,0 +1,22 @@
<?php
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);
?>

BIN
doc/Enedis-NOI-CPT_54E.pdf Normal file

Binary file not shown.

View File

@ -4,57 +4,26 @@
*
* @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
$lastToken = array();
require "taglist.php";
require $configDir . "/config.php";
require $configDir . "/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
'parity' => $parity
));
while(!$bbSerialPort)
@ -64,7 +33,6 @@ while(!$bbSerialPort)
}
logger(INFO, _("Connected to serial port: " . $portName));
$lastToken = array();
while(1)
{
@ -82,18 +50,28 @@ while(1)
$string .= $lt . "\t";
}
}
// put each line of teleinformation stream in an array
$array = explode("\n", $string . $data);
//echo "LastToken " .EOL;print_r($lastToken);echo EOL;
// loop to analyze tags
// loop to analyze each line of teleinformation stream
foreach ($array as $key => $string)
{
$lastKey = array_key_last($array);
$token = array();
// 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
$token[] = strtok($string, "\t");
while($token[] = strtok("\t"))
{}
//test if the last tag is completed or not
//test if the precedent last tag is completed or not
if ($key === $lastKey)
{
//print("repere 1");
@ -128,42 +106,34 @@ while(1)
}
}
array_pop($token);
// checksum check
if (checkCRC($string, end($token)))
{
//add tag to list of tag to publish
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 = "{";
$linkyDatas = '{"friendlyName":"' . $friendlyName . '",';
json_add($data, $linkyDatas);
//echo $data[0] . "=>" . $data[1] .EOL;
}
}else
{
@ -173,20 +143,15 @@ function mkLinkyDatas($data)
$linkyDatas .= "}";
publish($linkyDatas);
logger(DEBUG, _("# Starting new array #"));
$linkyDatas = "{"; //Reinitialisation of array
//$linkyDatas[][$data[0]] = $data[1];
$linkyDatas = '{"friendlyName":"' . $friendlyName . '",'; //Reinitialisation of array
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)

View File

@ -40,7 +40,6 @@ function messageReceived($message)
{
$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));
}
}
@ -56,11 +55,9 @@ function publish($payload, $commande="")
$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);
@ -125,12 +122,9 @@ function publishResponse($mid)
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;
}
}

View File

@ -53,10 +53,6 @@ function signalHandler($signal)
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);