add ability to set static ip from web ui when in dev mode

pull/556/head
Jon Vadney 2016-12-25 07:03:56 +00:00
rodzic 2b5037eb31
commit 6198fc2ce3
6 zmienionych plików z 251 dodań i 205 usunięć

Wyświetl plik

@ -1034,6 +1034,7 @@ type settings struct {
OwnshipModeS string
WatchList string
DeveloperMode bool
StaticIps []string
}
type status struct {
@ -1096,6 +1097,7 @@ func defaultSettings() {
globalSettings.ReplayLog = false //TODO: 'true' for debug builds.
globalSettings.OwnshipModeS = "F00000"
globalSettings.DeveloperMode = false
globalSettings.StaticIps = make([]string, 0)
}
func readSettings() {

Wyświetl plik

@ -21,6 +21,7 @@ import (
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"syscall"
"text/template"
@ -319,6 +320,26 @@ func handleSettingsSetRequest(w http.ResponseWriter, r *http.Request) {
continue
}
globalSettings.OwnshipModeS = fmt.Sprintf("%02X%02X%02X", hexn[0], hexn[1], hexn[2])
case "StaticIps":
ipsStr := val.(string)
ips := strings.Split(ipsStr, " ")
if ipsStr == "" {
ips = make([]string, 0)
}
re, _ := regexp.Compile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
err := ""
for _, ip := range ips {
// Verify IP format
if !re.MatchString(ip) {
err = err + "Invalid IP: " + ip + ". "
}
}
if err != "" {
log.Printf("handleSettingsSetRequest:StaticIps: %s\n", err)
continue
}
globalSettings.StaticIps = ips
default:
log.Printf("handleSettingsSetRequest:json: unrecognized key:%s\n", key)
}

Wyświetl plik

@ -115,6 +115,14 @@ func getDHCPLeases() (map[string]string, error) {
}
}
// Add IP's set through the settings page
if globalSettings.StaticIps != nil {
for _, ip := range globalSettings.StaticIps {
ret[ip] = ""
}
}
// Added the ability to have static IP hosts stored in /etc/stratux-static-hosts.conf
dat2, err := ioutil.ReadFile(extra_hosts_file)

Wyświetl plik

@ -134,7 +134,9 @@ Stratux makes available a webserver to retrieve statistics which may be useful t
"ReplayLog": true,
"PPM": 0,
"OwnshipModeS": "F00000",
"WatchList": ""
"WatchList": "",
"DeveloperMode": false,
"StaticIps": []
}
```
* `http://192.168.10.1/setSettings` - set device settings. Use an HTTP POST of JSON content in the format given above - posting only the fields containing the settings to be modified.

Wyświetl plik

@ -1,200 +1,210 @@
angular.module('appControllers').controller('SettingsCtrl', SettingsCtrl); // get the main module contollers set
SettingsCtrl.$inject = ['$rootScope', '$scope', '$state', '$location', '$window', '$http']; // Inject my dependencies
// create our controller function with all necessary logic
function SettingsCtrl($rootScope, $scope, $state, $location, $window, $http) {
$scope.$parent.helppage = 'plates/settings-help.html';
var toggles = ['UAT_Enabled', 'ES_Enabled', 'Ping_Enabled', 'GPS_Enabled', 'DisplayTrafficSource', 'DEBUG', 'ReplayLog'];
var settings = {};
for (i = 0; i < toggles.length; i++) {
settings[toggles[i]] = undefined;
}
$scope.update_files = '';
function loadSettings(data) {
settings = angular.fromJson(data);
// consider using angular.extend()
$scope.rawSettings = angular.toJson(data, true);
$scope.visible_serialout = false;
if ((settings.SerialOutputs !== undefined) && (settings.SerialOutputs !== null) && (settings.SerialOutputs['/dev/serialout0'] !== undefined)) {
$scope.Baud = settings.SerialOutputs['/dev/serialout0'].Baud;
$scope.visible_serialout = true;
}
$scope.UAT_Enabled = settings.UAT_Enabled;
$scope.ES_Enabled = settings.ES_Enabled;
$scope.Ping_Enabled = settings.Ping_Enabled;
$scope.GPS_Enabled = settings.GPS_Enabled;
$scope.DisplayTrafficSource = settings.DisplayTrafficSource;
$scope.DEBUG = settings.DEBUG;
$scope.ReplayLog = settings.ReplayLog;
$scope.PPM = settings.PPM;
$scope.WatchList = settings.WatchList;
$scope.OwnshipModeS = settings.OwnshipModeS;
$scope.DeveloperMode = settings.DeveloperMode;
}
function getSettings() {
// Simple GET request example (note: responce is asynchronous)
$http.get(URL_SETTINGS_GET).
then(function (response) {
loadSettings(response.data);
// $scope.$apply();
}, function (response) {
$scope.rawSettings = "error getting settings";
for (i = 0; i < toggles.length; i++) {
settings[toggles[i]] = false;
}
});
};
function setSettings(msg) {
// Simple POST request example (note: responce is asynchronous)
$http.post(URL_SETTINGS_SET, msg).
then(function (response) {
loadSettings(response.data);
// $scope.$apply();
}, function (response) {
$scope.rawSettings = "error setting settings";
for (i = 0; i < toggles.length; i++) {
settings[toggles[i]] = false;
}
});
}
getSettings();
$scope.$watchGroup(toggles, function (newValues, oldValues, scope) {
var newsettings = {}
var dirty = false;
for (i = 0; i < newValues.length; i++) {
if ((newValues[i] !== undefined) && (settings[toggles[i]] !== undefined)) {
if (newValues[i] !== settings[toggles[i]]) {
settings[toggles[i]] = newValues[i];
newsettings[toggles[i]] = newValues[i];
dirty = true;
};
}
}
if (dirty) {
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
});
$scope.updateppm = function () {
settings["PPM"] = 0
if (($scope.PPM !== undefined) && ($scope.PPM !== null) && ($scope.PPM !== settings["PPM"])) {
settings["PPM"] = parseInt($scope.PPM);
newsettings = {
"PPM": settings["PPM"]
};
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
};
$scope.updateBaud = function () {
settings["Baud"] = 0
if (($scope.Baud !== undefined) && ($scope.Baud !== null) && ($scope.Baud !== settings["Baud"])) {
settings["Baud"] = parseInt($scope.Baud);
newsettings = {
"Baud": settings["Baud"]
};
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
};
$scope.updatewatchlist = function () {
if ($scope.WatchList !== settings["WatchList"]) {
settings["WatchList"] = "";
if ($scope.WatchList !== undefined) {
settings["WatchList"] = $scope.WatchList.toUpperCase();
}
newsettings = {
"WatchList": settings["WatchList"]
};
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
};
$scope.updatemodes = function () {
if ($scope.OwnshipModeS !== settings["OwnshipModeS"]) {
settings["OwnshipModeS"] = $scope.OwnshipModeS.toUpperCase();
newsettings = {
"OwnshipModeS": $scope.OwnshipModeS.toUpperCase()
};
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
};
$scope.postShutdown = function () {
$window.location.href = "/";
$location.path('/home');
$http.post(URL_SHUTDOWN).
then(function (response) {
// do nothing
// $scope.$apply();
}, function (response) {
// do nothing
});
};
$scope.postReboot = function () {
$window.location.href = "/";
$location.path('/home');
$http.post(URL_REBOOT).
then(function (response) {
// do nothing
// $scope.$apply();
}, function (response) {
// do nothing
});
};
$scope.setUploadFile = function (files) {
$scope.update_files = files;
$scope.$apply();
}
$scope.resetUploadFile = function () {
$scope.update_files = '';
$scope.$apply();
}
$scope.uploadFile = function () {
var fd = new FormData();
//Take the first selected file
var file = $scope.update_files[0];
// check for empty string
if (file === undefined || file === null) {
alert ("update file not selected")
return;
}
var filename = file.name;
// check for expected file naming convention
var re = /^update.*\.sh$/;
if (!re.exec(filename)) {
alert ("file does not appear to be an update")
return;
}
fd.append("update_file", file);
$http.post(URL_UPDATE_UPLOAD, fd, {
withCredentials: true,
headers: {
'Content-Type': undefined
},
transformRequest: angular.identity
}).success(function (data) {
alert("success. wait 60 seconds and refresh home page to verify new version.");
window.location.replace("/");
}).error(function (data) {
alert("error");
});
};
};
angular.module('appControllers').controller('SettingsCtrl', SettingsCtrl); // get the main module contollers set
SettingsCtrl.$inject = ['$rootScope', '$scope', '$state', '$location', '$window', '$http']; // Inject my dependencies
// create our controller function with all necessary logic
function SettingsCtrl($rootScope, $scope, $state, $location, $window, $http) {
$scope.$parent.helppage = 'plates/settings-help.html';
var toggles = ['UAT_Enabled', 'ES_Enabled', 'Ping_Enabled', 'GPS_Enabled', 'DisplayTrafficSource', 'DEBUG', 'ReplayLog'];
var settings = {};
for (i = 0; i < toggles.length; i++) {
settings[toggles[i]] = undefined;
}
$scope.update_files = '';
function loadSettings(data) {
settings = angular.fromJson(data);
// consider using angular.extend()
$scope.rawSettings = angular.toJson(data, true);
$scope.visible_serialout = false;
if ((settings.SerialOutputs !== undefined) && (settings.SerialOutputs !== null) && (settings.SerialOutputs['/dev/serialout0'] !== undefined)) {
$scope.Baud = settings.SerialOutputs['/dev/serialout0'].Baud;
$scope.visible_serialout = true;
}
$scope.UAT_Enabled = settings.UAT_Enabled;
$scope.ES_Enabled = settings.ES_Enabled;
$scope.Ping_Enabled = settings.Ping_Enabled;
$scope.GPS_Enabled = settings.GPS_Enabled;
$scope.DisplayTrafficSource = settings.DisplayTrafficSource;
$scope.DEBUG = settings.DEBUG;
$scope.ReplayLog = settings.ReplayLog;
$scope.PPM = settings.PPM;
$scope.WatchList = settings.WatchList;
$scope.OwnshipModeS = settings.OwnshipModeS;
$scope.DeveloperMode = settings.DeveloperMode;
$scope.StaticIps = settings.StaticIps;
}
function getSettings() {
// Simple GET request example (note: responce is asynchronous)
$http.get(URL_SETTINGS_GET).
then(function (response) {
loadSettings(response.data);
// $scope.$apply();
}, function (response) {
$scope.rawSettings = "error getting settings";
for (i = 0; i < toggles.length; i++) {
settings[toggles[i]] = false;
}
});
};
function setSettings(msg) {
// Simple POST request example (note: responce is asynchronous)
$http.post(URL_SETTINGS_SET, msg).
then(function (response) {
loadSettings(response.data);
// $scope.$apply();
}, function (response) {
$scope.rawSettings = "error setting settings";
for (i = 0; i < toggles.length; i++) {
settings[toggles[i]] = false;
}
});
}
getSettings();
$scope.$watchGroup(toggles, function (newValues, oldValues, scope) {
var newsettings = {}
var dirty = false;
for (i = 0; i < newValues.length; i++) {
if ((newValues[i] !== undefined) && (settings[toggles[i]] !== undefined)) {
if (newValues[i] !== settings[toggles[i]]) {
settings[toggles[i]] = newValues[i];
newsettings[toggles[i]] = newValues[i];
dirty = true;
};
}
}
if (dirty) {
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
});
$scope.updateppm = function () {
settings["PPM"] = 0
if (($scope.PPM !== undefined) && ($scope.PPM !== null) && ($scope.PPM !== settings["PPM"])) {
settings["PPM"] = parseInt($scope.PPM);
newsettings = {
"PPM": settings["PPM"]
};
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
};
$scope.updateBaud = function () {
settings["Baud"] = 0
if (($scope.Baud !== undefined) && ($scope.Baud !== null) && ($scope.Baud !== settings["Baud"])) {
settings["Baud"] = parseInt($scope.Baud);
newsettings = {
"Baud": settings["Baud"]
};
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
};
$scope.updatewatchlist = function () {
if ($scope.WatchList !== settings["WatchList"]) {
settings["WatchList"] = "";
if ($scope.WatchList !== undefined) {
settings["WatchList"] = $scope.WatchList.toUpperCase();
}
newsettings = {
"WatchList": settings["WatchList"]
};
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
};
$scope.updatemodes = function () {
if ($scope.OwnshipModeS !== settings["OwnshipModeS"]) {
settings["OwnshipModeS"] = $scope.OwnshipModeS.toUpperCase();
newsettings = {
"OwnshipModeS": $scope.OwnshipModeS.toUpperCase()
};
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
};
$scope.updatestaticips = function () {
if ($scope.StaticIps !== settings.StaticIps) {
newsettings = {
"StaticIps": $scope.StaticIps === undefined? "" : $scope.StaticIps.join(' ')
};
// console.log(angular.toJson(newsettings));
setSettings(angular.toJson(newsettings));
}
};
$scope.postShutdown = function () {
$window.location.href = "/";
$location.path('/home');
$http.post(URL_SHUTDOWN).
then(function (response) {
// do nothing
// $scope.$apply();
}, function (response) {
// do nothing
});
};
$scope.postReboot = function () {
$window.location.href = "/";
$location.path('/home');
$http.post(URL_REBOOT).
then(function (response) {
// do nothing
// $scope.$apply();
}, function (response) {
// do nothing
});
};
$scope.setUploadFile = function (files) {
$scope.update_files = files;
$scope.$apply();
}
$scope.resetUploadFile = function () {
$scope.update_files = '';
$scope.$apply();
}
$scope.uploadFile = function () {
var fd = new FormData();
//Take the first selected file
var file = $scope.update_files[0];
// check for empty string
if (file === undefined || file === null) {
alert ("update file not selected")
return;
}
var filename = file.name;
// check for expected file naming convention
var re = /^update.*\.sh$/;
if (!re.exec(filename)) {
alert ("file does not appear to be an update")
return;
}
fd.append("update_file", file);
$http.post(URL_UPDATE_UPLOAD, fd, {
withCredentials: true,
headers: {
'Content-Type': undefined
},
transformRequest: angular.identity
}).success(function (data) {
alert("success. wait 60 seconds and refresh home page to verify new version.");
window.location.replace("/");
}).error(function (data) {
alert("error");
});
};
};

Wyświetl plik

@ -135,7 +135,12 @@
<div class="panel-heading">Developer Options</div>
<div class="panel-body">
<div class="col-xs-12">
<p>Coming soon</p>
<div class="form-group reset-flow">
<label class="control-label col-xs-5">Static IPs</label>
<form name="staticipForm" ng-submit="updatestaticips()" novalidate>
<input class="col-xs-7" type="string" required ng-model="StaticIps" ng-list=" " ng-trim="false" placeholder="space-delimited ip's to send network data" ng-blur="updatestaticips()" />
</form>
</div>
</div>
</div>
</div>
@ -143,8 +148,7 @@
</div>
<!--
<div class="col-sm-12">
<div class="col-sm-12" ng-show="DeveloperMode" >
<div class="panel panel-default">
<div class="panel-heading">Raw Configuration</div>
<div class="panel-body">
@ -153,7 +157,6 @@
</div>
</div>
</div>
-->
<div class="col-sm-12">
<h3 ui-if="rebooting" ui-state="rebooting">Stratux is rebooting. You may need to reconnect WiFi once it reboots.</h3>