Add temporaire BDD table to store temporaire incomming temperature.
Save only every interval set.
This commit is contained in:
parent
fb6be459c2
commit
0d495e41bb
|
|
@ -2,11 +2,12 @@ package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"fmt"
|
"time"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
|
|
||||||
"datahouse/models/temperature"
|
"datahouse/models/temperature"
|
||||||
|
temperatureTmp "datahouse/models/temperature/temp"
|
||||||
"datahouse/models/sensor"
|
"datahouse/models/sensor"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -18,14 +19,39 @@ func (c *AddTempController) Get() {
|
||||||
mac := c.Ctx.Input.Param(":sensor")
|
mac := c.Ctx.Input.Param(":sensor")
|
||||||
val, _ := strconv.Atoi(c.Ctx.Input.Param(":val"))
|
val, _ := strconv.Atoi(c.Ctx.Input.Param(":val"))
|
||||||
|
|
||||||
fmt.Println("MAC: ", mac)
|
|
||||||
|
|
||||||
s := sensor.GetSensorByMac(mac)
|
s := sensor.GetSensorByMac(mac)
|
||||||
if s == nil {
|
if s == nil || s.Id == 0 {
|
||||||
sensor.AddSensor(mac)
|
sensor.AddSensor(mac)
|
||||||
s = sensor.GetSensorByMac(mac)
|
s = sensor.GetSensorByMac(mac)
|
||||||
}
|
}
|
||||||
|
|
||||||
temperature.AddData(s.Id, int64(val))
|
addToTempBdd(s, val)
|
||||||
|
saveInBDD(s, val)
|
||||||
|
|
||||||
c.Ctx.Output.Body([]byte(strconv.FormatInt(s.Interval, 10)))
|
c.Ctx.Output.Body([]byte(strconv.FormatInt(s.Interval, 10)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save into temporaire database.
|
||||||
|
*/
|
||||||
|
func addToTempBdd(snsor *sensor.SensorTable, val int) {
|
||||||
|
nowUTC := time.Now().UTC()
|
||||||
|
tempTmp := temperatureTmp.GetTemp(snsor.Id)
|
||||||
|
if tempTmp == nil || tempTmp.Id == 0 { // Add new entry for sensor
|
||||||
|
temperatureTmp.AddData(snsor.Id, int64(val))
|
||||||
|
} else { // Update entry for sensor
|
||||||
|
temperatureTmp.UpdateTemp(snsor.Id, int64(val), nowUTC)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save into database.
|
||||||
|
*/
|
||||||
|
func saveInBDD(snsor *sensor.SensorTable, val int) {
|
||||||
|
nowUTC := time.Now()
|
||||||
|
lastTempRecord := temperature.GetLastTempForSensor(snsor.Id)
|
||||||
|
intervalTmp := nowUTC.Unix() - lastTempRecord.HorodateGMT.Unix() - int64(timezoneOffset)
|
||||||
|
if intervalTmp >= snsor.Interval {
|
||||||
|
temperature.AddData(snsor.Id, int64(val))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@ import (
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
|
|
||||||
"datahouse/models/sensor"
|
"datahouse/models/sensor"
|
||||||
"datahouse/models/temperature"
|
// "datahouse/models/temperature"
|
||||||
|
temperatureTmp "datahouse/models/temperature/temp"
|
||||||
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -43,7 +44,8 @@ func getLastTemps() ([]SensorPrint) {
|
||||||
sens.Name = s.SensorMAC
|
sens.Name = s.SensorMAC
|
||||||
}
|
}
|
||||||
|
|
||||||
t := temperature.GetLastTempForSensor(s.Id)
|
// t := temperature.GetLastTempForSensor(s.Id)
|
||||||
|
t := temperatureTmp.GetTemp(s.Id)
|
||||||
sens.Value = strconv.FormatInt(t.Value, 10)
|
sens.Value = strconv.FormatInt(t.Value, 10)
|
||||||
sens.Unit = "°C"
|
sens.Unit = "°C"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,72 @@
|
||||||
|
package temp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/astaxie/beego/orm"
|
||||||
|
|
||||||
|
"datahouse/models/database"
|
||||||
|
"datahouse/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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue