Skip to content

Commit

Permalink
Bunch of bugfixes...
Browse files Browse the repository at this point in the history
  • Loading branch information
mschlenstedt committed Aug 10, 2021
1 parent 8c3d7ea commit d16821c
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 45 deletions.
5 changes: 5 additions & 0 deletions bin/create1wgpio.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
grep -i "dtoverlay=w1-gpio,gpiopin=" /boot/config.txt #überprüfen ob die enträge schon vorhanden sind
if [ $? -eq 0 ]; then echo"" #wenn sie schon vorhanden sind nichts tun
else echo "dtoverlay=w1-gpio,gpiopin=4,pullup=on" >> /boot/config.txt #ansonsten die Einträge einfügen
fi
2 changes: 1 addition & 1 deletion bin/createftdi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ if [ ! $1 ]; then
echo "usage: $0 SERIAL"
fi

echo "server: device = ftdi:s:0x0403:0x6001:$1" > REPLACELBPDATADIR/ftdidevices.dat
echo "server: device = ftdi:s:0x0403:0x6001:$1" > /opt/loxberry/data/plugins/1-wire-ng/ftdidevices.dat
35 changes: 26 additions & 9 deletions bin/owfs2mqtt.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
use LoxBerry::System;
use LoxBerry::Log;
use LoxBerry::JSON;
use Getopt::Long;
use Net::MQTT::Simple;
use OWNet;
use Time::HiRes qw ( sleep time );
use CGI;
#use CGI;
#use warnings;
use strict;
use Data::Dumper;
Expand Down Expand Up @@ -55,8 +56,17 @@
};

# Command line options
my $cgi = CGI->new;
$cgi->import_names('R');
#my $cgi = CGI->new;
#$cgi->import_names('R');

# Commandline options
# CGI doesn't work from other CGI skripts... :-(
#my $cgi = CGI->new;
#my $q = $cgi->Vars;
my $verbose;
my $bus;
GetOptions ('verbose=s' => \$verbose,
'bus=s' => \$bus);

# Logging
# Create a logging object
Expand All @@ -67,22 +77,22 @@ package => '1-wire-ng',
);

# Verbose
if ($R::verbose || $R::v) {
if ($verbose) {
$log->stdout(1);
$log->loglevel(7);
}

LOGSTART "Starting owfs2mqtt";

# Bus to read
if ($R::bus eq "") {
if ($bus eq "") {
$log->stdout(1);
LOGERR "You have to specify the bus you would like to read. Exiting.";
exit 1;
} else {
LOGINF "Reading from Bus.$R::bus";
$bus = "/bus." . $R::bus;
LOGTITLE "Daemon owfs2mqtt for Bus.$R::bus";
LOGINF "Reading from Bus.$bus";
$bus = "/bus." . $bus;
LOGTITLE "Daemon owfs2mqtt for Bus.$bus";
}

# Read OWFS Configuration
Expand Down Expand Up @@ -149,6 +159,8 @@ package => '1-wire-ng',
if (!$mqtttopic) {
$mqtttopic = "owfs";
}
# Check config

# Connect
&mqttconnect();

Expand Down Expand Up @@ -345,7 +357,8 @@ sub readdevices
# Set default values
my @temp = split(/,/,$devices);
for (@temp) {
if ( $_ =~ /^\/bus\.\d*\/(\d){2}.*$/ ) {
if ( $_ =~ /^\/bus\.\d*\/[0-9a-fA-F]{2}.*$/ ) {
# if ( $_ =~ /^\/bus\.\d*\/(\d){2}.*$/ ) { # Old
my $device = $_;
$device =~ s/^\/bus\.\d*//s;
$device =~ s/^\/*//s;
Expand Down Expand Up @@ -506,6 +519,10 @@ sub mqttconnect
$mqttbroker = $mqttcfg->{"server"};
$mqttport = $mqttcfg->{"port"};
}
if (!$mqttbroker || !$mqttport) {
LOGERR "MQTT isn't configured completely. I need at least broker and port.";
exit (1);
};
LOGDEB "MQTT Settings: User: $mqtt_username; Pass: $mqtt_password; Broker: $mqttbroker; Port: $mqttport";

# Connect
Expand Down
84 changes: 59 additions & 25 deletions bin/watchdog.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@
use LoxBerry::System;
use LoxBerry::Log;
use LoxBerry::JSON;
use Getopt::Long;
use OWNet;
use CGI;
#use CGI;
#use warnings;
use strict;
use Data::Dumper;
#use Data::Dumper;

# Version of this script
my $version = "0.1.0.1";
my $version = "2.0.0";

# Command line options
my $cgi = CGI->new;
$cgi->import_names('R');
#my $cgi = CGI->new;
#$cgi->import_names('R');

# Globals
my @busses;
Expand All @@ -23,6 +24,8 @@
my $owserver;
my $error;
my $verboseval="0";
my $verbose;
my $action;

# Logging
# Create a logging object
Expand All @@ -32,8 +35,15 @@ package => '1-wire-ng',
addtime => 1,
);

# Commandline options
# CGI doesn't work from other CGI skripts... :-(
#my $cgi = CGI->new;
#my $q = $cgi->Vars;
GetOptions ('verbose=s' => \$verbose,
'action=s' => \$action);

# Verbose
if ($R::verbose || $R::v) {
if ($verbose) {
$verboseval="1";
$log->stdout(1);
$log->loglevel(7);
Expand All @@ -42,13 +52,19 @@ package => '1-wire-ng',
LOGSTART "Starting Watchdog";

# Root
if ($<) {
$log->stdout(1);
LOGERR "This script has to be run as root.";
exit (1);
#if ($<) {
# $log->stdout(1);
# LOGERR "This script has to be run as root.";
# exit (1);
#}

# Lock
my $status = LoxBerry::System::lock(lockfile => '1-wire-ng-watchdog', wait => 120);
if ($status) {
print "$status currently running - Quitting.";
exit (1);
}


# Read OWFS Configuration
my $owfscfgfile = $lbpconfigdir . "/owfs.json";
my $jsonobjowfs = LoxBerry::JSON->new();
Expand All @@ -68,25 +84,25 @@ package => '1-wire-ng',
LOGDEB "Server Port: $serverport";

# Todo
if ( $R::action eq "start" ) {
if ( $action eq "start" ) {

&start();

}

elsif ( $R::action eq "stop" ) {
elsif ( $action eq "stop" ) {

&stop();

}

elsif ( $R::action eq "restart" ) {
elsif ( $action eq "restart" ) {

&restart();

}

elsif ( $R::action eq "check" ) {
elsif ( $action eq "check" ) {

&check();

Expand Down Expand Up @@ -115,9 +131,9 @@ sub start

LOGINF "START called...";
LOGINF "Starting OWServer...";
system ("systemctl start owserver");
system ("sudo systemctl start owserver");
sleep (1);
system ("systemctl start owhttpd");
system ("sudo systemctl start owhttpd");
sleep (1);
&readbusses();

Expand All @@ -127,8 +143,13 @@ sub start
my $bus = $_;
$bus =~ s/^\/bus\.//;
LOGINF "Starting owfs2mqtt for $_...";
LOGDEB "Call: $lbpbindir/owfs2mqtt.pl bus=$bus verbose=$verboseval";
system ("su loxberry -c \"$lbpbindir/owfs2mqtt.pl bus=$bus verbose=$verboseval &\"");
LOGDEB "Call: $lbpbindir/owfs2mqtt.pl --bus=$bus --verbose=$verboseval";
eval {
system("$lbpbindir/owfs2mqtt.pl --bus=$bus --verbose=$verboseval &");
} or do {
my $error = $@ || 'Unknown failure';
LOGERR "Could not start $lbpbindir/owfs2mqtt.pl --bus=$bus --verbose=$verboseval - $error";
};

}

Expand All @@ -141,10 +162,10 @@ sub stop

LOGINF "STOP called...";
LOGINF "Stopping OWServer...";
system ("pkill -f owserver"); # kill needed because stop does take too long (until timeout)
system ("systemctl stop owserver");
system ("sudo pkill -f owserver"); # kill needed because stop does take too long (until timeout)
system ("sudo systemctl stop owserver");
sleep (1);
system ("systemctl stop owhttpd");
system ("sudo systemctl stop owhttpd");
sleep (1);

LOGINF "Stopping owfs2mqtt instances...";
Expand Down Expand Up @@ -173,17 +194,22 @@ sub check
my $output;
my $errors;
my $exitcode;

# Creating tmp file with failed checks
if (!-e "/dev/shm/1-wire-ng-watchdog-fails.dat") {
my $response = LoxBerry::System::write_file("/dev/shm/1-wire-ng-watchdog-fails.dat", "0");
}

# owserver
$output = qx(systemctl -q status owserver);
$output = qx(sudo systemctl -q status owserver);
$exitcode = $? >> 8;
if ($exitcode != 0) {
LOGERR "owServer seems to be dead - Error $exitcode";
$errors++;
}

# owhttpd
$output = qx(systemctl -q status owhttpd);
$output = qx(sudo systemctl -q status owhttpd);
$exitcode = $? >> 8;
if ($exitcode != 0) {
LOGERR "owhttpd seems to be dead - Error $exitcode";
Expand All @@ -200,9 +226,16 @@ sub check


if ($errors) {
&restart();
my $fails = LoxBerry::System::read_file("/dev/shm/1-wire-ng-watchdog-fails.dat");
chomp ($fails);
if ($fails > 9) {
LOGERR "Too many failures. Will stop watchdogging... Check your configuration and start services manually.";
} else {
&restart();
}
} else {
LOGINF "All processes seems to be alive. Nothing to do.";
my $response = LoxBerry::System::write_file("/dev/shm/1-wire-ng-watchdog-fails.dat", "0");
}

return(0);
Expand Down Expand Up @@ -272,5 +305,6 @@ sub owconnect
END {

LOGEND "This is the end - My only friend, the end...";
LoxBerry::System::unlock(lockfile => '1-wire-ng-watchdog');

}
2 changes: 1 addition & 1 deletion config/owfs.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"serial2usb" : "false",
"serverport" : "4304",
"tempscale" : "C",
"uncached" : "false",
"uncached" : "true",
"usb" : "true"
}
4 changes: 2 additions & 2 deletions 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.0.1
VERSION=2.0.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 Expand Up @@ -60,7 +60,7 @@ REBOOT=true
# with all future versions, please set this to false or leave it empty..
# Note! If you use the new Plugin Interface V2 - I think you will, because you
# currently read THIS file ;-) - please set the Minimum version to 0.3.0!
LB_MINIMUM=2.0.0.4
LB_MINIMUM=2.2.1
LB_MAXIMUM=false

# If your plugin runs only on a special architecture (e.g. if you use the GPIOs
Expand Down
2 changes: 2 additions & 0 deletions sudoers/sudoers
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
loxberry ALL = NOPASSWD: REPLACELBPBINDIR/watchdog.pl
loxberry ALL = NOPASSWD: REPLACELBPBINDIR/create1wgpio.sh
loxberry ALL = NOPASSWD: /usr/bin/pkill -f owserver
1 change: 1 addition & 0 deletions templates/lang/language_de.ini
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ LABEL_SERIAL2USB="Aktiviere Serial2USB Busmaster (z. B. LinkUSB&trade;)"
HINT_OWFSADAPTERS="Wähle hier, welche 1-Wire Busmaster Du an Deinen LoxBerry angeschlossen hast. Wenn Du die Einstellung hier geändert hast, <b>starte Deinen LoxBerry neu</b>, nachdem Du die Einstellungen gespeichert hast."
LABEL_I2C="Aktiviere I2C Busmaster"
LABEL_GPIO="Aktiviere Raspberry GPIO Busmaster"
BUTTON_RESTART="Neustart"
BUTTON_SAVE="Speichern und neu starten"
BUTTON_OPENOWFS="OWFS WebUI"
HINT_SAVE_ERROR="Ein Fehler ist aufgetreten."
Expand Down
4 changes: 4 additions & 0 deletions templates/lang/language_en.ini
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@ LABEL_SERIAL2USB="Use Serial2USB Busmasters (e.g. LinkUSB&trade;)"
HINT_OWFSADAPTERS="Choose which type of 1-Wire Busmasters you have connected to your LoxBerry. If you changed these settings, <b>make sure to reboot your LoxBerry</b> after you have saved your configuration."
LABEL_I2C="Use I2C Busmasters"
LABEL_GPIO="Use Raspberry GPIO Busmaster"
BUTTON_RESTART="Restart"
BUTTON_SAVE="Save and Restart"
BUTTON_OPENOWFS="OWFS WebUI"
HINT_SAVE_ERROR="Some error occurred"
HINT_SAVE_SAVING="Saving..."
HINT_SAVE_OK="New data saved."
MSG_OWServer_REFRESH="OWServer Status Refreshing"
MSG_OWServer_RUNNING="OWServer running"
MSG_OWServer_NOTRUNNING="OWServer not running"
MSG_OWHTTPD_REFRESH="OWHTTPD Status Refreshing"
MSG_OWHTTPD_RUNNING="OWHTTPD running"
MSG_OWHTTPD_NOTRUNNING="OWHTTPD not running"
MSG_OWFS2MQTT_REFRESH="OWFS2MQTT Status Refreshing"
MSG_OWFS2MQTT_RUNNING="OWFS2MQTT running"
MSG_OWFS2MQTT_NOTRUNNING="OWFS2MQTT not running"
MSG_FAILED_PID="Failed to query PID"
Expand Down
Loading

0 comments on commit d16821c

Please sign in to comment.