Commit dfab81b9 authored by AlaskaMapScience's avatar AlaskaMapScience

Add occupied status to alert conditions

parent 76f70a45
...@@ -97,7 +97,7 @@ class AlertAdminInline(admin.StackedInline): ...@@ -97,7 +97,7 @@ class AlertAdminInline(admin.StackedInline):
fieldsets = ( fieldsets = (
(None, {'fields': ( ('active', 'sensor'), (None, {'fields': ( ('active', 'sensor'),
('condition', 'test_value', 'read_count'), ('condition', 'test_value', 'read_count'),
('only_if_bldg', 'only_if_bldg_mode'), ('only_if_bldg', 'only_if_bldg_mode','only_if_bldg_status'),
('alert_message',), ('alert_message',),
('priority', 'wait_before_next'), ('priority', 'wait_before_next'),
('recipients',) ('recipients',)
......
# Generated by Django 2.2.4 on 2021-04-02 22:52
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('bmsapp', '0032_auto_20190926_1206'),
]
operations = [
migrations.AddField(
model_name='alertcondition',
name='only_if_bldg_status',
field=models.CharField(blank=True, choices=[('Occupied', 'Occupied'), ('Unoccupied', 'Unoccupied')], max_length=15, null=True, verbose_name='and this status'),
),
]
...@@ -8,6 +8,7 @@ from django.core.mail import send_mail ...@@ -8,6 +8,7 @@ from django.core.mail import send_mail
import requests import requests
import bmsapp.data_util import bmsapp.data_util
import bmsapp.formatters import bmsapp.formatters
import bmsapp.schedule
from . import sms_gateways from . import sms_gateways
import yaml import yaml
...@@ -409,6 +410,21 @@ class Building(models.Model): ...@@ -409,6 +410,21 @@ class Building(models.Model):
return props return props
def current_status(self, ts = time.time()):
"""Returns the occupied/unoccupied status of a building at a given time
"""
if self.schedule:
if self.timezone:
scheduleObj = bmsapp.schedule.Schedule(self.schedule, self.timezone)
else:
scheduleObj = bmsapp.schedule.Schedule(self.schedule, 'US/Alaska')
if scheduleObj.is_occupied(ts):
return 'Occupied'
else:
return 'Unoccupied'
else:
return 'Occupied'
class Meta: class Meta:
ordering = ['title'] ordering = ['title']
...@@ -716,6 +732,7 @@ class AlertCondition(models.Model): ...@@ -716,6 +732,7 @@ class AlertCondition(models.Model):
# fields to qualify the condition test according to building mode # fields to qualify the condition test according to building mode
only_if_bldg = models.ForeignKey(Building, models.SET_NULL, verbose_name='But only if building', blank=True, null=True) only_if_bldg = models.ForeignKey(Building, models.SET_NULL, verbose_name='But only if building', blank=True, null=True)
only_if_bldg_mode = models.ForeignKey(BuildingMode, models.SET_NULL, verbose_name='is in this mode', blank=True, null=True) only_if_bldg_mode = models.ForeignKey(BuildingMode, models.SET_NULL, verbose_name='is in this mode', blank=True, null=True)
only_if_bldg_status = models.CharField(verbose_name='and this status', max_length=15, blank=True, null=True, choices=(('Occupied','Occupied'),('Unoccupied','Unoccupied')))
# alert message. If left blank a message will be created from other field values. # alert message. If left blank a message will be created from other field values.
alert_message = models.TextField(max_length=400, blank=True, alert_message = models.TextField(max_length=400, blank=True,
...@@ -854,10 +871,13 @@ class AlertCondition(models.Model): ...@@ -854,10 +871,13 @@ class AlertCondition(models.Model):
# Loop through the requested number of last readings, testing whether # Loop through the requested number of last readings, testing whether
# the alert conditions are satisfied for all the readings. # the alert conditions are satisfied for all the readings.
# First see if there was a building mode test requested and test it. # First see if there was a building mode test requested and test it.
if self.only_if_bldg is not None and self.only_if_bldg_mode is not None: if self.only_if_bldg is not None:
if self.only_if_bldg.current_mode != self.only_if_bldg_mode: if self.only_if_bldg_mode is not None and (self.only_if_bldg.current_mode != self.only_if_bldg_mode):
# Failed building mode test # Failed building mode test
return None return None
if self.only_if_bldg_status is not None and (self.only_if_bldg.current_status(last_reads[0]['ts']) != self.only_if_bldg_status):
# Failed building status test
return None
# Alert condition must be true for each of the requested readings # Alert condition must be true for each of the requested readings
for read in last_reads: for read in last_reads:
......
...@@ -10,7 +10,7 @@ class Schedule: ...@@ -10,7 +10,7 @@ class Schedule:
""" This class represents an occupied/unoccupied schedule for a facility. """ This class represents an occupied/unoccupied schedule for a facility.
""" """
def __init__(self, schedule_text, tz_name): def __init__(self, schedule_text, tz_name = 'US/Alaska'):
""" Constructs a Schedule object. """ Constructs a Schedule object.
'schedule_text' is a string that describes the occupied periods 'schedule_text' is a string that describes the occupied periods
......
...@@ -66,7 +66,7 @@ ...@@ -66,7 +66,7 @@
<tr id="{{ alert.id }}"> <tr id="{{ alert.id }}">
<td></td> <td></td>
<td class="bmon-sensor-id">{{ alert.id }}</td> <td class="bmon-sensor-id">{{ alert.id }}</td>
<td>{{ alert.ts }}</td> <td>{{ alert.when }}</td>
<td>{{ alert.message }}</td> <td>{{ alert.message }}</td>
</tr> </tr>
{% endfor %} {% endfor %}
......
...@@ -789,8 +789,9 @@ def alert_log(request): ...@@ -789,8 +789,9 @@ def alert_log(request):
db = bmsdata.BMSdata() db = bmsdata.BMSdata()
db.cursor.execute('SELECT * FROM [_alert_log]') db.cursor.execute('SELECT * FROM [_alert_log]')
alert_list = [dict(r) for r in db.cursor.fetchall()] alert_list = [{**x,'when':time.strftime('%Y-%m-%d %M:%S',time.localtime(x['ts']))} for x in [dict(r) for r in db.cursor.fetchall()]]
# time.strftime('%Y-%m-%dT%M:%S',time.localtime(alert_list[0]['ts']))
ctx = base_context() ctx = base_context()
ctx.update({'alert_list': alert_list}) ctx.update({'alert_list': alert_list})
return render_to_response('bmsapp/alert-log.html', ctx) return render_to_response('bmsapp/alert-log.html', ctx)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment