diff --git a/controllers/notifications.go b/controllers/notifications.go index 680eb30..8dd149e 100644 --- a/controllers/notifications.go +++ b/controllers/notifications.go @@ -20,6 +20,7 @@ type NotificationView struct { Condition string Value int64 Sensor string + Enable bool } type SensorView struct { @@ -50,6 +51,7 @@ func (c *NotificationsController) Get() { n.Id = nv.Id n.Condition = nv.Condition n.Value = nv.Value + n.Enable = nv.Enable sensr := sensor.GetSensorByNotificationId(n.Id) if sensr != nil { @@ -79,12 +81,13 @@ func (c *NotificationsController) Post() { condition := c.Input().Get("condition") valueStr := c.Input().Get("value") sensMAC := c.Input().Get("sensor-mac") + enableStr := c.Input().Get("enable") 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 { + if notification.UpdateNotificationCondition(notifId, u.Id, condition, value, enableStr == "on") == nil { oldSensor := sensor.GetSensorByNotificationId(notifId) newSensor := sensor.GetSensorByMac(sensMAC) if oldSensor.Id != 0 && newSensor.Id != 0 && oldSensor.Id != newSensor.Id { @@ -101,7 +104,7 @@ func (c *NotificationsController) Post() { } else if action == "add" && errValue == nil { sens := sensor.GetSensorByMac(sensMAC) if sens.Id != 0 { - notifId, err := notification.AddNotificationCondition(u.Id, condition, value) + notifId, err := notification.AddNotificationCondition(u.Id, condition, value, enableStr == "on") if err == nil { sens.AddNotification(notifId) } diff --git a/models/notification/notification.go b/models/notification/notification.go index 7a34791..115a94e 100644 --- a/models/notification/notification.go +++ b/models/notification/notification.go @@ -26,19 +26,20 @@ type NotificationCondition struct { UserId int64 Condition string Value int64 + Enable bool } func init() { orm.RegisterModel(new(NotificationCondition)) } -func AddNotificationCondition(userId int64, condition string, value int64) (int64, error) { +func AddNotificationCondition(userId int64, condition string, value int64, enable bool) (int64, error) { o := orm.NewOrm() o.Using(database.Alias) - return o.Insert(&NotificationCondition{UserId: userId, Condition: condition, Value: value}) + return o.Insert(&NotificationCondition{UserId: userId, Condition: condition, Value: value, Enable: enable}) } -func UpdateNotificationCondition(id, userId int64, condition string, value int64) error { +func UpdateNotificationCondition(id, userId int64, condition string, value int64, enable bool) error { o := orm.NewOrm() o.Using(database.Alias) @@ -47,6 +48,7 @@ func UpdateNotificationCondition(id, userId int64, condition string, value int64 n.UserId = userId n.Condition = condition n.Value = value + n.Enable = enable _, err := o.Update(n) return err } @@ -90,6 +92,7 @@ func GetAllNotificationCondition() []NotificationCondition { n.UserId = utils.GetInt(m, "UserId") n.Condition = utils.GetString(m, "Condition") n.Value = utils.GetInt(m, "Value") + n.Enable = utils.GetBool(m, "Enable") ret = append(ret, *n) } } @@ -111,6 +114,7 @@ func GetNotificationConditionByUser(idUser int64) []NotificationCondition { n.UserId = utils.GetInt(m, "UserId") n.Condition = utils.GetString(m, "Condition") n.Value = utils.GetInt(m, "Value") + n.Enable = utils.GetBool(m, "Enable") ret = append(ret, *n) } } @@ -131,6 +135,7 @@ func GetNotificationCondition(id int64) *NotificationCondition { ret.UserId = utils.GetInt(m, "UserId") ret.Condition = utils.GetString(m, "Condition") ret.Value = utils.GetInt(m, "Value") + ret.Enable = utils.GetBool(m, "Enable") } } @@ -158,7 +163,7 @@ func (n *NotificationCondition) IsConditionValidate(val int64) bool { } func (n *NotificationCondition) Notify(val int64, message string) { - if n.IsConditionValidate(val) { + if n.Enable && n.IsConditionValidate(val) { u := user.GetUser(n.UserId) if u != nil && u.JID != "" { xmpp_manager.SendMessage(u.JID, "", "["+n.Condition+" "+strconv.FormatInt(n.Value, 10)+"] "+message) diff --git a/models/utils/utils.go b/models/utils/utils.go index 01be234..7b7d267 100644 --- a/models/utils/utils.go +++ b/models/utils/utils.go @@ -33,6 +33,18 @@ func GetInt(m orm.Params, param string) int64 { return ret } +func GetBool(m orm.Params, param string) bool { + var ret bool + ret = false + + switch i := m[param].(type) { + case bool: + ret = i + } + + return ret +} + func GetTime(m orm.Params, param string) time.Time { var ret time.Time diff --git a/views/notifications.tpl b/views/notifications.tpl index 37eecf2..a340207 100644 --- a/views/notifications.tpl +++ b/views/notifications.tpl @@ -15,6 +15,7 @@