From a1fe00772854c7a053c4cf09b37563fc768c697e Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Mon, 23 Sep 2019 18:21:42 +0200 Subject: [PATCH] implement payment --- main.go | 13 -------- metrics.go | 37 +++++++++++++++------ persistence.go | 37 +++++++++++++++++---- snippets/scales.html | 55 ++++++++++++++++++++++++------- stripe.go | 78 +++++++++++++++++++++----------------------- 5 files changed, 138 insertions(+), 82 deletions(-) diff --git a/main.go b/main.go index 19a6db0..5447be8 100644 --- a/main.go +++ b/main.go @@ -8,19 +8,6 @@ import ( "time" ) -type Scale struct { - Deveui string - Alias string - Alarmactive string - Smsnumber string - Last_measurement string - Last_temp string - Last_humidity string - Last_weight string - Last_pressure string - Last_accu_level string -} - func serveTemplate(w http.ResponseWriter, r *http.Request) { logit("Called URL: "+r.URL.Path) // wennn kein File angegeben ist: index.html diff --git a/metrics.go b/metrics.go index 968d4ea..13981e8 100644 --- a/metrics.go +++ b/metrics.go @@ -12,16 +12,18 @@ import ( ) type OneMetric struct { - Deveui string - Alias string - Alarmactive string - Smsnumber string - Timestamp string - Temperature string - Humidity string - Weight string - Pressure string - BatteryPercent string + Deveui string + Alias string + Alarmactive string + Smsnumber string + Timestamp string + Temperature string + Humidity string + Weight string + Pressure string + BatteryPercent string + ActiveUntil string + DaysUntilDeactivated int // berechneter Wert } @@ -252,6 +254,19 @@ func lastmetricsHandler(response http.ResponseWriter, request *http.Request) { } } +func CalcDaysUntil(mydate string) int { + var days int + layout := "01.01.2006" + t, err := time.Parse(layout, mydate) + + if err != nil { + days = 0 + } + days = int(t.Sub(time.Now()).Hours() / 24) + + return days +} + func getLastMetrics(deveui string) OneMetric { var res OneMetric @@ -320,5 +335,7 @@ func getLastMetrics(deveui string) OneMetric { res.Alias = getDevAlias(deveui) res.Alarmactive = getDevAlarmactive(deveui) res.Smsnumber = getDevSmsnumber(deveui) + res.ActiveUntil = getActiveUntil(deveui) + res.DaysUntilDeactivated = CalcDaysUntil(res.ActiveUntil) return res } diff --git a/persistence.go b/persistence.go index 640359d..d0aa5e8 100644 --- a/persistence.go +++ b/persistence.go @@ -51,11 +51,11 @@ func ping(c redis.Conn) error { } type Dev struct { - Deveui string - Alias string - Alarmactive string - Smsnumber string - ActiveUntil string // Abo bezahlt bis TT.MM.YYYY + Deveui string + Alias string + Alarmactive string + Smsnumber string + ActiveUntil string // Abo bezahlt bis TT.MM.YYYY } func initDB() { @@ -205,6 +205,29 @@ func getDevSmsnumber(deveui string) string { return res } +func getActiveUntil(deveui string) string { + res := "" + + if deveui == "" { + return res + } + + + conn := globalPool.Get() + defer conn.Close() + + logit("getActiveUntil: Deveui: "+deveui) + activeuntil, err := redis.String(conn.Do("HGET", devPrefix+deveui, "active_until")) + if err == nil { + logit("getActiveUntil: activeuntil: "+activeuntil) + res = activeuntil + } else { + log.Print(err) + } + + return res +} + func prolongActivation(deveui string, years int) (string, error) { conn := globalPool.Get() defer conn.Close() @@ -226,9 +249,9 @@ func prolongActivation(deveui string, years int) (string, error) { var t_new time.Time if t.Before(time.Now()) { - t_new = time.Now().AddDate(1, 0, 0) + t_new = time.Now().AddDate(years, 0, 0) } else { - t_new = t.AddDate(1, 0, 0) + t_new = t.AddDate(years, 0, 0) } active_until_new := t_new.Format(layout) diff --git a/snippets/scales.html b/snippets/scales.html index 5daa8b4..813382f 100644 --- a/snippets/scales.html +++ b/snippets/scales.html @@ -53,9 +53,10 @@ Device ID: {{range .LastMetrics}} -
-

{{.Alias}}

+
+

{{.Alias}}

letzte übermittelte Messung: {{.Timestamp}}

@@ -117,9 +118,10 @@ Device ID:
- - + aktiv bis +
+{{ if (lt .DaysUntilDeactivated 1095) }}

 

@@ -141,11 +143,14 @@ Device ID:
+{{ end }} {{end}} + +