Skip to content

Commit

Permalink
Mosquitto not running handling
Browse files Browse the repository at this point in the history
Try to restart Mosquitto until it is running
  • Loading branch information
christianTF committed Nov 15, 2018
1 parent 4234229 commit 821c8ce
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 91 deletions.
122 changes: 67 additions & 55 deletions bin/updateconfig.pl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
use FindBin qw($Bin);
use lib "$Bin/libs";

use Time::HiRes;
use LoxBerry::IO;
use LoxBerry::Log;
use warnings;
use strict;
use CGI;

use LoxBerry::JSON::JSONIO;

Expand All @@ -21,16 +20,37 @@
my @subscriptions;


print "Configfile: $cfgfile\n";

my $cgi = CGI->new;
my $q = $cgi->Vars;

my $log = LoxBerry::Log->new (
name => 'Update Configuration',
stdout => 1,
stdout => defined $q->{param} ? 0 : 1 ,
loglevel => 7,
);

LOGSTART "Updating configuration during plugin installation";

$json = LoxBerry::JSON::JSONIO->new();
$cfg = $json->open(filename => $cfgfile);

if(!$cfg) {
LOGCRIT "Could not read json configuration. Possibly not a valid json?";
return;
}

if($q->{section} and $q->{param}) {
my $val = $cfg->{$q->{section}}{$q->{param}};
print $val;
if ( $val =~ /^[0-9,.E]+$/ ) {
exit ($val);
} elsif (is_enabled($val)) {
exit 1;
} elsif (is_disabled($val)) {
exit 0;
}
}

update_config();
LOGEND "Config updated";
Expand All @@ -41,59 +61,51 @@ sub update_config
LOGOK "Reading config file";
# $LoxBerry::JSON::JSONIO::DEBUG = 1;

$json = LoxBerry::JSON::JSONIO->new();
$cfg = $json->open(filename => $cfgfile);

if(!$cfg) {
LOGCRIT "Could not read json configuration. Possibly not a valid json?";
return;
} else {

# Setting default values
LOGOK "Config was read. Checking values";
if(! defined $cfg->{Main}{msno}) {
$cfg->{Main}{msno} = 1;
LOGINF "Setting Miniserver to " . $cfg->{Main}{msno};
$changed++;
}
if(! defined $cfg->{Main}{udpport}) {
$cfg->{Main}{udpport} = 11883;
LOGINF "Setting Miniserver UDP Out-Port to " . $cfg->{Main}{udpport};
$changed++;
}
if(! defined $cfg->{Main}{enable_mosquitto}) {
$cfg->{Main}{enable_mosquitto} = '1';
LOGINF "Setting 'Enable local Mosquitto broker' to " . $cfg->{Main}{enable_mosquitto};
$changed++;
}
if(! defined $cfg->{Main}{brokeraddress}) {
$cfg->{Main}{brokeraddress} = 'localhost';
LOGINF "Setting MQTT broker address to " . $cfg->{Main}{brokeraddress};
$changed++;
}
if(! defined $cfg->{Main}{convert_booleans}) {
$cfg->{Main}{convert_booleans} = 1;
LOGINF "Setting 'Convert booleans' to " . $cfg->{Main}{convert_booleans};
$changed++;
}

if(! defined $cfg->{Main}{udpinport}) {
$cfg->{Main}{udpinport} = 11883;
LOGINF "Setting MQTT gateway UDP In-Port to " . $cfg->{Main}{udpinport};
$changed++;
}

$json->write();

LOGINF "Config:";
LOGDEB Dumper($cfg);
if($changed == 0) {
LOGOK "No changes in your configuration.";
LOGTITLE "No settings updated";
} else {
LOGWARN "$changed parameters updated. Check the new settings if it matches your configuration.";
LOGTITLE "$changed parameters updated";
LOGOK "Config was read. Checking values";
if(! defined $cfg->{Main}{msno}) {
$cfg->{Main}{msno} = 1;
LOGINF "Setting Miniserver to " . $cfg->{Main}{msno};
$changed++;
}
if(! defined $cfg->{Main}{udpport}) {
$cfg->{Main}{udpport} = 11883;
LOGINF "Setting Miniserver UDP Out-Port to " . $cfg->{Main}{udpport};
$changed++;
}
if(! defined $cfg->{Main}{enable_mosquitto}) {
$cfg->{Main}{enable_mosquitto} = '1';
LOGINF "Setting 'Enable local Mosquitto broker' to " . $cfg->{Main}{enable_mosquitto};
$changed++;
}
if(! defined $cfg->{Main}{brokeraddress}) {
$cfg->{Main}{brokeraddress} = 'localhost';
LOGINF "Setting MQTT broker address to " . $cfg->{Main}{brokeraddress};
$changed++;
}
if(! defined $cfg->{Main}{convert_booleans}) {
$cfg->{Main}{convert_booleans} = 1;
LOGINF "Setting 'Convert booleans' to " . $cfg->{Main}{convert_booleans};
$changed++;
}

if(! defined $cfg->{Main}{udpinport}) {
$cfg->{Main}{udpinport} = 11883;
LOGINF "Setting MQTT gateway UDP In-Port to " . $cfg->{Main}{udpinport};
$changed++;
}

$json->write();

LOGINF "Config:";
LOGDEB Dumper($cfg);
if($changed == 0) {
LOGOK "No changes in your configuration.";
LOGTITLE "No settings updated";
} else {
LOGWARN "$changed parameters updated. Check the new settings if it matches your configuration.";
LOGTITLE "$changed parameters updated";
}

}

19 changes: 18 additions & 1 deletion daemon/daemon
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
#!/bin/bash
/etc/init.d/mosquitto restart

# Request if local Mosquitto is enabled
MOSQENABLED="$(REPLACELBPBINDIR/updateconfig.pl section=Main param=enable_mosquitto)"

if [ "$MOSQENABLED" -eq "1" ]; then
for i in {1...20}
do
# Check if it is running
pgrep mosquitto
exitcode=$?
if [[ $exitcode != 0 ]]; then
# Restart if not
systemctl restart mosquitto.service
sleep 5
fi
done
fi

su loxberry -c "cd REPLACELBPBINDIR ; REPLACELBPBINDIR/mqttgateway.pl > /dev/null 2>&1 &"
7 changes: 7 additions & 0 deletions postinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ PBIN=$LBPBIN/$PDIR
# echo "<INFO> Plugin Log folder (on RAMDISK!) is: $PLOG"
# echo "<INFO> Plugin CONFIG folder is: $PCONFIG"


echo "<INFO> Copy back existing config files"
cp -f -r /tmp/$ARGV1\_upgrade/config/$ARGV3/* $ARGV5/config/plugins/$ARGV3/

echo "<INFO> Remove temporary folders"
rm -f -r /tmp/$ARGV1\_upgrade

echo "<INFO> Updating configuration"
perl $PBIN/updateconfig.pl

Expand Down
35 changes: 0 additions & 35 deletions postupgrade.sh

This file was deleted.

0 comments on commit 821c8ce

Please sign in to comment.