diff --git a/controllers/relay.go b/controllers/relay.go new file mode 100644 index 0000000..b420f62 --- /dev/null +++ b/controllers/relay.go @@ -0,0 +1,78 @@ +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")) +} diff --git a/controllers/sensors.go b/controllers/sensors.go index aa058f4..e760a46 100644 --- a/controllers/sensors.go +++ b/controllers/sensors.go @@ -3,6 +3,7 @@ package controllers import ( "github.com/astaxie/beego" + "datahouse/models/relay" "datahouse/models/sensor" "datahouse/models/temperature" "datahouse/models/variables" @@ -27,8 +28,10 @@ func (c *SensorsController) Prepare() { func (c *SensorsController) Get() { sensors := sensor.GetAllSensor() + relays := relay.GetAllRelay() c.Data["sensors"] = sensors + c.Data["relays"] = relays c.TplNames = "sensors.tpl" } diff --git a/models/relay/relay.go b/models/relay/relay.go new file mode 100644 index 0000000..2b5aff9 --- /dev/null +++ b/models/relay/relay.go @@ -0,0 +1,126 @@ +package relay + +import ( + "github.com/astaxie/beego/orm" + + "datahouse/models/database" + "datahouse/models/utils" +) + + + +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}) +} diff --git a/models/variables/variables.go b/models/variables/variables.go index e406fe3..a0954ae 100644 --- a/models/variables/variables.go +++ b/models/variables/variables.go @@ -4,12 +4,20 @@ const ( Version = "0.0.3" SessionName = "Session_Data_House" + + sensorMacRegex = ":sensor([0-9A-Fa-f:]+)" ) var ( RootRoute = "/" - AddTempRoute = "/add/temp/:sensor([0-9A-Fa-f:]+)/:val([0-9]+)" - ViewTempRoute = "/view/temp" + + AddTempRoute = "/add/temp/"+sensorMacRegex+"/:val([0-9]+)" + AddRelayRoute = "/add/relay/"+sensorMacRegex + + ViewTempRoute = "/view/temp" + ViewRelaysRoute = "/view/relay" + ViewRelayRoute = "/view/relay/"+sensorMacRegex + SensorsRoute = "/sensors" LoginRoute = "/login" UserRoute = "/user" diff --git a/routers/router.go b/routers/router.go index a85bf15..caf4a34 100644 --- a/routers/router.go +++ b/routers/router.go @@ -8,8 +8,14 @@ import ( func init() { beego.Router(variables.RootRoute, &controllers.MainController{}) + beego.Router(variables.AddTempRoute, &controllers.AddTempController{}) + beego.Router(variables.AddRelayRoute, &controllers.AddRelayController{}) + beego.Router(variables.ViewTempRoute, &controllers.ViewTempController{}) + beego.Router(variables.ViewRelaysRoute, &controllers.ViewRelayController{}) + beego.Router(variables.ViewRelayRoute, &controllers.ViewRelayController{}) + beego.Router(variables.SensorsRoute, &controllers.SensorsController{}) beego.Router(variables.LoginRoute, &controllers.LoginController{}) beego.Router(variables.UserRoute, &controllers.UserController{}) diff --git a/static/css/jumbotron-narrow.css b/static/css/jumbotron-narrow.css new file mode 100644 index 0000000..3b80b6b --- /dev/null +++ b/static/css/jumbotron-narrow.css @@ -0,0 +1,36 @@ +/* Main marketing message and sign up button */ +.jumbotron { + text-align: center; + border-bottom: 1px solid #e5e5e5; +} +.jumbotron .btn { + padding: 14px 24px; + font-size: 21px; +} + +/* Supporting marketing content */ +.marketing { + margin: 40px 0; +} +.marketing p + h4 { + margin-top: 28px; +} + +/* Responsive: Portrait tablets and up */ +@media screen and (min-width: 768px) { + /* Remove the padding we set earlier */ + .header, + .marketing, + .footer { + padding-right: 0; + padding-left: 0; + } + /* Space out the masthead */ + .header { + margin-bottom: 30px; + } + /* Remove the bottom border on the jumbotron for visual effect */ + .jumbotron { + border-bottom: 0; + } +} diff --git a/static/css/style.css b/static/css/style.css new file mode 100644 index 0000000..67f4168 --- /dev/null +++ b/static/css/style.css @@ -0,0 +1,21 @@ +/* make sidebar nav vertical */ +@media (min-width: 768px) { + .sidebar-nav .navbar .navbar-collapse { + padding: 0; + max-height: none; + } + .sidebar-nav .navbar ul { + float: none; + } + .sidebar-nav .navbar ul:not { + display: block; + } + .sidebar-nav .navbar li { + float: none; + display: block; + } + .sidebar-nav .navbar li a { + padding-top: 12px; + padding-bottom: 12px; + } +} diff --git a/static/img/bulbOff.png b/static/img/bulbOff.png new file mode 100644 index 0000000..4de0ba5 Binary files /dev/null and b/static/img/bulbOff.png differ diff --git a/static/img/bulbOn.png b/static/img/bulbOn.png new file mode 100644 index 0000000..4cbda6c Binary files /dev/null and b/static/img/bulbOn.png differ diff --git a/views/base/head.html b/views/base/head.html index bdca40b..d967953 100644 --- a/views/base/head.html +++ b/views/base/head.html @@ -9,6 +9,7 @@ + + + + +
+ {{template "content" .}} +
+ +{{end}} + + +{{define "content"}} + {{if .isRelaySelected}} +
+

{{.relayDescription}}

+
+

Toggle

+
+

+
+ {{end}} +{{end}} + + +{{define "extrajs"}} + +{{end}} diff --git a/views/sensors.tpl b/views/sensors.tpl index 894112c..7a0c391 100644 --- a/views/sensors.tpl +++ b/views/sensors.tpl @@ -1,8 +1,11 @@ {{template "base/base.html" .}} {{define "meta"}} {{end}} +{{define "extrajs"}} +{{end}} {{define "body"}}
+ {{range $key, $val := .sensors}}
@@ -14,17 +17,6 @@
-


@@ -38,5 +30,27 @@
{{end}} + +
+ + {{range $key, $val := .relays}} +
+
+
+

{{$val.Mac}}

+ +
+
+
+ + +
+

+ + +
+
+
+ {{end}}
{{end}} diff --git a/views/temp.tpl b/views/temp.tpl index 92c427c..3849936 100644 --- a/views/temp.tpl +++ b/views/temp.tpl @@ -75,6 +75,10 @@ }); {{end}} + +{{define "extrajs"}} +{{end}} + {{define "body"}}

Temperatures