"""Sirius models.
-Revision ID: 5d8749c214d
+Revision ID: 145d9ba88d1e
Revises: None
-Create Date: 2015-01-21 12:06:37.092841
+Create Date: 2015-01-23 12:08:26.850006
"""
# revision identifiers, used by Alembic.
-revision = '5d8749c214d'
+revision = '145d9ba88d1e'
down_revision = None
from alembic import op
sa.Column('entry', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
- op.create_table('twitter_o_auth',
- sa.Column('id', sa.Integer(), nullable=False),
- sa.Column('created', sa.DateTime(), nullable=True),
- sa.Column('user_id', sa.Integer(), nullable=True),
- sa.Column('screen_name', sa.String(), nullable=True),
- sa.Column('token', sa.String(), nullable=True),
- sa.Column('token_secret', sa.String(), nullable=True),
- sa.ForeignKeyConstraint(['user_id'], [u'user.id'], ),
- sa.PrimaryKeyConstraint('id'),
- sa.UniqueConstraint('screen_name')
- )
op.create_table('printer',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['owner_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
+ op.create_table('twitter_o_auth',
+ sa.Column('id', sa.Integer(), nullable=False),
+ sa.Column('created', sa.DateTime(), nullable=True),
+ sa.Column('user_id', sa.Integer(), nullable=True),
+ sa.Column('screen_name', sa.String(), nullable=True),
+ sa.Column('token', sa.String(), nullable=True),
+ sa.Column('token_secret', sa.String(), nullable=True),
+ sa.ForeignKeyConstraint(['user_id'], [u'user.id'], ),
+ sa.PrimaryKeyConstraint('id'),
+ sa.UniqueConstraint('screen_name')
+ )
op.create_table('claim_code',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('created', sa.DateTime(), nullable=True),
sa.Column('pixels', sa.LargeBinary(), nullable=True),
+ sa.Column('print_id', sa.Integer(), nullable=True),
sa.Column('sender_id', sa.Integer(), nullable=True),
sa.Column('target_printer_id', sa.Integer(), nullable=True),
sa.Column('response_timestamp', sa.DateTime(), nullable=True),
sa.Column('failure_message', sa.String(), nullable=True),
sa.ForeignKeyConstraint(['sender_id'], ['user.id'], ),
sa.ForeignKeyConstraint(['target_printer_id'], ['printer.id'], ),
- sa.PrimaryKeyConstraint('id')
+ sa.PrimaryKeyConstraint('id'),
+ sa.UniqueConstraint('print_id')
)
op.create_index(op.f('ix_message_created'), 'message', ['created'], unique=False)
### end Alembic commands ###
op.drop_index(op.f('ix_message_created'), table_name='message')
op.drop_table('message')
op.drop_table('claim_code')
- op.drop_table('printer')
op.drop_table('twitter_o_auth')
+ op.drop_table('printer')
op.drop_table('device_log')
op.drop_table('user')
op.drop_table('bridge')
import base64
import datetime
+import logging
from sirius.models.db import db
from sirius.models import hardware
from sirius.models import user
+logger = logging.getLogger(__name__)
+
+
TIMEOUT_SECONDS = 60
class Message(db.Model):
created = db.Column(db.DateTime, default=datetime.datetime.utcnow, index=True)
pixels = db.Column(db.LargeBinary)
+ print_id = db.Column(db.Integer, unique=True)
+
sender_id = db.Column(db.Integer, db.ForeignKey('user.id'))
sender = db.relationship('User', backref=db.backref('messages', lazy='dynamic'))
response_timestamp=utcnow,
failure_message="Timed out",
))
+ db.session.commit()
def base64_pixels(self):
return base64.b64encode(self.pixels)
+
+
+ @classmethod
+ def ack(cls, return_code, command_id, utcnow=None):
+ if return_code != 0:
+ # TODO map codes to error messages
+ failure_message = 'Problem printing: {}'.format(return_code)
+ else:
+ failure_message = None
+
+ if utcnow is None:
+ utcnow = datetime.datetime.utcnow()
+
+ message = cls.query.filter_by(print_id=command_id).first()
+
+ if message is None:
+ logger.error("Ack'ed unknown message %s.", command_id)
+ return
+
+ message.response_timestamp = utcnow
+ message.failure_message = failure_message
+
+ db.session.add(message)
+ db.session.commit()
import messages
import json
import logging
+from sqlalchemy import desc
from sirius.coding import encoders
from sirius.coding import decoders
from sirius import stats
+from sirius.models.db import db
from sirius.models import hardware
+from sirius.models import messages as model_messages
logger = logging.getLogger(__name__)
self.connected_devices = set()
# NB 0 is an invalid command id (AKA file-id) so we start at
# 10. This took me an hour to debug, do not change!
- self.next_command_id = 10
+
+ last = model_messages.Message.query.order_by(desc('id')).first()
+ if last is None:
+ next_print_id = 0
+ else:
+ next_print_id = last.id + 1
+ db.session.commit()
+
+ print next_print_id
+
+ self.next_command_id = next_print_id
def send_message(device_address, message):
bridge_state.pending_commands[bridge_state.next_command_id] = message
bridge_by_address[bridge_state.address].next_command_id += 1
- return True
+ return bridge_state.next_command_id - 1
def accept(ws):
logger.error('Unexpected response (command_id not known) %r', x)
return
+ model_messages.Message.ack(x.return_code, x.command_id)
+
# TODO: some commands need special handling. E.g. what happens
# on invalid claim codes? How do we communicate send-errors or
# timeouts back to the user?
pixels=pixels,
)
+ # Note that we don't really handle the case of a disconnected
+ # printer yet. It f the printer isn't connected we'll silently
+ # swallow the message.
+ next_print_id = protocol_loop.send_message(printer.device_address, hardware_message)
+
+ if next_print_id is False:
+ pass # TODO - note immediately that we could not print in
+ # failure_message.
+
# Store the same message in the model.
model_message = model_messages.Message(
+ print_id=next_print_id,
pixels=bytearray(pixels),
sender_id=login.current_user.id,
target_printer=printer,
)
db.session.add(model_message)
- # Note that we don't really handle the case of a disconnected
- # printer yet. It f the printer isn't connected we'll silently
- # swallow the message.
- protocol_loop.send_message(printer.device_address, hardware_message)
-
return flask.redirect(flask.url_for(
'printer_overview.printer_overview',
user_id=login.current_user.id,