# Path: dobby/models/command.py
# class Command(Base):
#     """A Command is a text to be recognized by a Recognizer. It is attached to a Scenario and will trigger it"""
#     __tablename__ = 'commands'
#     id = Column(Integer, primary_key=True)
#     scenario_id = Column(Integer, ForeignKey('scenarios.id', ondelete='cascade'))
#     text = Column(Unicode(200))
# 
#     scenario = relationship('Scenario', back_populates='commands')
# 
#     def __repr__(self):
#         return "<Command('%s')>" % self.text


from dobby.models.command import Command
from models.actions import Action
from models.scenario import Scenario
from triggers import CommandEvent, RecognitionEvent
import Queue
import logging
import random
import threading

    def stop(self):
        """Stop the thread"""
        self._stop = True

    def run(self):
        while not self._stop:
            # Get the event from the queue
            try:
                event = self.event_queue.get(timeout=1)
            except Queue.Empty:
                continue
            
            # Fire a Scenario directly if the event is an CommandEvent
            if isinstance(event, CommandEvent):
                logger.debug(u'CommandEvent caught with command "%s"' % event.command)
                scenario = self.session.query(Scenario).join(Command).filter(Command.text == event.command).first()

            # Launch recognition and analyze the first voice command
            elif isinstance(event, RecognitionEvent):
                # Send a random confirmation message
                if self.confirmation_messages:
                    self.tts_queue.put(random.sample(self.confirmation_messages, 1)[0])
                # Monitor the recognized voice commands and catch the first one
                recognition_queue = Queue.Queue()
                self.recognizer.subscribe(recognition_queue)
                try:
                    recognition = recognition_queue.get(timeout=self.recognition_timeout)
                    recognized_command = unicode(recognition)
                    logger.debug(u'RecognitionEvent caught with voice command "%s"' % recognized_command)

# Path: dobby/models/command.py
# class Command(Base):
#     """A Command is a text to be recognized by a Recognizer. It is attached to a Scenario and will trigger it"""
#     __tablename__ = 'commands'
#     id = Column(Integer, primary_key=True)
#     scenario_id = Column(Integer, ForeignKey('scenarios.id', ondelete='cascade'))
#     text = Column(Unicode(200))
# 
#     scenario = relationship('Scenario', back_populates='commands')
# 
#     def __repr__(self):
#         return "<Command('%s')>" % self.text


# Path: dobby/controller.py
from dobby.models.command import Command
from models.actions import Action
from models.scenario import Scenario
from triggers import CommandEvent, RecognitionEvent
import Queue
import logging
import random
import threading


    def stop(self):
        """Stop the thread"""
        self._stop = True

    def run(self):
        while not self._stop:
            # Get the event from the queue
            try:
                event = self.event_queue.get(timeout=1)
            except Queue.Empty:
                continue
            
            # Fire a Scenario directly if the event is an CommandEvent
            if isinstance(event, CommandEvent):
                logger.debug(u'CommandEvent caught with command "%s"' % event.command)
                scenario = self.session.query(Scenario).join(Command).filter(Command.text == event.command).first()

            # Launch recognition and analyze the first voice command
            elif isinstance(event, RecognitionEvent):
                # Send a random confirmation message
                if self.confirmation_messages:
                    self.tts_queue.put(random.sample(self.confirmation_messages, 1)[0])
                # Monitor the recognized voice commands and catch the first one
                recognition_queue = Queue.Queue()
                self.recognizer.subscribe(recognition_queue)
                try:
                    recognition = recognition_queue.get(timeout=self.recognition_timeout)
                    recognized_command = unicode(recognition)
                    logger.debug(u'RecognitionEvent caught with voice command "%s"' % recognized_command)

                    scenario = self.session.query(Scenario).join(Command).filter(Command.text == recognized_command).first()