Esempio n. 1
1
    def connect(self, server):
        self.server = server
        
        self.meteor = MeteorClient('ws://{}/websocket'.format(self.server))
        self.meteor.on('connected', self.connected)
        self.meteor.connect()

        self.state = 'connecting'
        self.trigger_event('connecting')
Esempio n. 2
0
    def __init__(self,
                 url,
                 magic_phrase,
                 max_turns=10,
                 callback=None,
                 callback_params=1,
                 msg_q=False):
        print('starting service')
        self.start_proba = 1.0
        self.magic_phrase = magic_phrase
        self.url = replace_localhost(url)
        self.bot = Alice()
        self.max_turns = max_turns
        self.sending_message = False
        self._id = None
        self.use_msg_q = msg_q  # msg_q sets whether or not we are queueing messages
        self.websocket = 'ws://%s/websocket' % self.url
        self.client = MeteorClient(self.websocket)
        self.client.ddp_client.ddpsocket.extra_headers = [('Bot', 'true')]
        print(self.client.ddp_client.ddpsocket.handshake_headers)
        self.client.connect()

        self.idle_time = 3 * 60
        self.thread_time = 2
        self.max_retry = 3
Esempio n. 3
0
 def __init__(self,
              username,
              password,
              api_key,
              debug=False,
              no_meteor=False):
     self.username = username
     self.encoded_password = password.encode() if password else None
     if not no_meteor:
         self.meteor_client = MeteorClient(SOCKET_BASE, debug=debug)
     else:
         self.meteor_client = None
     self.http = DicecloudHTTP(API_BASE, api_key, debug=debug)
     self.logged_in = False
     self.debug = debug
 def __init__(self, config):
     self.config = config
     self.universes = {}
     self.fades = {}
     
     self.last = 0
     self.ready = False
     
     self.meteor = MeteorClient(self.config['server'])
     self.meteor.on('connected', self.connect_cb)
     self.meteor.connect()
Esempio n. 5
0
    def initMeteorConnect(self):
        """
        this will use library python-meteor in order to escablish session to selected meteor

        :return:
        """
        self.logm("MWorkerLib:initMeteorConnect:", "meteor python lib client iniciaisation...")
        self.meteorClient = MeteorClient('ws://127.0.0.1:3000/websocket', auto_reconnect=True)
        self.meteorClient.connect()
        self.subscribeCollectionSynchron(['unassigned', 'active', 'trash'], self.SYNCHRO_TIMEOUT)

        self.logm("MWorkerLib:initMeteorConnect:", "meteor python lib client iniciaisation done...")
Esempio n. 6
0
 def __init__(self, server_url='ws://localhost:3000/websocket', worker=None):
     self.server_url = server_url
     self.client = MeteorClient(server_url)
     self.client.on('subscribed', self.subscribed)
     self.client.on('unsubscribed', self.unsubscribed)
     self.client.on('added', self.added)
     self.client.on('changed', self.changed)
     self.client.on('removed', self.removed)
     self.client.on('connected', self.connected)
     self.client.on('logged_in', self.logged_in)
     self.client.on('logged_out', self.logged_out)
     self.worker = worker
     self.connected = False
     self.ready = False
Esempio n. 7
0
def report_sequence_status(fixture_id, cavity, status, progress):
    lclient = MeteorClient('ws://127.0.0.1:3000/websocket', auto_reconnect=True)
    try:
        lclient.connect()
    except Exception as e:
        print (e)
    try:
        lclient.call('setCavityStatus', [fixture_id, cavity['id'], status, progress], callback_function)
    except Exception as e:
        print (e)
    try:
        lclient.close()
    except Exception as e:
        print (e)
Esempio n. 8
0
def report_lock_status(fixture_id, lock, status):
    lclient = MeteorClient('ws://127.0.0.1:3000/websocket', auto_reconnect=True)
    try:
        lclient.connect()
    except Exception as e:
        print (e)
    try:
        lclient.call('setLockStatus', [fixture_id, lock, status], callback_function)
    except Exception as e:
        print (e)
    try:
        lclient.close()
    except Exception as e:
        print (e)
Esempio n. 9
0
    def __init__(self, user, password, server='demo.rocket.chat'):
        self.username = user
        self.password = password
        self.server = server
        self.debug = True

        self._prefixs = []

        self.client = client = MeteorClient('wss://demo.rocket.chat/websocket')

        # registering internal handlers
        self.client.on('connected', self._connected)
        self.client.on('closed', self._closed)
        self.client.on('logging_in', self._logging_in)
        self.client.on('failed', self._failed)
        self.client.on('added', self._added)
        self.client.on('changed', self._changed)
        self.client.on('unsubscribed', self._unsubscribed)
        self.client.on('subscribed', self._subscribed)
Esempio n. 10
0
    def __init__(self, user, password, server):
        self.username = user
        self.password = password
        self.server = server
        self.debug = True

        self._prefixs = []

        self.client = client = MeteorClient(server)

        # registering internal handlers (callbacks)
        self.client.on('logging_in', self._logging_in)
        self.client.on('connected', self._connected)
        self.client.on('closed', self._closed)
        self.client.on('failed', self._failed)
        self.client.on('added', self._added)
        self.client.on('changed', self._changed)
        self.client.on('unsubscribed', self._unsubscribed)
        self.client.on('subscribed', self._subscribed)
        self.messages = []
Esempio n. 11
0
    def __init__(self, username, password, ui):
        self.username = username
        self.password = password
        self.ui = ui
        self.now = mktime(datetime.now().timetuple())*1e3
        self.resume_token = ''
        self.client = MeteorClient('wss://kwak.io/websocket')
        self.client.connect()
        self.client.login(self.username, self.password,
            token=self.resume_token, callback=self.logged_in)

        self.hot_channels = []
        self.hot_channels_name = []
        self.all_channels_name = []
        self.current_channel = 'dev'
        self.client.call('getHotChannels', [], self.set_hot_channels_name)
        self.client.call('channelList', [], self.set_all_channels_name)

        self.client.on('connected', self.connected)
        self.client.on('added', self.added)
Esempio n. 12
0
def get_new_login(server_url, callback=None):
    """ queries the server to give a new login"""
    server_url = replace_localhost(server_url)
    # connects to the server
    websocket = 'ws://%s/websocket' % server_url
    client = MeteorClient(websocket)
    client.connect()

    def cb(error, data):
        client.close()
        if callback:
            callback(error, data)

    # queries the method with a magicphrase
    client.call('getBotUsername', magic_phrase, cb)
ledcontrol6 = 15

ledname1 = 'First' 
ledname2 = 'Second'
ledname3 = 'Third'
ledname4 = 'Forth'
ledname5 = 'Fifth'
ledname6 = 'Sixth'

from MeteorClient import MeteorClient





client = MeteorClient('ws://127.0.0.1:3000/websocket')
#client = MeteorClient('ws://user-status.meteor.com/websocket')

def subscribed(subscription):
    print('* SUBSCRIBED {}'.format(subscription))
    # conn = client.find_one('items', selector={'name': getHwAddr('eth0') })
    if subscription == 'local-items':
        def getHwAddr(ifname):
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
            return ':'.join(['%02x' % ord(char) for char in info[18:24]])
        print getHwAddr('eth0') 
        # new part
        newmac = getHwAddr('eth0')
        conn = client.find_one('items', selector={'name': newmac })
        print(format(conn))
Esempio n. 14
0
class DicecloudClient:
    instance = None
    user_id = None

    def __init__(self,
                 username,
                 password,
                 api_key,
                 debug=False,
                 no_meteor=False):
        self.username = username
        self.encoded_password = password.encode() if password else None
        if not no_meteor:
            self.meteor_client = MeteorClient(SOCKET_BASE, debug=debug)
        else:
            self.meteor_client = None
        self.http = DicecloudHTTP(API_BASE, api_key, debug=debug)
        self.logged_in = False
        self.debug = debug

    def initialize(self):
        if self.meteor_client is None:
            log.info(f"Initialized without Meteor.")
            return
        log.info(f"Initializing Dicecloud Meteor client (debug={TESTING})")
        self.meteor_client.connect()
        loops = 0
        while (not self.meteor_client.connected) and loops < 100:
            time.sleep(0.1)
            loops += 1
        log.info(f"Connected to Dicecloud in {loops/10} seconds")

        def on_login(error, data):
            if data:
                type(self).user_id = data.get('id')
                self.logged_in = True
            else:
                log.warning(error)
                raise LoginFailure()

        self.meteor_client.login(self.username,
                                 self.encoded_password,
                                 callback=on_login)
        loops = 0
        while not self.logged_in and loops < 100:
            time.sleep(0.1)
            loops += 1
        log.info(f"Logged in as {self.user_id}")

    def ensure_connected(self):
        if self.logged_in:  # everything is fine:tm:
            return
        self.initialize()

    def _get_list_id(self, character_id, list_name=None):
        """
        :param character: (str) the character to get the spell list ID of.
        :param list_name: (str) The name of the spell list to look for. Returns default if not passed.
        :return: (str) The default list id.
        """
        char = self.get_character(character_id)
        if list_name:
            spell_list = next((l for l in char.get('spellLists', [])
                               if l['name'].lower() == list_name.lower()),
                              None)
        else:
            spell_list = next((l for l in char.get('spellLists', [])), None)
        if not spell_list:
            raise InsertFailure("No spell list found on sheet.")
        return spell_list['_id']

    def get_character(self, char_id):
        return self.http.get(f'/character/{char_id}/json')

    def add_spell(self, character, spell):
        """Adds a spell to the dicecloud list."""
        return self.add_spells(character, [spell])

    def add_spells(self, character_id, spells, spell_list=None):
        """
        :param character_id: (str) The character to add spells for.
        :param spells: (list) The list of spells to add
        :param spell_list: (str) The spell list name to search for in Dicecloud.
        """
        list_id = self._get_list_id(character_id, spell_list)
        if not list_id:  # still
            raise InsertFailure("No matching spell lists on origin sheet.")
        return self.http.post(
            f'/api/character/{character_id}/spellList/{list_id}',
            [s.to_dict() for s in spells])

    def create_character(self,
                         name: str = "New Character",
                         gender: str = None,
                         race: str = None,
                         backstory: str = None):
        data = {'name': name}
        if gender is not None:
            data['gender'] = gender
        if race is not None:
            data['race'] = race
        if backstory is not None:
            data['backstory'] = backstory

        data['settings'] = {'viewPermission': 'public'}  # sharing is caring!
        response = self.http.post('/api/character', data)
        return response['id']

    def delete_character(self, char_id: str):
        self.http.delete(f'/api/character/{char_id}')

    def get_user_id(self, username: str):
        username = urllib.parse.quote_plus(username)
        user_id = self.http.get(f'/api/user?username={username}')
        return user_id['id']

    def transfer_ownership(self, char_id: str, user_id: str):
        self.http.put(f'/api/character/{char_id}/owner', {'id': user_id})

    def insert_feature(self, char_id, feature):
        return (self.insert_features(char_id, [feature]))[0]

    def insert_features(self, char_id: str, features: list):
        response = self.http.post(f'/api/character/{char_id}/feature',
                                  [f.to_dict() for f in features])
        return response

    def insert_proficiency(self, char_id, prof):
        return (self.insert_proficiencies(char_id, [prof]))[0]

    def insert_proficiencies(self, char_id: str, profs: list):
        response = self.http.post(f'/api/character/{char_id}/prof',
                                  [p.to_dict() for p in profs])
        return response

    def insert_effect(self, char_id, effect):
        return (self.insert_effects(char_id, [effect]))[0]

    def insert_effects(self, char_id: str, effects: list):
        response = self.http.post(f'/api/character/{char_id}/effect',
                                  [e.to_dict() for e in effects])
        return response

    def insert_class(self, char_id, klass):
        return (self.insert_classes(char_id, [klass]))[0]

    def insert_classes(self, char_id: str, classes: list):
        response = self.http.post(f'/api/character/{char_id}/class',
                                  [c.to_dict() for c in classes])
        return response
Esempio n. 15
0
#!/usr/bin/env python
""" Check to see if an process is running. If not, restart. Run this in a cron job """
import os
import sys
import time
import fcntl, socket, struct
from MeteorClient import MeteorClient
# client = MeteorClient('ws://127.0.0.1:3030/websocket')
client = MeteorClient('ws://127.0.0.1:3000/websocket')
process_name = "/root/smartag-python.py"  # change this to the name of your process
#process_name= "meteor_run.sh"
# process_name= "/home/atanas/MeteorProjects/iot/serverstate.py"


def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
    return ':'.join(['%02x' % ord(char) for char in info[18:24]])


print getHwAddr('eth0')
# new part
newmac = getHwAddr('eth0')
client.connect()

tmp = os.popen("ps -Af").read()
print tmp[:]

if process_name in tmp[:]:
    print "LOCAL SERVER RUNNING"
    print process_name
Esempio n. 16
0
        mc.subscribe("buttons", callback=mcSubscribeCallback)
        mc.subscribe("screensets", callback=mcSubscribeCallback)
        mc.subscribe("organisations", callback=mcSubscribeCallback)
        print("{}: Subscribied".format(nicetime(time.time())))
        event.set()
        return True
    except Exception as ex:
        print(
            "mcLoggedIn. Already subscribed, exception type {}, exception: {}".
            format(type(ex), ex.args))
        return False


if __name__ == '__main__':
    meteor_websocket = "ws://staging.spur.site/websocket"
    mc = MeteorClient(meteor_websocket)
    mc.on('connected', mcConnected)
    mc.on('logging_in', mcLoggingIn)
    mc.on('logged_in', mcLoggedIn)
    mc.on('logged_out', mcLoggedOut)
    mc.on('failed', mcFailed)
    mc.on('closed', mcClosed)
    mc.on('subscribed', mcSubscribed)
    mc.on('added', mcAdded)
    mc.on('changed', mcChanged)
    mc.on('removed', mcRemoved)
    print("{}: Connecting".format(nicetime(time.time())))
    event = threading.Event()
    mc.connect()
    subscribe()
    event.wait()
class AoikRocketChatErrbot(ErrBot):
    """
    Errbot backend for Rocket.Chat.

    The backend logs in as a Rocket.Chat user, receiving and sending messages.
    """
    def __init__(self, config):
        """
        Constructor.

        :param config: Errbot's config module.

        :return: None.
        """
        # Call super method
        super(AoikRocketChatErrbot, self).__init__(config)

        # Get the backend's config object
        self._config_obj = getattr(self.bot_config, _CONFIG_OBJ_KEY, None)

        # Get logging level from env variable or config object
        log_level = orig_log_level = self._get_config(
            CONFIG_KEYS.BOT_LOG_LEVEL, None)

        # If not specified
        if log_level is None:
            # Get logging level from config module
            log_level = orig_log_level = getattr(self.bot_config,
                                                 CONFIG_KEYS.BOT_LOG_LEVEL,
                                                 None)

            # If not specified
            if log_level is None:
                # Use default
                log_level = logging.DEBUG

        # If the logging level is string, e.g. 'DEBUG'.
        # This means it may be an attribute name of the `logging` module.
        if isinstance(log_level, str):
            # Get attribute value from the `logging` module
            log_level = getattr(logging, log_level, None)

        # Error message
        error_msg = None

        # If the logging level is not int
        if not isinstance(log_level, int):
            # Get message
            error_msg = 'Config `BOT_LOG_LEVEL` value is invalid: {}'.format(
                repr(orig_log_level))

            # Log message
            self._log_error(error_msg)

            # Raise error
            raise ValueError(error_msg)

        # Get logger
        self._logger = logging.getLogger('aoikrocketchaterrbot')

        # Set logging level
        self._logger.setLevel(log_level)

        # Get message
        msg = '# ----- Created logger -----\nBOT_LOG_LEVEL: {}'.format(
            log_level)

        # Log message
        self._logger.debug(msg)

        # Get rocket chat server URI
        self._server_uri = self._get_config(CONFIG_KEYS.SERVER_URI)

        # If server URI is not specified
        if self._server_uri is None:
            # Get message
            error_msg = 'Missing config `SERVER_URI`.'

            # Log message
            self._log_error(error_msg)

            # Raise error
            raise ValueError(error_msg)

        # Get login username
        self._login_username = self._get_config(CONFIG_KEYS.LOGIN_USERNAME)

        # If login username is not specified
        if self._login_username is None:
            # Get message
            error_msg = 'Missing config `LOGIN_USERNAME`.'

            # Log message
            self._log_error(error_msg)

            # Raise error
            raise ValueError(error_msg)

        # Get login password
        self._login_password = self._get_config(CONFIG_KEYS.LOGIN_PASSWORD)

        # If login password is not specified
        if self._login_password is None:
            # Get message
            error_msg = 'Missing config `LOGIN_PASSWORD`.'

            # Log message
            self._log_error(error_msg)

            # Raise error
            raise ValueError(error_msg)

        # If login password is not bytes
        if not isinstance(self._login_password, bytes):
            # Convert login password to bytes
            self._login_password = bytes(self._login_password, 'utf-8')

        # Create login user's identifier object.
        #
        # This attribute is required by superclass.
        #
        self.bot_identifier = self.build_identifier(self._login_username)

        # Event set when the the meteor client has done topic subscribing.
        #
        # When the event is set, the meteor client is in one of the two states:
        # - The topic subscribing has succeeded and the meteor client has
        #   started handling messages.
        # - The topic subscribing has failed and the meteor client has been
        #   closed.
        #
        # The rationale is that the loop at 65ZNO uses the meteor client's
        # `connected` attribute to decide whether continue, and the attribute
        # value is ready for use only after this event is set.
        self._subscribing_done_event = Event()

        # Event set when the meteor client calls the `closed` callback at
        # 3DMYH.
        #
        # The rationale is that the main thread code at 5W6XQ has to wait until
        # the meteor client is closed and the `closed` callback hooked at 7MOJX
        # is called. This ensures the cleanup is fully done.
        #
        self._meteor_closed_event = Event()

    @property
    def mode(self):
        """
        Get mode name.

        :return: Mode name.
        """
        # Return mode name
        return 'aoikrocketchaterrbot'

    def _log_debug(self, msg):
        """
        Log debug message.

        :param msg: Message to log.

        :return: None.
        """
        # Log the message
        self._logger.debug(msg)

    def __hash__(self):
        """Bots are now stored as a key in the bot so they need to be hashable."""
        return id(self)

    def _log_error(self, msg):
        """
        Log error message.

        :param msg: Message to log.

        :return: None.
        """
        # Log the message
        self._logger.error(msg)

    def _get_config(self, key, default=None):
        """
        Get config value from env variable or config object.

        Env variable takes precedence.

        :param key: Config key.

        :param default: Default value.

        :return: Config value.
        """
        # Get env variable name
        env_var_name = _ENV_VAR_NAME_PREFIX + key

        # Get config value from env variable
        config_value = os.environ.get(env_var_name, None)

        # If not specified
        if config_value is None:
            # If not have config object
            if self._config_obj is None:
                # Use default
                config_value = default

            # If have config object
            else:
                # Get config value from config object
                config_value = getattr(self._config_obj, key, default)

        # Return config value
        return config_value

    def _get_bool_config(self, key, default=None):
        """
        Get boolean config value from env variable or config object.

        Env variable takes precedence.

        :param key: Config key.

        :param default: Default value.

        :return: Config value.
        """
        # Get config value
        config_value = self._get_config(key=key, default=default)

        # If config value is false.
        # This aims to handle False, 0, and None.
        if not config_value:
            # Return False
            return False

        # If config value is not false
        else:
            # Get config value's string in lower case
            config_value_str_lower = str(config_value).lower()

            # Consider '0', case-insensitive 'false' and 'no' as false,
            # otherwise as true.
            return config_value_str_lower not in ['0', 'false', 'no']

    def _patch_meteor_client(self):
        """
        Patch meteor client to fix an existing bug.

        :return: None.
        """
        # Get whether need patch meteor client. Default is True.
        need_patch = self._get_bool_config(CONFIG_KEYS.PATCH_METEOR_CLIENT,
                                           True)

        # If need patch meteor client
        if need_patch:
            # Create `change_data` function
            def change_data(self, collection, id, fields, cleared):
                """
                Callback called when data change occurred.

                :param self: CollectionData object.

                :param collection: Data collection key.

                :param id: Data item key.

                :param fields: Data fields changed.

                :param cleared: Data fields to be cleared.

                :return None.
                """
                # If the data collection key not exists
                #
                # The original `change_data` function assumes it is existing,
                # but it is not in some cases.
                #
                if collection not in self.data:
                    # Add data collection
                    self.data[collection] = {}

                # If the data item key not exists.
                #
                # The original `change_data` function assumes it is existing,
                # but it is not in some cases.
                #
                if id not in self.data[collection]:
                    # Add data item
                    self.data[collection][id] = {}

                # For each data field changed
                for key, value in fields.items():
                    # Add to the data item
                    self.data[collection][id][key] = value

                # For each data field to be cleared
                for key in cleared:
                    # Delete from the data item
                    del self.data[collection][id][key]

            # Store original `change_data`.
            #
            # pylint: disable=protected-access
            CollectionData._orig_change_data = CollectionData.change_data
            # pylint: enable=protected-access

            # Replace original `change_data`
            CollectionData.change_data = change_data

    def build_identifier(self, username):
        """
        Create identifier object for given username.

        :param username: Rocket chat user name.

        :return: RocketChatUser instance.
        """
        # Create identifier object
        return RocketChatUser(username)

    def serve_forever(self):
        """
        Run the bot.

        Called by the Errbot framework.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- serve_forever -----')

        # Patch meteor client
        self._patch_meteor_client()

        # Get whether reconnect is enabled
        reconnect_enabled = self._get_bool_config(
            CONFIG_KEYS.RECONNECT_ENABLED,
            default=True,
        )

        try:
            # Loop
            while True:
                try:
                    # Run for once
                    self.serve_once()

                # If have error
                except Exception:
                    # Log message
                    self._log_error(('# ----- `serve_once` failed with error'
                                     ' -----\n{}').format(format_exc()))

                # If reconnect is enabled
                if reconnect_enabled:
                    # Get message
                    msg = ('# ----- Sleep before reconnect -----\n'
                           'Interval: {:.2f}').format(self._reconnection_delay)

                    # Log message
                    self._log_debug(msg)

                    # Sleep before reconnect
                    self._delay_reconnect()

                    # Log message
                    self._log_debug('# ----- Wake up to reconnect -----')

                    # Continue the loop
                    continue

                # If reconnect is not enabled
                else:
                    # Break the loop
                    break

        # If have `KeyboardInterrupt`
        except KeyboardInterrupt:
            # Do not treat as error
            pass

        # Always do
        finally:
            # Call `shutdown`
            self.shutdown()

        # Log message
        self._log_debug('# ===== serve_forever =====')

    def serve_once(self):
        """
        Run the bot until the connection is disconnected.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- serve_once -----')

        # Log message
        self._log_debug(('# ----- Create meteor client -----\n'
                         'SERVER_URI: {}').format(self._server_uri))

        # Create meteor client
        self._meteor_client = MeteorClient(
            self._server_uri,
            # Disable the meteor client's auto reconnect.
            # Let `serve_forever` handle reconnect.
            auto_reconnect=False,
        )

        # Log message
        self._log_debug('# ----- Hook meteor client callbacks -----')

        # 5DI82
        # Hook meteor client `connected` callback
        self._meteor_client.on('connected', self._meteor_connected_callback)

        # 2RAYF
        # Hook meteor client `changed` callback
        self._meteor_client.on('changed', self._meteor_changed_callback)

        # 4XIZB
        # Hook meteor client `added` callback
        self._meteor_client.on('added', self._meteor_added_callback)

        # 2JEIK
        # Hook meteor client `removed` callback
        self._meteor_client.on('removed', self._meteor_removed_callback)

        # 32TF2
        # Hook meteor client `failed` callback
        self._meteor_client.on('failed', self._meteor_failed_callback)

        # 5W6RX
        # Hook meteor client `reconnected` callback
        self._meteor_client.on('reconnected',
                               self._meteor_reconnected_callback)

        # 7MOJX
        # Hook meteor client `closed` callback
        self._meteor_client.on('closed', self._meteor_closed_callback)

        # Clear the event
        self._subscribing_done_event.clear()

        # Clear the event
        self._meteor_closed_event.clear()

        # Log message
        self._log_debug('# ----- Connect to meteor server -----')

        try:
            # Connect to meteor server.
            #
            # If the connecting succeeds, the meteor client's thread will call
            # `self._meteor_connected_callback` hooked at 5DI82. The login,
            # topic subscribing, and message handling are run in that thread.
            #
            # The main thread merely waits until the meteor client is closed,
            # meanwhile it calls heartbeat function at interval if specified.
            #
            self._meteor_client.connect()

        # If have error
        except:
            # Log message
            self._log_debug('# ----- Connecting failed -----')

            # Log message
            self._log_debug('# ----- Unhook meteor client callbacks -----')

            # Remove meteor client callbacks
            self._meteor_client.remove_all_listeners()

            # Remove meteor client reference
            self._meteor_client = None

            # The two events below should not have been set if the connecting
            # failed. Just in case.
            #
            # Clear the event
            self._subscribing_done_event.clear()

            # Clear the event
            self._meteor_closed_event.clear()

            # Raise the error
            raise

        # Get whether heartbeat is enabled
        heartbeat_enabled = self._get_bool_config(
            CONFIG_KEYS.HEARTBEAT_ENABLED)

        try:
            # Wait until the topic subscribing is done in another thread at
            # 5MS7A
            self._subscribing_done_event.wait()

            # If heartbeat is enabled
            if heartbeat_enabled:
                # Get heartbeat function
                heartbeat_func = self._get_config(CONFIG_KEYS.HEARTBEAT_FUNC)

                # Assert the function is callable
                assert callable(heartbeat_func), repr(heartbeat_func)

                # Get heartbeat interval
                heartbeat_interval = self._get_config(
                    CONFIG_KEYS.HEARTBEAT_INTERVAL,
                    default=10,
                )

                # Convert heartbeat interval to float
                heartbeat_interval = float(heartbeat_interval)

                # 65ZNO
                # Loop until the meteor client is disconnected
                while self._meteor_client.connected:
                    # Send heartbeat
                    heartbeat_func(self)

                    # Sleep before sending next heartbeat
                    time.sleep(heartbeat_interval)

            # 5W6XQ
            # Wait until the meteor client is closed and the `closed` callback
            # is called at 3DMYH
            self._meteor_closed_event.wait()

        # If have error
        except:
            # Close meteor client.
            #
            # This will cause `self._meteor_closed_callback` to be called,
            # which will set the `self._meteor_closed_event` below.
            #
            self._meteor_client.close()

            # See 5W6XQ
            self._meteor_closed_event.wait()

            # Raise the error
            raise

        # Always do
        finally:
            # Log message
            self._log_debug('# ----- Unhook meteor client callbacks -----')

            # Remove meteor client callbacks
            self._meteor_client.remove_all_listeners()

            # Remove meteor client reference.
            #
            # Do this before calling `callback_presence` below so that the
            # plugins will not be able to access the already closed client.
            #
            self._meteor_client = None

            # Log message
            self._log_debug('# ----- Call `callback_presence` -----')

            # Call `callback_presence`
            self.callback_presence(
                Presence(identifier=self.bot_identifier, status=OFFLINE))

            # Log message
            self._log_debug('# ----- Call `disconnect_callback` -----')

            # Call `disconnect_callback` to unload plugins
            self.disconnect_callback()

            # Clear the event
            self._subscribing_done_event.clear()

            # Clear the event
            self._meteor_closed_event.clear()

        # Log message
        self._log_debug('# ===== serve_once =====')

    def _meteor_connected_callback(self):
        """
        Callback called when the meteor client is connected.

        Hooked at 5DI82.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- _meteor_connected_callback -----')

        # Log message
        self._log_debug(
            '# ----- Log in to meteor server -----\nUser: {}'.format(
                self._login_username))

        # Log in to meteor server
        self._meteor_client.login(
            user=self._login_username,
            password=self._login_password,
            # 2I0GP
            callback=self._meteor_login_callback,
        )

    def _meteor_login_callback(self, error_info, success_info):
        """
        Callback called when the meteor client has succeeded or failed login.

        Hooked at 2I0GP.

        :param error_info: Error info.

        :param success_info: Success info.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- _meteor_login_callback -----')

        # If have error info
        if error_info is not None:
            # Get message
            msg = 'Login failed:\n{}'.format(pformat(error_info, width=1))

            # Log message
            self._log_debug(msg)

            # Close meteor client.
            # This will cause `_meteor_closed_callback` be called.
            self._meteor_client.close()

        # If not have error info
        else:
            # Get message
            msg = 'Login succeeded:\n{}'.format(pformat(success_info, width=1))

            # Log message
            self._log_debug(msg)

            # Subscribe to message events
            self._meteor_client.subscribe(
                # Topic name
                name='stream-room-messages',
                params=[
                    # All messages from rooms the rocket chat user has joined
                    '__my_messages__',
                    False,
                ],
                # 6BKIR
                callback=self._meteor_subscribe_callback,
            )

    def _meteor_subscribe_callback(self, error_info):
        """
        Callback called when the meteor client has succeeded or failed \
            subscribing.

        Hooked at 6BKIR.

        :param error_info: Error info.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- _meteor_subscribe_callback -----')

        # If have error info
        if error_info is not None:
            # Get message
            msg = 'Subscribing failed:\n{}'.format(pformat(error_info,
                                                           width=1))

            # Log message
            self._log_debug(msg)

            # Close meteor client.
            # This will cause `self._meteor_closed_callback` to be called.
            self._meteor_client.close()

        # If not have error info
        else:
            # Log message
            self._log_debug('Subscribing succeeded.')

            # Log message
            self._log_debug('# ----- Call `connect_callback` -----')

            # Call `connect_callback` to load plugins.
            #
            # This is called in meteor client's thread.
            # Plugins should not assume they are loaded from the main thread.
            #
            self.connect_callback()

            # Log message
            self._log_debug('# ----- Call `callback_presence` -----')

            # Call `callback_presence`
            self.callback_presence(
                Presence(identifier=self.bot_identifier, status=ONLINE))

            # Log message
            self._log_debug(
                '# ----- Hook callback `_meteor_changed_callback` -----')

            # Reset reconnection count
            self.reset_reconnection_count()

            # 5MS7A
            # Set the topic subscribing is done
            self._subscribing_done_event.set()

    def _meteor_changed_callback(self, collection, id, fields, cleared):
        """
        Callback called when the meteor client received message.

        Hooked at 2RAYF.

        :param collection: Data collection key.

        :param id: Data item key.

        :param fields: Data fields changed.

        :param cleared: Data fields to be cleared.

        :return: None.
        """
        # If is message event
        if collection == 'stream-room-messages':
            # Get `args` value
            args = fields.get('args', None)

            # If `args` value is list
            if isinstance(args, list):
                # For each message info
                for msg_info in args:
                    # Get message
                    msg = msg_info.get('msg', None)

                    # If have message
                    if msg is not None:
                        # Get sender info
                        sender_info = msg_info['u']

                        # Get sender username
                        sender_username = sender_info['username']

                        # If the sender is not the bot itself
                        if sender_username != self._login_username:
                            # Create sender's identifier object
                            sender_identifier = self.build_identifier(
                                sender_username)

                            # Create extras info
                            extras = {
                                # 2QTGO
                                'msg_info': msg_info,
                            }

                            # Create received message object
                            msg_obj = Message(
                                body=msg,
                                frm=sender_identifier,
                                to=self.bot_identifier,
                                extras=extras,
                            )

                            # Log message
                            self._log_debug(
                                '# ----- Call `callback_message` -----')

                            # Call `callback_message` to dispatch the message
                            # to plugins
                            self.callback_message(msg_obj)

    def _meteor_added_callback(self, collection, id, fields):
        """
        Callback called when the meteor client emits `added` event.

        Hooked at 4XIZB.

        :param collection: Data collection key.

        :param id: Data item key.

        :param fields: Data fields.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- _meteor_added_callback -----')

    def _meteor_removed_callback(self, collection, id):
        """
        Callback called when the meteor client emits `removed` event.

        Hooked at 2JEIK.

        :param collection: Data collection key.

        :param id: Data item key.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- _meteor_removed_callback -----')

    def _meteor_failed_callback(self):
        """
        Callback called when the meteor client emits `failed` event.

        Hooked at 32TF2.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- _meteor_failed_callback -----')

    def _meteor_reconnected_callback(self):
        """
        Callback called when the meteor client emits `reconnected` event.

        Hooked at 5W6RX.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- _meteor_reconnected_callback -----')

    def _meteor_closed_callback(self, code, reason):
        """
        Callback called when the meteor client emits `closed` event.

        Hooked at 7MOJX.

        :param code: Close code.

        :param reason: Close reason.

        :return: None.
        """
        # Log message
        self._log_debug(
            '# ----- _meteor_closed_callback -----\nCode: {}\nReason: {}'.
            format(code, reason))

        # Set the topic subscribing is done
        self._subscribing_done_event.set()

        # 3DMYH
        # Set the meteor client's `closed` event is emitted
        self._meteor_closed_event.set()

    def build_reply(self, mess, text=None, private=False, threaded=False):
        """
        Create reply message object.

        Used by `self.send_simple_reply`.

        :param mess: The original message object.

        :param text: Reply message text.

        :param private: Whether the reply message is private.

        :return: Message object.
        """
        # Create reply message object
        reply = Message(
            body=text,
            frm=mess.to,
            to=mess.frm,
            extras={
                # 5QXGV
                # Store the original message object
                'orig_msg': mess
            })

        # Return reply message object
        return reply

    def prefix_groupchat_reply(self, message, identifier):
        """
        Add group chat prefix to the message.

        Used by `self.send` and `self.send_simple_reply`.

        :param message: Message object to send.

        :param identifier: The message receiver's identifier object.

        :return: None.
        """
        # Do nothing

    def send_rocketchat_message(self, params):
        """
        Send message to meteor server.

        :param params: RPC method `sendMessage`'s parameters.

        :return: None.
        """
        # If argument `params` is not list
        if not isinstance(params, list):
            # Put it in a list
            params = [params]

        # Send message to meteor server
        self._meteor_client.call(
            method='sendMessage',
            params=params,
        )

    def send_message(self, mess):
        """
        Send message to meteor server.

        Used by `self.split_and_send_message`. `self.split_and_send_message` is
        used by `self.send` and `self.send_simple_reply`.

        :param mess: Message object to send.

        :return: None.
        """
        # Call super method to dispatch to plugins
        super(AoikRocketChatErrbot, self).send_message(mess)

        # Get original message object.
        #
        # The key is set at 5QXGV and 3YRCT.
        #
        orig_msg = mess.extras['orig_msg']

        # Get original message info.
        #
        # The key is set at 2QTGO
        #
        msg_info = orig_msg.extras['msg_info']

        # Get room ID
        room_id = msg_info['rid']

        # Send message to meteor server
        self.send_rocketchat_message(params={
            'rid': room_id,
            'msg': mess.body,
        })

    def send(
        self,
        identifier,
        text,
        in_reply_to=None,
        groupchat_nick_reply=False,
    ):
        """
        Send message to meteor server.

        :param identifier: Receiver's identifier object.

        :param text: Message text to send.

        :param in_reply_to: Original message object.

        :param groupchat_nick_reply: Whether the message to send is group chat.

        `self.prefix_groupchat_reply` will be called to process the message if
        it is group chat.

        :return: None.
        """
        # If the identifier object is not Identifier instance
        if not isinstance(identifier, Identifier):
            # Get message
            error_msg = (
                'Argument `identifier` is not Identifier instance: {}').format(
                    repr(identifier))

            # Raise error
            raise ValueError(error_msg)

        # If the original message is not given
        if in_reply_to is None:
            # Get message
            error_msg = 'Argument `in_reply_to` must be given.'

            # Raise error
            raise ValueError(error_msg)

        # Create message object
        msg_obj = Message(
            body=text,
            frm=in_reply_to.to,
            to=identifier,
            extras={
                # 3YRCT
                # Store the original message object
                'orig_msg': in_reply_to,
            },
        )

        # Get group chat prefix from config
        group_chat_prefix = self.bot_config.GROUPCHAT_NICK_PREFIXED

        # If the receiver is a room
        if isinstance(identifier, Room):
            # If have group chat prefix,
            # or the message is group chat.
            if group_chat_prefix or groupchat_nick_reply:
                # Call `prefix_groupchat_reply` to process the message
                self.prefix_groupchat_reply(msg_obj, in_reply_to.frm)

        # Send the message
        self.split_and_send_message(msg_obj)

    def query_room(self, room):
        """
        Query room info. Not implemented.

        :param room: Room ID.

        :return: None.
        """
        # Return None
        return None

    def rooms(self):
        """
        Get room list. Not implemented.

        :return: Empty list.
        """
        # Return empty list
        return []

    def change_presence(self, status=ONLINE, message=''):
        """
Esempio n. 18
0
def insert_callback(error, data):
    if error:
        print(error)
        return
    print(data)


def callback_function(error, result):
    print("callback")
    if error:
        print(error)
        return
    print(result)


client = MeteorClient("ws://127.0.0.1:3000/websocket")

client.on("connected", connected)
client.on("failed", failed)

client.connect()

timestamp = str(datetime.now())
lat = 1.2956
lng = 103.7767
speed = 50
bearing = 50
altitude = 50

client.insert(
    "Telemetry",
ledcontrol2 = 16
ledcontrol3 = 18
ledcontrol4 = 22
ledcontrol5 = 11
ledcontrol6 = 15

ledname1 = 'First'
ledname2 = 'Second'
ledname3 = 'Third'
ledname4 = 'Forth'
ledname5 = 'Fifth'
ledname6 = 'Sixth'

from MeteorClient import MeteorClient

client = MeteorClient('ws://127.0.0.1:3000/websocket')
# client = MeteorClient('ws://testddp.meteor.com/websocket')


def subscribed(subscription):
    print('* SUBSCRIBED {}'.format(subscription))
    # conn = client.find_one('items', selector={'name': getHwAddr('eth0') })
    # if subscription == 'local-items':
    #     def getHwAddr(ifname):
    #         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #         info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    #         return ':'.join(['%02x' % ord(char) for char in info[18:24]])
    #     print getHwAddr('eth0')
    #     # new part
    #     newmac = getHwAddr('eth0')
    #     conn = client.find_one('items', selector={'name': newmac })
Esempio n. 20
0
class DDP(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.meteor = conf.get('ddp', 'meteor')

        self.client = MeteorClient(self.meteor, debug=False)
        self.client.on('added', self.on_added)
        self.client.on('changed', self.on_changed)
        self.client.on('subscribed', self.on_subscribed)
        self.client.on('connected', self.on_connected)
        self.client.on('removed', self.on_removed)
        self.client.on('closed', self.on_closed)
        self.client.on('logged_in', self.on_logged_in)

        self.displayName = conf.get('sussexlogin', 'room_name')
        self.vu_min = -70
        self.vu_range = 40
        self.do_vu = 0
        self.last_vu = None
        self.ip = socket.gethostbyname(socket.gethostname())
        self.id = conf.get('ingest', 'hostname')
        self._user = conf.get('ddp', 'user')
        self._password = conf.get('ddp', 'password')
        self._http_host = conf.get('ddp', 'http_host')
        self._audiostream_port = conf.get('audiostream', 'port') or 31337
        self.netreg_id = conf.get('ddp', 'netreg_id')
        self.store_audio = conf.get_boolean('ddp', 'store_audio')
        self.paused = False
        self.recording = False
        self.currentMediaPackage = None
        self.currentProfile = None
        self.has_disconnected = False

        cam_available = conf.get('sussexlogin',
                                 'cam_available') or cam_available
        if cam_available in ('True', 'true', True, '1', 1):
            self.cam_available = 1
        elif cam_available in ('False', 'false', False, '0', 0):
            self.cam_available = 0
        else:
            self.cam_available = int(cam_available)

        self.audiofaders = []
        faders = conf.get('ddp', 'audiofaders').split()
        for fader in faders:
            audiofader = {}
            fader = 'audiofader-' + fader
            audiofader['name'] = conf.get(fader, 'name')
            audiofader['display'] = conf.get(fader, 'display')
            audiofader['min'] = conf.get_int(fader, 'min')
            audiofader['max'] = conf.get_int(fader, 'max')
            audiofader['type'] = conf.get(fader, 'type')
            audiofader['setrec'] = conf.get_boolean(fader, 'setrec')
            audiofader['mute'] = conf.get_boolean(fader, 'mute')
            audiofader['unmute'] = conf.get_boolean(fader, 'unmute')
            audiofader['setlevel'] = conf.get_int(fader, 'setlevel')
            try:
                audiofader['control'] = alsaaudio.Mixer(
                    control=audiofader['name'])
                self.audiofaders.append(audiofader)
            except Exception as e:
                logger.warn(e)
        fd, eventmask = self.audiofaders[0]['control'].polldescriptors()[0]
        self.watchid = gobject.io_add_watch(fd, eventmask, self.mixer_changed)

        dispatcher.connect('galicaster-init', self.on_init)
        dispatcher.connect('update-rec-vumeter', self.vumeter)
        dispatcher.connect('galicaster-notify-timer-short', self.heartbeat)
        dispatcher.connect('start-before', self.on_start_recording)
        dispatcher.connect('restart-preview', self.on_stop_recording)
        dispatcher.connect('update-rec-status', self.on_rec_status_update)

    def run(self):
        self.connect()

    def connect(self):
        if not self.has_disconnected:
            try:
                self.client.connect()
            except Exception:
                logger.warn('DDP connection failed')

    def update(self, collection, query, update):
        if self.client.connected and self.subscribedTo('GalicasterControl'):
            try:
                self.client.update(collection,
                                   query,
                                   update,
                                   callback=self.update_callback)
            except Exception:
                logger.warn("Error updating document "
                            "{collection: %s, query: %s, update: %s}" %
                            (collection, query, update))

    def insert(self, collection, document):
        if self.client.connected and self.subscribedTo('GalicasterControl'):
            try:
                self.client.insert(collection,
                                   document,
                                   callback=self.insert_callback)
            except Exception:
                logger.warn(
                    "Error inserting document {collection: %s, document: %s}" %
                    (collection, document))

    def heartbeat(self, element):
        if self.client.connected:
            self.update_images()
        else:
            self.connect()

    def on_start_recording(self, sender, id):
        self.recording = True
        self.currentMediaPackage = self.media_package_metadata(id)
        self.currentProfile = context.get_state().profile.name
        self.update('rooms', {'_id': self.id}, {
            '$set': {
                'currentMediaPackage': self.currentMediaPackage,
                'currentProfile': self.currentProfile,
                'recording': self.recording
            }
        })

    def on_stop_recording(self, sender=None):
        self.recording = False
        self.currentMediaPackage = None
        self.currentProfile = None
        self.update('rooms', {'_id': self.id}, {
            '$unset': {
                'currentMediaPackage': '',
                'currentProfile': ''
            },
            '$set': {
                'recording': self.recording
            }
        })
        self.update_images(1.5)

    def on_init(self, data):
        self.update_images(1.5)

    def update_images(self, delay=0):
        worker = Thread(target=self._update_images, args=(delay, ))
        worker.start()

    def _update_images(self, delay):
        time.sleep(delay)
        files = {}
        audio_devices = ['audiotest', 'autoaudio', 'pulse']
        for track in context.get_state().profile.tracks:
            if track.device not in audio_devices:
                file = os.path.join('/tmp', track.file + '.jpg')
                try:
                    if (os.path.getctime(file) > time.time() - 3):
                        files[track.flavor] = (track.flavor + '.jpg',
                                               open(file, 'rb'), 'image/jpeg')
                except Exception:
                    logger.warn("Unable to check date of or open file (%s)" %
                                file)
        im = ImageGrab.grab(bbox=(10, 10, 1280, 720), backend='imagemagick')
        im.thumbnail((640, 360))
        output = cStringIO.StringIO()
        if im.mode != "RGB":
            im = im.convert("RGB")
        im.save(output, format="JPEG")
        files['galicaster'] = ('galicaster.jpg', output.getvalue(),
                               'image/jpeg')
        try:
            # add verify=False for testing self signed certs
            requests.post("%s/image/%s" % (self._http_host, self.id),
                          files=files,
                          auth=(self._user, self._password))
        except Exception:
            logger.warn('Unable to post images')

    def mixer_changed(self, source=None, condition=None, reopen=True):
        if reopen:
            for audiofader in self.audiofaders:
                audiofader['control'] = alsaaudio.Mixer(
                    control=audiofader['name'])
        self.update_audio()
        return True

    def vumeter(self, element, data):
        if self.do_vu == 0:
            if data == "Inf":
                data = 0
            else:
                if data < -self.vu_range:
                    data = -self.vu_range
                elif data > 0:
                    data = 0
            data = int(((data + self.vu_range) / float(self.vu_range)) * 100)
            if data != self.last_vu:
                update = {'vumeter': data}
                self.update('rooms', {'_id': self.id}, {'$set': update})
                self.last_vu = data
        self.do_vu = (self.do_vu + 1) % 20

    def on_rec_status_update(self, element, data):
        is_paused = data == 'Paused'
        if is_paused:
            self.update_images(.75)
        if self.paused != is_paused:
            self.update('rooms', {'_id': self.id},
                        {'$set': {
                            'paused': is_paused
                        }})
            self.paused = is_paused
        if data == '  Recording  ':
            subprocess.call(['killall', 'maliit-server'])
            self.update_images(.75)

    def media_package_metadata(self, id):
        mp = context.get_repository().get(id)
        line = mp.metadata_episode.copy()
        duration = mp.getDuration()
        line["duration"] = long(duration / 1000) if duration else None
        # Does series_title need sanitising as well as duration?
        created = mp.getDate()
        line["created"] = calendar.timegm(created.utctimetuple())
        for key, value in mp.metadata_series.iteritems():
            line["series_" + key] = value
        for key, value in line.iteritems():
            if value in [None, []]:
                line[key] = ''
        return line

    def subscription_callback(self, error):
        if error:
            logger.warn("Subscription callback returned error: %s" % error)

    def insert_callback(self, error, data):
        if error:
            logger.warn("Insert callback returned error: %s" % error)

    def update_callback(self, error, data):
        if error:
            logger.warn("Update callback returned error: %s" % error)

    def on_subscribed(self, subscription):
        if (subscription == 'GalicasterControl'):
            me = self.client.find_one('rooms')
            stream_key = uuid.uuid4().get_hex()

            # Data to push when inserting or updating
            data = {
                'displayName': self.displayName,
                'ip': self.ip,
                'paused': self.paused,
                'recording': self.recording,
                'heartbeat': int(time.time()),
                'camAvailable': self.cam_available,
                'netregId': self.netreg_id,
                'inputs': self.inputs(),
                'stream': {
                    'port': self._audiostream_port,
                    'key': stream_key
                },
                'galicasterVersion': galicaster.__version__
            }
            if self.currentMediaPackage:
                data['currentMediaPackage'] = self.currentMediaPackage
            if self.currentProfile:
                data['currentProfile'] = self.currentProfile

            if me:
                # Items to unset
                unset = {}
                if not self.currentMediaPackage:
                    unset['currentMediaPackage'] = ''
                if not self.currentProfile:
                    unset['currentProfile'] = ''

                # Update to push
                update = {'$set': data}

                if unset:
                    update['$unset'] = unset
                self.update('rooms', {'_id': self.id}, update)
            else:
                audio = self.read_audio_settings()
                data['_id'] = self.id
                data['audio'] = audio
                self.insert('rooms', data)

    def inputs(self):
        inputs = {'presentations': ['Presentation']}
        inputs['cameras'] = []
        labels = conf.get('sussexlogin', 'matrix_cam_labels')
        cam_labels = []
        if labels:
            cam_labels = [l.strip() for l in labels.split(',')]
        for i in range(0, self.cam_available):
            label = cam_labels[i] if i < len(cam_labels) else "Camera %d" % (
                i + 1)
            inputs['cameras'].append(label)
        return inputs

    def set_audio(self, fields):
        faders = fields.get('audio')
        if faders:
            for fader in faders:
                mixer = None
                level = fader.get('level')
                for audiofader in self.audiofaders:
                    if audiofader['name'] == fader['name']:
                        mixer = audiofader['control']
                        break
                if mixer:
                    l, r = mixer.getvolume(fader['type'])
                    if level >= 0 and l != level:
                        mixer.setvolume(level, 0, fader['type'])
                        mixer.setvolume(level, 1, fader['type'])
            if self.store_audio:
                # Relies on no password sudo access for current user to alsactl
                subprocess.call(['sudo', 'alsactl', 'store'])

    def on_added(self, collection, id, fields):
        self.set_audio(fields)
        self.update_audio()

    def on_changed(self, collection, id, fields, cleared):
        self.set_audio(fields)
        me = self.client.find_one('rooms')
        if self.paused != me['paused']:
            self.set_paused(me['paused'])
        if context.get_state().is_recording != me['recording']:
            self.set_recording(me)

    def on_removed(self, collection, id):
        self.on_subscribed(None)

    def set_paused(self, new_status):
        self.paused = new_status
        dispatcher.emit("toggle-pause-rec")

    def set_recording(self, me):
        self.recording = me['recording']
        if self.recording:
            meta = me.get('currentMediaPackage', {}) or {}
            profile = me.get('currentProfile', 'nocam')
            series = (meta.get('series_title', ''), meta.get('isPartOf', ''))
            user = {
                'user_name': meta.get('creator', ''),
                'user_id': meta.get('rightsHolder', '')
            }
            title = meta.get('title', 'Unknown')
            dispatcher.emit('sussexlogin-record',
                            (user, title, series, profile))
        else:
            dispatcher.emit("stop-record", '')

    def on_connected(self):
        logger.info('Connected to Meteor')
        token = conf.get('ddp', 'token')
        self.client.login(self._user, self._password, token=token)

    def on_logged_in(self, data):
        conf.set('ddp', 'token', data['token'])
        conf.update()
        try:
            self.client.subscribe('GalicasterControl',
                                  params=[self.id],
                                  callback=self.subscription_callback)
        except Exception:
            logger.warn('DDP subscription failed')

    def on_closed(self, code, reason):
        self.has_disconnected = True
        logger.error('Disconnected from Meteor: err %d - %s' % (code, reason))

    def update_audio(self):
        me = self.client.find_one('rooms')
        audio = self.read_audio_settings()
        update = False
        if me:
            mAudio = me.get('audio')
            mAudioNames = [x['name'] for x in mAudio]
            audioNames = [x['name'] for x in audio]
            if set(mAudioNames) != set(audioNames):
                update = True
            if not update:
                for key, fader in enumerate(audio):
                    if mAudio[key].get('level') != fader.get('level'):
                        update = True
            if update:
                self.update('rooms', {'_id': self.id},
                            {'$set': {
                                'audio': audio
                            }})

    def read_audio_settings(self):
        audio_settings = []
        for audiofader in self.audiofaders:
            if audiofader['display']:
                audio_settings.append(self.control_values(audiofader))
            # ensure fixed values
            mixer = audiofader['control']
            if audiofader['setrec']:
                mixer.setrec(1)
            if audiofader['mute']:
                mixer.setmute(1)
            if audiofader['unmute']:
                mixer.setmute(0)
            if audiofader['setlevel'] >= 0:
                mixer.setvolume(audiofader['setlevel'], 0, audiofader['type'])
                if 'Joined Playback Volume' not in mixer.volumecap():
                    mixer.setvolume(audiofader['setlevel'], 1,
                                    audiofader['type'])
        return audio_settings

    def control_values(self, audiofader):
        controls = {}
        left, right = audiofader['control'].getvolume(audiofader['type'])
        controls['min'] = audiofader['min']
        controls['max'] = audiofader['max']
        controls['level'] = left
        controls['type'] = audiofader['type']
        controls['name'] = audiofader['name']
        controls['display'] = audiofader['display']
        return controls

    def subscribedTo(self, publication):
        return self.client.subscriptions.get(publication) is not None
Esempio n. 21
0
                msg['short_summary'] = "Not available"
                msg['topics'] = "Not available"

            logging.debug("Updating Edits with message %s", msg)
            client.insert('edits', msg)

#--------------------------------------------------------#
#                       Main                             #
#--------------------------------------------------------#
if __name__ == "__main__":

    # Connect to meteor server
    ws = "ws://ddp--8162-wikiedits.meteor.com/websocket"
    #ws = "ws://127.0.0.1:3000/websocket"
    logging.info("Connecting to meteor server")
    client = MeteorClient(ws, auto_reconnect=True, auto_reconnect_timeout=10)
    client.connect()
    client.subscribe('piedata')
    client.subscribe('edits')
    client.subscribe('bardata')
    logging.info("Connected")

    logging.info("Starting IRC server")
    # Start Wikipedia bot
    server = 'irc.wikimedia.org'
    port = 6667
    nickname = 'elyasebot'
    channel = '#en.wikipedia'
    bot = WikiBot(channel, nickname, server, port)
    bot.start()
Esempio n. 22
0
class DDP(Thread):

    def __init__(self):
        Thread.__init__(self)
        self.meteor = conf.get('ddp', 'meteor')

        self.client = MeteorClient(self.meteor, debug=False)
        self.client.on('added', self.on_added)
        self.client.on('changed', self.on_changed)
        self.client.on('subscribed', self.on_subscribed)
        self.client.on('connected', self.on_connected)
        self.client.on('removed', self.on_removed)
        self.client.on('closed', self.on_closed)
        self.client.on('logged_in', self.on_logged_in)

        self.displayName = conf.get('sussexlogin', 'room_name')
        self.vu_min = -70
        self.vu_range = 40
        self.do_vu = 0
        self.last_vu = None
        self.ip = socket.gethostbyname(socket.gethostname())
        self.id = conf.get('ingest', 'hostname')
        self._user = conf.get('ddp', 'user')
        self._password = conf.get('ddp', 'password')
        self._http_host = conf.get('ddp', 'http_host')
        self._audiostream_port = conf.get('audiostream', 'port') or 31337
        self.netreg_id = conf.get('ddp', 'netreg_id')
        self.store_audio = conf.get_boolean('ddp', 'store_audio')
        self.paused = False
        self.recording = False
        self.currentMediaPackage = None
        self.currentProfile = None
        self.has_disconnected = False

        cam_available = conf.get(
            'sussexlogin',
            'cam_available') or cam_available
        if cam_available in ('True', 'true', True, '1', 1):
            self.cam_available = 1
        elif cam_available in ('False', 'false', False, '0', 0):
            self.cam_available = 0
        else:
            self.cam_available = int(cam_available)

        self.audiofaders = []
        faders = conf.get('ddp', 'audiofaders').split()
        for fader in faders:
            audiofader = {}
            fader = 'audiofader-' + fader
            audiofader['name'] = conf.get(fader, 'name')
            audiofader['display'] = conf.get(fader, 'display')
            audiofader['min'] = conf.get_int(fader, 'min')
            audiofader['max'] = conf.get_int(fader, 'max')
            audiofader['type'] = conf.get(fader, 'type')
            audiofader['setrec'] = conf.get_boolean(fader, 'setrec')
            audiofader['mute'] = conf.get_boolean(fader, 'mute')
            audiofader['unmute'] = conf.get_boolean(fader, 'unmute')
            audiofader['setlevel'] = conf.get_int(fader, 'setlevel')
            try:
                audiofader['control'] = alsaaudio.Mixer(
                    control=audiofader['name'])
                self.audiofaders.append(audiofader)
            except Exception as e:
                logger.warn(e)
        fd, eventmask = self.audiofaders[0]['control'].polldescriptors()[0]
        self.watchid = gobject.io_add_watch(fd, eventmask, self.mixer_changed)

        dispatcher.connect('galicaster-init', self.on_init)
        dispatcher.connect('update-rec-vumeter', self.vumeter)
        dispatcher.connect('galicaster-notify-timer-short', self.heartbeat)
        dispatcher.connect('start-before', self.on_start_recording)
        dispatcher.connect('restart-preview', self.on_stop_recording)
        dispatcher.connect('update-rec-status', self.on_rec_status_update)

    def run(self):
        self.connect()

    def connect(self):
        if not self.has_disconnected:
            try:
                self.client.connect()
            except Exception:
                logger.warn('DDP connection failed')

    def update(self, collection, query, update):
        if self.client.connected and self.subscribedTo('GalicasterControl'):
            try:
                self.client.update(
                    collection,
                    query,
                    update,
                    callback=self.update_callback)
            except Exception:
                logger.warn(
                    "Error updating document "
                    "{collection: %s, query: %s, update: %s}" %
                    (collection, query, update))

    def insert(self, collection, document):
        if self.client.connected and self.subscribedTo('GalicasterControl'):
            try:
                self.client.insert(
                    collection,
                    document,
                    callback=self.insert_callback)
            except Exception:
                logger.warn(
                    "Error inserting document {collection: %s, document: %s}" %
                    (collection, document))

    def heartbeat(self, element):
        if self.client.connected:
            self.update_images()
        else:
            self.connect()

    def on_start_recording(self, sender, id):
        self.recording = True
        self.currentMediaPackage = self.media_package_metadata(id)
        self.currentProfile = context.get_state().profile.name
        self.update(
            'rooms', {
                '_id': self.id
            }, {
                '$set': {
                    'currentMediaPackage': self.currentMediaPackage,
                    'currentProfile': self.currentProfile,
                    'recording': self.recording
                }
            })

    def on_stop_recording(self, sender=None):
        self.recording = False
        self.currentMediaPackage = None
        self.currentProfile = None
        self.update(
            'rooms', {
                '_id': self.id
            }, {
                '$unset': {
                    'currentMediaPackage': '',
                    'currentProfile': ''
                }, '$set': {
                    'recording': self.recording
                }
            })
        self.update_images(1.5)

    def on_init(self, data):
        self.update_images(1.5)

    def update_images(self, delay=0):
        worker = Thread(target=self._update_images, args=(delay,))
        worker.start()

    def _update_images(self, delay):
        time.sleep(delay)
        files = {}
        audio_devices = ['audiotest', 'autoaudio', 'pulse']
        for track in context.get_state().profile.tracks:
            if track.device not in audio_devices:
                file = os.path.join('/tmp', track.file + '.jpg')
                try:
                    if(os.path.getctime(file) > time.time() - 3):
                        files[track.flavor] = (track.flavor + '.jpg',
                                               open(file, 'rb'),
                                               'image/jpeg')
                except Exception:
                    logger.warn("Unable to check date of or open file (%s)"
                                % file)
        im = ImageGrab.grab(bbox=(10, 10, 1280, 720), backend='imagemagick')
        im.thumbnail((640, 360))
        output = cStringIO.StringIO()
        if im.mode != "RGB":
            im = im.convert("RGB")
        im.save(output, format="JPEG")
        files['galicaster'] = ('galicaster.jpg', output.getvalue(),
                               'image/jpeg')
        try:
            # add verify=False for testing self signed certs
            requests.post(
                "%s/image/%s" %
                (self._http_host, self.id), files=files, auth=(
                    self._user, self._password))
        except Exception:
            logger.warn('Unable to post images')

    def mixer_changed(self, source=None, condition=None, reopen=True):
        if reopen:
            for audiofader in self.audiofaders:
                audiofader['control'] = alsaaudio.Mixer(
                    control=audiofader['name'])
        self.update_audio()
        return True

    def vumeter(self, element, data):
        if self.do_vu == 0:
            if data == "Inf":
                data = 0
            else:
                if data < -self.vu_range:
                    data = -self.vu_range
                elif data > 0:
                    data = 0
            data = int(((data + self.vu_range) / float(self.vu_range)) * 100)
            if data != self.last_vu:
                update = {'vumeter': data}
                self.update('rooms', {'_id': self.id}, {'$set': update})
                self.last_vu = data
        self.do_vu = (self.do_vu + 1) % 20

    def on_rec_status_update(self, element, data):
        is_paused = data == 'Paused'
        if is_paused:
            self.update_images(.75)
        if self.paused != is_paused:
            self.update(
                'rooms', {
                    '_id': self.id}, {
                    '$set': {
                        'paused': is_paused}})
            self.paused = is_paused
        if data == '  Recording  ':
            subprocess.call(['killall', 'maliit-server'])
            self.update_images(.75)

    def media_package_metadata(self, id):
        mp = context.get_repository().get(id)
        line = mp.metadata_episode.copy()
        duration = mp.getDuration()
        line["duration"] = long(duration / 1000) if duration else None
        # Does series_title need sanitising as well as duration?
        created = mp.getDate()
        line["created"] = calendar.timegm(created.utctimetuple())
        for key, value in mp.metadata_series.iteritems():
            line["series_" + key] = value
        for key, value in line.iteritems():
            if value in [None, []]:
                line[key] = ''
        return line

    def subscription_callback(self, error):
        if error:
            logger.warn("Subscription callback returned error: %s" % error)

    def insert_callback(self, error, data):
        if error:
            logger.warn("Insert callback returned error: %s" % error)

    def update_callback(self, error, data):
        if error:
            logger.warn("Update callback returned error: %s" % error)

    def on_subscribed(self, subscription):
        if(subscription == 'GalicasterControl'):
            me = self.client.find_one('rooms')
            stream_key = uuid.uuid4().get_hex()

            # Data to push when inserting or updating
            data = {
                'displayName': self.displayName,
                'ip': self.ip,
                'paused': self.paused,
                'recording': self.recording,
                'heartbeat': int(time.time()),
                'camAvailable': self.cam_available,
                'netregId': self.netreg_id,
                'inputs': self.inputs(),
                'stream': {
                    'port': self._audiostream_port,
                    'key': stream_key
                },
                'galicasterVersion': galicaster.__version__
            }
            if self.currentMediaPackage:
                data['currentMediaPackage'] = self.currentMediaPackage
            if self.currentProfile:
                data['currentProfile'] = self.currentProfile

            if me:
                # Items to unset
                unset = {}
                if not self.currentMediaPackage:
                    unset['currentMediaPackage'] = ''
                if not self.currentProfile:
                    unset['currentProfile'] = ''

                # Update to push
                update = {
                    '$set': data
                }

                if unset:
                    update['$unset'] = unset
                self.update('rooms', {'_id': self.id}, update)
            else:
                audio = self.read_audio_settings()
                data['_id'] = self.id
                data['audio'] = audio
                self.insert('rooms', data)

    def inputs(self):
        inputs = {
            'presentations': ['Presentation']
        }
        inputs['cameras'] = []
        labels = conf.get('sussexlogin', 'matrix_cam_labels')
        cam_labels = []
        if labels:
            cam_labels = [l.strip() for l in labels.split(',')]
        for i in range(0, self.cam_available):
            label = cam_labels[i] if i < len(
                cam_labels) else "Camera %d" % (i + 1)
            inputs['cameras'].append(label)
        return inputs

    def set_audio(self, fields):
        faders = fields.get('audio')
        if faders:
            for fader in faders:
                mixer = None
                level = fader.get('level')
                for audiofader in self.audiofaders:
                    if audiofader['name'] == fader['name']:
                        mixer = audiofader['control']
                        break
                if mixer:
                    l, r = mixer.getvolume(fader['type'])
                    if level >= 0 and l != level:
                        mixer.setvolume(level, 0, fader['type'])
                        mixer.setvolume(level, 1, fader['type'])
            if self.store_audio:
                # Relies on no password sudo access for current user to alsactl
                subprocess.call(['sudo', 'alsactl', 'store'])

    def on_added(self, collection, id, fields):
        self.set_audio(fields)
        self.update_audio()

    def on_changed(self, collection, id, fields, cleared):
        self.set_audio(fields)
        me = self.client.find_one('rooms')
        if self.paused != me['paused']:
            self.set_paused(me['paused'])
        if context.get_state().is_recording != me['recording']:
            self.set_recording(me)

    def on_removed(self, collection, id):
        self.on_subscribed(None)

    def set_paused(self, new_status):
        self.paused = new_status
        dispatcher.emit("toggle-pause-rec")

    def set_recording(self, me):
        self.recording = me['recording']
        if self.recording:
            meta = me.get('currentMediaPackage', {}) or {}
            profile = me.get('currentProfile', 'nocam')
            series = (meta.get('series_title', ''), meta.get('isPartOf', ''))
            user = {'user_name': meta.get('creator', ''),
                    'user_id': meta.get('rightsHolder', '')}
            title = meta.get('title', 'Unknown')
            dispatcher.emit('sussexlogin-record',
                            (user, title, series, profile))
        else:
            dispatcher.emit("stop-record", '')

    def on_connected(self):
        logger.info('Connected to Meteor')
        token = conf.get('ddp', 'token')
        self.client.login(self._user, self._password, token=token)

    def on_logged_in(self, data):
        conf.set('ddp', 'token', data['token'])
        conf.update()
        try:
            self.client.subscribe(
                'GalicasterControl',
                params=[
                    self.id],
                callback=self.subscription_callback)
        except Exception:
            logger.warn('DDP subscription failed')

    def on_closed(self, code, reason):
        self.has_disconnected = True
        logger.error('Disconnected from Meteor: err %d - %s' % (code, reason))

    def update_audio(self):
        me = self.client.find_one('rooms')
        audio = self.read_audio_settings()
        update = False
        if me:
            mAudio = me.get('audio')
            mAudioNames = [x['name'] for x in mAudio]
            audioNames = [x['name'] for x in audio]
            if set(mAudioNames) != set(audioNames):
                update = True
            if not update:
                for key, fader in enumerate(audio):
                    if mAudio[key].get('level') != fader.get('level'):
                        update = True
            if update:
                self.update(
                    'rooms', {
                        '_id': self.id}, {
                        '$set': {
                            'audio': audio}})

    def read_audio_settings(self):
        audio_settings = []
        for audiofader in self.audiofaders:
            if audiofader['display']:
                audio_settings.append(
                    self.control_values(audiofader)
                )
            # ensure fixed values
            mixer = audiofader['control']
            if audiofader['setrec']:
                mixer.setrec(1)
            if audiofader['mute']:
                mixer.setmute(1)
            if audiofader['unmute']:
                mixer.setmute(0)
            if audiofader['setlevel'] >= 0:
                mixer.setvolume(audiofader['setlevel'], 0, audiofader['type'])
                if 'Joined Playback Volume' not in mixer.volumecap():
                    mixer.setvolume(audiofader['setlevel'],
                                    1, audiofader['type'])
        return audio_settings

    def control_values(self, audiofader):
        controls = {}
        left, right = audiofader['control'].getvolume(audiofader['type'])
        controls['min'] = audiofader['min']
        controls['max'] = audiofader['max']
        controls['level'] = left
        controls['type'] = audiofader['type']
        controls['name'] = audiofader['name']
        controls['display'] = audiofader['display']
        return controls

    def subscribedTo(self, publication):
        return self.client.subscriptions.get(publication) is not None
Esempio n. 23
0
    def __init__(self):
        Thread.__init__(self)
        self.meteor = conf.get('ddp', 'meteor')

        self.client = MeteorClient(self.meteor, debug=False)
        self.client.on('added', self.on_added)
        self.client.on('changed', self.on_changed)
        self.client.on('subscribed', self.on_subscribed)
        self.client.on('connected', self.on_connected)
        self.client.on('removed', self.on_removed)
        self.client.on('closed', self.on_closed)
        self.client.on('logged_in', self.on_logged_in)

        self.displayName = conf.get('sussexlogin', 'room_name')
        self.vu_min = -70
        self.vu_range = 40
        self.do_vu = 0
        self.last_vu = None
        self.ip = socket.gethostbyname(socket.gethostname())
        self.id = conf.get('ingest', 'hostname')
        self._user = conf.get('ddp', 'user')
        self._password = conf.get('ddp', 'password')
        self._http_host = conf.get('ddp', 'http_host')
        self._audiostream_port = conf.get('audiostream', 'port') or 31337
        self.netreg_id = conf.get('ddp', 'netreg_id')
        self.store_audio = conf.get_boolean('ddp', 'store_audio')
        self.paused = False
        self.recording = False
        self.currentMediaPackage = None
        self.currentProfile = None
        self.has_disconnected = False

        cam_available = conf.get(
            'sussexlogin',
            'cam_available') or cam_available
        if cam_available in ('True', 'true', True, '1', 1):
            self.cam_available = 1
        elif cam_available in ('False', 'false', False, '0', 0):
            self.cam_available = 0
        else:
            self.cam_available = int(cam_available)

        self.audiofaders = []
        faders = conf.get('ddp', 'audiofaders').split()
        for fader in faders:
            audiofader = {}
            fader = 'audiofader-' + fader
            audiofader['name'] = conf.get(fader, 'name')
            audiofader['display'] = conf.get(fader, 'display')
            audiofader['min'] = conf.get_int(fader, 'min')
            audiofader['max'] = conf.get_int(fader, 'max')
            audiofader['type'] = conf.get(fader, 'type')
            audiofader['setrec'] = conf.get_boolean(fader, 'setrec')
            audiofader['mute'] = conf.get_boolean(fader, 'mute')
            audiofader['unmute'] = conf.get_boolean(fader, 'unmute')
            audiofader['setlevel'] = conf.get_int(fader, 'setlevel')
            try:
                audiofader['control'] = alsaaudio.Mixer(
                    control=audiofader['name'])
                self.audiofaders.append(audiofader)
            except Exception as e:
                logger.warn(e)
        fd, eventmask = self.audiofaders[0]['control'].polldescriptors()[0]
        self.watchid = gobject.io_add_watch(fd, eventmask, self.mixer_changed)

        dispatcher.connect('galicaster-init', self.on_init)
        dispatcher.connect('update-rec-vumeter', self.vumeter)
        dispatcher.connect('galicaster-notify-timer-short', self.heartbeat)
        dispatcher.connect('start-before', self.on_start_recording)
        dispatcher.connect('restart-preview', self.on_stop_recording)
        dispatcher.connect('update-rec-status', self.on_rec_status_update)
Esempio n. 24
0
import sys
from MeteorClient import MeteorClient

if len(sys.argv) < 3:
    print 'Usage: import.py <url> <cmd>'
    sys.exit(1)

client = MeteorClient('ws://' + sys.argv[1] + '/websocket')
client.connect()

if sys.argv[2] == 'coffman':
    lat = -93.232234
    lon = 44.974536
    client.call('insertFakeJob', [lat, lon, sys.argv[3]])
elif sys.argv[2] == 'stpaul':
    lat = -93.0688954
    lon = 44.8985577
    client.call('insertFakeJob', [lat, lon, sys.argv[3]])
else:
    print 'Unknown command', sys.argv[2]
#import pymongo
#con = pymongo.MongoClient(host='54.201.12.92', port=8001)
#con.database_names()
import time
from MeteorClient import MeteorClient

client = MeteorClient('ws://www.snowdrone.xyz/websocket')
client.connect()
def subscription_callback(error):
    if error:
        print(error)
def insert_callback(error, data):
    if error:
        print(error)
        return
    print(data)
def update_callback(error, data):
    if error:
        print(error)
        return
    print(data)
def remove_callback(error, data):
    if error:
        print(error)
        return
    print(data)

client.subscribe('snowDroneTest4', callback=subscription_callback)
#all_posts = client.find('snowDroneTest3')
#print all_posts
boolTrue = True
Esempio n. 26
0
    exit()
    
sensorList = sensors.get('sensors')
username = config.get('username')
password = config.get('password')

if not sensorList:
    logging.error ('No sensor defined in .sensors')
    exit()

    
    
# Begin main application
logging.info ('Setting meteor service')
global client
client = MeteorClient(host)
logging.info ('Connecting to service')
client.connect()
client.on('connected', onConnected)
client.on('reconnected', onConnected)

try:
    logging.info ('Setting GPIO and start listening to sensors')
    GPIO.setmode(GPIO.BCM)
    for sensor in filter(getInterruptingOnly,sensorList):
        registerSensorCallback(sensor)

    while True:
        for sensor in filter(getTemperatureOnly, sensorList):
            readTemp(sensor)
        time.sleep(pollingTime)
Esempio n. 27
0
import time

import yaml
from MeteorClient import MeteorClient
import numpy as np
import scipy as sp
import os
import json
import importlib

from fusedwind.core.problem_builder import FUSEDProblem


client = MeteorClient('ws://127.0.0.1:3000/websocket')

def callback_function(error, result):
    if error:
        print(error)
        return
    print(result)

def subscribed(subscription):
    print('* SUBSCRIBED {}'.format(subscription))


def unsubscribed(subscription):
    print('* UNSUBSCRIBED {}'.format(subscription))

problems = {}

Esempio n. 28
0
    def __init__(self):
        Thread.__init__(self)
        self.meteor = conf.get('ddp', 'meteor')

        self.client = MeteorClient(self.meteor, debug=False)
        self.client.on('added', self.on_added)
        self.client.on('changed', self.on_changed)
        self.client.on('subscribed', self.on_subscribed)
        self.client.on('connected', self.on_connected)
        self.client.on('removed', self.on_removed)
        self.client.on('closed', self.on_closed)
        self.client.on('logged_in', self.on_logged_in)

        self.displayName = conf.get('sussexlogin', 'room_name')
        self.vu_min = -70
        self.vu_range = 40
        self.do_vu = 0
        self.last_vu = None
        self.ip = socket.gethostbyname(socket.gethostname())
        self.id = conf.get('ingest', 'hostname')
        self._user = conf.get('ddp', 'user')
        self._password = conf.get('ddp', 'password')
        self._http_host = conf.get('ddp', 'http_host')
        self._audiostream_port = conf.get('audiostream', 'port') or 31337
        self.netreg_id = conf.get('ddp', 'netreg_id')
        self.store_audio = conf.get_boolean('ddp', 'store_audio')
        self.paused = False
        self.recording = False
        self.currentMediaPackage = None
        self.currentProfile = None
        self.has_disconnected = False

        cam_available = conf.get('sussexlogin',
                                 'cam_available') or cam_available
        if cam_available in ('True', 'true', True, '1', 1):
            self.cam_available = 1
        elif cam_available in ('False', 'false', False, '0', 0):
            self.cam_available = 0
        else:
            self.cam_available = int(cam_available)

        self.audiofaders = []
        faders = conf.get('ddp', 'audiofaders').split()
        for fader in faders:
            audiofader = {}
            fader = 'audiofader-' + fader
            audiofader['name'] = conf.get(fader, 'name')
            audiofader['display'] = conf.get(fader, 'display')
            audiofader['min'] = conf.get_int(fader, 'min')
            audiofader['max'] = conf.get_int(fader, 'max')
            audiofader['type'] = conf.get(fader, 'type')
            audiofader['setrec'] = conf.get_boolean(fader, 'setrec')
            audiofader['mute'] = conf.get_boolean(fader, 'mute')
            audiofader['unmute'] = conf.get_boolean(fader, 'unmute')
            audiofader['setlevel'] = conf.get_int(fader, 'setlevel')
            try:
                audiofader['control'] = alsaaudio.Mixer(
                    control=audiofader['name'])
                self.audiofaders.append(audiofader)
            except Exception as e:
                logger.warn(e)
        fd, eventmask = self.audiofaders[0]['control'].polldescriptors()[0]
        self.watchid = gobject.io_add_watch(fd, eventmask, self.mixer_changed)

        dispatcher.connect('galicaster-init', self.on_init)
        dispatcher.connect('update-rec-vumeter', self.vumeter)
        dispatcher.connect('galicaster-notify-timer-short', self.heartbeat)
        dispatcher.connect('start-before', self.on_start_recording)
        dispatcher.connect('restart-preview', self.on_stop_recording)
        dispatcher.connect('update-rec-status', self.on_rec_status_update)
Esempio n. 29
0
# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

from scrapy import signals
from scrapy.contrib.exporter import CsvItemExporter
from MeteorClient import MeteorClient
# try to reconnect every second
#client = MeteorClient('ws://127.0.0.1:3333/websocket', auto_reconnect=True, auto_reconnect_timeout=1)
client = MeteorClient('ws://127.0.0.1:3004/websocket', auto_reconnect=True, auto_reconnect_timeout=1)
client.connect()

def insert_callback(error, data):
    if error:
        print(error)
        return
    print(data)

class GameListingPipeline(object):

    def __init__(self):
        self.files = {}

    @classmethod
    def from_crawler(cls, crawler):
        pipeline = cls()
        crawler.signals.connect(pipeline.spider_opened, signals.spider_opened)
        crawler.signals.connect(pipeline.spider_closed, signals.spider_closed)
        print collection
        print fields

def changed(collection, id, fields, cleared):
    print ("changed:{}".format(id) + " fields: {}".format(fields))

def connected():
    print('* CONNECTED')
    client.login("test", "test")

def subscription_callback(error):
    if error:
        print(error)

try:
    client = MeteorClient('ws://rbmobility.emtmadrid.es:3333/websocket', auto_reconnect = True, auto_reconnect_timeout = 5, debug = False)
    client.on('subscribed', subscribed)
    client.on('unsubscribed', unsubscribed)
    client.on('added', added)
    client.on('connected', connected)
    client.on('changed',changed)   

    client.connect()

    # ctrl + c to kill the script

    while True:
        try:
            time.sleep(1)

        except KeyboardInterrupt:
Esempio n. 31
0
import time

from MeteorClient import MeteorClient

client = MeteorClient('ws://127.0.0.1:3000/websocket', debug=True)
client.connect()

client.subscribe('SessionsList')
name = time.strftime("%I:%M:%S")
time.sleep(1)

client.insert('Sessions', {'title': name})
time.sleep(1)

# confirm that a publication was started based on the session name passed in
client.subscribe(name)
time.sleep(1)
Esempio n. 32
0
class ConnectionManager():

    def __init__(self, server_url='ws://localhost:3000/websocket', worker=None):
        self.server_url = server_url
        self.client = MeteorClient(server_url)
        self.client.on('subscribed', self.subscribed)
        self.client.on('unsubscribed', self.unsubscribed)
        self.client.on('added', self.added)
        self.client.on('changed', self.changed)
        self.client.on('removed', self.removed)
        self.client.on('connected', self.connected)
        self.client.on('logged_in', self.logged_in)
        self.client.on('logged_out', self.logged_out)
        self.worker = worker
        self.connected = False
        self.ready = False

    def connect(self):
        self.client.connect()

    def connected(self):
        self.connected = True
        print('connected to ' + self.server_url)
        #self.client.login('test', '*****')
        if not 'workers.worker' in self.client.subscriptions:
            self.client.subscribe(
                'workers.worker', [self.worker.id, self.worker.token])

    def logged_in(self, data):
        self.userId = data['id']
        print('* LOGGED IN {}'.format(data))

    def subscribed(self, subscription):
        print('* SUBSCRIBED {}'.format(subscription))
        self.ready = True
        if subscription == 'workers.worker':
            if self.client.find_one('workers', selector={'_id': self.worker.id}):
                print('-----Worker {} found-----'.format(self.worker.id))
                if not 'widgets.worker' in self.client.subscriptions:
                    self.client.subscribe(
                        'widgets.worker', [self.worker.id, self.worker.token])
            else:
                raise Exception('Failed to find the worker with id:{} token{}'.format(
                    self.worker.id, self.worker.token))

        if subscription == 'widgets.worker':
            print('widgets of this worker SUBSCRIBED-')

        elif subscription == 'tasks.worker':
            print('* tasks of this worker SUBSCRIBED-')

    def added(self, collection, id, fields):
        print('* ADDED {} {}'.format(collection, id))
        # for key, value in fields.items():
        #    print('  - FIELD {} {}'.format(key, value))
        if collection == 'tasks':
            if not self.worker.workTasks.has_key(id):
                if fields.has_key('worker') and fields['worker'] == self.worker.id:
                    taskDoc = self.client.find_one('tasks', selector={'_id': id})
                    widget = self.worker.get_registered_widget(taskDoc['widgetId'])
                    if widget:
                        task = Task(taskDoc, self.worker, self.client)
                        if task and task.id:
                            self.worker.add_task(task)
                        else:
                            # remove task if widget is not registered
                            self.client.call('tasks.update.worker', [
                                                   id, self.worker.id, self.worker.token, {'$set': {'visible2worker': False}}])
                    else:
                        # remove task if widget is not registered
                        self.client.call('tasks.update.worker', [
                                               id, self.worker.id, self.worker.token, {'$set': {'visible2worker': False}}])

        elif collection == 'users':
            self.userName = fields['username']
        elif collection == 'widgets':
            # widget = fields#self.client.find_one('widgets', selector={'name':
            widget_ = Widget(self.client.find_one(
                'widgets', selector={'_id': id}), self.worker, self.client)
            if widget_.id:
                self.worker.register_widget(widget_)
                if not 'tasks.worker' in self.client.subscriptions:
                    self.client.subscribe(
                        'tasks.worker', [self.worker.id, self.worker.token])

    def changed(self, collection, id, fields, cleared):
        #print('* CHANGED {} {}'.format(collection, id))
        # for key, value in fields.items():
        #    print('  - FIELD {} {}'.format(key, value))
        # for key, value in cleared.items():
        #    print('  - CLEARED {} {}'.format(key, value))
        if collection == 'tasks':
            if self.worker.workTasks.has_key(id):
                task = self.worker.workTasks[id]
                for key, value in fields.items():
                    if key == 'cmd':
                        self.worker.execute_task_cmd(task, key, value)
                    elif key == 'worker':
                        self.worker.task_worker_changed(task, key, value)

                    if task.processor.changeCallbackDict.has_key(key):
                        for changeCallback in task.processor.changeCallbackDict[key]:
                            try:
                                changeCallback(task, key, value)
                            except Exception as e:
                                traceback.print_exc()
                                task.set('status.error', traceback.format_exc())

                for key, value in cleared.items():
                    if key == 'cmd':
                        self.worker.execute_task_cmd(task, key, value)
                    elif key == 'worker':
                        self.worker.task_worker_changed(task, key, value)

                    if task.processor.changeCallbackDict.has_key(key):
                        for changeCallback in task.processor.changeCallbackDict[key]:
                            try:
                                changeCallback(task, key, value)
                            except Exception as e:
                                traceback.print_exc()
                                task.set('status.error', traceback.format_exc())

            else:
                if fields.has_key('worker') and fields['worker'] == self.worker.id:
                    self.worker.add_task(id)

                #print('task is not in worktask list: ' + id)
        if collection == 'widgets':
            widget_ = Widget(self.client.find_one(
                'widgets', selector={'_id': id}), self.worker, self.client)
            if widget_.id:
                self.worker.register_widget(widget_)

            if fields.has_key('workers'):
                if fields['workers'].has_key(self.worker.id):
                    #print('worker config changed')
                    worker = fields['workers'][self.worker.id]
                    if worker.has_key('cmd'):
                        self.worker.execute_worker_cmd(worker['cmd'])

    def removed(self, collection, id):
        print('* REMOVED {} {}'.format(collection, id))
        if collection == 'tasks':
            if self.worker.workTasks.has_key(id):
                task = self.worker.workTasks[id]
                self.worker.remove_task(task)
                for cb in task.processor.removeCallbackList:
                    cb(task)

    def unsubscribed(self, subscription):
        print('* UNSUBSCRIBED {}'.format(subscription))

    def logged_out():
        self.userId = None
        print('* LOGGED OUT')

    def subscription_callback(self, error):
        if error:
            print(error)

    def run(self):
        # (sort of) hacky way to keep the client alive
        # ctrl + c to kill the script
        try:
            while True:
                time.sleep(1)
        except:
            traceback.print_exc()
        finally:
            self.stop()
            print('server exited')

    def stop(self):
        try:
            for task in self.worker.workTasks:
                if task.processor:
                    task.processor.stop()
        except Exception as e:
            pass
        self.worker['status'] = 'stopped'
        for subscription in self.client.subscriptions.copy():
            self.client.unsubscribe(subscription)
            print('start upload')
        opts = {
            "file": {"name": fname, "type": fileType, "size": fileSize, "meta": meta},
            "fileId": self.fileId,
            "chunkSize": chunkSize,
            "fileLength": 1 if chunkCount <= 0 else chunkCount,
        }
        self.finished = False
        self.error = False
        returnMeta = self.transport == 'http'
        self.client.call(self.methodNames['_Start'], [
                         opts, returnMeta], self._upload_start_callback)
        return self.fileId

if __name__ == '__main__':
    client = MeteorClient('ws://127.0.0.1:3000/websocket')
    client.connect()

    # upload example, work with Meteor-Files example: demo-simplest-upload
    # server code: https://github.com/VeliovGroup/Meteor-Files/tree/master/demo-simplest-upload
    client.subscribe('files');
    uploader = Uploader(client, 'files', transport='ddp', verbose=True)

    #import time
    #t0 = time.time()

    uploader.upload("test.jpeg")
    while not uploader.finished:
        time.sleep(0.1)

    #t1 = time.time()
Esempio n. 34
0
                "meta": meta
            },
            "fileId": self.fileId,
            "chunkSize": chunkSize,
            "fileLength": 1 if chunkCount <= 0 else chunkCount,
        }
        self.finished = False
        self.error = False
        returnMeta = self.transport == 'http'
        self.client.call(self.methodNames['_Start'], [opts, returnMeta],
                         self._upload_start_callback)
        return self.fileId


if __name__ == '__main__':
    client = MeteorClient('ws://127.0.0.1:3000/websocket')
    client.connect()

    # upload example, work with Meteor-Files example: demo-simplest-upload
    # server code: https://github.com/VeliovGroup/Meteor-Files/tree/master/demo-simplest-upload
    client.subscribe('files')
    uploader = Uploader(client, 'files', transport='ddp', verbose=True)

    #import time
    #t0 = time.time()

    uploader.upload("test.jpeg")
    while not uploader.finished:
        time.sleep(0.1)

    #t1 = time.time()
Esempio n. 35
0
             configuration.LED_B_PINS[0], connection)
for i in range(1, len(configuration.LED_R_PINS)):
    led.addLED(configuration.LED_R_PINS[i], configuration.LED_G_PINS[i],
               configuration.LED_B_PINS[i])

led.setColor(0, 0, 255)

arduino = ArduinoApi(connection=connection)
arduino.pinMode(configuration.PUMP_PIN, arduino.OUTPUT)
for pin in configuration.VALVE_PINS:
    arduino.pinMode(pin, arduino.OUTPUT)

scale = Hx711(configuration.LOAD_CELL_DOUT_PIN,
              configuration.LOAD_CELL_SCK_PIN, connection)

client = MeteorClient(configuration.SERVER)


def write_log(type, message):
    client.call('log.insert', [type, message])


def update_callback(error, data):
    if error:
        write_log('error', 'Error on update: {}'.format(error))


def insert_callback(error, data):
    if error:
        write_log('error', 'Error on insert: {}'.format(error))
Esempio n. 36
0
class DisplayMinion(App):
    action_map = {
        'media': MediaAction,
        'playlist': PlaylistAction,
        'song': SongAction,
        'presentation': PresentationAction,
        'presentationslide': PresentationAction,
        'timer': TimerAction,
        'camera': GStreamerAction,
        'clear-layer': Action
    }
    
    def __init__(self, **kwargs):
        self._id = None
        self.server = None
        self.ready = False

        self.state = 'disconnected' # 'disconnected' => 'connecting' => 'connected' => 'registering' => 'registered'
        self.binds = {}
        
        self.layers = {}

        self.sections = []
        self.last_blocks = None
        
        self.fullscreen = False
        
        self.defaults = json.load(open('common/default_settings.json'))
        
        super(DisplayMinion, self).__init__(**kwargs)

    def debug(self, *args):
        print(*args)
#        if self.config.get('debug'):
#            print(*args)

    def bind(self, event, function):
        if self.binds.get(event):
            self.binds[event].append(function)
            
        else:
            self.binds[event] = [function]
            
    def trigger_event(self, event):
        event_data = {'event': event, 'client': self}

        for function in self.binds.get(event, []):
            function(event_data)

    def connect(self, server):
        self.server = server
        
        self.meteor = MeteorClient('ws://{}/websocket'.format(self.server))
        self.meteor.on('connected', self.connected)
        self.meteor.connect()

        self.state = 'connecting'
        self.trigger_event('connecting')

    def connected(self):
        self.state = 'connected'
        self.trigger_event('connected')

        self.debug('Connected to server')

        self.time = MeteorTime(self.meteor)
        Clock.schedule_interval(self.time.update, 0.5)

        self.collections = 0
        self.collections_ready = 0

        for collection in ['settings', 'stages', 'minions',
                           'media', 'mediaplaylists',
                           'songs', 'songarrangements', 'songsections',
                           'presentations', 'presentationslides']:

            self.collections += 1
            self.meteor.subscribe(collection, callback=self.subscription_ready)

    def subscription_ready(self, err):
        if err: self.debug(err)
        self.collections_ready += 1

        if self.collections_ready >= self.collections:
            self.trigger_event('loaded')
            self.debug('All subscriptions ready')

    def register(self, _id):
        self._id = _id
        self.meteor.call('minionConnect', [_id], self.prep)

        self.state = 'registering'
        self.trigger_event('registering')
        
    def prep(self, e, r):
        self.state = 'registered'
        self.trigger_event('registered')
    
        self.debug('Registered')
        
        self.meteor.on('added', self.added)
        self.meteor.on('changed', self.changed)
        
        self.minion = self.meteor.find_one('minions', selector = {'_id': self._id});
        self.stage = self.meteor.find_one('stages', selector = {'_id': self.minion['stage']})

        self.update_minion_settings(self.minion)

        Clock.create_trigger(self.update_layers)()
        Clock.schedule_once(self.update_minion_blocks, 0)
        
        self.ready = True
            
    def added(self, collection, _id, fields):
        self.changed(collection, _id, fields, None)
        
    def changed(self, collection, _id, fields, cleared):
        if not self.ready: return
        
        if collection == 'minions' and _id == self._id:
            self.minion = self.meteor.find_one('minions', selector = {'_id': self._id});
            self.update_minion_settings(self.minion)
        
        if collection == 'stages' and _id == self.minion['stage']:
            self.stage = self.meteor.find_one('stages', selector = {'_id': self.minion['stage']})
            Clock.create_trigger(self.update_layers)()
            
    def update_minion_settings(self, minion):
        if not minion['settings']['blocks'] == self.last_blocks:
            self.last_blocks = minion['settings']['blocks']
            Clock.schedule_once(self.update_minion_blocks, 0)
        
        if not minion['settings'].get('mediaminion_width', 0) == self.source.disp_size[0] or \
           not minion['settings'].get('mediaminion_height', 0) == self.source.disp_size[1]:
            self.source.disp_size = (int(minion['settings'].get('mediaminion_width') or 0), int(minion['settings'].get('mediaminion_height') or 0))
            self.source.resize()
        
    def update_minion_blocks(self, dt):
        # Note: Sections were originally named "blocks", so far I've been too lazy to rewrite all the cedarserver code to reflect the new name. -IHS
        start_length = len(self.sections)
        block_delta = len(self.minion['settings']['blocks']) - start_length

        if block_delta > 0:
            for n in range(block_delta):
                config = self.minion['settings']['blocks'][start_length + n]
                
                section = Section(
                    source = self.source,
                    block = config,
                    client = self
                )
                
                self.layout.add_widget(section)
                self.sections.append(section)
                
        elif block_delta < 0:
            for n in range(abs(block_delta)):
                section = self.sections.pop()
                self.layout.remove_widget(section)
        
        for index, section in enumerate(self.sections):
            config = self.minion['settings']['blocks'][index]
            if not section.block == config: # TODO add brightness etc.
                section.block = config
                section.recalc()
    
    def update_layers(self, dt = None):
        layers = self.stage.get('layers', [])

        for layer, action in layers.items():
            if not layer in self.minion['layers']: continue
        
            if action and self.layers.get(layer):
                # Test if new action is the same as the current one
                current = self.layers.get(layer).action

                if action['_id'] == current['_id']:
                    if action['type'] == 'song':
                        if action.get('args') and current.get('args') and \
                           action['args']['section'] == current['args']['section'] and \
                           action['args']['index'] == current['args']['index']:
                            continue
                    
                    elif action['type'] == 'presentation':
                        if action.get('args') and current.get('args') and \
                           action['args']['order'] == current['args']['order'] and \
                           action['args']['fillin'] == current['args']['fillin']:
                            continue
                            
                    else: continue
            
            if action and self.action_map.get(action['type']):
                self.layers[layer] = self.action_map[action['type']](action, self.layers.get(layer) or None, self)

                self.layers[layer].show()
                
            elif action == None and self.layers.get(layer):
                self.layers[layer].hide()
                self.layers[layer].remove()
                self.layers[layer] = None
            
                
    def get_layer_index(self, target_layer):
        layers = self.stage['settings']['layers']
        layer_index = layers.index(target_layer)

        higher_layers = layers[layer_index + 1:]
        widget_index = 0

        for layer in higher_layers:
            higher_action = self.layers.get(layer)
 
            if higher_action:
                higher_index = higher_action.get_current_widget_index()
                
                if not higher_index == None:
                    widget_index = higher_index + 1
                    break
                
        return widget_index
        

    def add_layer_widget(self, new_widget, layer):
        # TODO switch to this behavior once https://github.com/kivy/kivy/issues/4293 is resolved
        # self.source.add_widget(widget, index = self.get_layer_index(layer))
        
        widgets = self.source.children[:]
        new_index = self.get_layer_index(layer)
        
        for widget in widgets:
            self.source.remove_widget(widget)
        
        widgets.insert(new_index, new_widget)
        widgets.reverse()
        
        for widget in widgets:
            self.source.add_widget(widget)

    
    def remove_widget(self, widget):
        self.source.remove_widget(widget)

    def get_application_config(self):
        return super(DisplayMinion, self).get_application_config('~/.%(appname)s.ini')

    def build_config(self, config):
        config.setdefaults('connection', {
            'server': 'localhost:3000',
            '_id': '',
            'autoconnect': 'no',
        })
        
        config.setdefaults('window', {
            'fullscreen': 'no'
        })
        
        config.setdefaults('outputs', {
            'shmsink': 'no'
        })
   
    def toggle_fullscreen(self, thing, touch):        
        if not self.ui.layout.collide_point(*touch.pos):
            if self.fullscreen: Window.fullscreen = 0
            else: Window.fullscreen = 'auto'
            self.fullscreen = not self.fullscreen
    
    def on_stop(self):
        self.source.stop()
        
    def build(self):
        self.title = 'Cedar Display Client'
        
        if self.config.get('window', 'fullscreen') == 'yes':
            Window.fullscreen = 'auto'
        
        if kivy.utils.platform is 'windows':
            self.icon = 'logo/logo-128x128.png'
        else:
            self.icon = 'logo/logo-1024x1024.png'

        self.source = DisplaySource(self, pos_hint = {'x': 1, 'y': 1}, size_hint = [None, None])
        self.source.bind(on_touch_down = self.toggle_fullscreen)

        self.layout = FloatLayout()
        self.layout.add_widget(self.source)
                
        self.ui = UserInterface(self)
        
        return self.layout
Esempio n. 37
0
#!/usr/bin/env python

""" Check to see if an process is running. If not, restart. Run this in a cron job """
import os
import sys 
import time
import fcntl, socket, struct
from MeteorClient import MeteorClient
# client = MeteorClient('ws://127.0.0.1:3030/websocket')
client = MeteorClient('ws://127.0.0.1:3000/websocket')
process_name= "/root/smartag-python.py" # change this to the name of your process
#process_name= "meteor_run.sh"
# process_name= "/home/atanas/MeteorProjects/iot/serverstate.py"

def getHwAddr(ifname):
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    return ':'.join(['%02x' % ord(char) for char in info[18:24]])
print getHwAddr('eth0')	
    # new part
newmac = getHwAddr('eth0')
client.connect()


tmp = os.popen("ps -Af").read()
print tmp[:]


if process_name in tmp[:]:
    print "LOCAL SERVER RUNNING"
    print process_name
Esempio n. 38
0
import time

from MeteorClient import MeteorClient

client = MeteorClient('ws://127.0.0.1:3000/websocket', debug=True)
client.connect()

client.subscribe('SessionsList')

time.sleep(1)

sessions = client.find('sessions')
print(sessions)

time.sleep(1)
    def serve_once(self):
        """
        Run the bot until the connection is disconnected.

        :return: None.
        """
        # Log message
        self._log_debug('# ----- serve_once -----')

        # Log message
        self._log_debug(('# ----- Create meteor client -----\n'
                         'SERVER_URI: {}').format(self._server_uri))

        # Create meteor client
        self._meteor_client = MeteorClient(
            self._server_uri,
            # Disable the meteor client's auto reconnect.
            # Let `serve_forever` handle reconnect.
            auto_reconnect=False,
        )

        # Log message
        self._log_debug('# ----- Hook meteor client callbacks -----')

        # 5DI82
        # Hook meteor client `connected` callback
        self._meteor_client.on('connected', self._meteor_connected_callback)

        # 2RAYF
        # Hook meteor client `changed` callback
        self._meteor_client.on('changed', self._meteor_changed_callback)

        # 4XIZB
        # Hook meteor client `added` callback
        self._meteor_client.on('added', self._meteor_added_callback)

        # 2JEIK
        # Hook meteor client `removed` callback
        self._meteor_client.on('removed', self._meteor_removed_callback)

        # 32TF2
        # Hook meteor client `failed` callback
        self._meteor_client.on('failed', self._meteor_failed_callback)

        # 5W6RX
        # Hook meteor client `reconnected` callback
        self._meteor_client.on('reconnected',
                               self._meteor_reconnected_callback)

        # 7MOJX
        # Hook meteor client `closed` callback
        self._meteor_client.on('closed', self._meteor_closed_callback)

        # Clear the event
        self._subscribing_done_event.clear()

        # Clear the event
        self._meteor_closed_event.clear()

        # Log message
        self._log_debug('# ----- Connect to meteor server -----')

        try:
            # Connect to meteor server.
            #
            # If the connecting succeeds, the meteor client's thread will call
            # `self._meteor_connected_callback` hooked at 5DI82. The login,
            # topic subscribing, and message handling are run in that thread.
            #
            # The main thread merely waits until the meteor client is closed,
            # meanwhile it calls heartbeat function at interval if specified.
            #
            self._meteor_client.connect()

        # If have error
        except:
            # Log message
            self._log_debug('# ----- Connecting failed -----')

            # Log message
            self._log_debug('# ----- Unhook meteor client callbacks -----')

            # Remove meteor client callbacks
            self._meteor_client.remove_all_listeners()

            # Remove meteor client reference
            self._meteor_client = None

            # The two events below should not have been set if the connecting
            # failed. Just in case.
            #
            # Clear the event
            self._subscribing_done_event.clear()

            # Clear the event
            self._meteor_closed_event.clear()

            # Raise the error
            raise

        # Get whether heartbeat is enabled
        heartbeat_enabled = self._get_bool_config(
            CONFIG_KEYS.HEARTBEAT_ENABLED)

        try:
            # Wait until the topic subscribing is done in another thread at
            # 5MS7A
            self._subscribing_done_event.wait()

            # If heartbeat is enabled
            if heartbeat_enabled:
                # Get heartbeat function
                heartbeat_func = self._get_config(CONFIG_KEYS.HEARTBEAT_FUNC)

                # Assert the function is callable
                assert callable(heartbeat_func), repr(heartbeat_func)

                # Get heartbeat interval
                heartbeat_interval = self._get_config(
                    CONFIG_KEYS.HEARTBEAT_INTERVAL,
                    default=10,
                )

                # Convert heartbeat interval to float
                heartbeat_interval = float(heartbeat_interval)

                # 65ZNO
                # Loop until the meteor client is disconnected
                while self._meteor_client.connected:
                    # Send heartbeat
                    heartbeat_func(self)

                    # Sleep before sending next heartbeat
                    time.sleep(heartbeat_interval)

            # 5W6XQ
            # Wait until the meteor client is closed and the `closed` callback
            # is called at 3DMYH
            self._meteor_closed_event.wait()

        # If have error
        except:
            # Close meteor client.
            #
            # This will cause `self._meteor_closed_callback` to be called,
            # which will set the `self._meteor_closed_event` below.
            #
            self._meteor_client.close()

            # See 5W6XQ
            self._meteor_closed_event.wait()

            # Raise the error
            raise

        # Always do
        finally:
            # Log message
            self._log_debug('# ----- Unhook meteor client callbacks -----')

            # Remove meteor client callbacks
            self._meteor_client.remove_all_listeners()

            # Remove meteor client reference.
            #
            # Do this before calling `callback_presence` below so that the
            # plugins will not be able to access the already closed client.
            #
            self._meteor_client = None

            # Log message
            self._log_debug('# ----- Call `callback_presence` -----')

            # Call `callback_presence`
            self.callback_presence(
                Presence(identifier=self.bot_identifier, status=OFFLINE))

            # Log message
            self._log_debug('# ----- Call `disconnect_callback` -----')

            # Call `disconnect_callback` to unload plugins
            self.disconnect_callback()

            # Clear the event
            self._subscribing_done_event.clear()

            # Clear the event
            self._meteor_closed_event.clear()

        # Log message
        self._log_debug('# ===== serve_once =====')
Esempio n. 40
0
class MWorker:
    multiprocessPluginManager = ""  # yapsy manager
    meteorClient = ""  # python-meteor client
    # session=requests.Session()               #http client
    meteorUrl = "ws://127.0.0.1:3000/websocket"
    CYCLE_DEFAULT_DELAY = 20  # 10 seconds
    SYNCHRO_TIMEOUT = 10  # meteor functions are asynchron. this is delay for how long i should wait till
    # it is considered as fail... 2 sec are default
    subscription_status = {}  # this will assure, subscription is ready...    "subscription_name":true/false
    workername = "localhostworker"  # this is worker name - this must be initialised at moment of start, and
    # must by uniqued...

    data = ""
    error = ""
    syncwaitdone = False

    def __init__(self, meteorUrl):
        """
        constructor
        :return:
        """

        self.meteorUrl = meteorUrl
        self.logm("MWorker:constructor", "python meteor client initialisation:")
        self.initMeteorConnect()
        self.logm("MWorker:constructor", "getting unassigned")
        # self.infiniteCycle()
        self.getUnassigned()

    def infiniteCycle(self):
        """
        this is to keep thread running - it can be interupted by ctrl+c


        :return:
        """
        while True:
            try:
                time.sleep(1)
            except KeyboardInterrupt:
                break

    def initMeteorConnect(self):
        """
        this will use library python-meteor in order to escablish session to selected meteor

        :return:
        """
        self.logm("MWorkerLib:initMeteorConnect:", "meteor python lib client iniciaisation...")
        self.meteorClient = MeteorClient('ws://127.0.0.1:3000/websocket', auto_reconnect=True)
        self.meteorClient.connect()
        self.subscribeCollectionSynchron(['unassigned', 'active', 'trash'], self.SYNCHRO_TIMEOUT)

        self.logm("MWorkerLib:initMeteorConnect:", "meteor python lib client iniciaisation done...")

    def meteorCollectionSubscribed(self, subscription):
        self.subscription_status[subscription] = True;
        self.logm("MWorkerLib:subscribed:", 'SUBSCRIBED {}'.format(subscription))

    def meteorConnected(self):
        self.logm("MWorkerLib:connected:", ' CONNECTED')

    def initYapsy(self):
        """
        inicialisation of yapsi subsystems...
        :return:
        """
        self.multiprocessPluginManager = MultiprocessPluginManager()
        self.multiprocessPluginManager.setPluginPlaces(["plugins"])
        self.multiprocessPluginManager.collectPlugins()
        self.logm("MWorkerLib:initYapsy:", "following pluggins ready")
        for pluginInfo in self.multiprocessPluginManager.getAllPlugins():
            self.logm("MWorkerLib:initYapsy:", ">" + pluginInfo.name)

    def isSubscriptionProcessDone(self):
        """
        this will check, if in self.subscription_status   all collections are set to true
        :return: true if all requested collections are subscribed, orthervice false..
        """
        kk = self.subscription_status.keys()

        for col in kk:
            if self.subscription_status[col] == False:
                self.logm("MWorkerLib:isSubscriptionProcessDone:", self.subscription_status)
                return False
        return True

    def subscribeCollectionSynchron(self, collections, timeout):
        """
        this is synchron method. this means, there is loop for "timeout" seconds,
        so subscription can be estalished...
        :param: timeout  - number of seconds for cycle this function will have
        :param: collections   - array of collections to be subscribed with self.meteorClient...
                                ['unassigned', 'active', 'trash']
        :return: will return true, or collection if some is false
        """
        self.logm("MWorkerLib:subscribeCollectionsynchron:", "begining with subscription")
        self.meteorClient.on('subscribed', self.meteorCollectionSubscribed)

        self.meteorClient.on('connected', self.meteorConnected)

        for col in collections:
            self.subscription_status[col] = False
            self.meteorClient.subscribe(col)
        self.logm("MWorkerLib:subscribeCollectionSynchron",
                  "subscription init done, now waiting...:" + str(self.meteorClient.subscriptions.keys()))

        epoch = int(time.time())
        while (((int(time.time()) - epoch) < timeout) and (
                (self.isSubscriptionProcessDone() == False))):
            try:
                time.sleep(1)
                self.logm("MWorkerLib:subscribeCollectionSynchron",
                          "inside waiting cycle :" + str(self.meteorClient.subscriptions.keys()))
            except KeyboardInterrupt:
                break
        if (self.isSubscriptionProcessDone() == False):
            self.logm("MWorkerLib:subscribeCollectionSynchron",
                      "some requested subscription failed...:" + str(self.subscription_status))
            return False
        else:
            self.logm("MWorkerLib:subscribeCollectionSynchron", "requested subscription successfuly subscribed...")
            return True

    def getUnassigned(self):
        """
        get list of unassigned tasks
        :return: list of unassigned tasks
        """

        self.logm("MWorkerLib:getUnassigned", "geting all unassigned")
        all_posts = self.meteorClient.find('unassigned')

        self.logm("MWorkerLib:getUnassigned", "found following unassigned:" + str(all_posts))

    def subscription_callback(self, error):
        if error:
            self.logm("MWorkerLib:subscription_callback", "subsribing to unassigned failed")
            return
        self.logm("MWorkerLib:subscription_callback", "subsribing to unassigned succeed")

    def logm(self, tag="undefined tag", text="undefined text"):
        """
         logger wrapper ....
        :param tag:  tag
        :param level:   level ( 1..10 )
        :param text:   dscription of logging
        :return:
        """
        timestamp = self.getTimestamp()
        print("{d} t:{a}   t:{c}   ".format(a=tag, c=text, d=timestamp))

    ####################################### support methods
    def getTimestamp(self):
        ts = time.time()
        return datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')

    def callViewToolkit(self, method, parameters, callback):
        """
        this will execute call...
        please note, for simple geting collection data, no need to create method...


        :param method:    name of function
        :param parameters: what to give to viewtoolkit
        :callback  -  as all meteor calls are async, callback is required
            def callback_function(error, result):
                if error:
                    print(error)
                    return
                print(result)


        :return:
        """
        self.logm("MWorkerLib:callViewToolkit", "method:" + method + " with parameters:" + parameters + " called")
        self.meteorClient.call(method, parameters, callback)

    def isPluginActiveInPluginManager(self, pluginname):
        """
        this is to check if plugin which is parameter exist in worker, and if yes, if it is activated...
        :param pluginname:
        :return: true in case plugin is activated
        """

        for pluginInfo in self.multiprocessPluginManager.getAllPlugins():
            self.logm("MWorkerLib:isPlugginActiveInPluginManager:",
                      "checking plugin " + pluginInfo.name + "against " + pluginname)
            if (pluginname == pluginInfo.name):
                self.logm("MWorkerLib:isPlugginActiveInPluginManager:",
                          "plugin was found, let see if it is activated " + str(pluginInfo.is_activated))
                if pluginInfo.is_activated:
                    self.logm("MWorkerLib:isPlugginActiveInPluginManager:",
                              "yes, plugin exist and was activated. returning true")
                    return True
        self.logm("MWorkerLib:isPlugginActiveInPluginManager:",
                  "could not found plugin you uare asking for, or it is not activated, returning False")
        return False

    def activatePlugin(self, pluginname):
        """
        plugin activation itself
        :param pluginname:
        :return:
        """
        self.logm("MWorkerLib:isPlugginActiveInPluginManager:activatePlugin",
                  "plugin {a} will be activated now...".format(a=pluginname))
        self.multiprocessPluginManager.activatePluginByName(pluginname)
        self.logm("MWorkerLib:isPlugginActiveInPluginManager:activatePlugin",
                  "plugin {a} was activated...".format(a=pluginname))

    def moveJobFromActiveToTrash(self,jobname):
        """
        will move job by its name  from active to trash
        :param jobname:
        :return:
        """
        #kontrola, zda trash ma v sobe vytvoreneho workera:
        if(self.isWorkerInTrash()==False):
            self.createWorkerRecordInTrash()

        job=self.getJobInActiveByName(jobname)
        if (job==None):
            return False
        if self.createJobInTrashSynchronised(job):
            self.deleteJobFromActiveSynchronised(jobname)

    def deleteJobFromActiveSynchronised(self,jobname):
        """
        delete job from active
        :param jobname:
        :return:
        """
        result = False
        self.syncwaitdone = False  # zacatek locku
        self.logm("MWorkerLib:deleteJobFromActiveSynchronised",
                  "worker:" + self.workername + " is going to remove job from active " + str(jobname))
        self.meteorClient.remove( 'active',{'_id':self.workername,'jobs._id':jobname}, callback=self.callback_synchro)
        res = self.syncWait()  # konec locku
        self.logm("MWorkerLib:deleteJobFromActiveSynchronised",
                  "worker:" + self.workername + " removed " + str(jobname)+ "result is "+str(res))
        return res




    def createJobInTrashSynchronised(self,job):
        """
        this will create job in trash collection under worker name...

        :param job:
        :return:
        """

        #inseritng job
        self.logm("MWorkerLib:createJobInTrash",
                  "worker:" + self.workername + " is going to insert to trash following: " + str(job))
        self.syncwaitdone = False  # zacatek locku
        self.meteorClient.update('trash', {'_id': self.workername}, {'$push': {"jobs":job}},
                                 callback=self.callback_synchro)
        res = self.syncWait()  # konec locku
        return res

    def     getJobInActiveByName(self,jobname):
        """
        this will get job back
        :param jobname:  name of job in active ...
        :return:  return job, or None
        """
        res = self.meteorClient.find_one('active',{'_id':self.workername,'jobs.jobname':jobname})
        if res==[]:
            return None
        return res

    def moveJobFromUnassignedToActiveSynchronised(self, jobname):
        """
        this will shift job from unassigned to active... based on job name, which must be unique in hwhile system
        :param jobname:
        :return:
        """
        #kontrola, zda existuje worker v active, a kdyz ne, tak se vlozi podle template
        if(self.isWorkerInActive()==False):
            self.logm("MWorkerLib:moveJobFromUnassignedToActiveSynchronised",
                  " dont see record for worker {w} in active !! creating it ".format(w=self.workername))
            self.createWorkerRecordInActive()


        resulttmp = self.findJobinUnassigned(jobname)
        if(len(resulttmp)!=1):
            raise NoJobFound(" given job name "+jobname+" doesnt exist")
        job=resulttmp[0]
        self.logm("MWorkerLib:moveJobFromUnassignedToActiveSynchronised",
                  " job  {a} will be activated now...".format(a=str(job)))
        if (self.createMyJobInActiveSynchronised(job)):
            if(self.deleteJobFromUnassignedSynchronised(job)):
                self.logm("MWorkerLib:moveJobFromUnassignedToActiveSynchronised",
                  " job inserting to db is ok, job deleted from unassigned ")
                return True
        self.logm("MWorkerLib:moveJobFromUnassignedToActiveSynchronised",
                  "job inserting to db failed")
        return False


    def isWorkerInTrash(self):
        """
        will check is self.workername can be found in trash
        :return: true if it can be found, false if not
        """
        result = self.meteorClient.find('trash',{"_id":self.workername})
        self.logm("MWorkerLib:isWorkerInTrash",
                  "checking, if i see my worker name in trash "+str(result))
        if len(result)==1:
            self.logm("MWorkerLib:isWorkerInTrash",
                  " worker with my name found in trash, erturning true")
            return True
        else:
            self.logm("MWorkerLib:isWorkerInTrash",
                  "found nothing, returning False")
            return False


    def isWorkerInActive(self):
        """
        will check is self.workername can be found in active
        :return: true if it can be found, false if not
        """
        result = self.meteorClient.find('active',{"_id":self.workername})
        self.logm("MWorkerLib:isWorkerInActive",
                  "checking, if i see my worker name in active "+str(result))
        if len(result)==1:
            self.logm("MWorkerLib:isWorkerInActive",
                  " worker with my name found in active, erturning true")
            return True
        else:
            self.logm("MWorkerLib:isWorkerInActive",
                  "found nothing, returning False")
            return False

    def createWorkerRecordInActive(self):
        """
        will create new worker - it will not be checking if there is already worker created in active...
        :return:
        """
        template={"_id":self.workername,"worker":self.workername,"workerusername":"******","jobs":[]}
        self.logm("MWorkerLib:createWorkerRecordInActive",
                  "worker:" + self.workername + " is going to by added to collection active ")
        self.syncwaitdone = False  # zacatek locku
        self.meteorClient.insert('active',   template,
                                 callback=self.callback_synchro)
        res = self.syncWait()  # konec locku
        return res


    def createWorkerRecordInTrash(self):
        """
        will create new worker - it will not be checking if there is already worker created in active...
        :return:
        """
        template={"_id":self.workername,"worker":self.workername,"workerusername":"******","jobs":[]}
        self.logm("MWorkerLib:createWorkerRecordInTrash",
                  "worker:" + self.workername + " is going to by added to collection trash ")
        self.syncwaitdone = False  # zacatek locku
        self.meteorClient.insert('trash',   template,
                                 callback=self.callback_synchro)
        res = self.syncWait()  # konec locku
        return res


    def findJobinUnassigned(self, jobname):
        """
        will return job from unassigned by jobname
        this is synchron method... it will wait for ( synchron_timeout ) seconds, and return false
        :param job:
        :return:  false on timeout ( synchron_timeout ) or if return is different from 1.
        """
        return self.meteorClient.find('unassigned', {"_id": jobname})

    def createMyJobInActiveSynchronised(self, job):
        """
        this will take as input dict of job... and add it to jobs[] in proper worker...
        :param job:  dict of job...
        :return: false on timeout ( synchron_timeout ) or if return is different from 1.
        """
        template = {
            'pluginname': job['pluginname'],
            'jobname': job['jobname'],
            
            'starttime': int(time.time()),  # epocha
            'ttl': job['expectedttl'],
            'inputparams': job['inputparams'],
            'progress': 0,
            'result': "undefined"
        }

        self.logm("MWorkerLib:createMyJobInActive",
                  "worker:" + self.workername + " is going to insert to active following: " + str(job))
        self.syncwaitdone = False  # zacatek locku
        self.meteorClient.update('active', {'_id': self.workername}, {'$push': {'jobs': template}},
                                 callback=self.callback_synchro)
        res = self.syncWait()  # konec locku
        return res

    def callback_synchro(self, error, data):

        self.data = data
        self.error = error
        if error:
            self.logm("MWorkerLib:update_callback_synchro",
                      "worker:" + self.workername + " failed error is " + str(error))
            self.syncwaitdone = True
            return

        self.logm("MWorkerLib:update_callback_synchro", "is ok. number of updates is " + str(data))
        self.syncwaitdone = True

    def syncWait(self):
        """
        this will wait till data of error are not empty.
        it will do for self.SYNCHRO_TIMEOUT seconds. then it will return False

        also it will check what is return value from request. if update is 0,
        false, othervice true
        """

        epoch = int(time.time())
        while (((int(time.time()) - epoch) < self.SYNCHRO_TIMEOUT) and (self.syncwaitdone == False)):
            try:
                time.sleep(1)
                self.logm("MWorkerLib:syncWait",
                          "inside waiting cycle :")
            except KeyboardInterrupt:
                break

        if (self.syncwaitdone == False):
            # cycle was broken before timeou
            return False

        try:
            # zkouska, zda se updatoval aspon jeden radek
            if (int(self.data) == 0):
                # mongo changed nothing, but there was no error
                return False
            else:
                return True
        except:
            pass

        # nothing found, cykluus end on timeout
        return False

    def deleteJobFromUnassignedSynchronised(self, job):
        """
        this will delete selected job from unassigned
        this is synchron method... it will wait for ( synchron_timeout ) seconds, and return false

        :param job:
        :return: false on timeout ( synchron_timeout ) or if return is different from 1.
        """
        self.syncwaitdone = False  # zacatek locku
        self.logm("MWorkerLib:createMyJobInActive",
                  "worker:" + self.workername + " is going to remove job from unassigned " + str(job))
        self.meteorClient.remove('unassigned', {'_id': job['_id']}, callback=self.callback_synchro)
        res = self.syncWait()  # konec locku
        self.logm("MWorkerLib:createMyJobInActive",
                  "worker:" + self.workername + " removed " + str(job)+ "result is "+str(res))
        return res
    #########################testing


    def testScenario1(self):
        """
        this is scenario 1...
        this will move job from unassigned to active, then from active to trash...
        also it will list unassigned tasks
        :return:



        self.logm("MWorkerLib:testScenario1", "start test scenario 1")
        self.logm("MWorkerLib:testScenario1", "moving from unassigned to active")
        all_posts = self.meteorClient.find('unassigned')
        #job = (all_posts).keys()

        #get first unassigned:
        firstUnassigned=self.meteorClient.find('unassigned')[0]
        ttltmp=100
        #pro testovaci ucely jsem vytvoril v active workera:
        # db.active.insert({"_id":"localhostworker","worker":"localhostworker","workerusername":"******","jobs":[]})

        template={
             'pluginname':firstUnassigned['pluginname'],
             'starttime':int(time.time()),    #epocha
             'ttl':ttltmp,
             'inputparams':firstUnassigned['inputparams'],
             'progress':0,
             'result':"undefined"
        }
        #insert to active:
        #musim pouzit id_ protoze klient rve ze kod je insecure a musi se pouzit id
        #s _id je dalsi problem. protoze interne mongodb pouziva objekt, ne stringu
        #tak bude vyhodnejsi nahradit pri vytvoreni workeru v active _id s nejakym retezcem  _
        worker=self.meteorClient.find_one('active',{"worker" : "localhostworker"})
        #worker["workerusername"]="******"
        self.logm("MWorkerLib:testScenario1", "worker:"+str(worker)+" "+worker['_id'])
        worker1=self.meteorClient.find_one('active',{"_id":worker["_id"]})

        self.logm("MWorkerLib:testScenario1", "worker:"+str(worker1)+" "+worker1['_id'])
        self.meteorClient.update('active',{'_id':worker['_id']},{'$push':{'jobs':template}},callback=update_callback)

        #self.infiniteCycle()
        self.logm("MWorkerLib:testScenario1", "deleting job from unassigned")
        self.meteorClient.remove('unassigned',{'_id':firstUnassigned['_id']},callback=remove_callback)




        time.sleep(5)
        """
        job = {'_id': "asdasdsad", 'jobname': "asdasd", 'pluginname': "asdasda", 'inputparams': "assad",
               'expectedttl': "100"}


        #otestovane a funkcni 11.2.2016:
        #tohle presune job s danym jmenem do active pod jmeno mistniho workera...
        #self.moveJobFromUnassignedToActiveSynchronised("blabla")

        self.moveJobFromActiveToTrash("blabla")
Esempio n. 41
0
class Client:
    def __init__(self, username, password, ui):
        self.username = username
        self.password = password
        self.ui = ui
        self.now = mktime(datetime.now().timetuple())*1e3
        self.resume_token = ''
        self.client = MeteorClient('wss://kwak.io/websocket')
        self.client.connect()
        self.client.login(self.username, self.password,
            token=self.resume_token, callback=self.logged_in)

        self.hot_channels = []
        self.hot_channels_name = []
        self.all_channels_name = []
        self.current_channel = 'dev'
        self.client.call('getHotChannels', [], self.set_hot_channels_name)
        self.client.call('channelList', [], self.set_all_channels_name)

        self.client.on('connected', self.connected)
        self.client.on('added', self.added)

    def set_hot_channels_name(self, error, result):
        if error:
            self.ui.chatbuffer_add(error)
            return
        self.hot_channels_name = result

    def set_all_channels_name(self, error, result):
        if error:
            self.ui.chatbuffer_add(error)
            return
        self.all_channels_name = result

    def subscribe_to_channel(self, channel):
        self.current_channel = channel
        try:
            self.client.unsubscribe('messages')
        except:
            pass
        self.ui.chatbuffer_add('* LISTENING TO CHANNEL {}'.format(channel))
        self.client.subscribe('messages', [self.current_channel])

    def subscribe_to_users(self, channel):
        self.client.subscribe('users', [channel])

    def added(self, collection, id, fields):
        # only add new messages, not backlog
        if collection == 'messages' and fields['time'] > self.now:
            # fields : channel | time | text | user
            self.ui.chatbuffer_add('{}\t{}: {}'.format(fields['time'], fields['user'], fields['text']))
        elif collection == 'users':
            # fields : username | profile | color
            if len(fields['profile']) > 0 and bool(fields['profile']['online']) == True:
                self.ui.userlist.append(fields['username'])
                self.ui.redraw_userlist()

    def connected(self):
        self.ui.chatbuffer_add('* CONNECTED')

    def logged_in(self, error, data):
        if error:
            self.ui.chatbuffer_add('LOGIN ERROR {}'.format(error))
        else:
            self.resume_token = data['token']
            self.client.call('setOnline', [])
            self.ui.chatbuffer_add('* LOGGED IN')
            
    def logout(self):
        self.ui.chatbuffer_add('* BYE (LOVELY DUCK)')
Esempio n. 42
0
REFINER1_ID = 'refiner1id'
REFINER2_ID = 'refiner12id'

OBJS = {
    # REFINER1_ID: {
    #     'sensor':
    #     'pin':
    # },
    # REFINER2_ID: {
    #     'sensor':
    #     'pin':
    # }
}

client = MeteorClient(URL)

# def hello():
#     print "hello, world"

# t = Timer(30.0, hello)
# t.start()


def temp(id):
    sensor = OBJS[id]['sensor']
    if sensor:
        t = sensor.get_temperature(W1ThermSensor.DEGREES_F)
        print 'Temp is: %.2f' % t
        return t
    else:
Esempio n. 43
0
import os

import time
from MeteorClient import MeteorClient

UNAME = os.environ.get('DICECLOUD_USER', '')
PWD = os.environ.get('DICECLOUD_PWD', '').encode()

client = MeteorClient('ws://dicecloud.com/websocket', debug=True)
client.connect()
print("Connected")
while not client.connected:
    time.sleep(0.1)
client.login(UNAME, PWD)
print("Logged in")

time.sleep(1)  # wait until users collection has updated

USER_ID = client.find_one('users', selector={'username': UNAME}).get('_id')
print("User ID: " + USER_ID)

char_id = 'Mtx98jb3c3wWcrWPj'


def main():
    # check_char()
    test_id()


def test_id():
    client.insert('characters', {
Esempio n. 44
0
 def __init__(self, debug=False):
     self.meteor_client = MeteorClient(SOCKET_BASE, debug=debug)
     self.http = DicecloudHTTP(API_BASE, API_KEY, debug=debug)
     self.logged_in = False
Esempio n. 45
0
import time

from MeteorClient import MeteorClient
from datetime import datetime, timedelta
from threading import Thread, Lock
from collections import namedtuple
from operator import attrgetter

USERNAME = '******'
PASSWORD = '******'
SERVER   = 'ws://152.228.80.179:3000/websocket'
client   = MeteorClient(SERVER)

# How do we handle changes that are close to the dispensing period?
# Keep a copy, then wipe the schedule. If the dosage is not scheduled for today... just add it
#   Otherwise, check if the time has already been passed... if it has skip it
#   If the time is close to the current time, check to see if it's already been/being dispensed
#       if it has, then skip it
#   Otherwise just add it...

class Scheduler:
    def __init__(self):
        # Initialize the schedule
        self.schedule = []
        # Start the dispensing task
        Thread(target = self.dispense).start()
    def refresh_schedule(self):
        threadLock.acquire()
        # In reality we shouldn't just naively wipe out the old schedule
        # like this
        self.schedule = []
Esempio n. 46
0
Giampaolo Rodola's nettop simulator here:
# https://github.com/giampaolo/psutil/blob/master/examples/nettop.py
# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""

import time
import sys
import psutil
from MeteorClient import MeteorClient

# DDP python client based on the example here: 
# https://github.com/hharnisc/python-meteor/blob/master/example.py

client = MeteorClient('ws://127.0.0.1:3000/websocket')

# Meteor client callbacks
def subscribed(subscription):
    print('* SUBSCRIBED {}'.format(subscription))

def connected():
    print('* CONNECTED')

def subscription_callback(error):
    if error:
        print(error)

def insert_callback(error, data):
    if error:
        print(error)
Esempio n. 47
0
import time
import math
import copy
import random
from drink_mixing import mymix
from gesaltdrinkomatic import *

from MeteorClient import MeteorClient

client = MeteorClient('ws://127.0.0.1:3000/websocket')

list_drinks=[];
list_drinks_alcohol=[];
list_drinks_bin=[];
#global list_drinks_alcohol_all
#global list_drinks_bin_all
#global list_drinks_all_final
list_drinks_alcohol_all=[];
list_drinks_bin_all=[];


global count_old
count_old=0
times=0


###########################  (Defining mix) ############################

mydict = {}

#initialize drink list,n_drinks,radii
Esempio n. 48
0
class MyProgram:
    def __init__(self):
        slackLog("Gate scanner started.")
        self.parents= {}
        #load parents
        self.client = MeteorClient('ws://chloe.asianhope.org:8080/websocket',debug=False)
        self.client.connect()
        self.client.login(METEOR_USERNAME,METEOR_PASSWORD)
        self.client.subscribe('cards')
        self.client.subscribe('scans')
        time.sleep(3) #give it some time to finish loading everything
        self.all_cards = self.client.find('cards')
        slackLog("Pulled records for: "+str(len(self.all_cards))+" cards")

        for card in self.all_cards:
            try:
                barcode = card['barcode']
                name = card['name']
                cardtype = card['type']
                expires = card['expires']
                profile = card.get('profile',barcode+".JPG") #default picture
                associations = card['associations']
                self.parents.update({barcode:card})

            except KeyError:
                slackLog(barcode+' has missing data',delay=True)

                pass
        # load style css for template
        screen = Gdk.Screen.get_default()
        css_provider = Gtk.CssProvider()
        css_provider.load_from_path('style.css')
        context = Gtk.StyleContext()
        context.add_provider_for_screen(screen, css_provider,
                                        Gtk.STYLE_PROVIDER_PRIORITY_USER)

        # connect to glade teamplate
        self.gladefile = "cardmanager.glade"
        self.glade = Gtk.Builder()
        self.glade.add_from_file(self.gladefile)
        self.glade.connect_signals(self)

        #get window from glade
        self.app_window=self.glade.get_object("main_window") # Window Name in GLADE
        self.app_window.fullscreen()
        # quit app
        self.app_window.connect("delete-event",Gtk.main_quit)

        #change color of window??
        #self.green = gtk.gdk.color_parse('green')
        #self.black = gtk.gdk.color_parse('black')
        #self.app_window.modify_bg(gtk.STATE_NORMAL,self.black)

        # get objects from glade
        self.header = self.glade.get_object("header")
        self.header_context = self.header.get_style_context()
        self.header_title = self.glade.get_object("header_title")
        self.parent_image = self.glade.get_object("img_parent")
        self.child_container = self.glade.get_object("grid1")
        self.button_search = self.glade.get_object("btn_search")
        self.entry = self.glade.get_object("search_input")
        self.pname = self.glade.get_object("lbl_pname")
        self.pbarcode = self.glade.get_object("lbl_pbarcode")
        self.pexpires = self.glade.get_object("lbl_pexpires")
        self.error_message = self.glade.get_object("lbl_error_message")

        #add event to button_search
        self.button_search.connect("clicked", self.search_button_clicked, "3")


        # display children images
        pixbuf = Pixbuf.new_from_file("static/logo.png")
        scaled_buf = pixbuf.scale_simple(CHILD_DIMENSIONS_X,CHILD_DIMENSIONS_Y,InterpType.BILINEAR)

        self.pickup_students = ['0']*9 #seed the list with the size we want
        for i in range(0,9):
            self.pickup_students[i] = Gtk.Image()
            self.pickup_students[i].set_from_pixbuf(scaled_buf)

        self.label=Gtk.Table(3,3,True)
        self.label.attach(self.pickup_students[0],0,1,0,1)
        self.label.attach(self.pickup_students[1],1,2,0,1)
        self.label.attach(self.pickup_students[2],2,3,0,1)

        self.label.attach(self.pickup_students[3],0,1,1,2)
        self.label.attach(self.pickup_students[4],1,2,1,2)
        self.label.attach(self.pickup_students[5],2,3,1,2)
        self.label.attach(self.pickup_students[6],0,1,2,3)
        self.label.attach(self.pickup_students[7],1,2,2,3)
        self.label.attach(self.pickup_students[8],2,3,2,3)
        self.label.set_col_spacings(10)
        self.label.set_row_spacings(10)
        # add lebel of image to container in glade
        self.child_container.add(self.label)

        # display parent picture
        pixbuf = Pixbuf.new_from_file("static/logo.png")
        scaled_buf = pixbuf.scale_simple(PARENT_DIMENSIONS_X,PARENT_DIMENSIONS_Y,InterpType.BILINEAR)
        self.parent_image.set_from_pixbuf(scaled_buf)


        # self.button_search.can_default(True)
        # self.button_search.grab_default()
        self.entry.set_activates_default(True)
        self.app_window.set_focus(self.entry)

        self.error_message.set_text("")
        self.app_window.show_all()
        return

    def search_button_clicked(self, widget, data=None):
        associations = []

        self.error_message.set_text("")
        # remove classes in header
        header_class_list = self.header_context.list_classes()
        for class_name in header_class_list:
            self.header_context.remove_class(class_name)

        for i in range(0,9):
            #make sure all pictures are reset
            pixbuf = Pixbuf.new_from_file("static/logo.png")
            scaled_buf = pixbuf.scale_simple(CHILD_DIMENSIONS_X,CHILD_DIMENSIONS_Y,InterpType.BILINEAR)
            self.pickup_students[i].set_from_pixbuf(scaled_buf)

        #grab pid
        pid = self.entry.get_text()
        slackLog('Scanned card: '+pid,delay=True)

        #do a lookup for the name
        try:
            #get parent information
            #parent_card = self.client.find_one('cards', selector={'barcode': pid})
            parent_card = self.parents[pid]
            slackLog('```'+str(parent_card)+'```',delay=True)

            if not parent_card:
                self.header_title.set_text("Invalid Card!")
                self.header_context.add_class('header_invalid_card')
                self.pname.set_text("Card Not Found!")
                self.pbarcode.set_text("XXXX")
                self.pexpires.set_text("xxxx-xx-xx")
                pixbuf = Pixbuf.new_from_file("static/NA.JPG")
                scaled_buf = pixbuf.scale_simple(PARENT_DIMENSIONS_X,PARENT_DIMENSIONS_Y,InterpType.BILINEAR)
                self.parent_image.set_from_pixbuf(scaled_buf)
            else:
                pname = parent_card.get('name', pid)
                parent_picture = parent_card.get('profile',pid+".JPG")
                expires = parent_card.get('expires',"Expiry not set")
                barcode = parent_card.get('barcode',"Barcode not set")
                associations = parent_card.get('associations',[])

                # if card expired
                if expires < date.today().isoformat():
                    associations = []
                    self.header_title.set_text("Card has expired!")
                    self.header_context.add_class('header_expired')
                else:
                    def getScanCallbackFunction(error, result):
                        # if cannot get scan, display error message
                        if error:
                            self.header_title.set_text('Scan failed!')
                            self.error_message.set_text(error['message'])
                            self.header_context.add_class('header_invalid_card')
                            return
                        else:
                            # if card no scan in, add new scan
                            if result == None:
                                # scan in
                                action = 'Security Scan'
                                value = 0.00
                                products = []
                                user = METEOR_USERNAME
                                self.client.call('scanIn',[pid,action,value,products,user],scanInCallbackFunction)
                            # if card already scan in, update scan
                            else:
                                # scan out
                                scan_id = result['_id']
                                self.client.call('scanOut',[scan_id],scanOutCallbackFunction)

                    def scanInCallbackFunction(error,result):
                        # to check if card scan-in success or error
                        if error:
                            self.header_title.set_text('Scan failed!')
                            self.error_message.set_text(error['message'])
                            self.header_context.add_class('header_invalid_card')
                        else:
                            self.header_title.set_text("Scan-in")
                            self.header_context.add_class('header_scan_in')
                    def scanOutCallbackFunction(error,result):
                        # to check if card scan-out success or error
                        if error:
                            self.header_title.set_text('Scan failed!')
                            self.error_message.set_text(error['message'])
                            self.header_context.add_class('header_invalid_card')
                        else:
                            self.header_title.set_text("Scan-out")
                            self.header_context.add_class('header_scan_out')

                    # get scan to check if scan in or scan out
                    self.client.call('get_scan',[pid],getScanCallbackFunction)

                self.pname.set_text(pname)
                self.pbarcode.set_text(barcode)
                self.pexpires.set_text(expires)
                # load picture
                try:
                    slackLog('loading parent picture: '+str(pid),delay=True)
                    fetchPhotosByID(parent_picture)
                    pixbuf = Pixbuf.new_from_file("resource/"+parent_picture)
                    scaled_buf = pixbuf.scale_simple(PARENT_DIMENSIONS_X,PARENT_DIMENSIONS_Y,InterpType.BILINEAR)
                    self.parent_image.set_from_pixbuf(scaled_buf)
                except Exception as inst:
                    slackLog("No parent picture for: "+pid,delay=True)
                    pixbuf = Pixbuf.new_from_file("static/unknown.jpg")
                    scaled_buf = pixbuf.scale_simple(PARENT_DIMENSIONS_X,PARENT_DIMENSIONS_Y,InterpType.BILINEAR)
                    self.parent_image.set_from_pixbuf(scaled_buf)

        except KeyError:
            slackLog('Scanned card: '+pid+' could not be found',delay=True)
            #display an error
            self.header_title.set_text("Invalid Card!")
            self.header_context.add_class('header_invalid_card')
            self.pname.set_text("Card Not Found!")
            self.pbarcode.set_text("XXXX")
            self.pexpires.set_text("xxxx-xx-xx")

            pixbuf = Pixbuf.new_from_file("static/NA.JPG")
            scaled_buf = pixbuf.scale_simple(PARENT_DIMENSIONS_X,PARENT_DIMENSIONS_Y,InterpType.BILINEAR)
            self.parent_image.set_from_pixbuf(scaled_buf)

            #reset everything
            self.entry.set_text('')
            self.app_window.set_focus(self.entry)
            self.app_window.show()


        #try and load the studnts starting after the parents name
        i = 0
        if(len(associations)):

            pool = ThreadPool(len(associations))
            results = pool.map(fetchPhotosByID,associations)
        for sid in associations:
            #if the student picture exists locally, load it
            try:
                pixbuf = Pixbuf.new_from_file("resource/"+sid+".JPG")
                scaled_buf = pixbuf.scale_simple(CHILD_DIMENSIONS_X,CHILD_DIMENSIONS_Y,InterpType.BILINEAR)
                self.pickup_students[i].set_from_pixbuf(scaled_buf)
            #if not, load the NA picture to indicate a student w/o a picture
            except:
                print("Unexpected error:```")
                print sys.exc_info()[0]
                pixbuf = Pixbuf.new_from_file("static/NA.JPG")
                scaled_buf = pixbuf.scale_simple(CHILD_DIMENSIONS_X,CHILD_DIMENSIONS_Y,InterpType.BILINEAR)
                self.pickup_students[i].set_from_pixbuf(scaled_buf)
            i+=1



        #clear entry box and reset focus
        self.entry.set_text('')
        self.app_window.set_focus(self.entry)
        self.app_window.show()
Esempio n. 49
0
import time

from MeteorClient import MeteorClient

client = MeteorClient('ws://127.0.0.1:3000/websocket')


def subscribed(subscription):
    print('* SUBSCRIBED {}'.format(subscription))


def unsubscribed(subscription):
    print('* UNSUBSCRIBED {}'.format(subscription))


def added(collection, id, fields):
    print('* ADDED {} {}'.format(collection, id))
    for key, value in fields.items():
        print('  - FIELD {} {}'.format(key, value))

    # query the data each time something has been added to
    # a collection to see the data `grow`
    all_lists = client.find('lists', selector={})
    print('Lists: {}'.format(all_lists))
    print('Num lists: {}'.format(len(all_lists)))

    # if collection == 'list' you could subscribe to the list here
    # with something like
    # client.subscribe('todos', id)
    # all_todos = client.find('todos', selector={})
    # print 'Todos: {}'.format(all_todos)
Esempio n. 50
0
    def __init__(self):
        slackLog("Gate scanner started.")
        self.parents= {}
        #load parents
        self.client = MeteorClient('ws://chloe.asianhope.org:8080/websocket',debug=False)
        self.client.connect()
        self.client.login(METEOR_USERNAME,METEOR_PASSWORD)
        self.client.subscribe('cards')
        self.client.subscribe('scans')
        time.sleep(3) #give it some time to finish loading everything
        self.all_cards = self.client.find('cards')
        slackLog("Pulled records for: "+str(len(self.all_cards))+" cards")

        for card in self.all_cards:
            try:
                barcode = card['barcode']
                name = card['name']
                cardtype = card['type']
                expires = card['expires']
                profile = card.get('profile',barcode+".JPG") #default picture
                associations = card['associations']
                self.parents.update({barcode:card})

            except KeyError:
                slackLog(barcode+' has missing data',delay=True)

                pass
        # load style css for template
        screen = Gdk.Screen.get_default()
        css_provider = Gtk.CssProvider()
        css_provider.load_from_path('style.css')
        context = Gtk.StyleContext()
        context.add_provider_for_screen(screen, css_provider,
                                        Gtk.STYLE_PROVIDER_PRIORITY_USER)

        # connect to glade teamplate
        self.gladefile = "cardmanager.glade"
        self.glade = Gtk.Builder()
        self.glade.add_from_file(self.gladefile)
        self.glade.connect_signals(self)

        #get window from glade
        self.app_window=self.glade.get_object("main_window") # Window Name in GLADE
        self.app_window.fullscreen()
        # quit app
        self.app_window.connect("delete-event",Gtk.main_quit)

        #change color of window??
        #self.green = gtk.gdk.color_parse('green')
        #self.black = gtk.gdk.color_parse('black')
        #self.app_window.modify_bg(gtk.STATE_NORMAL,self.black)

        # get objects from glade
        self.header = self.glade.get_object("header")
        self.header_context = self.header.get_style_context()
        self.header_title = self.glade.get_object("header_title")
        self.parent_image = self.glade.get_object("img_parent")
        self.child_container = self.glade.get_object("grid1")
        self.button_search = self.glade.get_object("btn_search")
        self.entry = self.glade.get_object("search_input")
        self.pname = self.glade.get_object("lbl_pname")
        self.pbarcode = self.glade.get_object("lbl_pbarcode")
        self.pexpires = self.glade.get_object("lbl_pexpires")
        self.error_message = self.glade.get_object("lbl_error_message")

        #add event to button_search
        self.button_search.connect("clicked", self.search_button_clicked, "3")


        # display children images
        pixbuf = Pixbuf.new_from_file("static/logo.png")
        scaled_buf = pixbuf.scale_simple(CHILD_DIMENSIONS_X,CHILD_DIMENSIONS_Y,InterpType.BILINEAR)

        self.pickup_students = ['0']*9 #seed the list with the size we want
        for i in range(0,9):
            self.pickup_students[i] = Gtk.Image()
            self.pickup_students[i].set_from_pixbuf(scaled_buf)

        self.label=Gtk.Table(3,3,True)
        self.label.attach(self.pickup_students[0],0,1,0,1)
        self.label.attach(self.pickup_students[1],1,2,0,1)
        self.label.attach(self.pickup_students[2],2,3,0,1)

        self.label.attach(self.pickup_students[3],0,1,1,2)
        self.label.attach(self.pickup_students[4],1,2,1,2)
        self.label.attach(self.pickup_students[5],2,3,1,2)
        self.label.attach(self.pickup_students[6],0,1,2,3)
        self.label.attach(self.pickup_students[7],1,2,2,3)
        self.label.attach(self.pickup_students[8],2,3,2,3)
        self.label.set_col_spacings(10)
        self.label.set_row_spacings(10)
        # add lebel of image to container in glade
        self.child_container.add(self.label)

        # display parent picture
        pixbuf = Pixbuf.new_from_file("static/logo.png")
        scaled_buf = pixbuf.scale_simple(PARENT_DIMENSIONS_X,PARENT_DIMENSIONS_Y,InterpType.BILINEAR)
        self.parent_image.set_from_pixbuf(scaled_buf)


        # self.button_search.can_default(True)
        # self.button_search.grab_default()
        self.entry.set_activates_default(True)
        self.app_window.set_focus(self.entry)

        self.error_message.set_text("")
        self.app_window.show_all()
        return
Esempio n. 51
0
class BotWrapper:
    def __init__(self,
                 url,
                 magic_phrase,
                 max_turns=10,
                 callback=None,
                 callback_params=1,
                 msg_q=False):
        print('starting service')
        self.start_proba = 1.0
        self.magic_phrase = magic_phrase
        self.url = replace_localhost(url)
        self.bot = Alice()
        self.max_turns = max_turns
        self.sending_message = False
        self._id = None
        self.use_msg_q = msg_q  # msg_q sets whether or not we are queueing messages
        self.websocket = 'ws://%s/websocket' % self.url
        self.client = MeteorClient(self.websocket)
        self.client.ddp_client.ddpsocket.extra_headers = [('Bot', 'true')]
        print(self.client.ddp_client.ddpsocket.handshake_headers)
        self.client.connect()

        self.idle_time = 3 * 60
        self.thread_time = 2
        self.max_retry = 3

    def restart_idler(self):
        ''' Restarts the idle watcher '''
        print('restarting idler')
        if hasattr(self, 'idler_thread') and self.idler_thread:
            self.idler_thread.cancel()
        self.idler_thread = threading.Timer(self.idle_time,
                                            self.idle_user_handler)
        self.idler_thread.start()

    def idle_user_handler(self):
        """ Handler that disconnects conversation in the event that a user leaves """

        print('user is idle disconnect')
        self.idler_thread = None
        self.end_convo()

    def login(self,
              user='******',
              pwd='botbot',
              callback=None,
              callback_params=0):
        print('logging in')

        def set_user(data):
            self.set_user_id(data['id'])
            print('user id set to', self._id)
            if callback and callback_params == 1:
                print('running callback with 1 parameter')
                callback(self)
            elif callback and callback_params == 0:
                callback()

        # TODO make this into threading timers.
        while not self._id:
            self.client.login(user,
                              pwd,
                              callback=func_wrap(set_user, params=1))
            time.sleep(0.5)

    def logout(self):
        self.client.logout()


#    def find_and_join_room(self):
#        """ Finds a room and joins it """
#        self.find_room(callback=(lambda roomId : self.join_room(roomId)))
#
#    def find_room(self, callback=None):
#        print('looking for an open room')
#        def room_callback():
#            print('looking for a room')
#            user = self.client.find_one('users')
#            print('user dict',user.items())
#            if user["in_convo"]:
#                roomObj = user["curConvo"]
#                print('roomid: ', roomObj)
#            else:
#                openrooms = self.client.find('convos') # {curSessions : {$lt  :2}}
#                roomObj = openrooms[0] if openrooms and len(openrooms) > 0 else -1
#
#            # TODO may have issues with room id when user is in convo
#            if roomObj != -1:
#                if type(roomObj) == str:
#                    print(roomObj, 'room')
#                    print('openrooms', openrooms)
#                callback(roomObj['_id'])
#                # Add user to room
#
#            else:
#                print('No rooms found. Back to the bat cave')
#        self.subscribe('currentUser',params=[], callback=func_wrap(
#            lambda : room_callback()
#            )
#        )

    def subscribe(self, collection, params=[], callback=None):
        """ Wrapper for subscribe to avoid issues with already subscribed rooms """
        try:
            print("subscribing to {}".format(collection))
            self.client.subscribe(collection, params, callback)
        except MeteorClientException:
            print(
                'Already subscribed to {}. Running callback with None'.format(
                    collection))
            if callback:
                callback(None)

    def join_room(self, roomId, otherUserId, callback=None):
        """ Join a room based on roomId """
        print('join room with id', roomId)
        self.roomId = roomId
        self.msg_queue = []
        self.available = False
        self.client.call(
            'convos.addUserToRoom',
            params=[roomId, self.magic_phrase],
            callback=func_wrap(lambda: self.subscribe(
                'chat', [roomId],
                func_wrap(lambda: self.subscribe(
                    'msgs', [roomId],
                    func_wrap(lambda: self.subscribe(
                        'currentUsers', [roomId],
                        func_wrap(lambda: self.watch_room(
                            roomId,
                            func_wrap(lambda: self.send_ready(
                                roomId, otherUserId, callback)))))))))))

    def send_ready(self, roomId, otherUserId, callback=None):
        self.client.call('convos.botReady',
                         params=[roomId, otherUserId, self.magic_phrase],
                         callback=callback)

    def unsubscribe(self, collection):
        """ Unsubscribe from the collection """
        try:
            self.client.unsubscribe(collection)
        except MeteorClientException:
            print('\t"{}" not subscribed to.'.format(collection))

    def end_convo(self):
        """ End the conversation """
        print('end conversation and unsubscribe from it all')
        self.client.remove_all_listeners('added')
        self.client.remove_all_listeners('changed')

        self.unsubscribe('chat')
        self.unsubscribe('msgs')
        self.unsubscribe('currentUsers')

        self.client.call('users.exitConvo', [])
        self.client.call('convos.updateRatings', [self.roomId, 'not'])
        self.available = True
        if hasattr(self, 'idler_thread') and self.idler_thread:
            self.idler_thread.cancel()

    def set_wpm(self):
        """ Set the words per minute of the bot """
        wpm = random.randint(150, 200)
        self.cps = 60 / (wpm * 5)
        print('Setting wpm : {} '.format(wpm))

    def prime_bot(self, convo_obj):
        """  the conversational bot """
        print('convo_obj', convo_obj)
        input_msg = 'hi'
        if 'msgs' in convo_obj and convo_obj['msgs']:
            topic_msg_id = convo_obj['msgs'][0]
            msg_obj = self.client.find_one('messages',
                                           selector={'_id': topic_msg_id})
            if msg_obj:
                input_msg = msg_obj['message']

        msg = self.bot.message(input_msg, self.roomId)
        if random.random() > self.start_proba:
            self.send_message(msg)

    def watch_room(self, roomId, callback=None):
        """
        Setup Event Listeneres for a room and checks to make sure that the room is updating
        """
        self.turns = 0
        convo_obj = self.client.find_one('convos', selector={'_id': roomId})
        self.room_closed = convo_obj['closed']
        self.set_wpm()

        self.last_message = ""
        self.confirmed_messages = [
        ]  # all messages sent by the user that have been confirmed
        self.thread = MessageHandlerThread(self)

        def message_added(collection, id, fields):
            """ callback for when a message is added """
            if (collection == 'messages' and 'message' in fields
                    and 'user' in fields):
                print(type(self._id), type(fields['user']), self._id,
                      fields['user'])
                if fields['user'] != self._id and self.last_message != fields[
                        'message']:
                    self.restart_idler()
                    self.receive_message(fields['message'])
                    self.last_message = fields['message']
                    self.thread.message_received = True
                elif fields['user'] == self._id:
                    print('\t messages from self detected')
                    self.confirmed_messages.append(fields['message'])

        self.client.on('added', message_added)

        def watch_convo(collection, id, fields, cleared):
            """ callback for when any part of the conversation is updated """
            if self.roomId and collection == "convos" and id == self.roomId:
                # print('\t',fields)
                if 'closed' in fields:
                    print('\tRoom is closed: ', fields['closed'])
                    self.room_closed = fields['closed']
                    self.end_convo()
                if 'msgs' in fields:
                    print('\tMessages updated in convo "{}"'.format(id))
                    # TODO this is bugggy
                    self.thread.convo_updated = True
                if 'turns' in fields:
                    print('\tTurns updated to "{}"'.format(fields['turns']))
                    self.turns = fields['turns']
            elif self.roomId == id:
                print(collection, id, fields)

        self.client.on('changed', watch_convo)
        # mark the bot as ready to talk
        self.restart_idler()
        self.prime_bot(convo_obj)
        print("before thread")
        self.thread.start()
        print("after thread")

        if callback:
            callback(None)

    def respond(self):
        """ Kind of a hacky way to respond to the conversation """
        print("responding")
        if self.msg_queue and self.use_msg_q:
            partner_msg = self.msg_queue[0]
            self.msg_queue = self.msg_queue[1:]
            msg = self.bot.message(partner_msg, self.roomId)
            print(msg)
            self.send_message(msg)

        if self.msg_queue and not self.sending_message:
            partner_msg = self.msg_queue[-1]
            self.msg_queue = self.msg_queue[:-1]
            msg = self.bot.message(partner_msg, self.roomId)
            print(msg)
            self.send_message(msg)

    def still_in_conv(self):
        """ Returns whether the conversation is still moving """
        in_conv = self.roomId != None and not self.client.find_one(
            'convos', selector={'_id': self.roomId})['closed']
        print('\tstill in conv', in_conv)
        if not in_conv:
            self.end_convo()
        print(
            '\tclosed: ',
            self.client.find_one('convos', selector={'_id':
                                                     self.roomId})['closed'])
        return in_conv

    def get_convo_dict(self):
        if self.roomId:
            return self.client.find_one('convos',
                                        selector={'_id': self.roomId})
        else:
            return {}

    def get_message(self, idx):
        ''' Returns the message at idx'''
        convo_dict = self.get_convo_dict()
        if convo_dict:
            topic_msg_id = convo_dict['msgs'][idx]
            msg_dict = self.client.find_one('messages',
                                            selector={'_id': topic_msg_id})
            # print(msg_dict)
            if msg_dict:
                return msg_dict['message']
        return ''

    def received_message(self, message):
        """ Checks whether the bot actually sent the message """
        # TODO add handler that removes a confirmed message to save memory
        return message in self.confirmed_messages

    def retry_message(self, message, retry=0, callback=None):
        """ Handler that makes attempts to connect a user back into a conversation """
        # TODO set as properties
        if retry == 0 or not self.received_message(
                message) and retry < self.max_retry:
            self.update_conversation(message, callback)

            if retry != 0:
                print('\t\tRetry {} of sending "{}"'.format(retry, message))

            t = threading.Timer(self.thread_time,
                                lambda: self.retry_message(message, retry + 1))
            t.start()
        elif retry >= self.max_retry:
            print(
                '\tMax retries reached - couldn\'t verify whether {} was received'
                .format(message))
        else:
            print('\t"{}" successfully received'.format(message))

    def update_conversation(self, message, callback=None):
        self.client.call('convos.updateChat', [message, self.roomId], callback)

    def _send_message(self, message, callback=None):
        self.last_message_sent = message
        if self.still_in_conv():
            self.retry_message(message, callback=callback)
        else:
            print('Not responding - conversation is OVER')
        self.sending_message = False

    def send_message(self, message, callback=None):
        # calculates typing speed based on rough cps for user
        sleep_time = self.cps * len(message)
        print("Preparing to send '{}' Waiting '{}' seconds.".format(
            message, sleep_time))
        t = threading.Timer(sleep_time,
                            lambda: self._send_message(message, callback))
        t.start()

    def receive_message(self, message):
        """ Called whenever the bot receives a message """
        print('Received "{}"'.format(message))
        self.msg_queue.append(message)
        # message = 'sup then' # self.bot.message(message)

        # self.send_message(message)

    def set_user_id(self, id):
        self.available = True
        print('set user id to ', id)
        self._id = id
Esempio n. 52
0
from MeteorClient import MeteorClient

filename = "2018-winter-students.csv"
season = "winter"
program = "after-school"
columns = [
    "Timestamp", "Email Address", "Child's Full Name", "Child's Address",
    "Child Date of Birth ", "Parent/Guardian Contact Name",
    "Parent/Guardian Phone Number", "Parent/Guardian Email Address",
    "Emergency Contact Name", "Emergency Phone Number", "Child's Uniform Size",
    "Does your child have any dietary restrictions?",
    "If your child has previously participated in KCTL, please identify his/her team."
]

client = MeteorClient('ws://127.0.0.1:80/websocket')
client.connect()


def calculate_age(year, month, day):
    today = date.today()
    return today.year - year - ((today.month, today.day) < (month, day))


def site_bucket(dob):
    print(dob)
    dob_l = dob.split("-")
    year = int(dob_l[2])
    month = int(dob_l[1])
    day = int(dob_l[0])
    age = calculate_age(year, month, day)
ledcontrol6 = 15

ledname1 = 'First' 
ledname2 = 'Second'
ledname3 = 'Third'
ledname4 = 'Forth'
ledname5 = 'Fifth'
ledname6 = 'Sixth'

from MeteorClient import MeteorClient





client = MeteorClient('ws://127.0.0.1:3000/websocket')
# client = MeteorClient('ws://testddp.meteor.com/websocket')

def subscribed(subscription):
    print('* SUBSCRIBED {}'.format(subscription))
    # conn = client.find_one('items', selector={'name': getHwAddr('eth0') })
    # if subscription == 'local-items':
    #     def getHwAddr(ifname):
    #         s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #         info = fcntl.ioctl(s.fileno(), 0x8927,  struct.pack('256s', ifname[:15]))
    #         return ':'.join(['%02x' % ord(char) for char in info[18:24]])
    #     print getHwAddr('eth0') 
    #     # new part
    #     newmac = getHwAddr('eth0')
    #     conn = client.find_one('items', selector={'name': newmac })
    #     print(format(conn))
Esempio n. 54
0
class DDP(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.meteor = conf.get('ddp', 'meteor')

        self.client = MeteorClient(self.meteor, debug=False)
        self.client.on('added', self.on_added)
        self.client.on('changed', self.on_changed)
        self.client.on('subscribed', self.on_subscribed)
        self.client.on('connected', self.on_connected)
        self.client.on('removed', self.on_removed)
        self.client.on('closed', self.on_closed)
        self.client.on('logged_in', self.on_logged_in)

        self.displayName = conf.get('ddp', 'room_name')
        self.vu_min = -50
        self.vu_range = 50
        self.vu_data = 0
        self.last_vu = None
        self.ip = conf.get('ingest', 'address')
        self.id = conf.get('ingest', 'hostname')
        self._user = conf.get('ddp', 'user')
        self._password = conf.get('ddp', 'password')
        self._http_host = conf.get('ddp', 'http_host')
        self._audiostream_port = conf.get('audiostream', 'port') or 31337
        self.store_audio = conf.get_boolean('ddp', 'store_audio')
        self.screenshot_file = conf.get('ddp', 'existing_screenshot')
        self.high_quality = conf.get_boolean('ddp', 'hq_snapshot')
        self.paused = False
        self.recording = False
        self.currentMediaPackage = None
        self.currentProfile = None
        self.has_disconnected = False
        screen = Gdk.Screen.get_default()
        self._screen_width = screen.get_width()
        self._screen_height = screen.get_height()
        self.cardindex = None

        cam_available = conf.get('ddp', 'cam_available') or 0
        if cam_available in ('True', 'true', True, '1', 1):
            self.cam_available = 1
        elif cam_available in ('False', 'false', False, '0', 0):
            self.cam_available = 0
        else:
            self.cam_available = int(cam_available)
        # Getting audiostream params. either using existing audiostreaming server like icecast or the audiostream plugin
        if conf.get('ddp', 'existing_stream_host'):
            self._stream_host = conf.get('ddp', 'existing_stream_host')
        else:
            self._stream_host = self.ip

        if conf.get_int('ddp', 'existing_stream_port'):
            self._audiostream_port = conf.get_int('ddp',
                                                  'existing_stream_port')
        else:
            self._audiostream_port = conf.get_int('audiostream',
                                                  'port') or 31337

        if conf.get('ddp', 'existing_stream_key'):
            self.stream_key = conf.get('ddp', 'existing_stream_key')
        else:
            self.stream_key = uuid.uuid4().get_hex()

        if conf.get('ddp', 'extra_params'):
            self.extra_params_list = conf.get('ddp', 'extra_params').split(';')
        else:
            self.extra_params_list = []
        logger.info(
            'audiostream URI: {}'.format('http://' + self._stream_host + ':' +
                                         str(self._audiostream_port) + '/' +
                                         self.stream_key))

        dispatcher.connect('init', self.on_init)
        dispatcher.connect('recorder-vumeter', self.vumeter)
        dispatcher.connect('timer-short', self.update_vu)
        dispatcher.connect('timer-short', self.heartbeat)
        dispatcher.connect('recorder-started', self.on_start_recording)
        dispatcher.connect('recorder-stopped', self.on_stop_recording)
        dispatcher.connect('recorder-status', self.on_rec_status_update)

    def run(self):
        self.connect()

    def connect(self):
        if not self.has_disconnected:
            try:
                self.client.connect()
            except Exception:
                logger.warn('DDP connection failed')

    def update(self, collection, query, update):
        if self.client.connected and self.subscribedTo('GalicasterControl'):
            try:
                self.client.update(collection,
                                   query,
                                   update,
                                   callback=self.update_callback)
            except Exception:
                logger.warn("Error updating document "
                            "{collection: %s, query: %s, update: %s}" %
                            (collection, query, update))

    def insert(self, collection, document):
        if self.client.connected and self.subscribedTo('GalicasterControl'):
            try:
                self.client.insert(collection,
                                   document,
                                   callback=self.insert_callback)
            except Exception:
                logger.warn(
                    "Error inserting document {collection: %s, document: %s}" %
                    (collection, document))

    def heartbeat(self, element):
        if self.client.connected:
            self.update_images()
        else:
            self.connect()

    def on_start_recording(self, sender, id):
        self.recording = True
        self.currentMediaPackage = self.media_package_metadata(id)
        self.currentProfile = conf.get_current_profile().name
        self.update('rooms', {'_id': self.id}, {
            '$set': {
                'currentMediaPackage': self.currentMediaPackage,
                'currentProfile': self.currentProfile,
                'recording': self.recording
            }
        })

    def on_stop_recording(self, mpid, sender=None):
        self.recording = False
        self.currentMediaPackage = None
        self.currentProfile = None
        self.update('rooms', {'_id': self.id}, {
            '$unset': {
                'currentMediaPackage': '',
                'currentProfile': ''
            },
            '$set': {
                'recording': self.recording
            }
        })
        self.update_images(1.5)

    def on_init(self, data):
        self.update_images(1.5)

    def update_images(self, delay=0.0):
        worker = Thread(target=self._update_images, args=(delay, ))
        worker.start()

    def _update_images(self, delay):
        time.sleep(delay)
        files = {}

        if not self.screenshot_file:
            # take a screenshot with pyscreenshot
            im = ImageGrab.grab(bbox=(0, 0, self._screen_width,
                                      self._screen_height),
                                backend='imagemagick')
        else:
            try:
                # used if screenshot already exists
                im = Image.open(self.screenshot_file)
            except IOError as e:
                logger.warn("Unable to open screenshot file {0}".format(
                    self.screenshot_file))
                return
        output = cStringIO.StringIO()
        image_format = 'JPEG'
        if not self.high_quality:
            im.thumbnail((640, 360), Image.ANTIALIAS)
        else:
            image_format = 'PNG'

        if im.mode != "RGB":
            im = im.convert("RGB")
        im.save(output, format=image_format
                )  # to reduce jpeg size use param: optimize=True
        files['galicaster'] = ('galicaster.jpg', output.getvalue(),
                               'image/jpeg')
        try:
            # add verify=False for testing self signed certs
            requests.post(
                "%s/image/%s" % (self._http_host, self.id),
                files=files,
                auth=(self._user, self._password
                      ))  # to ignore ssl verification, use param: verify=False
        except Exception:
            logger.warn('Unable to post images')

    def vumeter(self, element, data, data_chan2, vu_bool):
        if data == "Inf":
            data = 0
        else:
            if data < -self.vu_range:
                data = -self.vu_range
            elif data > 0:
                data = 0
        self.vu_data = int(
            ((data + self.vu_range) / float(self.vu_range)) * 100)

    def update_vu(self, element):
        if self.vu_data != self.last_vu:
            update = {'vumeter': self.vu_data}
            self.update('rooms', {'_id': self.id}, {'$set': update})
            self.last_vu = self.vu_data

    def on_rec_status_update(self, element, data):
        if data == 'paused':
            is_paused = True
        else:
            is_paused = False
        if is_paused:
            self.update_images(.75)
        if self.paused == is_paused:
            self.update('rooms', {'_id': self.id},
                        {'$set': {
                            'paused': is_paused
                        }})
            self.paused = is_paused
        if data == 'recording':
            self.update_images(.75)

    def media_package_metadata(self, id):
        mp = context.get('recorder').current_mediapackage
        line = mp.metadata_episode
        duration = mp.getDuration()
        line["duration"] = long(duration / 1000) if duration else None
        # FIXME Does series_title need sanitising as well as duration?
        created = mp.getDate()
        # line["created"] = calendar.timegm(created.utctimetuple())
        for key, value in mp.metadata_series.iteritems():
            line["series_" + key] = value
        for key, value in line.iteritems():
            if value in [None, []]:
                line[key] = ''
        # return line
        return line

    def subscription_callback(self, error):
        if error:
            logger.warn("Subscription callback returned error: %s" % error)

    def insert_callback(self, error, data):
        if error:
            logger.warn("Insert callback returned error: %s" % error)

    def update_callback(self, error, data):
        if error:
            logger.warn("Update callback returned error: %s" % error)

    def on_subscribed(self, subscription):
        if (subscription == 'GalicasterControl'):
            me = self.client.find_one('rooms')
            # Data to push when inserting or updating
            data = {
                'displayName': self.displayName,
                'ip': self.ip,
                'paused': self.paused,
                'recording': self.recording,
                'heartbeat': int(time.time()),
                'camAvailable': self.cam_available,
                'inputs': self.inputs(),
                'stream': {
                    'host': self._stream_host,
                    'port': self._audiostream_port,
                    'key': self.stream_key
                }
            }
            # Parse extra Meteor Mongodb collection elements and append
            for params in self.extra_params_list:
                param = params.split(':')
                data[param[0]] = param[1]

            if self.currentMediaPackage:
                data['currentMediaPackage'] = self.currentMediaPackage
            if self.currentProfile:
                data['currentProfile'] = self.currentProfile

            if me:
                # Items to unset
                unset = {}
                if not self.currentMediaPackage:
                    unset['currentMediaPackage'] = ''
                if not self.currentProfile:
                    unset['currentProfile'] = ''

                # Update to push
                update = {'$set': data}

                if unset:
                    update['$unset'] = unset
                self.update('rooms', {'_id': self.id}, update)
            else:
                data['_id'] = self.id
                self.insert('rooms', data)

    def inputs(self):
        inputs = {'presentations': ['Presentation']}
        inputs['cameras'] = []
        labels = conf.get('ddp', 'cam_labels')
        cam_labels = []
        if labels:
            cam_labels = [l.strip() for l in labels.split(',')]
        for i in range(0, self.cam_available):
            label = cam_labels[i] if i < len(cam_labels) else "Camera %d" % (
                i + 1)
            inputs['cameras'].append(label)
        return inputs

    def on_added(self, collection, id, fields):
        pass

    def on_changed(self, collection, id, fields, cleared):
        me = self.client.find_one('rooms')
        if self.paused != me['paused']:
            self.set_paused(me['paused'])

        if context.get('recorder').is_recording() != me['recording']:
            self.set_recording(me)

    def on_removed(self, collection, id):
        self.on_subscribed(None)

    def set_paused(self, new_status):
        if not self.paused:
            self.paused = new_status
            context.get('recorder').pause()
        else:
            self.paused = False
            context.get('recorder').resume()

    def set_recording(self, me):
        self.recording = me['recording']
        if self.recording:
            # FIXME: Metadata isn't passed to recorder
            meta = me.get('currentMediaPackage', {}) or {}
            profile = me.get('currentProfile', 'nocam')
            series = (meta.get('series_title', ''), meta.get('isPartOf', ''))
            user = {
                'user_name': meta.get('creator', ''),
                'user_id': meta.get('rightsHolder', '')
            }
            title = meta.get('title', 'Unknown')
            context.get('recorder').record()
        else:
            context.get('recorder').stop()

    def on_connected(self):
        logger.info('Connected to Meteor')
        token = conf.get('ddp', 'token')
        self.client.login(self._user, self._password, token=token)

    def on_logged_in(self, data):
        conf.set('ddp', 'token', data['token'])
        conf.update()
        try:
            self.client.subscribe('GalicasterControl',
                                  params=[self.id],
                                  callback=self.subscription_callback)
        except Exception:
            logger.warn('DDP subscription failed')

    def on_closed(self, code, reason):
        self.has_disconnected = True
        logger.error('Disconnected from Meteor: err %d - %s' % (code, reason))

    def subscribedTo(self, publication):
        return self.client.subscriptions.get(publication) != None
Esempio n. 55
0
class DicecloudClient:
    instance = None
    user_id = None

    def __init__(self, debug=False):
        self.meteor_client = MeteorClient(SOCKET_BASE, debug=debug)
        self.http = DicecloudHTTP(API_BASE, API_KEY, debug=debug)
        self.logged_in = False

    @classmethod
    def getInstance(cls):
        if cls.instance is None:
            try:
                cls.instance = cls(debug=TESTING)
                cls.instance.initialize()
            except:
                return None
        return cls.instance

    def initialize(self):
        log.info(f"Initializing Dicecloud Meteor client (debug={TESTING})")
        self.meteor_client.connect()
        loops = 0
        while (not self.meteor_client.connected) and loops < 100:
            time.sleep(0.1)
            loops += 1
        log.info(f"Connected to Dicecloud in {loops/10} seconds")

        def on_login(error, data):
            if data:
                type(self).user_id = data.get('id')
                self.logged_in = True
            else:
                log.warning(error)
                raise LoginFailure()

        self.meteor_client.login(UNAME, PWD, callback=on_login)
        loops = 0
        while not self.logged_in and loops < 100:
            time.sleep(0.1)
            loops += 1
        log.info(f"Logged in as {self.user_id}")

    async def ensure_connected(self):
        if self.logged_in:  # everything is fine:tm:
            return
        asyncio.get_event_loop().run_in_executor(None, self.initialize)

    async def _get_list_id(self, character, list_name=None):
        """
        :param character: (Character) the character to get the spell list ID of.
        :param list_name: (str) The name of the spell list to look for. Returns default if not passed.
        :return: (str) The default list id.
        """
        char_id = character.upstream[10:]

        char = await self.get_character(char_id)
        if list_name:
            list_id = next((l for l in char.get('spellLists', [])
                            if l['name'].lower() == list_name.lower()), None)
        else:
            list_id = next((l for l in char.get('spellLists', [])), None)
        return list_id

    async def get_character(self, charId):
        return await self.http.get(f'/character/{charId}/json')

    async def add_spell(self, character, spell):
        """Adds a spell to the dicecloud list."""
        return await self.add_spells(character, [spell])

    async def add_spells(self, character, spells, spell_list=None):
        """
        :param character: (Character) The character to add spells for.
        :param spells: (list) The list of spells to add
        :param spell_list: (str) The spell list name to search for in Dicecloud.
        """
        list_id = await self._get_list_id(character, spell_list)
        if not list_id:  # still
            raise InsertFailure(
                "No matching spell lists on origin sheet. Run `!update` if this seems incorrect."
            )
        return await self.http.post(
            f'/api/character/{character.upstream[10:]}/spellList/{list_id}',
            [s.to_dicecloud() for s in spells])

    async def create_character(self,
                               name: str = "New Character",
                               gender: str = None,
                               race: str = None,
                               backstory: str = None):
        data = {'name': name, 'writers': [self.user_id]}
        if gender is not None:
            data['gender'] = gender
        if race is not None:
            data['race'] = race
        if backstory is not None:
            data['backstory'] = backstory

        data['settings'] = {'viewPermission': 'public'}  # sharing is caring!
        response = await self.http.post('/api/character', data)
        return response['id']

    async def delete_character(self, charId: str):
        await self.http.delete(f'/api/character/{charId}')

    async def get_user_id(self, username: str):
        username = urllib.parse.quote_plus(username)
        userId = await self.http.get(f'/api/user?username={username}')
        return userId['id']

    async def transfer_ownership(self, charId: str, userId: str):
        await self.http.put(f'/api/character/{charId}/owner', {'id': userId})

    async def insert_feature(self, charId, feature):
        return (await self.insert_features(charId, [feature]))[0]

    async def insert_features(self, charId: str, features: list):
        response = await self.http.post(f'/api/character/{charId}/feature',
                                        [f.to_dict() for f in features])
        return response

    async def insert_proficiency(self, charId, prof):
        return (await self.insert_proficiencies(charId, [prof]))[0]

    async def insert_proficiencies(self, charId: str, profs: list):
        response = await self.http.post(f'/api/character/{charId}/prof',
                                        [p.to_dict() for p in profs])
        return response

    async def insert_effect(self, charId, effect):
        return (await self.insert_effects(charId, [effect]))[0]

    async def insert_effects(self, charId: str, effects: list):
        response = await self.http.post(f'/api/character/{charId}/effect',
                                        [e.to_dict() for e in effects])
        return response

    async def insert_class(self, charId, klass):
        return (await self.insert_classes(charId, [klass]))[0]

    async def insert_classes(self, charId: str, classes: list):
        response = await self.http.post(f'/api/character/{charId}/class',
                                        [c.to_dict() for c in classes])
        return response
Esempio n. 56
0
    def __init__(self):
        Thread.__init__(self)
        self.meteor = conf.get('ddp', 'meteor')

        self.client = MeteorClient(self.meteor, debug=False)
        self.client.on('added', self.on_added)
        self.client.on('changed', self.on_changed)
        self.client.on('subscribed', self.on_subscribed)
        self.client.on('connected', self.on_connected)
        self.client.on('removed', self.on_removed)
        self.client.on('closed', self.on_closed)
        self.client.on('logged_in', self.on_logged_in)

        self.displayName = conf.get('ddp', 'room_name')
        self.vu_min = -50
        self.vu_range = 50
        self.vu_data = 0
        self.last_vu = None
        self.ip = conf.get('ingest', 'address')
        self.id = conf.get('ingest', 'hostname')
        self._user = conf.get('ddp', 'user')
        self._password = conf.get('ddp', 'password')
        self._http_host = conf.get('ddp', 'http_host')
        self._audiostream_port = conf.get('audiostream', 'port') or 31337
        self.store_audio = conf.get_boolean('ddp', 'store_audio')
        self.screenshot_file = conf.get('ddp', 'existing_screenshot')
        self.high_quality = conf.get_boolean('ddp', 'hq_snapshot')
        self.paused = False
        self.recording = False
        self.currentMediaPackage = None
        self.currentProfile = None
        self.has_disconnected = False
        screen = Gdk.Screen.get_default()
        self._screen_width = screen.get_width()
        self._screen_height = screen.get_height()
        self.cardindex = None

        cam_available = conf.get('ddp', 'cam_available') or 0
        if cam_available in ('True', 'true', True, '1', 1):
            self.cam_available = 1
        elif cam_available in ('False', 'false', False, '0', 0):
            self.cam_available = 0
        else:
            self.cam_available = int(cam_available)
        # Getting audiostream params. either using existing audiostreaming server like icecast or the audiostream plugin
        if conf.get('ddp', 'existing_stream_host'):
            self._stream_host = conf.get('ddp', 'existing_stream_host')
        else:
            self._stream_host = self.ip

        if conf.get_int('ddp', 'existing_stream_port'):
            self._audiostream_port = conf.get_int('ddp',
                                                  'existing_stream_port')
        else:
            self._audiostream_port = conf.get_int('audiostream',
                                                  'port') or 31337

        if conf.get('ddp', 'existing_stream_key'):
            self.stream_key = conf.get('ddp', 'existing_stream_key')
        else:
            self.stream_key = uuid.uuid4().get_hex()

        if conf.get('ddp', 'extra_params'):
            self.extra_params_list = conf.get('ddp', 'extra_params').split(';')
        else:
            self.extra_params_list = []
        logger.info(
            'audiostream URI: {}'.format('http://' + self._stream_host + ':' +
                                         str(self._audiostream_port) + '/' +
                                         self.stream_key))

        dispatcher.connect('init', self.on_init)
        dispatcher.connect('recorder-vumeter', self.vumeter)
        dispatcher.connect('timer-short', self.update_vu)
        dispatcher.connect('timer-short', self.heartbeat)
        dispatcher.connect('recorder-started', self.on_start_recording)
        dispatcher.connect('recorder-stopped', self.on_stop_recording)
        dispatcher.connect('recorder-status', self.on_rec_status_update)
Esempio n. 57
0
class Client:
    def __init__(self, username, password, ui):
        self.username = username
        self.password = password
        self.ui = ui
        self.ui.print_logo()
        self.now = mktime(datetime.now().timetuple())*1e3
        self.resume_token = ''
        self.client = MeteorClient('wss://kwak.io/websocket')
        self.client.connect()
        self.client.login(self.username, self.password,
            token=self.resume_token, callback=self.logged_in)

        self.hot_channels = []
        self.hot_channels_name = []
        self.all_channels_name = []
        self.current_channel = 'dev'
        self.client.call('getHotChannels', [], self.set_hot_channels_name)
        self.client.call('channelList', [], self.set_all_channels_name)

        self.client.on('connected', self.connected)
        self.client.on('added', self.added)

        """ Not usable right now """
        """self.client.update('users', {'username': self.username},
                           {'profile.chans': self.current_channel}, callback=self.update_callback)
        """

    """
    def update_callback(self, error, result):
        self.ui.chatbuffer_add("UPDATED !")
        self.ui.chatbuffer_add(str(error))
        self.ui.chatbuffer_add(str(result))
    """

    def set_hot_channels_name(self, error, result):
        if error:
            self.ui.chatbuffer_add(error)
            return
        self.hot_channels_name = result
        self.ui.chanlist = self.hot_channels_name
        self.ui.redraw_ui()

    def set_all_channels_name(self, error, result):
        if error:
            self.ui.chatbuffer_add(error)
            return
        self.all_channels_name = result

    def subscribe_to_channel(self, channel):
        self.current_channel = channel
        try:
            self.client.unsubscribe('messages')
        except:
            pass
        self.ui.chatbuffer_add('* LISTENING TO CHANNEL {}'.format(channel))
        self.ui.redraw_chathead(channel)
        self.client.subscribe('messages', [self.current_channel])

    def subscribe_to_users(self, channel):
        self.client.subscribe('users', [[channel]])

    def added(self, collection, id, fields):
        # only add new messages, not backlog
        if collection == 'messages' and fields['time'] > self.now:
            # fields : channel | time | text | user
            timestamp = int(fields['time']) // 1000
            timestamp = datetime.fromtimestamp(timestamp).strftime('%H:%M')
            self.ui.chatbuffer_add('{} {}: {}'.format(
                timestamp, fields['user'], fields['text']))
        elif collection == 'users':
            # fields : username | profile | color
            if len(fields['profile']) and bool(fields['profile'].get('online', False)):
                self.ui.userlist.append(fields['username'])
                self.ui.redraw_ui()

    def connected(self):
        self.ui.chatbuffer_add('* CONNECTED')

    def logged_in(self, error, data):
        if error:
            self.ui.chatbuffer_add('LOGIN ERROR {}'.format(error))
        else:
            self.resume_token = data['token']
            self.client.call('setOnline', [])
            self.ui.chatbuffer_add('* LOGGED IN')
            # add self.username in userlist when logged and fix duplicates names

    def logout(self):
        self.ui.chatbuffer_add('* BYE (LOVELY DUCK)')
Esempio n. 58
-1
    def __init__(self, queue):
        self.run = None
        self.queue = queue
        self.thread = threading.Thread(target=self.work)
        self.client = MeteorClient('ws://127.0.0.1:3000/websocket')
        self.client.on('connected', self.connected_event)
        self.client.connect()

        self.configs = {
            # run
            'start_run': {
                'method': 'startRun',
                'callback': self.handle_run_call
            },

            # suites
            'start_suite': {'method': 'startSuite'},
            'end_suite': {'method': 'endSuite'},

            # tests
            'start_test': {'method': 'startTest'},
            'end_test': {'method': 'endTest'},

            # keywords
            'start_keyword': {'method': 'startKeyword'},
            'end_keyword': {'method': 'endKeyword'},

            # messages
            'log_message': {'method': 'logMessage'}
        }