From b8f1add31f8402894db86812d16e916611483b41 Mon Sep 17 00:00:00 2001 From: Chteufleur Date: Sat, 3 Oct 2015 18:02:51 +0200 Subject: [PATCH] Add possibility to specify interval mesure. --- controllers/addTemp.go | 13 ++++++++----- controllers/sensors.go | 13 ++++++++++--- models/sensor/sensor.go | 8 +++++++- views/base/footer.html | 1 + views/sensors.tpl | 16 ++++++++++++---- 5 files changed, 38 insertions(+), 13 deletions(-) diff --git a/controllers/addTemp.go b/controllers/addTemp.go index ed734f6..c6f1cb7 100644 --- a/controllers/addTemp.go +++ b/controllers/addTemp.go @@ -2,6 +2,7 @@ package controllers import ( "strconv" + "fmt" "github.com/astaxie/beego" @@ -14,15 +15,17 @@ type AddTempController struct { } func (c *AddTempController) Get() { - sens := c.Ctx.Input.Param(":sensor") + mac := c.Ctx.Input.Param(":sensor") val, _ := strconv.Atoi(c.Ctx.Input.Param(":val")) - s := sensor.GetSensorByMac(sens) + fmt.Println("MAC: ", mac) + + s := sensor.GetSensorByMac(mac) if s == nil { - sensor.AddSensor(sens) - s = sensor.GetSensorByMac(sens) + sensor.AddSensor(mac) + s = sensor.GetSensorByMac(mac) } temperature.AddData(s.Id, int64(val)) - c.Ctx.Output.Body([]byte("OK")) + c.Ctx.Output.Body([]byte(strconv.FormatInt(s.Interval, 10))) } diff --git a/controllers/sensors.go b/controllers/sensors.go index 37c9de2..d4f9413 100644 --- a/controllers/sensors.go +++ b/controllers/sensors.go @@ -4,6 +4,8 @@ import ( "github.com/astaxie/beego" "datahouse/models/sensor" + + "strconv" ) type SensorsController struct { @@ -21,8 +23,13 @@ func (c *SensorsController) Get() { func (c *SensorsController) Post() { description := c.Input().Get("description") mac := c.Input().Get("mac") + intervalStr := c.Input().Get("interval") + interval, err := strconv.ParseInt(intervalStr, 10, 0) - sensor.UpdateSensor(mac, description) - - c.Redirect("/sensors", 302) + if err == nil { + sensor.UpdateSensor(mac, description, interval) + c.Redirect("/sensors", 302) + } else { + c.Redirect("/404", 404) + } } diff --git a/models/sensor/sensor.go b/models/sensor/sensor.go index 15037ba..c663022 100644 --- a/models/sensor/sensor.go +++ b/models/sensor/sensor.go @@ -7,10 +7,12 @@ import ( "datahouse/models/utils" ) + type SensorTable struct { Id int64 SensorMAC string Description string + Interval int64 } func init() { @@ -31,6 +33,7 @@ func GetSensorByMac(sensorMac string) (*SensorTable) { ret.Id = utils.GetInt(m, "Id") ret.SensorMAC = utils.GetString(m, "SensorMAC") ret.Description = utils.GetString(m, "Description") + ret.Interval = utils.GetInt(m, "Interval") break } } @@ -68,6 +71,7 @@ func GetAllSensor() ([]SensorTable) { r.Id = utils.GetInt(m, "Id") r.SensorMAC = utils.GetString(m, "SensorMAC") r.Description = utils.GetString(m, "Description") + r.Interval = utils.GetInt(m, "Interval") ret = append(ret, *r) } @@ -88,19 +92,21 @@ func GetSensor(id int64) (*SensorTable) { ret.Id = utils.GetInt(m, "Id") ret.SensorMAC = utils.GetString(m, "SensorMAC") ret.Description = utils.GetString(m, "Description") + ret.Interval = utils.GetInt(m, "Interval") } } return ret } -func UpdateSensor(mac, description string) { +func UpdateSensor(mac, description string, interval int64) { o := orm.NewOrm() o.Using(database.Alias) s := GetSensorByMac(mac) if o.Read(s) == nil { s.Description = description + s.Interval = interval o.Update(s) } } diff --git a/views/base/footer.html b/views/base/footer.html index 2b13144..7fd532c 100644 --- a/views/base/footer.html +++ b/views/base/footer.html @@ -1,3 +1,4 @@ +