diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index f6b80c3..d7c4650 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -751,15 +751,42 @@ class StatsViewSet(viewsets.ViewSet, List): @action(detail=False, methods=['post']) def alarm(self, request): + # Sample messages: + # {'data': 'Connected'} + # {'data': 'Trouble status on'} + # {'data': 'Exit delay in progress: Partition 1'} + # {'data': 'Disarmed: Partition 1'} + # {'data': 'Armed away: Partition 1'} + # {'data': 'Alarm: Partition 1'} + # {'data': 'Alarm: Partition 2'} + # {'data': 'Zone alarm restored: 1'} + # {'data': 'Zone alarm restored: 3'} + # {'data': 'Disarmed: Partition 1'} + # {'data': 'Disarmed: Partition 2'} + try: - alarm = dict(time=time.time(), data=int(request.data['data'])) - cache.set('alarm', alarm) - return Response(200) - except ValueError: - raise exceptions.ValidationError(dict(data='Invalid integer.')) + data = str(request.data['data']) except KeyError: raise exceptions.ValidationError(dict(data='This field is required.')) + logging.info('Alarm data: %s', data) + + alarm = None + if data.startswith('Armed'): + alarm = 'Armed' + elif data.startswith('Disarmed'): + alarm = 'Disarmed' + elif data.startswith('Exit delay in progress'): + alarm = 'Exit delay' + elif data.startswith('Alarm'): + alarm = 'TRIGGERED!' + + if alarm: + logging.info('Settings alarm status to: %s', alarm) + cache.set('alarm', alarm) + + return Response(200) + @action(detail=False, methods=['post']) def track(self, request): if 'name' not in request.data: diff --git a/webclient/src/Home.js b/webclient/src/Home.js index 547bd47..578dae3 100644 --- a/webclient/src/Home.js +++ b/webclient/src/Home.js @@ -254,11 +254,7 @@ export function Home(props) { const getTrackAgo = (x) => stats && stats.track && stats.track[x] ? moment.unix(stats.track[x]['time']).tz('America/Edmonton').fromNow() : ''; const getTrackName = (x) => stats && stats.track && stats.track[x] && stats.track[x]['first_name'] ? stats.track[x]['first_name'] : 'Unknown'; - //const alarmStat = () => stats && stats.alarm && moment().unix() - stats.alarm['time'] < 300 ? stats.alarm['data'] < 270 ? 'Armed' : 'Disarmed' : 'Unknown'; - const alarmStat = () => 'Unknown'; - - //const doorOpenStat = () => alarmStat() === 'Disarmed' && stats.alarm['data'] > 360 ? ', door open' : ''; - const doorOpenStat = () => ''; + const alarmStat = () => stats && stats.alarm ? stats.alarm.toString() : 'Unknown'; // toString prevents crash from cached alarm Object const closedStat = (x) => stats && stats.closing ? moment().unix() > stats.closing['time'] ? 'Closed' : 'Open until ' + stats.closing['time_str'] : 'Unknown'; @@ -401,7 +397,7 @@ export function Home(props) {
ORD3 printer: {printer3dStat('ord3')}
- {user &&Alarm status: {alarmStat()}{doorOpenStat()}
} + {user &&Alarm status: {alarmStat()}
} {user &&Hosting status: {closedStat()}
}