DataHouse/controllers/relay.go

79 lines
1.4 KiB
Go

package controllers
import (
"github.com/astaxie/beego"
"github.com/astaxie/beego/httplib"
"datahouse/models/variables"
"datahouse/models/relay"
"strings"
)
type ViewRelayController struct {
beego.Controller
}
func (c *ViewRelayController) Prepare() {
c.Data["IsViewRelay"] = true;
c.Data["version"] = variables.Version
}
func (c *ViewRelayController) Get() {
c.Data["relays"] = relay.GetAllRelay()
mac := c.Ctx.Input.Param(":sensor")
r := relay.GetRelayByMac(mac)
if r.Id != 0 {
c.Data["isRelaySelected"] = true
c.Data["relayMac"] = r.Mac
if r.Description != "" {
c.Data["relayDescription"] = r.Description
} else {
c.Data["relayDescription"] = r.Mac
}
}
c.TplNames = "relay.tpl"
}
func (c *ViewRelayController) Post() {
mac := c.Ctx.Input.Param(":sensor")
r := relay.GetRelayByMac(mac)
ret := ""
if r.Id != 0 {
ret += r.Mac+"/"
if r.IpAddress != "" {
getRep, err := httplib.Get("http://"+r.IpAddress+"/status").String()
if err == nil {
ret += getRep
} else {
relay.UpdateSensorIpAddress(r.Mac, "")
}
}
}
c.Ctx.Output.Body([]byte(ret))
}
type AddRelayController struct {
beego.Controller
}
func (c *AddRelayController) Prepare() {
c.Data["version"] = variables.Version
}
func (c *AddRelayController) Get() {
ip := strings.Split(c.Ctx.Request.RemoteAddr, ":")[0]
mac := c.Ctx.Input.Param(":sensor")
relay.UpdateSensorIpAddress(mac, ip)
c.Ctx.Output.Body([]byte("OK"))
}