PKaK_P1microdrop_client/__init__.py""" Library for controlling Microdrop """ __version__ = '0.0.1' import asyncio from threading import Timer from time import sleep, time from random import randint import pydash as _ from mqttclient import MqttClient DEFAULT_TIMEOUT = 10000.0 def generate_id(): timestamp = str(time()).replace(".","") randnum = randint(1,1000) return f'microdrop-py-{timestamp}-{randnum}' class MyMqttClient(MqttClient): def __init__(self): self._name = generate_id() super().__init__() def listen(self): self.trigger('client-ready', 'null') @property def name(self): return self._name class Microdrop: """ Library for controlling Microdrop """ def __init__(self): self.mqttclient = MyMqttClient() self.loop = asyncio.get_event_loop() def execute(self, protocol, *args, **kwargs): self.loop.run_until_complete(protocol(self, *args, **kwargs)) def safe(self, method): return lambda *m, **kw: self.loop.call_soon_threadsafe(method, *m, **kw) def reset_client(self, timeout=DEFAULT_TIMEOUT): """ Create new client """ future = asyncio.Future() self.mqttclient.client.disconnect() del self.mqttclient self.mqttclient = MyMqttClient() def client_ready(*args): self.connected = True if (not future.done()): future.set_result('client is ready') if self.mqttclient.connected: client_ready(self) else: self.mqttclient.on('client-ready', self.safe(client_ready)) self.timeout(future, timeout) return future def timeout(self, future, timeout=DEFAULT_TIMEOUT): msg = f'timed out in {timeout}ms' def t(): if (not future.done()): future.set_exception(Exception(msg)) Timer(timeout/1000.0, t).start() async def get_state(self, sender, prop, timeout=DEFAULT_TIMEOUT): """ Get state of microdrop property """ LABEL = '' topic = f'microdrop/{sender}/state/{prop}' try: await self.reset_client() future = asyncio.Future() def state_msg(payload): if (not future.done()): future.set_result(payload) self.mqttclient.on_state_msg(sender, prop, self.safe(state_msg)) self.timeout(future, timeout) return await future except Exception as err: raise self.dump_stack([LABEL, topic], err) async def get_subscriptions(self, receiver, timeout=DEFAULT_TIMEOUT): LABEL = f'' try: if not isinstance(val, dict): msg = {} _.set_(msg, prop, val) val = msg await self.reset_client() return await self.call_action(receiver, prop, val, 'put', timeout) except Exception as err: raise self.dump_stack(LABEL, err) async def trigger_plugin(self, receiver, action, val={}, timeout=DEFAULT_TIMEOUT): LABEL = f'' try: await self.reset_client() result = await self.call_action(receiver, action, val, 'trigger', timeout) return result except Exception as err: raise self.dump_stack(LABEL, err) async def call_action(self, receiver, action, val, msg_type='trigger', timeout=DEFAULT_TIMEOUT): LABEL = '' no_timeout = False if (timeout == -1): timeout = DEFAULT_TIMEOUT no_timeout = True topic = f'microdrop/{msg_type}/{receiver}/{action}' sub = f'microdrop/{receiver}/notify/{self.mqttclient.name}/action' _.set_(val, "__head__.plugin_name", self.mqttclient.name) future = asyncio.Future() def notify_msg(payload): _.pull(self.mqttclient.subscriptions, sub) self.mqttclient.client.unsubscribe(sub) # XXX: no method to remove routes for wheezy.routing # ignoring for now, but might causes issues in the future if (payload['status']): if (payload['status'] != 'success'): if (not future.done()): future.set_exception(self.dump_stack(LABEL, Exception(payload['response']))) else: print(f'warning: no status key for topic: {topic}') if (not future.done()): future.set_result(payload) self.mqttclient.on_notify_msg(receiver, action, self.safe(notify_msg)) self.mqttclient.send_message(topic, val) if (not no_timeout): self.timeout(future, timeout) return await future def dump_stack(self, label, err): return Exception(_.flatten_deep([label, err.args])) PK^K ##(microdrop_client-0.0.1.dist-info/LICENSEBSD 2-Clause License Copyright (c) 2017, Lucas Zeer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. PK!H0RR&microdrop_client-0.0.1.dist-info/WHEEL1 W% 0n1JBZS`ޝ/Qf:3&.~l" kW*дY00?PK!H]a$9 )microdrop_client-0.0.1.dist-info/METADATAuAO0 8'݆!hԡ MCIT~=(e qo k$:D㝄X'Z5*:MuF;bf!bZ T!((3ݷ$ۖ2CP mV 1O(bݔ9_4AB xSkНL1 4 &g{ w槝H3cSߧtNBqt T"G@WC#l1G Z0 PK!H5'microdrop_client-0.0.1.dist-info/RECORDͻv0o H(")Q(SįdIӖqrgU?cV>z@ 9 9rѫ:MF/)#i@RPA%)zU[;<2Rdϖ[r V4AE