From 5ace8d60ada8e7aa08c6f40329dcf19a2fb0fcd7 Mon Sep 17 00:00:00 2001 From: Tom Hunger Date: Wed, 14 Jan 2015 15:42:37 +0000 Subject: [PATCH] Add management command to fake a newly reset little printer calling home. --- README.md | 20 +++++++++++++++++++- manage.py | 2 ++ sirius/fake/__init__.py | 22 ---------------------- sirius/fake/commands.py | 37 +++++++++++++++++++++++++++++++++++++ sirius/web/webapp.py | 2 +- 5 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 sirius/fake/commands.py diff --git a/README.md b/README.md index eaf8e06..6ffac79 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ First point your bridge at your laptop, port 5002 (or similar). Then run: -```sh +```console $ bin/gunicorn -k flask_sockets.worker sirius.server:app -b 0.0.0.0:5002 -w 1 ``` @@ -59,3 +59,21 @@ We are also deriving this hardware xor when a device calls home with an "encryption_key_required". In that case we connect the device to the claim code via the hardware-xor and send back the correct encryption key. + +## Creating fake printers + +Resetting the actual hardware all the time gets a bit tiresome so +there's a fake command that creates unclaimed fake little printers: + +```console +$ ./manage.py fake printer +[...] +Created printer + address: 602d48d344b746f5 + DB id: 8 + secret: 66a596840f + claim code: 5oop-e9dp-hh7v-fjqo +``` + +Functionally there is no difference between resetting and creating a +new printer so we don't distinguish between the two. diff --git a/manage.py b/manage.py index 828edaa..3b9f8b9 100755 --- a/manage.py +++ b/manage.py @@ -9,6 +9,7 @@ if os.path.exists('.env'): os.environ[name] = value from sirius.web import webapp +from sirius.fake import commands as fake_commands from flask.ext.script import Manager, Shell from flask.ext.migrate import Migrate, MigrateCommand @@ -20,6 +21,7 @@ def make_shell_context(): return dict(app=app, db=webapp.db) manager.add_command("shell", Shell(make_context=make_shell_context)) manager.add_command('db', MigrateCommand) +manager.add_command('fake', fake_commands.fake_manager) @manager.command diff --git a/sirius/fake/__init__.py b/sirius/fake/__init__.py index e922e90..e69de29 100644 --- a/sirius/fake/__init__.py +++ b/sirius/fake/__init__.py @@ -1,22 +0,0 @@ -"""Fake a few printer interactions for testing. -""" -import os - -from flask.ext import script -from sirius.models import hardware -from sirius.coding import bitshuffle - -def sub_opts(app, **kwargs): - pass - -fake_manager = script.Manager(sub_opts, usage="Fake printer interactions.") - -@fake_manager.command -def create_printer(): - device_address = os.urandom(8).encode('hex') - xor = bitshuffle.hardware_xor_from_device_address(device_address) - - claim_code = '' - - hardware.Printer( - ) diff --git a/sirius/fake/commands.py b/sirius/fake/commands.py new file mode 100644 index 0000000..cfdec7c --- /dev/null +++ b/sirius/fake/commands.py @@ -0,0 +1,37 @@ +"""Fake a few printer interactions for testing. +""" +from __future__ import print_function + +import os + +from flask.ext import script +from sirius.models import hardware +from sirius.models.db import db +from sirius.coding import bitshuffle +from sirius.coding import claiming + +def sub_opts(app, **kwargs): + pass + +fake_manager = script.Manager(sub_opts, usage="Fake printer interactions.") + +@fake_manager.command +def printer(): + device_address = os.urandom(8).encode('hex') + xor = bitshuffle.hardware_xor_from_device_address(device_address) + secret = os.urandom(5).encode('hex') + + cc = claiming.encode(xor, int(secret, 16)) + printer = hardware.Printer( + device_address=device_address, + hardware_xor=xor, + ) + db.session.add(printer) + db.session.commit() + + print('Created printer') + print(' address: {}'.format(device_address)) + print(' DB id: {}'.format(printer.id)) + print(' secret: {}'.format(secret)) + print(' xor: {}'.format(xor)) + print(' claim code: {}'.format(cc)) diff --git a/sirius/web/webapp.py b/sirius/web/webapp.py index 44daa68..5298a0d 100644 --- a/sirius/web/webapp.py +++ b/sirius/web/webapp.py @@ -55,5 +55,5 @@ def create_app(config_name): def api_v1_connection(ws): protocol_loop.accept(ws) - logger.info('Creating app.') + logger.debug('Creating app.') return app -- 2.47.3