Skip to content

Commit

Permalink
config ui
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbyquaker committed Jun 10, 2018
1 parent eda8453 commit e503733
Show file tree
Hide file tree
Showing 11 changed files with 581 additions and 6 deletions.
22 changes: 16 additions & 6 deletions addon_files/redmatic/bin/redmatic
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,29 @@ Stop () {
kill -0 $PSPID 2>/dev/null
if [ $? -eq 0 ]
then
sleep 10
sleep 3
kill -KILL $PSPID 2>/dev/null
fi
logger -t homematic -p user.info "stopped node-red"
echo "OK"
else
echo "Node-RED not running"
exit 1
fi
}

Start () {
echo -n "Starting Node-RED: "
$NODE $RED -s $SETTINGS 2>&1 | logger -p user.info -t node-red &
logger -t homematic -p user.info "started node-red"
echo "OK"
PSPID=`ps -o pid,comm,args | awk '{if($3 == "node-red" || $4 ~ /node-red/){print $1}}'`
if [ "$PSPID" != "" ]
then
echo "Node-RED already running"
exit 1
else
echo -n "Starting Node-RED: "
start-stop-daemon -S -q -b --exec $ADDON_DIR/bin/redmaticLoader
logger -t homematic -p user.info "started node-red"
echo "OK"
fi
}

case "$1" in
Expand All @@ -53,7 +63,6 @@ case "$1" in
Stop
sleep 1
Start
exit 0
;;

info)
Expand All @@ -63,6 +72,7 @@ case "$1" in
echo "Name: RedMatic"
echo "Version: $VERSION_ADDON"
echo "Update: /addons/redmatic/update_check.cgi"
echo "Config-Url: /addons/redmatic/settings.cgi"
echo "Operations: restart uninstall"
;;

Expand Down
17 changes: 17 additions & 0 deletions addon_files/redmatic/bin/redmaticLoader
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

CONF_DIR=/usr/local/etc/config
ADDON_DIR=/usr/local/addons/redmatic
WWW_DIR=/usr/local/etc/config/addons/www/redmatic

NODE=$ADDON_DIR/bin/node

export PATH=$ADDON_DIR/bin:$PATH
export NO_UPDATE_NOTIFIER=true

RED_DIR=$ADDON_DIR/lib/node_modules/node-red
RED=$RED_DIR/red.js

SETTINGS=$ADDON_DIR/lib/settings.js

$NODE $RED -s $SETTINGS 2>&1 | logger -p user.info -t node-red
4 changes: 4 additions & 0 deletions addon_files/redmatic/www/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.alert {
width: 300px;
margin: 4px;
}
36 changes: 36 additions & 0 deletions addon_files/redmatic/www/getconfig.cgi
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/tclsh

load tclrega.so

catch {
set input $env(QUERY_STRING)
set pairs [split $input &]
foreach pair $pairs {
if {0 != [regexp "^(\[^=]*)=(.*)$" $pair dummy varname val]} {
set $varname $val
}
}
}

if {[info exists sid] > 0} {
# Session prüfen
if {
([string index $sid 0] != "@")
|| ([string index $sid [expr [string length $sid] -1]] != "@")
|| ([string length $sid] != 12)} {
puts {error: session invalid}
} else {
regsub -all {@} $sid "" sid
set res [lindex [rega_script "Write(system.GetSessionVarStr('$sid'));"] 1]
if {$res != ""} {
# gültige Session
set fp [open "/usr/local/addons/redmatic/etc/settings.json" r]
puts -nonewline [read $fp]
close $fp
} else {
puts {error: session invalid}
}
}
} else {
puts {error: no session}
}
248 changes: 248 additions & 0 deletions addon_files/redmatic/www/js/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
$(document).ready(() => {
const bcrypt = dcodeIO.bcrypt;

const $loglevel = $('#loglevel');

const $adminauthType = $('#adminauth-type');
const $adminauthCreds = $('#adminauth-credentials');
const $adminauthUser = $('#adminauth-user');
const $adminauthPass1 = $('#adminauth-pass1');
const $adminauthPass2 = $('#adminauth-pass2');
const $adminauthSet = $('#adminauth-set');

const $nodeauthType = $('#nodeauth-type');
const $nodeauthCreds = $('#nodeauth-credentials');
const $nodeauthUser = $('#nodeauth-user');
const $nodeauthPass1 = $('#nodeauth-pass1');
const $nodeauthPass2 = $('#nodeauth-pass2');
const $nodeauthSet = $('#nodeauth-set');

const $staticauthType = $('#staticauth-type');
const $staticauthCreds = $('#staticauth-credentials');
const $staticauthUser = $('#staticauth-user');
const $staticauthPass1 = $('#staticauth-pass1');
const $staticauthPass2 = $('#staticauth-pass2');
const $staticauthSet = $('#staticauth-set');

const $alertSaved = $('#alert-saved');
const $alertError = $('#alert-error');
const $alertRestart = $('#alert-restart');

const $restart = $('#restart');

let config;

function alert($elem) {
$elem.addClass('show');
setTimeout(() => {
$elem.removeClass('show');
}, 1600);
}

function save() {
console.log(config);
$.post({
url: 'setconfig.cgi' + location.search,
data: JSON.stringify(config),
success: function (data) {
console.log(data);
if ($.trim(data) === 'ok') {
alert($alertSaved);
} else {
alert($alertError);
}
}
}).fail(() => {
alert($alertError);
});
}

$.get('getconfig.cgi' + location.search, (data, success) => {
config = JSON.parse(data);
console.log(config, success);
$loglevel.val(config.logging.console.level);

if (config.adminAuth) {
$adminauthType.val(config.adminAuth.type);
$adminauthCreds.show();
$adminauthUser.val(config.adminAuth.users[0].username);
}

if (config.httpNodeAuth) {
$nodeauthType.val('basic');
$nodeauthCreds.show();
$nodeauthUser.val(config.httpNodeAuth.user);
}

if (config.httpStaticAuth) {
$staticauthType.val('basic');
$staticauthCreds.show();
$staticauthUser.val(config.httpStaticAuth.user);
}

});

$loglevel.change(() => {
config.logging.console.level = $loglevel.val();
save();
});

$adminauthType.change(() => {
switch ($adminauthType.val()) {
case 'credentials':
$adminauthCreds.show();
break;
default:
$adminauthCreds.hide();
delete config.adminAuth;
save();
}
});

$nodeauthType.change(() => {
switch ($nodeauthType.val()) {
case 'basic':
$nodeauthCreds.show();
break;
default:
$nodeauthCreds.hide();
delete config.httpNodeAuth;
save();
}
});

$staticauthType.change(() => {
switch ($staticauthType.val()) {
case 'basic':
$staticauthCreds.show();
break;
default:
$staticauthCreds.hide();
delete config.httpStaticAuth;
save();
}
});

$adminauthSet.click(() => {
const user = $.trim($adminauthUser.val());
const pw1 = $adminauthPass1.val();
const pw2 = $adminauthPass2.val();

let valid = true;

if (!user) {
$adminauthUser.addClass('is-invalid');
valid = false;
} else {
$adminauthUser.removeClass('is-invalid');
}

if (!pw1 || pw1 !== pw2) {
$adminauthPass1.addClass('is-invalid');
$adminauthPass2.addClass('is-invalid');
valid = false;
} else {
$adminauthPass1.removeClass('is-invalid');
$adminauthPass2.removeClass('is-invalid');
}

if (valid) {
config = Object.assign(config, {
adminAuth: {
type: 'credentials',
users: [{
username: user,
password: bcrypt.hashSync(pw1, 8),
permissions: '*'
}]
}
});
save();
}
});

$staticauthSet.click(() => {
const user = $.trim($staticauthUser.val());
const pw1 = $staticauthPass1.val();
const pw2 = $staticauthPass2.val();

let valid = true;

if (!user) {
$staticauthUser.addClass('is-invalid');
valid = false;
} else {
$staticauthUser.removeClass('is-invalid');
}

if (!pw1 || pw1 !== pw2) {
$staticauthPass1.addClass('is-invalid');
$staticauthPass2.addClass('is-invalid');
valid = false;
} else {
$staticauthPass1.removeClass('is-invalid');
$staticauthPass2.removeClass('is-invalid');
}

if (valid) {
config = Object.assign(config, {
httpStaticAuth: {
user,
pass: bcrypt.hashSync(pw1, 8),
}
});
save();
}
});

$nodeauthSet.click(() => {
const user = $.trim($nodeauthUser.val());
const pw1 = $nodeauthPass1.val();
const pw2 = $nodeauthPass2.val();

let valid = true;

if (!user) {
$nodeauthUser.addClass('is-invalid');
valid = false;
} else {
$nodeauthUser.removeClass('is-invalid');
}

if (!pw1 || pw1 !== pw2) {
$nodeauthPass1.addClass('is-invalid');
$nodeauthPass2.addClass('is-invalid');
valid = false;
} else {
$nodeauthPass1.removeClass('is-invalid');
$nodeauthPass2.removeClass('is-invalid');
}

if (valid) {
config = Object.assign(config, {
httpNodeAuth: {
user,
pass: bcrypt.hashSync(pw1, 8),
}
});
save();
}
});

$restart.click(() => {
$restart.addClass('disabled');
$.get({
url: 'restart.cgi' + location.search,
success: data => {
$restart.removeClass('disabled');
if (data.match(/Starting Node-RED: OK/)) {
alert($alertRestart);
} else {
alert($alertError);
}
}
});
});

});

9 changes: 9 additions & 0 deletions addon_files/redmatic/www/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "redmatic-www",
"private": true,
"dependencies": {
"bcryptjs": "^2.4.3",
"bootstrap": "^4.1.1",
"jquery": "^3.3.1"
}
}
Loading

0 comments on commit e503733

Please sign in to comment.