134 lines
2.8 KiB
Go
134 lines
2.8 KiB
Go
package relay
|
|
|
|
import (
|
|
"github.com/astaxie/beego/orm"
|
|
"github.com/astaxie/beego/httplib"
|
|
|
|
"git.kingpenguin.tk/chteufleur/datahouse.git/models/database"
|
|
"git.kingpenguin.tk/chteufleur/datahouse.git/models/utils"
|
|
|
|
"time"
|
|
)
|
|
|
|
type RelayTable struct {
|
|
Id int64
|
|
Mac string
|
|
Description string
|
|
IpAddress string
|
|
}
|
|
|
|
func init() {
|
|
orm.RegisterModel(new(RelayTable))
|
|
}
|
|
|
|
func GetRelayByMac(relayMac string) *RelayTable {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
|
|
ret := new(RelayTable)
|
|
var maps []orm.Params
|
|
_, err := o.QueryTable(new(RelayTable)).Filter("Mac", relayMac).Values(&maps)
|
|
if err == nil {
|
|
for _, m := range maps {
|
|
ret.Id = utils.GetInt(m, "Id")
|
|
ret.Mac = utils.GetString(m, "Mac")
|
|
ret.Description = utils.GetString(m, "Description")
|
|
ret.IpAddress = utils.GetString(m, "IpAddress")
|
|
break
|
|
}
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func GetAllRelay() []RelayTable {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
|
|
var ret []RelayTable
|
|
var maps []orm.Params
|
|
_, err := o.QueryTable(new(RelayTable)).Values(&maps)
|
|
if err == nil {
|
|
for _, m := range maps {
|
|
r := new(RelayTable)
|
|
r.Id = utils.GetInt(m, "Id")
|
|
r.Mac = utils.GetString(m, "Mac")
|
|
r.Description = utils.GetString(m, "Description")
|
|
r.IpAddress = utils.GetString(m, "IpAddress")
|
|
|
|
ret = append(ret, *r)
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func GetRelay(id int64) *RelayTable {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
|
|
var ret = new(RelayTable)
|
|
var maps []orm.Params
|
|
_, err := o.QueryTable(new(RelayTable)).Filter("Id", id).Values(&maps)
|
|
if err == nil {
|
|
for _, m := range maps {
|
|
ret.Id = utils.GetInt(m, "Id")
|
|
ret.Mac = utils.GetString(m, "Mac")
|
|
ret.Description = utils.GetString(m, "Description")
|
|
ret.IpAddress = utils.GetString(m, "IpAddress")
|
|
}
|
|
}
|
|
return ret
|
|
}
|
|
|
|
func UpdateSensorDescription(mac, description string) {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
|
|
s := GetRelayByMac(mac)
|
|
if o.Read(s) == nil {
|
|
s.Description = description
|
|
o.Update(s)
|
|
} else {
|
|
AddRelay(mac, description, "")
|
|
}
|
|
}
|
|
|
|
func UpdateSensorIpAddress(mac, ip string) {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
|
|
s := GetRelayByMac(mac)
|
|
if o.Read(s) == nil {
|
|
s.IpAddress = ip
|
|
o.Update(s)
|
|
} else {
|
|
AddRelay(mac, "", ip)
|
|
}
|
|
}
|
|
|
|
func AddRelay(mac, desc, ip string) {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
_, _ = o.Insert(&RelayTable{Mac: mac, Description: desc, IpAddress: ip})
|
|
}
|
|
|
|
func DeleteSensorByMac(mac string) {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
o.Delete(&RelayTable{Mac: mac})
|
|
}
|
|
|
|
func DeleteSensor(id int64) {
|
|
o := orm.NewOrm()
|
|
o.Using(database.Alias)
|
|
o.Delete(&RelayTable{Id: id})
|
|
}
|
|
|
|
func (relay *RelayTable) Toggle() (error) {
|
|
var err error
|
|
if relay.Id != 0 {
|
|
_, err = httplib.Get("http://"+relay.IpAddress+"/toggle").SetTimeout(3*time.Second, 3*time.Second).String()
|
|
}
|
|
return err
|
|
}
|