Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mqtt support #1

Merged
merged 3 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 140 additions & 37 deletions bin/p1decrypter.py

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions config/p1decrypter-default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ SEND_TO_UDP=1
MINISERVER_ID=1
UDP_HOST=
UDP_PORT=54321
SEND_MQTT=0
MQTT_USE_GATEWAY=0
MQTT_BROKER=localhost
MQTT_BROKER_USERNAME=
MQTT_BROKER_PASSWORD=
MQTT_BROKER_PORT=1883
MQTT_TOPIC_PREFIX=p1decrypter
MQTT_TOPIC_QOS=1
SEND_TO_SERIAL_PORT=0
SERIAL_OUTPUT_PORT=/dev/p1decrypter
SERIAL_OUTPUT_BAUDRATE=115200
Expand Down
8 changes: 8 additions & 0 deletions config/p1decrypter.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ SEND_TO_UDP=1
MINISERVER_ID=1
UDP_HOST=
UDP_PORT=54321
SEND_MQTT=0
MQTT_USE_GATEWAY=0
MQTT_BROKER=localhost
MQTT_BROKER_USERNAME=
MQTT_BROKER_PASSWORD=
MQTT_BROKER_PORT=1883
MQTT_TOPIC_PREFIX=p1decrypter
MQTT_TOPIC_QOS=1
SEND_TO_SERIAL_PORT=0
SERIAL_OUTPUT_PORT=/dev/p1decrypter
SERIAL_OUTPUT_BAUDRATE=115200
Expand Down
6 changes: 3 additions & 3 deletions cron/cron.05min
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

# Will be executed as user "loxberry".

enabled=$(awk -F "=" '/ENABLED/ {print $2}' /opt/loxberry/config/plugins/p1decrypter/p1decrypter.cfg)
enabled=$(awk -F "=" '/ENABLED/ {print $2}' REPLACELBPCONFIGDIR/p1decrypter.cfg)

if ! pgrep -f p1decrypter.py >/dev/null && [[ $enabled == "1" || $1 == "1" ]] && [ -e /opt/loxberry/bin/plugins/p1decrypter/p1decrypter.py ]
if ! pgrep -f p1decrypter.py >/dev/null && [[ $enabled == "1" || $1 == "1" ]] && [ -e REPLACELBPBINDIR/p1decrypter.py ]
then
/usr/bin/python3 /opt/loxberry/bin/plugins/p1decrypter/p1decrypter.py KEY --logfile=/opt/loxberry/log/plugins/p1decrypter/p1decrypter.log --configfile=/opt/loxberry/config/plugins/p1decrypter/p1decrypter.cfg >> /opt/loxberry/log/plugins/p1decrypter/p1decrypter.log 2>&1
/usr/bin/python3 REPLACELBPBINDIR/p1decrypter.py KEY --logfile=REPLACELBPLOGDIR/p1decrypter.log --configfile=REPLACELBPCONFIGDIR/p1decrypter.cfg >> REPLACELBPLOGDIR/p1decrypter.log 2>&1
fi
3 changes: 2 additions & 1 deletion dpkg/apt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
#
python3
python3-serial
python3-pycryptodome
python3-pycryptodome
python3-paho-mqtt
2 changes: 1 addition & 1 deletion plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ [email protected]
# The version of your plugin - important if you would like to write an
# upgrade script. Use a correct syntax, which is supported by LoxBerry:
# More info: http://www.loxwiki.eu/x/LYG3AQ
VERSION=1.1.1
VERSION=1.2.0

# Short name and prefered subfolder of your Plugin (do not use blanks - they
# will be filtered - and use lowercase only)! Used for script names in
Expand Down
14 changes: 9 additions & 5 deletions postupgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ PBIN=$LBPBIN/$PDIR
echo "<INFO> Copy back existing config files"
cp -v /tmp/$PTEMPDIR/_upgrade/config/p1decrypter.cfg $PCONFIG/p1decrypter.cfg

# echo "<INFO> Adding new config parameters"
# grep -q -F "VERBOSE=" $PCONFIG/p1decrypter.cfg || echo "VERBOSE=0" >> $PCONFIG/p1decrypter.cfg
echo "<INFO> Adding new config parameters"
grep -q -F "SEND_MQTT=" $PCONFIG/p1decrypter.cfg || echo "SEND_MQTT=0" >> $PCONFIG/p1decrypter.cfg
grep -q -F "MQTT_USE_GATEWAY=" $PCONFIG/p1decrypter.cfg || echo "MQTT_USE_GATEWAY=0" >> $PCONFIG/p1decrypter.cfg
grep -q -F "MQTT_BROKER=" $PCONFIG/p1decrypter.cfg || echo "MQTT_BROKER=" >> $PCONFIG/p1decrypter.cfg
grep -q -F "MQTT_BROKER_USERNAME=" $PCONFIG/p1decrypter.cfg || echo "MQTT_BROKER_USERNAME=" >> $PCONFIG/p1decrypter.cfg
grep -q -F "MQTT_BROKER_PASSWORD=" $PCONFIG/p1decrypter.cfg || echo "MQTT_BROKER_PASSWORD=" >> $PCONFIG/p1decrypter.cfg
grep -q -F "MQTT_BROKER_PORT=" $PCONFIG/p1decrypter.cfg || echo "MQTT_BROKER_PORT=1883" >> $PCONFIG/p1decrypter.cfg
grep -q -F "MQTT_TOPIC_PREFIX=" $PCONFIG/p1decrypter.cfg || echo "MQTT_TOPIC_PREFIX=p1decrypter" >> $PCONFIG/p1decrypter.cfg
grep -q -F "MQTT_TOPIC_QOS=" $PCONFIG/p1decrypter.cfg || echo "MQTT_TOPIC_QOS=1" >> $PCONFIG/p1decrypter.cfg

echo "<INFO> Remove temporary folders"
rm -rf /tmp/$PTEMPDIR/_upgrade

echo "<INFO> Start P1 Decrypter"
/bin/bash $PDIR/system/cron/cron.05min/p1decrypter > /dev/null 2>&1 &

exit 0
4 changes: 2 additions & 2 deletions prerelease.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
# the donwload link here


VERSION=1.1.1
VERSION=1.2.0

# Download URL of the ZIP Archive
ARCHIVEURL=https://github.com/metrophos/LoxBerry-Plugin-P1-Decrypter/archive/1.1.1.zip
ARCHIVEURL=https://github.com/metrophos/LoxBerry-Plugin-P1-Decrypter/archive/1.2.0.zip

# URL for further information about this release
INFOURL=https://github.com/metrophos/LoxBerry-Plugin-P1-Decrypter/releases
149 changes: 118 additions & 31 deletions templates/content.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<td>Value Mapping</td>
<td>
<TMPL_VAR MAPPING>
<span class="hint">Value mapping of Smart Meter output. Only one per line <i>'label','regex'</i></span>
<span class="hint">Value mapping of Smart Meter output. Only one per line: <i>'label','regex'</i></span>
</td>
</tr>
<tr>
Expand All @@ -35,20 +35,7 @@
</td>
</tr>
<tr>
<td>Miniserver UDP-Port</td>
<td>
<TMPL_VAR UDP_PORT>
</td>
</tr>
<tr>
<td>SEND_TO_UDP</td>
<td>
<TMPL_VAR SEND_TO_UDP>
</td>
</tr>
<tr>
<td><h2>Advanced Options</h2></td>
<td></td>
<td colspan="2"><h2>Advanced Options</h2></td>
</tr>
<tr>
<td>Serial Input Port</td>
Expand Down Expand Up @@ -80,19 +67,113 @@
<TMPL_VAR AAD>
</td>
</tr>
<tr>
<td>Raw</td>
<td>
<TMPL_VAR RAW>
<p class="hint">
Disable mapping and send encrypted Smart Meter output directly
</p>
</td>
</tr>
<tr>
<td>Verbose</td>
<td>
<TMPL_VAR VERBOSE>
<p class="hint">
Advanced debug log
</p>
</td>
</tr>
<tr>
<td colspan="2"><h3>UDP Options</h3></td>
</tr>
<tr>
<td>Send over UDP</td>
<td>
<TMPL_VAR SEND_TO_UDP>
</td>
</tr>
<tr>
<td>Miniserver UDP-Port</td>
<td>
<TMPL_VAR UDP_PORT>
</td>
</tr>
<tr>
<td>UDP Host</td>
<td>
<TMPL_VAR UDP_HOST>
<span class="hint">If set, this option overrules the Miniserver as host</span>
</td>
</tr>
<tr>
<td colspan="2"><h3>MQTT Options</h3></td>
</tr>
<tr>
<td>Send over MQTT</td>
<td>
<TMPL_VAR SEND_MQTT>
<p class="hint">
Send data as MQTT Messages
</p>
</td>
</tr>
<tr>
<td>Use MQTT Gateway</td>
<td>
<TMPL_VAR MQTT_USE_GATEWAY>
<p class="hint">
Use MQTT Gateway Plugin configuration
</p>
</td>
</tr>
<tr class="mqtt">
<td>MQTT Broker</td>
<td>
<TMPL_VAR MQTT_BROKER>
</td>
</tr>
<tr class="mqtt">
<td>MQTT Broker Username</td>
<td>
<TMPL_VAR MQTT_BROKER_USERNAME>
</td>
</tr>
<tr class="mqtt">
<td>MQTT Broker Password</td>
<td>
<TMPL_VAR MQTT_BROKER_PASSWORD>
</td>
</tr>
<tr class="mqtt">
<td>MQTT Port</td>
<td>
<TMPL_VAR MQTT_BROKER_PORT>
</td>
</tr>
<tr>
<td>MQTT Topic Prefix</td>
<td>
<TMPL_VAR MQTT_TOPIC_PREFIX>
<p class="hint mqtt_topic_prefix"></p>
</td>
</tr>
<tr>
<td>MQTT Topic QOS</td>
<td>
<TMPL_VAR MQTT_TOPIC_QOS>
</td>
</tr>
<tr>
<td colspan="2"><h3>Serial Output Options</h3></td>
</tr>
<tr>
<td>Send To Serial Port</td>
<td>
<TMPL_VAR SEND_TO_SERIAL_PORT>
<p class="hint">
P1 Decrypter will send data to specified output serial port.
Send data to specified output serial port.
Generating virtual serial port example: <i>socat -d -d pty,raw,echo=0,link=/dev/p1decrypter pty,raw,echo=0,link=/dev/smartmeter/p1decrypter</i>
</p>
</td>
Expand Down Expand Up @@ -121,21 +202,6 @@
<TMPL_VAR SERIAL_OUTPUT_STOPBITS>
</td>
</tr>
<tr>
<td>Raw</td>
<td>
<TMPL_VAR RAW>
<p class="hint">
Disable mapping and send encrypted Smart Meter output directly
</p>
</td>
</tr>
<tr>
<td>Verbose</td>
<td>
<TMPL_VAR VERBOSE>
</td>
</tr>
<tr>
<td>&nbsp</td>
<td>
Expand All @@ -155,6 +221,27 @@
$('select[name=send_to_serial_port]').attr('data-role', 'flipswitch');
$('select[name=raw]').attr('data-role', 'flipswitch');
$('select[name=verbose]').attr('data-role', 'flipswitch');
$('select[name=send_mqtt]').attr('data-role', 'flipswitch');
$('select[name=mqtt_use_gateway]').attr('data-role', 'flipswitch');

if($('select[name=mqtt_use_gateway]').val() === '0') {
$('.mqtt').show();
$('.mqtt_topic_prefix').hide();
} else {
$('.mqtt').hide();
$('.mqtt_topic_prefix').text(`Please don't forget to add MQTT Subscriptions: ${$('input[name=mqtt_topic_prefix]').val()}/#`);
$('.mqtt_topic_prefix').show();
}
$('select[name=mqtt_use_gateway]').on('change', function() {
if($('select[name=mqtt_use_gateway]').val() === '0') {
$('.mqtt').show();
$('.mqtt_topic_prefix').hide();
} else {
$('.mqtt').hide();
$('.mqtt_topic_prefix').text(`Please don't forget to add MQTT Subscriptions: ${$('input[name=mqtt_topic_prefix]').val()}/#`);
$('.mqtt_topic_prefix').show();
}
});

// Reset to default
let reset = false;
Expand Down
64 changes: 64 additions & 0 deletions webfrontend/htmlauth/index.cgi
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ if ($R::action eq "Save & Restart") {
$p1decrypterCfg->param('P1DECRYPTER.AAD', $R::aad);
$p1decrypterCfg->param('P1DECRYPTER.SEND_TO_UDP', $R::send_to_udp);
$p1decrypterCfg->param('P1DECRYPTER.UDP_HOST', $R::udp_host);

$p1decrypterCfg->param('P1DECRYPTER.SEND_MQTT', $R::send_mqtt);
$p1decrypterCfg->param('P1DECRYPTER.MQTT_USE_GATEWAY', $R::mqtt_use_gateway);
$p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER', $R::mqtt_broker);
$p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER_USERNAME', $R::mqtt_broker_username);
$p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER_PASSWORD', $R::mqtt_broker_password);
$p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER_PORT', $R::mqtt_broker_port);
$p1decrypterCfg->param('P1DECRYPTER.MQTT_TOPIC_PREFIX', $R::mqtt_topic_prefix);
$p1decrypterCfg->param('P1DECRYPTER.MQTT_TOPIC_QOS', $R::mqtt_topic_qos);

$p1decrypterCfg->param('P1DECRYPTER.SEND_TO_SERIAL_PORT', $R::send_to_serial_port);
$p1decrypterCfg->param('P1DECRYPTER.SERIAL_OUTPUT_PORT', $R::serial_output_port);
$p1decrypterCfg->param('P1DECRYPTER.SERIAL_OUTPUT_BAUDRATE', $R::serial_output_baudrate);
Expand All @@ -66,6 +76,16 @@ if ($R::action eq "Reset default configruation") {
$p1decrypterCfg->param('P1DECRYPTER.AAD', $p1decrypterDefaultCfg->param('P1DECRYPTER.AAD'));
$p1decrypterCfg->param('P1DECRYPTER.SEND_TO_UDP', $p1decrypterDefaultCfg->param('P1DECRYPTER.SEND_TO_UDP'));
$p1decrypterCfg->param('P1DECRYPTER.UDP_HOST', $p1decrypterDefaultCfg->param('P1DECRYPTER.UDP_HOST'));

$p1decrypterCfg->param('P1DECRYPTER.SEND_MQTT', $p1decrypterDefaultCfg->param('P1DECRYPTER.SEND_MQTT'));
$p1decrypterCfg->param('P1DECRYPTER.MQTT_USE_GATEWAY', $p1decrypterDefaultCfg->param('P1DECRYPTER.SEND_MQTT'));
$p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER', $p1decrypterDefaultCfg->param('P1DECRYPTER.MQTT_BROKER'));
$p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER_USERNAME', $p1decrypterDefaultCfg->param('P1DECRYPTER.MQTT_BROKER_USERNAME'));
$p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER_PASSWORD', $p1decrypterDefaultCfg->param('P1DECRYPTER.MQTT_BROKER_PASSWORD'));
$p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER_PORT', $p1decrypterDefaultCfg->param('P1DECRYPTER.MQTT_BROKER_PORT'));
$p1decrypterCfg->param('P1DECRYPTER.MQTT_TOPIC_PREFIX', $p1decrypterDefaultCfg->param('P1DECRYPTER.MQTT_TOPIC_PREFIX'));
$p1decrypterCfg->param('P1DECRYPTER.MQTT_TOPIC_QOS', $p1decrypterDefaultCfg->param('P1DECRYPTER.MQTT_TOPIC_QOS'));

$p1decrypterCfg->param('P1DECRYPTER.SEND_TO_SERIAL_PORT', $p1decrypterDefaultCfg->param('P1DECRYPTER.SEND_TO_SERIAL_PORT'));
$p1decrypterCfg->param('P1DECRYPTER.SERIAL_OUTPUT_PORT', $p1decrypterDefaultCfg->param('P1DECRYPTER.SERIAL_OUTPUT_PORT'));
$p1decrypterCfg->param('P1DECRYPTER.SERIAL_OUTPUT_BAUDRATE', $p1decrypterDefaultCfg->param('P1DECRYPTER.SERIAL_OUTPUT_BAUDRATE'));
Expand All @@ -92,6 +112,16 @@ $R::serial_input_stopbits = $p1decrypterCfg->param('P1DECRYPTER.SERIAL_INPUT_STO
$R::aad = $p1decrypterCfg->param('P1DECRYPTER.AAD');
$R::send_to_udp = $p1decrypterCfg->param('P1DECRYPTER.SEND_TO_UDP');
$R::udp_host = $p1decrypterCfg->param('P1DECRYPTER.UDP_HOST');

$R::send_mqtt = $p1decrypterCfg->param('P1DECRYPTER.SEND_MQTT');
$R::mqtt_use_gateway = $p1decrypterCfg->param('P1DECRYPTER.MQTT_USE_GATEWAY');
$R::mqtt_broker = $p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER');
$R::mqtt_broker_username = $p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER_USERNAME');
$R::mqtt_broker_password = $p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER_PASSWORD');
$R::mqtt_broker_port = $p1decrypterCfg->param('P1DECRYPTER.MQTT_BROKER_PORT');
$R::mqtt_topic_prefix = $p1decrypterCfg->param('P1DECRYPTER.MQTT_TOPIC_PREFIX');
$R::mqtt_topic_qos = $p1decrypterCfg->param('P1DECRYPTER.MQTT_TOPIC_QOS');

$R::send_to_serial_port = $p1decrypterCfg->param('P1DECRYPTER.SEND_TO_SERIAL_PORT');
$R::serial_output_port = $p1decrypterCfg->param('P1DECRYPTER.SERIAL_OUTPUT_PORT');
$R::serial_output_baudrate = $p1decrypterCfg->param('P1DECRYPTER.SERIAL_OUTPUT_BAUDRATE');
Expand Down Expand Up @@ -178,6 +208,40 @@ $template->param(SEND_TO_UDP => $cgi->popup_menu(
## UDP_HOST textfield
$template->param(UDP_HOST => $cgi->textfield(-name => 'udp_host', -default => $R::udp_host));

## SEND_MQTT switch
$template->param(SEND_MQTT => $cgi->popup_menu(
-name => 'send_mqtt',
-values => \@switch,
-labels => \%switchLabels,
-default => $R::send_mqtt
));

## MQTT_USE_GATEWAY switch
$template->param(MQTT_USE_GATEWAY => $cgi->popup_menu(
-name => 'mqtt_use_gateway',
-values => \@switch,
-labels => \%switchLabels,
-default => $R::mqtt_use_gateway
));

## MQTT_BROKER textfield
$template->param(MQTT_BROKER => $cgi->textfield(-name => 'mqtt_broker', -default => $R::mqtt_broker));

## MQTT_BROKER_USERNAME textfield
$template->param(MQTT_BROKER_USERNAME => $cgi->textfield(-name => 'mqtt_broker_username', -default => $R::mqtt_broker_username));

## MQTT_BROKER_PASSWORD password field
$template->param(MQTT_BROKER_PASSWORD => $cgi->password_field(-name => 'mqtt_broker_password', -default => $R::mqtt_broker_password));

## MQTT_BROKER_PORT textfield
$template->param(MQTT_BROKER_PORT => $cgi->textfield(-name => 'mqtt_broker_port', -default => $R::mqtt_broker_port));

## MQTT_TOPIC_PREFIX textfield
$template->param(MQTT_TOPIC_PREFIX => $cgi->textfield(-name => 'mqtt_topic_prefix', -default => $R::mqtt_topic_prefix));

## MQTT_TOPIC_QOS textfield
$template->param(MQTT_TOPIC_QOS => $cgi->textfield(-name => 'mqtt_topic_qos', -default => $R::mqtt_topic_qos));

## SEND_TO_SERIAL_PORT switch
$template->param(SEND_TO_SERIAL_PORT => $cgi->popup_menu(
-name => 'send_to_serial_port',
Expand Down