Add the control of the relay button
This commit is contained in:
parent
7116d3239c
commit
a4473eb283
|
|
@ -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(""))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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{})
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
<br/>
|
||||
<footer class="footer">
|
||||
<div class="container">
|
||||
<p class="text-muted"><strong>DataHouse</strong> (collect your own private data)<span class="navbar-right">Version <strong>{{.version}}</strong></span></p>
|
||||
<p class="text-muted"><strong>DataHouse</strong><span class="navbar-right">Version <strong>{{.version}}</strong></span></p>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@
|
|||
<div class="jumbotron">
|
||||
<h3>{{.relayDescription}}</h3>
|
||||
<br/>
|
||||
<p><a class="btn btn-lg btn-primary" href="#" role="button">Toggle</a></p>
|
||||
<p><a id="toggleButton" class="btn btn-lg btn-primary" href="#" role="button">Toggle</a></p>
|
||||
<br/>
|
||||
<p><img id="{{.relayMac}}" src="/static/img/bulbOff.png" style="width: 100px;"/></p>
|
||||
</div>
|
||||
|
|
@ -96,6 +96,14 @@ var forEachRelay = function() {
|
|||
});
|
||||
}
|
||||
|
||||
|
||||
$("#toggleButton").click(function() {
|
||||
$.ajax({
|
||||
url: "/command/relay/"+$(".jumbotron img").attr("id"),
|
||||
method: "POST",
|
||||
});
|
||||
});
|
||||
|
||||
forEachRelay();
|
||||
setInterval(forEachRelay, 5000);
|
||||
</script>
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
{{range $key, $val := .sensors}}
|
||||
<form method="POST" action="/sensors" class="form-inline">
|
||||
<input type="hidden" name="type" value="tempSensor" />
|
||||
<div class="panel panel-info">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">{{$val.SensorMAC}}</h3>
|
||||
|
|
@ -35,6 +36,7 @@
|
|||
|
||||
{{range $key, $val := .relays}}
|
||||
<form method="POST" action="/sensors" class="form-inline">
|
||||
<input type="hidden" name="type" value="relay" />
|
||||
<div class="panel panel-warning">
|
||||
<div class="panel-heading">
|
||||
<h3 class="panel-title">{{$val.Mac}}</h3>
|
||||
|
|
|
|||
Loading…
Reference in New Issue