import base64
import datetime
import logging
+from sqlalchemy import desc
from sirius.models.db import db
from sirius.models import hardware
db.session.add(message)
db.session.commit()
+
+ @classmethod
+ def get_next_command_id(cls):
+ """Return the next command id to be used by the bridge. Command ids
+ are used to ack messages so we need to make sure we don't have
+ any collisions. The best way to do so is to pick the
+ next-highest number after the ones we already used up.
+ """
+ last = cls.query.order_by(desc('print_id')).first()
+ if last is None:
+ next_print_id = 1
+ else:
+ next_print_id = last.print_id + 1
+
+ db.session.commit()
+ return next_print_id
import messages
import json
import logging
-from sqlalchemy import desc
from sirius.coding import encoders
from sirius.coding import decoders
self.websocket = websocket
self.pending_commands = dict()
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!
-
- 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
+ # NB 0 is an invalid command id (AKA file-id) so we start at
+ # whatever the latest message id is to avoid collisions.
+ self.next_command_id = model_messages.Message.get_next_command_id()
def send_message(device_address, message):