diff --git a/controllers/notifications.go b/controllers/notifications.go new file mode 100644 index 0000000..680eb30 --- /dev/null +++ b/controllers/notifications.go @@ -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 +} diff --git a/models/notification/notification.go b/models/notification/notification.go index 6a4ec9d..7a34791 100644 --- a/models/notification/notification.go +++ b/models/notification/notification.go @@ -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) diff --git a/models/sensor/sensor.go b/models/sensor/sensor.go index bd79015..651e448 100644 --- a/models/sensor/sensor.go +++ b/models/sensor/sensor.go @@ -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 } diff --git a/models/variables/variables.go b/models/variables/variables.go index f300279..51c7827 100644 --- a/models/variables/variables.go +++ b/models/variables/variables.go @@ -14,17 +14,17 @@ const ( ) var ( -/* -———————————————————————————————————————————————————————————————————————————————— - Routes -———————————————————————————————————————————————————————————————————————————————— -*/ - RootRoute = "/" + /* + ———————————————————————————————————————————————————————————————————————————————— + Routes + ———————————————————————————————————————————————————————————————————————————————— + */ + RootRoute = "/" - AddTempRoute = "/add/temp/" + sensorMacRegex + "/:val([0-9]+)" - AddRelayRoute = "/add/relay/" + sensorMacRegex - TeleinfoAddRoute = "/teleinfo/add" - AddSoilMoistRoute = "/add/soil" /* Route for soil moisture sensors*/ + AddTempRoute = "/add/temp/" + sensorMacRegex + "/:val([0-9]+)" + AddRelayRoute = "/add/relay/" + sensorMacRegex + TeleinfoAddRoute = "/teleinfo/add" + AddSoilMoistRoute = "/add/soil" /* Route for soil moisture sensors*/ ViewTempRoute = "/view/temp" ViewRelaysRoute = "/view/relay" @@ -34,19 +34,19 @@ var ( ViewLogRoute = "/view/log" WebSocketLogRoute = "/view/log/join" - CommandRelayRoute = "/command/relay/" + sensorMacRegex + CommandRelayRoute = "/command/relay/" + sensorMacRegex SensorsRoute = "/sensors" LoginRoute = "/login/:route(.*)" LoginRouteNoRegex = "/login" UserRoute = "/user" - + NotificationsRoute = "/notifications" /* - ———————————————————————————————————————————————————————————————————————————————— - Logs - ———————————————————————————————————————————————————————————————————————————————— + ———————————————————————————————————————————————————————————————————————————————— + Logs + ———————————————————————————————————————————————————————————————————————————————— */ - LogType = logFile - LogParams = "{\""+LogFileName+"\":\""+LogFilePath+"\"}" + LogType = logFile + LogParams = "{\"" + LogFileName + "\":\"" + LogFilePath + "\"}" ) diff --git a/routers/router.go b/routers/router.go index 421c170..057f758 100644 --- a/routers/router.go +++ b/routers/router.go @@ -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{}) } diff --git a/views/base/navbar.html b/views/base/navbar.html index 38410f3..4f20b06 100644 --- a/views/base/navbar.html +++ b/views/base/navbar.html @@ -43,6 +43,11 @@ class="active" {{end}} >Utilisateur +
| # | +Condition | +Value | +Sensor | +Action | +
|---|