DataHouse/models/sensor/sensor.go

128 lines
2.7 KiB
Go

package sensor
import (
"github.com/astaxie/beego/orm"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/database"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/utils"
)
type SensorTable struct {
Id int64
SensorMAC string
Description string
Interval int64
}
func init() {
orm.RegisterModel(new(SensorTable))
}
func GetSensorByMac(sensorMac string) *SensorTable {
o := orm.NewOrm()
o.Using(database.Alias)
sensor := new(SensorTable)
ret := new(SensorTable)
var maps []orm.Params
_, err := o.QueryTable(sensor).Filter("SensorMAC", sensorMac).Values(&maps)
if err == nil {
for _, m := range maps {
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
}
}
return ret
}
func GetAllSensorIds() []int64 {
o := orm.NewOrm()
o.Using(database.Alias)
sensor := new(SensorTable)
var ret []int64
var maps []orm.Params
_, err := o.QueryTable(sensor).Values(&maps)
if err == nil {
for _, m := range maps {
ret = append(ret, utils.GetInt(m, "Id"))
}
}
return ret
}
func GetAllSensor() []SensorTable {
o := orm.NewOrm()
o.Using(database.Alias)
sensor := new(SensorTable)
var ret []SensorTable
var maps []orm.Params
_, err := o.QueryTable(sensor).Values(&maps)
if err == nil {
for _, m := range maps {
r := new(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)
}
}
return ret
}
func GetSensor(id int64) *SensorTable {
o := orm.NewOrm()
o.Using(database.Alias)
sensor := new(SensorTable)
var ret = new(SensorTable)
var maps []orm.Params
_, err := o.QueryTable(sensor).Filter("Id", id).Values(&maps)
if err == nil {
for _, m := range maps {
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, 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)
}
}
func AddSensor(sensorMac string) {
o := orm.NewOrm()
o.Using(database.Alias)
_, _ = o.Insert(&SensorTable{SensorMAC: sensorMac})
}
func DeleteSensorByMac(sensorMac string) {
o := orm.NewOrm()
o.Using(database.Alias)
o.Delete(&SensorTable{SensorMAC: sensorMac})
}
func DeleteSensor(sensorId int64) {
o := orm.NewOrm()
o.Using(database.Alias)
o.Delete(&SensorTable{Id: sensorId})
}