New page to administrate notifications.
This commit is contained in:
parent
a804cc70f5
commit
2e630d4fe1
|
|
@ -0,0 +1,124 @@
|
|||
package controllers
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
|
||||
"git.kingpenguin.tk/chteufleur/datahouse.git/models/notification"
|
||||
"git.kingpenguin.tk/chteufleur/datahouse.git/models/sensor"
|
||||
"git.kingpenguin.tk/chteufleur/datahouse.git/models/user"
|
||||
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
|
||||
)
|
||||
|
||||
type NotificationsController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
type NotificationView struct {
|
||||
Id int64
|
||||
Condition string
|
||||
Value int64
|
||||
Sensor string
|
||||
}
|
||||
|
||||
type SensorView struct {
|
||||
MAC string
|
||||
Name string
|
||||
}
|
||||
|
||||
func (c *NotificationsController) Prepare() {
|
||||
sess := c.GetSession(variables.SessionName)
|
||||
if sess == nil {
|
||||
c.Redirect(variables.LoginRouteNoRegex+variables.UserRoute, 302)
|
||||
} else {
|
||||
c.Data["IsAuthentificated"] = true
|
||||
}
|
||||
|
||||
c.Data["IsNotifications"] = true
|
||||
c.Data["version"] = variables.Version
|
||||
}
|
||||
|
||||
func (c *NotificationsController) Get() {
|
||||
var notifications []NotificationView
|
||||
var notifIds []int64
|
||||
u := c.getUserLogin()
|
||||
if u != nil {
|
||||
notif := notification.GetNotificationConditionByUser(u.Id)
|
||||
for _, nv := range notif {
|
||||
n := new(NotificationView)
|
||||
n.Id = nv.Id
|
||||
n.Condition = nv.Condition
|
||||
n.Value = nv.Value
|
||||
|
||||
sensr := sensor.GetSensorByNotificationId(n.Id)
|
||||
if sensr != nil {
|
||||
n.Sensor = sensr.GetName()
|
||||
notifications = append(notifications, *n)
|
||||
notifIds = append(notifIds, n.Id)
|
||||
}
|
||||
}
|
||||
}
|
||||
c.Data["notifications"] = notifications
|
||||
|
||||
var snsrs []SensorView
|
||||
sensors := sensor.GetAllSensor()
|
||||
for _, s := range sensors {
|
||||
snsrs = append(snsrs, SensorView{MAC: s.SensorMAC, Name: s.GetName()})
|
||||
}
|
||||
c.Data["sensors"] = snsrs
|
||||
|
||||
c.TplName = "notifications.tpl"
|
||||
}
|
||||
|
||||
func (c *NotificationsController) Post() {
|
||||
u := c.getUserLogin()
|
||||
|
||||
action := c.Input().Get("action")
|
||||
notifIdStr := c.Input().Get("id")
|
||||
condition := c.Input().Get("condition")
|
||||
valueStr := c.Input().Get("value")
|
||||
sensMAC := c.Input().Get("sensor-mac")
|
||||
|
||||
notifId, errNotifId := strconv.ParseInt(notifIdStr, 10, 64)
|
||||
value, errValue := strconv.ParseInt(valueStr, 10, 64)
|
||||
|
||||
if action == "modif" && errNotifId == nil && errValue == nil {
|
||||
if notification.UpdateNotificationCondition(notifId, u.Id, condition, value) == nil {
|
||||
oldSensor := sensor.GetSensorByNotificationId(notifId)
|
||||
newSensor := sensor.GetSensorByMac(sensMAC)
|
||||
if oldSensor.Id != 0 && newSensor.Id != 0 && oldSensor.Id != newSensor.Id {
|
||||
oldSensor.DeleteNotification(notifId)
|
||||
newSensor.AddNotification(notifId)
|
||||
}
|
||||
}
|
||||
} else if action == "delete" && errNotifId == nil && errValue == nil {
|
||||
notification.DeleteNotificationCondition(notifId)
|
||||
sens := sensor.GetSensorByNotificationId(notifId)
|
||||
if sens.Id != 0 {
|
||||
sens.DeleteNotification(notifId)
|
||||
}
|
||||
} else if action == "add" && errValue == nil {
|
||||
sens := sensor.GetSensorByMac(sensMAC)
|
||||
if sens.Id != 0 {
|
||||
notifId, err := notification.AddNotificationCondition(u.Id, condition, value)
|
||||
if err == nil {
|
||||
sens.AddNotification(notifId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.Redirect(variables.NotificationsRoute, 302)
|
||||
}
|
||||
|
||||
func (c *NotificationsController) getUserLogin() *user.User {
|
||||
ret := new(user.User)
|
||||
|
||||
login := c.GetSession(variables.SessionName)
|
||||
switch lo := login.(type) {
|
||||
case string:
|
||||
ret = user.GetUserByLogin(lo)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
|
@ -32,13 +32,13 @@ func init() {
|
|||
orm.RegisterModel(new(NotificationCondition))
|
||||
}
|
||||
|
||||
func AddNotificationCondition(userId int64, condition string, value int64) {
|
||||
func AddNotificationCondition(userId int64, condition string, value int64) (int64, error) {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
_, _ = o.Insert(&NotificationCondition{UserId: userId, Condition: condition, Value: value})
|
||||
return o.Insert(&NotificationCondition{UserId: userId, Condition: condition, Value: value})
|
||||
}
|
||||
|
||||
func UpdateNotificationCondition(id, userId int64, condition string, value int64) {
|
||||
func UpdateNotificationCondition(id, userId int64, condition string, value int64) error {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
|
||||
|
|
@ -47,8 +47,10 @@ func UpdateNotificationCondition(id, userId int64, condition string, value int64
|
|||
n.UserId = userId
|
||||
n.Condition = condition
|
||||
n.Value = value
|
||||
o.Update(n)
|
||||
_, err := o.Update(n)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteNotificationCondition(id int64) {
|
||||
|
|
@ -57,6 +59,65 @@ func DeleteNotificationCondition(id int64) {
|
|||
o.Delete(&NotificationCondition{Id: id})
|
||||
}
|
||||
|
||||
func GetAllNotificationConditionIds() []int64 {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
|
||||
notif := new(NotificationCondition)
|
||||
var ret []int64
|
||||
var maps []orm.Params
|
||||
_, err := o.QueryTable(notif).Values(&maps)
|
||||
if err == nil {
|
||||
for _, m := range maps {
|
||||
ret = append(ret, utils.GetInt(m, "Id"))
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func GetAllNotificationCondition() []NotificationCondition {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
|
||||
notif := new(NotificationCondition)
|
||||
var maps []orm.Params
|
||||
var ret []NotificationCondition
|
||||
_, err := o.QueryTable(notif).Values(&maps)
|
||||
if err == nil {
|
||||
for _, m := range maps {
|
||||
n := new(NotificationCondition)
|
||||
n.Id = utils.GetInt(m, "Id")
|
||||
n.UserId = utils.GetInt(m, "UserId")
|
||||
n.Condition = utils.GetString(m, "Condition")
|
||||
n.Value = utils.GetInt(m, "Value")
|
||||
ret = append(ret, *n)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func GetNotificationConditionByUser(idUser int64) []NotificationCondition {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
|
||||
var ret []NotificationCondition
|
||||
var maps []orm.Params
|
||||
_, err := o.QueryTable(new(NotificationCondition)).Filter("UserId", idUser).Values(&maps)
|
||||
if err == nil {
|
||||
for _, m := range maps {
|
||||
n := new(NotificationCondition)
|
||||
n.Id = utils.GetInt(m, "Id")
|
||||
n.UserId = utils.GetInt(m, "UserId")
|
||||
n.Condition = utils.GetString(m, "Condition")
|
||||
n.Value = utils.GetInt(m, "Value")
|
||||
ret = append(ret, *n)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func GetNotificationCondition(id int64) *NotificationCondition {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ import (
|
|||
"github.com/astaxie/beego/orm"
|
||||
|
||||
"git.kingpenguin.tk/chteufleur/datahouse.git/models/database"
|
||||
"git.kingpenguin.tk/chteufleur/datahouse.git/models/notification"
|
||||
"git.kingpenguin.tk/chteufleur/datahouse.git/models/utils"
|
||||
)
|
||||
|
||||
|
|
@ -108,6 +107,18 @@ func GetSensor(id int64) *SensorTable {
|
|||
return ret
|
||||
}
|
||||
|
||||
func GetSensorByNotificationId(notifId int64) *SensorTable {
|
||||
var ret *SensorTable = nil
|
||||
allSensors := GetAllSensor()
|
||||
for _, s := range allSensors {
|
||||
if s.IsContainNotificationId(notifId) {
|
||||
ret = &s
|
||||
break
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func UpdateSensor(mac, description string, interval int64) {
|
||||
o := orm.NewOrm()
|
||||
o.Using(database.Alias)
|
||||
|
|
@ -184,17 +195,30 @@ func (s *SensorTable) GetNotificationsIds() []int64 {
|
|||
return ret
|
||||
}
|
||||
|
||||
func (s *SensorTable) AddNotification(n notification.NotificationCondition) {
|
||||
s.Notifications += NOTIFICATION_DELIMITER + strconv.FormatInt(n.Id, 10)
|
||||
func (s *SensorTable) IsContainNotificationId(idNotif int64) bool {
|
||||
for _, b := range strings.Split(s.Notifications, NOTIFICATION_DELIMITER) {
|
||||
bb, err := strconv.Atoi(b)
|
||||
if err == nil && int64(bb) == idNotif {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *SensorTable) AddNotification(notifId int64) {
|
||||
if s.Notifications != "" {
|
||||
s.Notifications += NOTIFICATION_DELIMITER
|
||||
}
|
||||
s.Notifications += strconv.FormatInt(notifId, 10)
|
||||
s.update()
|
||||
}
|
||||
|
||||
func (s *SensorTable) DeleteNotification(n notification.NotificationCondition) {
|
||||
func (s *SensorTable) DeleteNotification(notifId int64) {
|
||||
newNotificationStr := ""
|
||||
for _, idStr := range strings.Split(s.Notifications, NOTIFICATION_DELIMITER) {
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err == nil {
|
||||
if int64(id) != n.Id {
|
||||
if int64(id) != notifId {
|
||||
if newNotificationStr != "" {
|
||||
newNotificationStr += NOTIFICATION_DELIMITER
|
||||
}
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ var (
|
|||
LoginRoute = "/login/:route(.*)"
|
||||
LoginRouteNoRegex = "/login"
|
||||
UserRoute = "/user"
|
||||
|
||||
NotificationsRoute = "/notifications"
|
||||
|
||||
/*
|
||||
————————————————————————————————————————————————————————————————————————————————
|
||||
|
|
|
|||
|
|
@ -28,5 +28,6 @@ func init() {
|
|||
beego.Router(variables.SensorsRoute, &controllers.SensorsController{})
|
||||
beego.Router(variables.LoginRoute, &controllers.LoginController{})
|
||||
beego.Router(variables.UserRoute, &controllers.UserController{})
|
||||
beego.Router(variables.NotificationsRoute, &controllers.NotificationsController{})
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,6 +43,11 @@
|
|||
class="active"
|
||||
{{end}}
|
||||
><a href="/user">Utilisateur</a></li>
|
||||
<li
|
||||
{{if .IsNotifications}}
|
||||
class="active"
|
||||
{{end}}
|
||||
><a href="/notifications">Notifications</a></li>
|
||||
{{end}}
|
||||
<li
|
||||
{{if .IsSensor}}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,108 @@
|
|||
{{template "base/base.html" .}}
|
||||
{{define "meta"}}
|
||||
{{end}}
|
||||
{{define "extrajs"}}
|
||||
{{end}}
|
||||
{{define "body"}}
|
||||
|
||||
{{ $senss := .sensors }}
|
||||
{{ $notifs := .notifications }}
|
||||
<br/>
|
||||
|
||||
<table class="table table-striped">
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<th>Condition</th>
|
||||
<th>Value</th>
|
||||
<th>Sensor</th>
|
||||
<th>Action</th>
|
||||
</tr>
|
||||
{{range $key, $val := $notifs}}
|
||||
<form method="POST" >
|
||||
<tr>
|
||||
<td>
|
||||
{{$val.Id}}
|
||||
<input type="hidden" name="id" value="{{$val.Id}}" />
|
||||
</td>
|
||||
<td>
|
||||
<select class="form-control" name="condition">
|
||||
<option value="<"
|
||||
{{if eq $val.Condition "<" }}
|
||||
selected
|
||||
{{end}}
|
||||
>lower</option>
|
||||
<option value="<="
|
||||
{{if eq $val.Condition "<=" }}
|
||||
selected
|
||||
{{end}}
|
||||
>lower or equals</option>
|
||||
<option value=">"
|
||||
{{if eq $val.Condition ">" }}
|
||||
selected
|
||||
{{end}}
|
||||
>greater</option>
|
||||
<option value=">="
|
||||
{{if eq $val.Condition ">=" }}
|
||||
selected
|
||||
{{end}}
|
||||
>greater or equals</option>
|
||||
<option value="!="
|
||||
{{if eq $val.Condition "!=" }}
|
||||
selected
|
||||
{{end}}
|
||||
>different</option>
|
||||
<option value="=="
|
||||
{{if eq $val.Condition "==" }}
|
||||
selected
|
||||
{{end}}
|
||||
>equals</option>
|
||||
</select>
|
||||
</td>
|
||||
<td><input type="number" class="form-control" placeholder="Value" name="value" value="{{$val.Value}}" /></td>
|
||||
<td>
|
||||
<select class="form-control" name="sensor-mac">
|
||||
{{range $kk, $sens := $senss}}
|
||||
<option value="{{$sens.MAC}}"
|
||||
{{ if eq $sens.Name $val.Sensor }}
|
||||
selected
|
||||
{{end}}
|
||||
>{{$sens.Name}}</option>
|
||||
{{end}}
|
||||
</select>
|
||||
</td>
|
||||
<td>
|
||||
<button type="submit" name="action" value="modif" class="btn btn-info btn-xs btn-block">Udpate</button>
|
||||
<button type="submit" name="action" value="delete" class="btn btn-danger btn-xs btn-block">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
</form>
|
||||
{{end}}
|
||||
<form method="POST" >
|
||||
<tr>
|
||||
<td></td>
|
||||
<td>
|
||||
<select class="form-control" name="condition">
|
||||
<option value="<">lower</option>
|
||||
<option value="<=">lower or equals</option>
|
||||
<option value=">">greater</option>
|
||||
<option value=">=">greater or equals</option>
|
||||
<option value="!=">different</option>
|
||||
<option value="==">equals</option>
|
||||
</select>
|
||||
</td>
|
||||
<td><input type="number" class="form-control" placeholder="Value" name="value" /></td>
|
||||
<td>
|
||||
<select class="form-control" name="sensor-mac">
|
||||
{{range $kk, $sens := $senss}}
|
||||
<option value="{{$sens.MAC}}">{{$sens.Name}}</option>
|
||||
{{end}}
|
||||
</select>
|
||||
</td>
|
||||
<td><button type="submit" name="action" value="add" class="btn btn-success btn-xs btn-block">Add</button></td>
|
||||
</tr>
|
||||
</form>
|
||||
</table>
|
||||
|
||||
<br/>
|
||||
|
||||
{{end}}
|
||||
Loading…
Reference in New Issue