Add the control of the relay button
This commit is contained in:
parent
7116d3239c
commit
a4473eb283
|
|
@ -10,6 +10,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
type ViewRelayController struct {
|
type ViewRelayController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
}
|
}
|
||||||
|
|
@ -59,7 +67,9 @@ func (c *ViewRelayController) Post() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
*/
|
||||||
|
|
||||||
type AddRelayController struct {
|
type AddRelayController struct {
|
||||||
beego.Controller
|
beego.Controller
|
||||||
|
|
@ -76,3 +86,25 @@ func (c *AddRelayController) Get() {
|
||||||
|
|
||||||
c.Ctx.Output.Body([]byte("OK"))
|
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() {
|
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")
|
description := c.Input().Get("description")
|
||||||
mac := c.Input().Get("mac")
|
mac := c.Input().Get("mac")
|
||||||
intervalStr := c.Input().Get("interval")
|
intervalStr := c.Input().Get("interval")
|
||||||
|
|
@ -57,3 +70,25 @@ func (c *SensorsController) Post() {
|
||||||
c.Redirect("/404", 404)
|
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
|
package variables
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Version = "0.0.3"
|
Version = "0.0.4"
|
||||||
|
|
||||||
SessionName = "Session_Data_House"
|
SessionName = "Session_Data_House"
|
||||||
|
|
||||||
|
|
@ -18,6 +18,8 @@ var (
|
||||||
ViewRelaysRoute = "/view/relay"
|
ViewRelaysRoute = "/view/relay"
|
||||||
ViewRelayRoute = "/view/relay/"+sensorMacRegex
|
ViewRelayRoute = "/view/relay/"+sensorMacRegex
|
||||||
|
|
||||||
|
CommandRelayRoute = "/command/relay/"+sensorMacRegex
|
||||||
|
|
||||||
SensorsRoute = "/sensors"
|
SensorsRoute = "/sensors"
|
||||||
LoginRoute = "/login"
|
LoginRoute = "/login"
|
||||||
UserRoute = "/user"
|
UserRoute = "/user"
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,8 @@ func init() {
|
||||||
beego.Router(variables.ViewRelaysRoute, &controllers.ViewRelayController{})
|
beego.Router(variables.ViewRelaysRoute, &controllers.ViewRelayController{})
|
||||||
beego.Router(variables.ViewRelayRoute, &controllers.ViewRelayController{})
|
beego.Router(variables.ViewRelayRoute, &controllers.ViewRelayController{})
|
||||||
|
|
||||||
|
beego.Router(variables.CommandRelayRoute, &controllers.CommandRelayController{})
|
||||||
|
|
||||||
beego.Router(variables.SensorsRoute, &controllers.SensorsController{})
|
beego.Router(variables.SensorsRoute, &controllers.SensorsController{})
|
||||||
beego.Router(variables.LoginRoute, &controllers.LoginController{})
|
beego.Router(variables.LoginRoute, &controllers.LoginController{})
|
||||||
beego.Router(variables.UserRoute, &controllers.UserController{})
|
beego.Router(variables.UserRoute, &controllers.UserController{})
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
<br/>
|
<br/>
|
||||||
<footer class="footer">
|
<footer class="footer">
|
||||||
<div class="container">
|
<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>
|
</div>
|
||||||
</footer>
|
</footer>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@
|
||||||
<div class="jumbotron">
|
<div class="jumbotron">
|
||||||
<h3>{{.relayDescription}}</h3>
|
<h3>{{.relayDescription}}</h3>
|
||||||
<br/>
|
<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/>
|
<br/>
|
||||||
<p><img id="{{.relayMac}}" src="/static/img/bulbOff.png" style="width: 100px;"/></p>
|
<p><img id="{{.relayMac}}" src="/static/img/bulbOff.png" style="width: 100px;"/></p>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -96,6 +96,14 @@ var forEachRelay = function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$("#toggleButton").click(function() {
|
||||||
|
$.ajax({
|
||||||
|
url: "/command/relay/"+$(".jumbotron img").attr("id"),
|
||||||
|
method: "POST",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
forEachRelay();
|
forEachRelay();
|
||||||
setInterval(forEachRelay, 5000);
|
setInterval(forEachRelay, 5000);
|
||||||
</script>
|
</script>
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
{{range $key, $val := .sensors}}
|
{{range $key, $val := .sensors}}
|
||||||
<form method="POST" action="/sensors" class="form-inline">
|
<form method="POST" action="/sensors" class="form-inline">
|
||||||
|
<input type="hidden" name="type" value="tempSensor" />
|
||||||
<div class="panel panel-info">
|
<div class="panel panel-info">
|
||||||
<div class="panel-heading">
|
<div class="panel-heading">
|
||||||
<h3 class="panel-title">{{$val.SensorMAC}}</h3>
|
<h3 class="panel-title">{{$val.SensorMAC}}</h3>
|
||||||
|
|
@ -35,6 +36,7 @@
|
||||||
|
|
||||||
{{range $key, $val := .relays}}
|
{{range $key, $val := .relays}}
|
||||||
<form method="POST" action="/sensors" class="form-inline">
|
<form method="POST" action="/sensors" class="form-inline">
|
||||||
|
<input type="hidden" name="type" value="relay" />
|
||||||
<div class="panel panel-warning">
|
<div class="panel panel-warning">
|
||||||
<div class="panel-heading">
|
<div class="panel-heading">
|
||||||
<h3 class="panel-title">{{$val.Mac}}</h3>
|
<h3 class="panel-title">{{$val.Mac}}</h3>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue