enhance settings handling
This commit is contained in:
parent
8b0367caf4
commit
410ee3253c
|
|
@ -0,0 +1,17 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Contains tells whether a contains x.
|
||||||
|
func Contains(a []string, x string) bool {
|
||||||
|
log.Println("Search for: "+x)
|
||||||
|
for _, n := range a {
|
||||||
|
log.Println("Piece of Array: "+n)
|
||||||
|
if x == n {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
1
main.go
1
main.go
|
|
@ -96,6 +96,7 @@ func main() {
|
||||||
http.HandleFunc("/confirm", confirmHandler)
|
http.HandleFunc("/confirm", confirmHandler)
|
||||||
http.HandleFunc("/metrics", metricsHandler)
|
http.HandleFunc("/metrics", metricsHandler)
|
||||||
http.HandleFunc("/lastmetrics", lastmetricsHandler)
|
http.HandleFunc("/lastmetrics", lastmetricsHandler)
|
||||||
|
http.HandleFunc("/save_scale_settings", save_scale_settingsHandler)
|
||||||
|
|
||||||
logit("Starting Web Application...")
|
logit("Starting Web Application...")
|
||||||
http.ListenAndServe("127.0.0.1:4000", nil)
|
http.ListenAndServe("127.0.0.1:4000", nil)
|
||||||
|
|
|
||||||
13
metrics.go
13
metrics.go
|
|
@ -24,19 +24,6 @@ type OneMetric struct {
|
||||||
BatteryPercent string
|
BatteryPercent string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Contains tells whether a contains x.
|
|
||||||
func Contains(a []string, x string) bool {
|
|
||||||
log.Println("Search for: "+x)
|
|
||||||
for _, n := range a {
|
|
||||||
log.Println("Piece of Array: "+n)
|
|
||||||
if x == n {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// metrics handler
|
// metrics handler
|
||||||
|
|
||||||
func metricsHandler(response http.ResponseWriter, request *http.Request) {
|
func metricsHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
"strings"
|
"strings"
|
||||||
"log"
|
"log"
|
||||||
"encoding/json"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
"github.com/gomodule/redigo/redis"
|
"github.com/gomodule/redigo/redis"
|
||||||
|
|
@ -50,56 +49,11 @@ func ping(c redis.Conn) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// User is a simple user struct for this example
|
|
||||||
type User struct {
|
|
||||||
Email string `json:"email"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
NewPassword string `json:"new_password"`
|
|
||||||
ConfirmId string `json:"confirm_id"`
|
|
||||||
LastLogin string `json:"last_login"`
|
|
||||||
MyDevs string `json:"my_devs"`
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
type Dev struct {
|
type Dev struct {
|
||||||
Deveui string `json:"deveui"`
|
Deveui string
|
||||||
Alias string `json:"alias"`
|
Alias string
|
||||||
Alarmactive string `json:"alarmactive"`
|
Alarmactive string
|
||||||
Smsnumber string `json:"smsnumber"`
|
Smsnumber string
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func setStruct(c redis.Conn, usr User) error {
|
|
||||||
|
|
||||||
// serialize User object to JSON
|
|
||||||
json, err := json.Marshal(usr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// SET object
|
|
||||||
_, err = c.Do("SET", userPrefix+usr.Email, json)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getStruct(c redis.Conn, username string) (User, error) {
|
|
||||||
|
|
||||||
usr := User{}
|
|
||||||
s, err := redis.String(c.Do("GET", userPrefix+username))
|
|
||||||
if err == redis.ErrNil {
|
|
||||||
logit("User does not exist:"+username)
|
|
||||||
} else if err != nil {
|
|
||||||
return usr, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = json.Unmarshal([]byte(s), &usr)
|
|
||||||
|
|
||||||
return usr, nil
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func initDB() {
|
func initDB() {
|
||||||
|
|
@ -126,6 +80,21 @@ func closeDB() {
|
||||||
globalPool.Close()
|
globalPool.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func updateScaleSettings(scaleSettings Dev) error {
|
||||||
|
conn := globalPool.Get()
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
// SET object
|
||||||
|
_, err := conn.Do("HMSET", devPrefix+scaleSettings.Deveui, "alias", scaleSettings.Alias, "alarmactive", scaleSettings.Alarmactive, "smsnumber", scaleSettings.Smsnumber)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func checkUserAvailable(username string) bool {
|
func checkUserAvailable(username string) bool {
|
||||||
logit("checkUserAvailable: User: "+username)
|
logit("checkUserAvailable: User: "+username)
|
||||||
conn := globalPool.Get()
|
conn := globalPool.Get()
|
||||||
|
|
@ -354,3 +323,4 @@ func confirmUser(confirm_id string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,80 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// scales handler
|
||||||
|
|
||||||
|
func save_scale_settingsHandler(response http.ResponseWriter, request *http.Request) {
|
||||||
|
name := getUserName(request)
|
||||||
|
if name != "" {
|
||||||
|
|
||||||
|
deveui, ok := request.URL.Query()["deveui"]
|
||||||
|
|
||||||
|
if !ok || len(deveui[0]) < 1 {
|
||||||
|
log.Println("Url Param 'deveui' is missing")
|
||||||
|
fmt.Fprintf(response, "{ \"rc\": 1, \"msg\": \"deveui must be specified in URL\" }")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// Query()["deveui"] will return an array of items,
|
||||||
|
// we only want the single item.
|
||||||
|
mydeveui := deveui[0]
|
||||||
|
|
||||||
|
if !(Contains(getMyDevs(name),mydeveui)) {
|
||||||
|
log.Println("specified 'deveui' does not belong to this user")
|
||||||
|
fmt.Fprintf(response, "{ \"rc\": 2, \"msg\": \"specified deveui does not belong to this user\" }")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Url Param 'deveui' is: " + string(mydeveui))
|
||||||
|
|
||||||
|
alias, ok2 := request.URL.Query()["alias"]
|
||||||
|
|
||||||
|
if !ok2 || len(alias[0]) < 1 {
|
||||||
|
log.Println("Url Param 'alias' is missing")
|
||||||
|
fmt.Fprintf(response, "{ \"rc\": 3, \"msg\": \"alias must be specified in URL\" }")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
myalias := alias[0]
|
||||||
|
|
||||||
|
alarmactive, ok3 := request.URL.Query()["alarmactive"]
|
||||||
|
|
||||||
|
if !ok3 || len(alarmactive[0]) < 1 {
|
||||||
|
log.Println("Url Param 'alarmactive' is missing")
|
||||||
|
fmt.Fprintf(response, "{ \"rc\": 4, \"msg\": \"alarmactive must be specified in URL\" }")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
myalarmactive := alarmactive[0]
|
||||||
|
|
||||||
|
smsnumber, ok4 := request.URL.Query()["smsnumber"]
|
||||||
|
|
||||||
|
if !ok4 || len(smsnumber[0]) < 1 {
|
||||||
|
log.Println("Url Param 'smsnumber' is missing")
|
||||||
|
fmt.Fprintf(response, "{ \"rc\": 5, \"msg\": \"smsnumber must be specified in URL\" }")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
mysmsnumber := smsnumber[0]
|
||||||
|
var mydev Dev
|
||||||
|
mydev.Deveui = mydeveui
|
||||||
|
mydev.Alias = myalias
|
||||||
|
mydev.Alarmactive = myalarmactive
|
||||||
|
mydev.Smsnumber = mysmsnumber
|
||||||
|
|
||||||
|
// now we try to save the settings
|
||||||
|
err := updateScaleSettings(mydev)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error to Update Device Settings")
|
||||||
|
fmt.Fprintf(response, "{ \"rc\": 6, \"msg\": \"error with saving device settings\" }")
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(response, "{ \"rc\": 0, \"msg\": \"SUCCESS\" }")
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(response, "{ \"rc\": 7, \"msg\": \"Only available for logged in users\" }")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -9,11 +9,16 @@
|
||||||
</header>
|
</header>
|
||||||
<section class="modal-card-body">
|
<section class="modal-card-body">
|
||||||
<!-- Content ... -->
|
<!-- Content ... -->
|
||||||
|
<p id="deveui" hidden></p>
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label id="label" class="label">Bezeichnung (1234567812345678)</label>
|
<label id="label" class="label">Bezeichnung (1234567812345678)</label>
|
||||||
<div class="control">
|
<div class="control has-icons-right">
|
||||||
<input id="alias" class="input" type="text">
|
<input id="alias" class="input" type="text" maxlength="16">
|
||||||
|
<span id="alias_exclamation" class="icon is-small is-right">
|
||||||
|
<i class="fas fa-exclamation-triangle"></i>
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
<p id="alias_errormsg" class="help is-danger"></p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="field">
|
<div class="field">
|
||||||
|
|
@ -27,9 +32,13 @@
|
||||||
|
|
||||||
<div class="field">
|
<div class="field">
|
||||||
<label class="label">Alarmnummer (SMS)</label>
|
<label class="label">Alarmnummer (SMS)</label>
|
||||||
<div class="control">
|
<div class="control has-icons-right">
|
||||||
<input id="smsnumber" class="input" type="text">
|
<input id="smsnumber" class="input" type="text" maxlength="16">
|
||||||
|
<span id="smsnumber_exclamation" class="icon is-small is-right">
|
||||||
|
<i class="fas fa-exclamation-triangle"></i>
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
<p id="smsnumber_errormsg" class="help is-danger"></p>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
<footer class="modal-card-foot">
|
<footer class="modal-card-foot">
|
||||||
|
|
@ -98,11 +107,23 @@
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
function validate(what, text) {
|
||||||
|
if (what == 'alias') {
|
||||||
|
var re = /^[a-zA-Z0-9 ]{1,16}$/;
|
||||||
|
} else if (what == 'smsnumber') {
|
||||||
|
var re = /^\+[0-9]{11,11}$/;
|
||||||
|
}
|
||||||
|
return re.test(text);
|
||||||
|
}
|
||||||
|
|
||||||
// A $( document ).ready() block.
|
// A $( document ).ready() block.
|
||||||
$( document ).ready(function() {
|
$( document ).ready(function() {
|
||||||
$(".show-modal").click(function() {
|
$(".show-modal").click(function() {
|
||||||
|
$("#alias_exclamation").hide();
|
||||||
|
$("#smsnumber_exclamation").hide();
|
||||||
var alias = $(this).prev().html();
|
var alias = $(this).prev().html();
|
||||||
var deveui = $(this).prev().attr('id').replace("alias_","");
|
var deveui = $(this).prev().attr('id').replace("alias_","");
|
||||||
|
$('#deveui').html(deveui);
|
||||||
var alarmactive = $('#alarmactive_'+deveui).html();
|
var alarmactive = $('#alarmactive_'+deveui).html();
|
||||||
var smsnumber = $('#smsnumber_'+deveui).html();
|
var smsnumber = $('#smsnumber_'+deveui).html();
|
||||||
|
|
||||||
|
|
@ -125,8 +146,64 @@ $("#modal-close").click(function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
$("#modal-save").click(function() {
|
$("#modal-save").click(function() {
|
||||||
console.log("save");
|
var alarmactive = "0";
|
||||||
$("#modal").removeClass("is-active");
|
if ($('#checkbox').prop('checked')) {
|
||||||
|
alarmactive = "1";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validation Code
|
||||||
|
if (!validate('alias',$('#alias').val())) {
|
||||||
|
$('#alias_errormsg').html('Ungültige Bezeichnung; erlaubte Zeichen A-Z, 0-9 und Leerschlag');
|
||||||
|
$("#alias").addClass("is-danger");
|
||||||
|
$("#alias_exclamation").show();
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
$('#alias_errormsg').html('');
|
||||||
|
$("#alias").removeClass("is-danger");
|
||||||
|
$("#alias_exclamation").hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!validate('smsnumber',$('#smsnumber').val())) {
|
||||||
|
$('#smsnumber_errormsg').html('Beispiel einer gültigen SMS Nummer: +41761234567');
|
||||||
|
$("#smsnumber").addClass("is-danger");
|
||||||
|
$("#smsnumber_exclamation").show();
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
$('#smsnumber_errormsg').html('');
|
||||||
|
$("#smsnumber").removeClass("is-danger");
|
||||||
|
$("#smsnumber_exclamation").hide();
|
||||||
|
}
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
url: "save_scale_settings",
|
||||||
|
type: "get", //send it through get method
|
||||||
|
dataType: "json",
|
||||||
|
data: {
|
||||||
|
deveui: $('#deveui').html(),
|
||||||
|
alias: $('#alias').val(),
|
||||||
|
smsnumber: $("#smsnumber").val(),
|
||||||
|
alarmactive: alarmactive
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
console.log('save success');
|
||||||
|
if (response.rc == 0) {
|
||||||
|
$('#alias_'+$('#deveui').html()).html($('#alias').val());
|
||||||
|
var alarmactive = "0";
|
||||||
|
if ($('#checkbox').prop('checked')) {
|
||||||
|
alarmactive = "1";
|
||||||
|
}
|
||||||
|
$('#alarmactive_'+$('#deveui').html()).html(alarmactive);
|
||||||
|
$('#smsnumber_'+$('#deveui').html()).html($('#smsnumber').val());
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function(xhr) {
|
||||||
|
console.log('save error');
|
||||||
|
//Do Something to handle error
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("save");
|
||||||
|
$("#modal").removeClass("is-active");
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue