Exemplo n.º 1
0
def gather_from_env(ap_ssid,ap_pass):
    env_vars = {
    'device_name' : str(uuid.uuid4())[:10],
    'device_id' : str(uuid.uuid4())[:10],
    'ssid' : ap_ssid,
    'ssid_pass' : ap_pass,
    'mqtt_host' : local_tools.lookup('mqtt')[0],
    'mqtt_port' : local_tools.lookup('mqtt')[1],
    'generator_name' : 'associative.py'
    }
    return env_vars
    def slurpd(self, device):

        slurped = []

        try:

            logger.info(device)
            name = device['name']
            addr = device['address']

            source = self.r.get("primitive_generic:{}".format(device['name']))
            source_ip, source_port = local_tools.lookup(source)

            zc = zerorpc.Client()
            zc.connect("tcp://{}:{}".format(source_ip, source_port))

            contents = zc('source', device['name'])

        except Exception as ex:
            logger.warn(ex)
            self.broadcast(channel="{class}::{function}::stderr",
                           message=str(ex),
                           subs=[{
                               "function": sys._getframe().f_code.co_name
                           }])
        return contents
Exemplo n.º 3
0
    def __init__(self):

        self.method = "primitive_virtual_camera"
        redis_ip, redis_port = local_tools.lookup('redis')
        self.r = redis.StrictRedis(host=redis_ip,
                                   port=str(redis_port),
                                   decode_responses=True)
        self.binary_r = redis.StrictRedis(host=redis_ip, port=str(redis_port))
        self.channel_delimiter = "/"
    def __init__(self):

        redis_ip, redis_port = local_tools.lookup('redis')
        self.r = redis.StrictRedis(host=redis_ip,
                                   port=str(redis_port),
                                   decode_responses=True)
        self.binary_r = redis.StrictRedis(host=redis_ip, port=str(redis_port))
        self.channel_delimiter = "/"
        self.channels = {}
        self.service_prefix = ""
        self.channels['exception'] = "{service}::{class}::stderr::{function}"
        self.channels['slurp'] = "slurp::{container}"
        self.channels['device'] = "slurp::{uid}::{container}"
Exemplo n.º 5
0
def listen(listen_on,listen_for):
    print("listening for {}".format(repr(listen_for)))
    redis_ip,redis_port = local_tools.lookup('redis')
    r = redis.StrictRedis(host=redis_ip, port=str(redis_port),decode_responses=True)
    pubsub = r.pubsub()
    pubsub.subscribe(listen_on)
    for item in pubsub.listen():
        #1 signals channel sub
        if item['data'] != 1:
            #{'pattern': None, 'data': 'hello world', 'channel': '/baz', 'type': 'message'}
            print("received {} on {}".format(item['data'],item['channel']))
            assert item['data'] == listen_for
            break
Exemplo n.º 6
0
    def slurpd(self, device):

        slurped = []

        try:

            logger.info(device)
            name = device['name']
            addr = device['address']

            source = self.r.get("{}:{}".format(self.method, device['name']))
            source_ip, source_port = local_tools.lookup(source)

            zc = zerorpc.Client()
            zc.connect("tcp://{}:{}".format(source_ip, source_port))

            contents = zc('source', device['name'])

            blob_uuid = str(uuid.uuid4())
            blob_key = "glworb_binary:" + blob_uuid
            self.binary_r.set(blob_key, contents)

            render_img_from_kv(binary_key, device['name'])

            self.broadcast(channel="slurp::{uid}::blob",
                           message=blob_key,
                           subs=[device, {
                               "blob_key": blob_key
                           }])

            slurped.append(blob_key)

        except Exception as ex:
            logger.warn(ex)
            self.broadcast(channel="{class}::{function}::stderr",
                           message=str(ex),
                           subs=[{
                               "function": sys._getframe().f_code.co_name
                           }])
        return slurped
    def broadcast(self, channel="", message="", subs=None):

        # subs is a list of dicts,
        # handle merging here
        substitutions = {}
        substitutions.update({"class": type(self).__name__})

        #zerorpc should be passed in as part of prefix...
        service_name = "zerorpc-{}".format(self.service_prefix +
                                           type(self).__name__)
        self_ip, self_port = local_tools.lookup(service_name)
        if self_ip and self_port:
            substitutions.update({"service": service_name})
            substitutions.update({"service_ip": self_ip})
            substitutions.update({"service_port": self_port})
            # hash of port:ip
            # substitutions.update({"service_hash":  })

        if subs is None:
            subs = []

        for s in subs:
            substitutions.update(s)

        formatted_channel = channel.format_map(Default(substitutions))
        formatted_channel = formatted_channel.replace("::",
                                                      self.channel_delimiter)

        # prepend a '/' for mqtt style channels
        if self.channel_delimiter == "/":
            formatted_channel = "/" + formatted_channel

        formatted_message = message.format_map(Default(substitutions))
        self.r.publish(formatted_channel, formatted_message)

        return (formatted_channel, formatted_message)
Exemplo n.º 8
0
def redis_connection():
    redis_ip,redis_port = local_tools.lookup('redis')
    r = redis.StrictRedis(host=redis_ip, port=str(redis_port),decode_responses=True)
    yield r
Exemplo n.º 9
0
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (c) 2018, Galen Curwen-McAdams

import redis
import consul
import io
import uuid
from contextlib import contextmanager
from PIL import Image, ImageDraw, ImageFont
from tesserocr import PyTessBaseAPI, PSM, image_to_text, OEM, RIL
from logzero import logger
from lings.routeling import route_broadcast
import local_tools

r_ip, r_port = local_tools.lookup('redis')
r = redis.StrictRedis(host=r_ip, port=str(r_port), decode_responses=True)
binary_r = redis.StrictRedis(host=r_ip, port=str(r_port))


@contextmanager
def open_image(uuid, key):
    key_bytes = None
    bytes_key = r.hget(uuid, key)
    key_bytes = binary_r.get(bytes_key)
    file = io.BytesIO()
    file.write(key_bytes)
    image = Image.open(file)
    yield image
    file = io.BytesIO()
    image.save(file, image.format)
Exemplo n.º 10
0
def redis_conn():

    r_ip, r_port = local_tools.lookup('redis')
    r = redis.StrictRedis(host=r_ip, port=str(r_port), decode_responses=True)
    return r
Exemplo n.º 11
0
def redis_binary_conn():

    r_ip, r_port = local_tools.lookup('redis')
    binary_r = redis.StrictRedis(host=r_ip, port=str(r_port))
    return binary_r