Add the possibility to disable a notification.

This commit is contained in:
Chteufleur 2017-01-17 20:23:02 +01:00
parent b387657f5a
commit 03d2e0fe8a
4 changed files with 37 additions and 6 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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

View File

@ -15,6 +15,7 @@
<th>Condition</th>
<th>Value</th>
<th>Sensor</th>
<th>Enable</th>
<th>Action</th>
</tr>
{{range $key, $val := $notifs}}
@ -70,6 +71,13 @@
{{end}}
</select>
</td>
<td>
<input type="checkbox" name="enable"
{{if $val.Enable}}
checked
{{end}}
/>
</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>
@ -98,6 +106,9 @@
{{end}}
</select>
</td>
<td>
<input type="checkbox" name="enable" checked />
</td>
<td><button type="submit" name="action" value="add" class="btn btn-success btn-xs btn-block">Add</button></td>
</tr>
</form>