package temp import ( "github.com/astaxie/beego/logs" "github.com/astaxie/beego/orm" "git.kingpenguin.tk/chteufleur/datahouse.git/models/database" "git.kingpenguin.tk/chteufleur/datahouse.git/models/utils" "git.kingpenguin.tk/chteufleur/datahouse.git/models/variables" "time" ) /** * Store last received data. */ type TempTableTmp struct { Id int64 HorodateGMT time.Time `orm:"auto_now;type(datetime)"` SensorID int64 Value int64 } var ( log = logs.NewLogger(10000) ) func init() { log.SetLogger(variables.LogType, variables.LogParams) orm.RegisterModel(new(TempTableTmp)) } /** * Add the value into the database. */ func AddData(sensor, value int64) { log.Info("Add tmp temperature {sensor: %s, value: %d}", sensor, value) 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) { log.Info("Update tmp temperature {sensorId: %d, value: %d}", sensorId, val) o := orm.NewOrm() o.Using(database.Alias) temp := GetTemp(sensorId) if o.Read(temp) == nil { temp.HorodateGMT = date temp.Value = val o.Update(temp) } }