Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion nx584/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def index_partitions():
LOG.exception('Failed to index partitions')


@app.route('/system')
def index_system():
try:
result = json.dumps({
'panel_id': CONTROLLER.system.panel_id,
'status_flags': CONTROLLER.system.status_flags,
})
return flask.Response(result, mimetype='application/json')
except Exception as e:
LOG.exception('Failed to index system')


@app.route('/command')
def command():
args = flask.request.args
Expand Down Expand Up @@ -166,6 +178,6 @@ def get_events():
@app.route('/version')
def get_version():
return flask.Response(json.dumps(
{'version': '1.2',
{'version': '1.3',
'last_active': int(CONTROLLER.last_active)}),
mimetype='application/json')
7 changes: 7 additions & 0 deletions nx584/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ def get_version(self):
else:
return r.json()['version']

def get_system(self):
r = self._session.get(self._url + '/system',
timeout=self._timeout)
if r.status_code == 200:
return r.json()
return None

def get_info(self):
r = self._session.get(self._url + '/version',
timeout=self._timeout)
Expand Down
6 changes: 6 additions & 0 deletions nx584_client
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ def do_version(clnt, args):
print(clnt.get_version())


def do_system(clnt, args):
pprint.pprint(clnt.get_system(), width=60)


def do_alive(clnt, args):
info = clnt.get_info()
dead = time.time() - info['last_active'] > 300
Expand Down Expand Up @@ -179,6 +183,8 @@ def main():
do_events(clnt, args)
elif args.command == 'version':
do_version(clnt, args)
elif args.command == 'system':
do_system(clnt, args)
elif args.command == 'alive':
return do_alive(clnt, args)

Expand Down
46 changes: 46 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest
from unittest import mock

from nx584 import api
from nx584 import model


class TestSystemEndpoint(unittest.TestCase):

def setUp(self):
api.app.config['TESTING'] = True
self.client = api.app.test_client()

@mock.patch.object(api, 'CONTROLLER')
def test_system_endpoint_returns_json(self, mock_controller):
mock_controller.system = model.System()
mock_controller.system.panel_id = 42
mock_controller.system.status_flags = ['AC fail', 'Low battery']

response = self.client.get('/system')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content_type, 'application/json')
data = response.get_json()
self.assertEqual(data['panel_id'], 42)
self.assertEqual(data['status_flags'], ['AC fail', 'Low battery'])

@mock.patch.object(api, 'CONTROLLER')
def test_system_endpoint_empty_flags(self, mock_controller):
mock_controller.system = model.System()
mock_controller.system.panel_id = 0
mock_controller.system.status_flags = []

response = self.client.get('/system')
self.assertEqual(response.status_code, 200)
data = response.get_json()
self.assertEqual(data, {'panel_id': 0, 'status_flags': []})

def test_system_endpoint_controller_none(self):
api.CONTROLLER = None

with self.assertRaises(TypeError):
self.client.get('/system')


if __name__ == '__main__':
unittest.main()
7 changes: 4 additions & 3 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@ def _call_all(self, clnt):
clnt.get_user('1234', 1)
clnt.put_user('1234', {'number': 1})
clnt.get_version()
clnt.get_system()
clnt.get_info()

def _timeouts(self, session):
calls = session.get.call_args_list + session.put.call_args_list
self.assertEqual(10, len(calls))
self.assertEqual(11, len(calls))
return [c.kwargs.get('timeout', 'missing') for c in calls]

def test_no_timeout_by_default(self):
clnt, session = self._client()
self._call_all(clnt)
self.assertEqual([None] * 10, self._timeouts(session))
self.assertEqual([None] * 11, self._timeouts(session))

def test_timeout(self):
clnt, session = self._client(timeout=10)
self._call_all(clnt)
self.assertEqual([10] * 10, self._timeouts(session))
self.assertEqual([10] * 11, self._timeouts(session))

def test_get_events_no_timeout_by_default(self):
clnt, session = self._client()
Expand Down
Loading