68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package temp
|
|
|
|
import (
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
"git.kingpenguin.tk/chteufleur/datahouse.git/models/database"
|
|
"git.kingpenguin.tk/chteufleur/datahouse.git/models/utils"
|
|
|
|
"time"
|
|
)
|
|
|
|
/**
|
|
* Store last received data.
|
|
*/
|
|
type TempTableTmp struct {
|
|
Id int64
|
|
HorodateGMT time.Time `orm:"auto_now;type(datetime)"`
|
|
SensorID int64
|
|
Value int64
|
|
}
|
|
|
|
func init() {
|
|
orm.RegisterModel(new(TempTableTmp))
|
|
}
|
|
|
|
/**
|
|
* Add the value into the database.
|
|
*/
|
|
func AddData(sensor, value int64) {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
|
|
tmpTable := new(TempTableTmp)
|
|
tmpTable.Value = value
|
|
tmpTable.SensorID = sensor
|
|
o.Insert(tmpTable)
|
|
}
|
|
|
|
func GetTemp(sensorId int64) *TempTableTmp {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
|
|
data := new(TempTableTmp)
|
|
var maps []orm.Params
|
|
_, err := o.QueryTable(new(TempTableTmp)).Filter("SensorID", sensorId).Values(&maps)
|
|
if err == nil {
|
|
for _, m := range maps {
|
|
data.Id = utils.GetInt(m, "Id")
|
|
data.HorodateGMT = utils.GetTime(m, "HorodateGMT")
|
|
data.SensorID = utils.GetInt(m, "SensorID")
|
|
data.Value = utils.GetInt(m, "Value")
|
|
}
|
|
}
|
|
return data
|
|
}
|
|
|
|
func UpdateTemp(sensorId, val int64, date time.Time) {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
|
|
temp := GetTemp(sensorId)
|
|
if o.Read(temp) == nil {
|
|
temp.HorodateGMT = date
|
|
temp.Value = val
|
|
o.Update(temp)
|
|
}
|
|
}
|