diff --git a/controllers/relay.go b/controllers/relay.go index b420f62..44e3615 100644 --- a/controllers/relay.go +++ b/controllers/relay.go @@ -10,6 +10,14 @@ import ( ) + + + +/* +-------------------------------------------------------------------------------- +*/ + + type ViewRelayController struct { beego.Controller } @@ -59,7 +67,9 @@ func (c *ViewRelayController) Post() { - +/* +-------------------------------------------------------------------------------- +*/ type AddRelayController struct { beego.Controller @@ -76,3 +86,25 @@ func (c *AddRelayController) Get() { c.Ctx.Output.Body([]byte("OK")) } + + + + +/* +-------------------------------------------------------------------------------- +*/ + +type CommandRelayController struct { + beego.Controller +} + + +func (c *CommandRelayController) Post() { + mac := c.Ctx.Input.Param(":sensor") + r := relay.GetRelayByMac(mac) + if r.Id != 0 { + httplib.Get("http://"+r.IpAddress+"/toggle").String() + } + + c.Ctx.Output.Body([]byte("")) +} diff --git a/controllers/sensors.go b/controllers/sensors.go index e760a46..e18dcdf 100644 --- a/controllers/sensors.go +++ b/controllers/sensors.go @@ -36,6 +36,19 @@ func (c *SensorsController) Get() { } func (c *SensorsController) Post() { + tpye := c.Input().Get("type") + if tpye == "tempSensor" { + c.PostTempSensors() + } else if tpye == "relay" { + c.PostRelay() + } else { + c.Redirect("/404", 404) + } +} + + + +func (c *SensorsController) PostTempSensors() { description := c.Input().Get("description") mac := c.Input().Get("mac") intervalStr := c.Input().Get("interval") @@ -57,3 +70,25 @@ func (c *SensorsController) Post() { c.Redirect("/404", 404) } } + + + +func (c *SensorsController) PostRelay() { + description := c.Input().Get("description") + mac := c.Input().Get("mac") + + isDelete := c.Input().Get("delete") + fmt.Println("Del: ", isDelete) + isSave := c.Input().Get("save") + fmt.Println("Save: ", isSave) + if isSave != "" { + relay.UpdateSensorDescription(mac, description) + c.Redirect("/sensors", 302) + } else if isDelete != "" { + s := relay.GetRelayByMac(mac) + sensor.DeleteSensor(s.Id) + c.Redirect("/sensors", 302) + } else { + c.Redirect("/404", 404) + } +} diff --git a/models/variables/variables.go b/models/variables/variables.go index a0954ae..36d5dff 100644 --- a/models/variables/variables.go +++ b/models/variables/variables.go @@ -1,7 +1,7 @@ package variables const ( - Version = "0.0.3" + Version = "0.0.4" SessionName = "Session_Data_House" @@ -18,6 +18,8 @@ var ( ViewRelaysRoute = "/view/relay" ViewRelayRoute = "/view/relay/"+sensorMacRegex + CommandRelayRoute = "/command/relay/"+sensorMacRegex + SensorsRoute = "/sensors" LoginRoute = "/login" UserRoute = "/user" diff --git a/routers/router.go b/routers/router.go index caf4a34..40054a2 100644 --- a/routers/router.go +++ b/routers/router.go @@ -16,6 +16,8 @@ func init() { beego.Router(variables.ViewRelaysRoute, &controllers.ViewRelayController{}) beego.Router(variables.ViewRelayRoute, &controllers.ViewRelayController{}) + beego.Router(variables.CommandRelayRoute, &controllers.CommandRelayController{}) + beego.Router(variables.SensorsRoute, &controllers.SensorsController{}) beego.Router(variables.LoginRoute, &controllers.LoginController{}) beego.Router(variables.UserRoute, &controllers.UserController{}) diff --git a/views/base/footer.html b/views/base/footer.html index 32ed3d6..dafae86 100644 --- a/views/base/footer.html +++ b/views/base/footer.html @@ -1,7 +1,7 @@
diff --git a/views/relay.tpl b/views/relay.tpl index 72c2fcd..77669cf 100644 --- a/views/relay.tpl +++ b/views/relay.tpl @@ -48,7 +48,7 @@

{{.relayDescription}}


-

Toggle

+

Toggle


@@ -96,6 +96,14 @@ var forEachRelay = function() { }); } + +$("#toggleButton").click(function() { + $.ajax({ + url: "/command/relay/"+$(".jumbotron img").attr("id"), + method: "POST", + }); +}); + forEachRelay(); setInterval(forEachRelay, 5000); diff --git a/views/sensors.tpl b/views/sensors.tpl index 7a0c391..77db4ea 100644 --- a/views/sensors.tpl +++ b/views/sensors.tpl @@ -8,6 +8,7 @@ {{range $key, $val := .sensors}}
+

{{$val.SensorMAC}}

@@ -35,6 +36,7 @@ {{range $key, $val := .relays}} +

{{$val.Mac}}