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)
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)
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)
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=''): """
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 client.call('systemonboot', [newmac]) if process_name not in tmp[:]: print "The process is not running." client.call('systemfail', [newmac]) sys.exit(0)
update_status(queue_item, 'finished') write_log('debug', 'Finishes item ' + queue_item['_id'] + ' from queue') for x in range(0, 10): led.update() sleep(0.05) led.rainbow(20000) client.call('configuration.setStatus', ["idle"], callback_function) write_log('debug', 'Cocktailmixer started') scale.setOffset(mixer_configuration['values']['scale_offset']) scale.setScale(mixer_configuration['values']['scale_ratio']) client.call('configuration.setStatus', ["idle"], callback_function) led.rainbow(20000) # continue forever while True: try: # find next item in queue or previously loaded check_queue() if mixer_status['value']['type'] == 'tare': tare_scale() client.call('configuration.setStatus', ["idle"], callback_function) if mixer_status['value']['type'] == 'start_calibrating': offset = tare_scale()
while True: try: 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]]) # new part newmac = getHwAddr('eth0') humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) #timenow = time.strftime("%X") timenow = time.asctime(time.localtime(time.time())) if humidity is not None and temperature is not None: print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format( temperature, humidity) #client.insert('sensors', {'name': 'DHT11 SENSOR 1' ,'temperature': temperature , 'humidity': humidity , 'time': timenow}) client.call('sensordata', [temperature, humidity, newmac]) else: print 'Failed to get reading. Try again!' a = 0 time.sleep(60.0 - ((time.time() - starttime) % 60.0)) except KeyboardInterrupt: break # client.update('items' , {'name': 'c4:54:44:84:70:b4' }, {'status': 'Not Active'}) client.unsubscribe('local-items') client.unsubscribe('local-leds') client.unsubscribe('local-sensors')
class LightingMinion: 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() def connect_cb(self): self.debug('Connection to Meteor established.') if not self.config.get('id'): self.debug('No id in settings, registering as new.') self.meteor.call('minionNew', [self.config['type']], self.register) else: self.register(None, self.config['id']) def register(self, err, id): self.config['id'] = id self.debug('Connecting with id ' + id) self.meteor.call('minionConnect', [id], self.prep) def prep(self, e, r): self.meteortime = MeteorTime(self.meteor) self.meteor.subscribe('lights') self.meteor.on('added', self.added) self.meteor.on('changed', self.changed) self.ola = OlaClient.OlaClient() self.olasock = self.ola.GetSocket() self.olasock.setblocking(False) self.selectargs = ([self.olasock], [], [], 0) self.ready = True print('Connected to server.') def debug(self, *args): if self.config.get('debug'): print(*args) def added(self, collection, id, fields): self.changed(collection, id, fields, None) def changed(self, collection, id, fields, cleared): light = self.meteor.find_one('lights', selector={'_id': id}) settings = light.get('settings') if not settings: return if light['minion'] == self.config['id']: self.debug('light changed: ', light['title'], fields) for channel in light['channels']: uni_num = channel['universe'] if not self.universes.get(uni_num): self.universes[uni_num] = array.array('B', [0] * 512) self.fades[uni_num] = {} uni = self.universes[uni_num] addr = channel['address'] - 1 curr = uni[addr] try: value = light['values'][light['channels'].index(channel)] * 255 except IndexError: continue if not value == curr: starttime = self.meteortime.now() - time.time() + settings['time'] # Calculate offset of server's time vs local system's time self.fades[uni_num][addr] = Fade(uni[addr], value, starttime, settings['fade'], uni, addr) def run(self): while True: start = time.time() if self.ready: r, w, e = select.select(*self.selectargs) if len(r) > 0: self.ola.SocketReady() if start - self.last >= 1: self.meteortime.update() self.last = time.time() for uni, fades in self.fades.items(): for addr, fade in tuple(fades.items()): fade.tick() if fade.finished: del self.fades[uni][addr] for num, uni in self.universes.items(): self.ola.SendDmx(num, uni, None) try: time.sleep(rate - (time.time() - start)) except ValueError: continue except IOError: continue
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
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)
a = 1 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]]) # new part newmac = getHwAddr('eth0') humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) #timenow = time.strftime("%X") timenow = time.asctime( time.localtime(time.time()) ) print a if humidity is not None and temperature is not None and a == 1: print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity) #client.insert('sensors', {'name': 'DHT11 SENSOR 1' ,'temperature': temperature , 'humidity': humidity , 'time': timenow}) hum = round(humidity, 2) tem = round(temperature, 2) client.call('sensordata' , [tem, hum, newmac]) else: print 'Failed to get reading or the meteor server is not running. Try again after 60 sec!' time.sleep(60.0 - ((time.time() - starttime) % 60.0)) except KeyboardInterrupt: break # client.update('items' , {'name': 'c4:54:44:84:70:b4' }, {'status': 'Not Active'}) client.unsubscribe('local-items') client.unsubscribe('local-leds') # client.unsubscribe('local-sensors')
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]
def subscription_callback(error): if error: print(error) client.on('subscribed', subscribed) client.on('unsubscribed', unsubscribed) client.on('added', added) client.on('changed', changed) client.on('connected', connected) client.connect() client.subscribe('items') client.subscribe('params') client.call('removeItems', ['Python*'], callback_function) #client.call('addItem', ['1+1'], callback_function) # (sort of) hacky way to keep the client alive # ctrl + c to kill the script while True: try: time.sleep(1) except KeyboardInterrupt: break client.unsubscribe('items') client.close()
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()
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)')
# 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 client.call('systemonboot', [newmac]) if process_name not in tmp[:]: print "The process is not running." client.call('systemfail', [newmac]) sys.exit(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
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)')
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")
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]]) # new part newmac = getHwAddr('eth0') humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) #timenow = time.strftime("%X") timenow = time.asctime(time.localtime(time.time())) print a if humidity is not None and temperature is not None and a == 1: print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format( temperature, humidity) #client.insert('sensors', {'name': 'DHT11 SENSOR 1' ,'temperature': temperature , 'humidity': humidity , 'time': timenow}) hum = round(humidity, 2) tem = round(temperature, 2) client.call('sensordata', [tem, hum, newmac]) else: print 'Failed to get reading or the meteor server is not running. Try again after 60 sec!' time.sleep(60.0 - ((time.time() - starttime) % 60.0)) except KeyboardInterrupt: break # client.update('items' , {'name': 'c4:54:44:84:70:b4' }, {'status': 'Not Active'}) client.unsubscribe('local-items') client.unsubscribe('local-leds') # client.unsubscribe('local-sensors')
# (sort of) hacky way to keep the client alive # ctrl + c to kill the script while True: try: 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]]) # new part newmac = getHwAddr('eth0') humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) #timenow = time.strftime("%X") timenow = time.asctime( time.localtime(time.time()) ) if humidity is not None and temperature is not None: print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity) #client.insert('sensors', {'name': 'DHT11 SENSOR 1' ,'temperature': temperature , 'humidity': humidity , 'time': timenow}) client.call('sensordata' , [temperature, humidity, newmac]) else: print 'Failed to get reading. Try again!' a = 0 time.sleep(60.0 - ((time.time() - starttime) % 60.0)) except KeyboardInterrupt: break # client.update('items' , {'name': 'c4:54:44:84:70:b4' }, {'status': 'Not Active'}) client.unsubscribe('local-items') client.unsubscribe('local-leds') client.unsubscribe('local-sensors')
class _MeteorSender(object): 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'} } def connected_event(self): self.connected = True self.thread = threading.Thread(target=self.work) self.thread.start() def handle_call(self, error, result): self.error = error self.called = True def handle_run_call(self, error, result): self.handle_call(error, result) self.run = result.get('run', None) def work(self): while True: data = self.queue.get() if data == FINISH: return if self.run: data.update({'run': self.run}) # Handle the data op = data['op'] del data['op'] if op not in self.configs: continue config = self.configs[op] method = config.get('method') callback = config.get('callback', self.handle_call) try: self.called = False self.client.call(method, [data], callback) while not self.called: time.sleep(0.05) except: pass def close(self): self.thread.join()