Add temperature sensor with /addTemp/SensorMAC/value
This commit is contained in:
parent
a67ef2a608
commit
951f6cf6bb
|
|
@ -0,0 +1,28 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
|
||||
"datahouse/models/temperature"
|
||||
"datahouse/models/sensor"
|
||||
)
|
||||
|
||||
type AddTempController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *AddTempController) Get() {
|
||||
sens := c.Ctx.Input.Param(":sensor")
|
||||
val, _ := strconv.Atoi(c.Ctx.Input.Param(":val"))
|
||||
|
||||
idSensor := sensor.GetSensorId(sens)
|
||||
if idSensor == -1 {
|
||||
sensor.AddSensor(sens)
|
||||
idSensor = sensor.GetSensorId(sens)
|
||||
}
|
||||
|
||||
temperature.AddData(idSensor, int64(val))
|
||||
c.Ctx.Output.Body([]byte("OK"))
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package sensor
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/orm"
|
||||
|
||||
"datahouse/models/database"
|
||||
"datahouse/models/utils"
|
||||
)
|
||||
|
||||
type SensorTable struct {
|
||||
Id int64
|
||||
SensorMAC string
|
||||
Description string
|
||||
}
|
||||
|
||||
func init() {
|
||||
orm.RegisterModel(new(SensorTable))
|
||||
}
|
||||
|
||||
|
||||
func GetSensorId(sensorMac string) int64 {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
|
||||
sensor := new(SensorTable)
|
||||
var ret int64
|
||||
ret = -1
|
||||
var maps []orm.Params
|
||||
_, err := o.QueryTable(sensor).Filter("SensorMAC", sensorMac).Values(&maps)
|
||||
if err == nil {
|
||||
for _, m := range maps {
|
||||
ret = utils.GetInt(m, "Id")
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
|
||||
func AddSensor(sensorMac string) {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
_, _ = o.Insert(&SensorTable{SensorMAC: sensorMac})
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ import (
|
|||
type tempTable struct {
|
||||
Id int64
|
||||
HorodateGMT time.Time `orm:"auto_now;type(datetime)"`
|
||||
SensorID int64
|
||||
Value int64
|
||||
}
|
||||
|
||||
|
|
@ -22,11 +23,12 @@ func init() {
|
|||
/**
|
||||
* Add the value into the database.
|
||||
*/
|
||||
func AddData(value int64) {
|
||||
func AddData(sensor, value int64) {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
|
||||
tmpTable := new(tempTable)
|
||||
tmpTable.Value = value
|
||||
tmpTable.SensorID = sensor
|
||||
o.Insert(tmpTable)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"github.com/astaxie/beego/orm"
|
||||
)
|
||||
|
||||
|
||||
func GetString(m orm.Params, param string) string {
|
||||
ret := ""
|
||||
switch i := m[param].(type) {
|
||||
case string:
|
||||
ret = i
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func GetInt(m orm.Params, param string) int64 {
|
||||
var ret int64
|
||||
ret = -1
|
||||
|
||||
switch i := m[param].(type) {
|
||||
case int, int32, int64:
|
||||
ret = i.(int64)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
|
@ -7,4 +7,5 @@ import (
|
|||
|
||||
func init() {
|
||||
beego.Router("/", &controllers.MainController{})
|
||||
beego.Router("/addTemp/:sensor([0-9A-F:]+)/:val([0-9]+)", &controllers.AddTempController{})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue