diff --git a/metrics.go b/metrics.go index e3fb587..44fe33d 100644 --- a/metrics.go +++ b/metrics.go @@ -19,6 +19,7 @@ type OneMetric struct { Readonly bool Alarmactive string Smsnumber string + Minmax string Timestamp string Temperature string Humidity string @@ -370,6 +371,7 @@ func getLastMetrics(deveui string) OneMetric { res.Readonly = false res.Alarmactive = getDevAlarmactive(deveui) res.Smsnumber = getDevSmsnumber(deveui) + res.Minmax = getDevMinmax(deveui) res.ActiveUntil = getActiveUntil(deveui) res.DaysUntilDeactivated = CalcDaysUntil(res.ActiveUntil) return res diff --git a/persistence.go b/persistence.go index d11b954..b0ccd97 100644 --- a/persistence.go +++ b/persistence.go @@ -55,6 +55,7 @@ type Dev struct { Alias string Alarmactive string Smsnumber string + Minmax string ActiveUntil string // Abo bezahlt bis TT.MM.YYYY } @@ -105,7 +106,7 @@ func updateScaleSettings(scaleSettings Dev) error { defer conn.Close() // SET object - _, err := conn.Do("HMSET", devPrefix+scaleSettings.Deveui, "alias", scaleSettings.Alias, "alarmactive", scaleSettings.Alarmactive, "smsnumber", scaleSettings.Smsnumber) + _, err := conn.Do("HMSET", devPrefix+scaleSettings.Deveui, "alias", scaleSettings.Alias, "alarmactive", scaleSettings.Alarmactive, "smsnumber", scaleSettings.Smsnumber, "minmax", scaleSettings.Minmax) if err != nil { return err } @@ -217,6 +218,28 @@ func getDevSmsnumber(deveui string) string { return res } +func getDevMinmax(deveui string) string { + res := "0,0" + + if deveui == "" { + return res + } + + conn := globalPool.Get() + defer conn.Close() + + logit("getDevMinMax: Deveui: " + deveui) + minmax, err := redis.String(conn.Do("HGET", devPrefix+deveui, "minmax")) + if err == nil { + logit("getDevAlarmactive: minmax: " + minmax) + res = minmax + } else { + log.Print(err) + } + + return res +} + func getActiveUntil(deveui string) string { res := "" diff --git a/scales.go b/scales.go index 33b240f..98ad37a 100644 --- a/scales.go +++ b/scales.go @@ -86,24 +86,41 @@ func save_scale_settingsHandler(response http.ResponseWriter, request *http.Requ return } + minmax, ok5 := request.URL.Query()["minmax"] + + if !ok5 || len(minmax[0]) < 1 { + log.Println("Url Param 'minmax' is missing") + fmt.Fprintf(response, "{ \"rc\": 6, \"msg\": \"minmax must be specified in URL\" }") + return + } + myminmax := minmax[0] + + match2, _ := regexp.MatchString(`^[0-9]{1,6},[0-9]{1,6}$`, myminmax) + if !(match2) { + log.Println("Url Param 'minmax' is not valid") + fmt.Fprintf(response, "{ \"rc\": 12, \"msg\": \"minmax is not valid, must be in in format 50000,80000\" }") + return + } + var mydev Dev mydev.Deveui = mydeveui mydev.Alias = myalias mydev.Alarmactive = myalarmactive mydev.Smsnumber = mysmsnumber + mydev.Minmax = myminmax // 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\" }") + fmt.Fprintf(response, "{ \"rc\": 7, \"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\" }") + fmt.Fprintf(response, "{ \"rc\": 8, \"msg\": \"Only available for logged in users\" }") } } diff --git a/snippets/scales.html b/snippets/scales.html index d07c90c..4a514a3 100644 --- a/snippets/scales.html +++ b/snippets/scales.html @@ -34,6 +34,16 @@
+{{.Alias}}{{ if not .Readonly }}{{ end }}
{{.Alarmactive}}
{{.Smsnumber}}
+{{.Minmax}}
letzte übermittelte Messung: {{.Timestamp}}