Beispiel #1
0
class PlugDHandler(object):
    def __init__(self):
        self.plug = PlugD()

    def html(self):
        html = os.path.expanduser("~/.plug/toggle_index.html")
        if not os.path.exists(html):
            return ""

        result = StringIO()
        with open(os.path.expanduser("~/.plug/toggle_index.html")) as fh:
            for line in fh:
                if "##PLUG_IMAGES##" in line:
                    self.button_list(result)
                elif "##HUM_TEM##" in line:
                    self.add_environment(result)
                else:
                    result.write(line)
        return result.getvalue()

    def button_list(self, output):
        states = self.plug.status()
        for state in states:
            output.write("<a class=\"plug_button\" href=\"toggle/%s\">" % (state.name))
            output.write("<img class=\"plug_icon\" src=\"icon/{0}\" alt=\"{0}\" /></a>".format(state.name))

    def add_environment(self, output):
        output.write(time.strftime("%H:%M ") + " ".join(hum_tem()))

    def icon(self, name, encoding="PNG"):
        states = self.plug.status()
        wanted = [state for state in states if state.name == name]
        if len(wanted) != 1:
            return None
        return PlugIcon(wanted[0]).to_binary(encoding=encoding)

    def command(self, com, target):
        com_table = {
            "on": "enable",
            "off": "disable",
            "toggle": "toggle"
            }
        if com not in com_table:
            return False
        com = com_table[com]

        states = self.plug.status()
        if len([state for state in states if state.name == target]) != 1:
            return False
        result = getattr(self.plug, com)(target)
        if result is None:
            return False
        return True

    def css(self, bare):
        css = os.path.expanduser("~/.plug/toggle_index.css")
        if os.path.exists(css):
            return open(css, 'r').read()
        else:
            return ""
Beispiel #2
0
class PlugDImageAnnotator(ImageAnnotator):
    def __init__(self, imgdata):
        super(PlugDImageAnnotator, self).__init__(imgdata)
        self.plug = PlugD()

    def annotate(self, encoding="JPEG"):
        states = self.plug.status()
        y_pos = 0
        for state in states:
            icon = PlugIcon(state).image
            self.image.paste(icon, (0, y_pos), icon)
            y_pos += icon.size[1]
        return self._encode(encoding=encoding)
class PlugDImageAnnotator(ImageAnnotator):
    def __init__(self, imgdata):
        super(PlugDImageAnnotator, self).__init__(imgdata)
        self.plug = PlugD()

    def annotate(self, encoding="JPEG"):
        states = self.plug.status()
        y_pos = 0
        for state in states:
            icon = PlugIcon(state).image
            self.image.paste(icon, (0, y_pos), icon)
            y_pos += icon.size[1]
        return self._encode(encoding=encoding)
Beispiel #4
0
class HumTemCamera(Camera):
    def __init__(self, *args, **kwargs):
        super(HumTemCamera, self).__init__(*args, **kwargs)
        self.plug = PlugD()

    def quickshot(self):
        if self.plug.lights_on():
            return super(HumTemCamera, self).quickshot()
        else:
            return self.snapshot()

    def annotate_text(self):
        return "%s %s" % ((super(HumTemCamera, self).annotate_text(), " ".join(hum_tem())))
Beispiel #5
0
class HumTemCamera(Camera):
    def __init__(self, *args, **kwargs):
        super(HumTemCamera, self).__init__(*args, **kwargs)
        self.plug = PlugD()

    def quickshot(self):
        if self.plug.lights_on():
            return super(HumTemCamera, self).quickshot()
        else:
            return self.snapshot()

    def annotate_text(self):
        return "%s %s" % (
            (super(HumTemCamera, self).annotate_text(), " ".join(hum_tem())))
Beispiel #6
0
 def __init__(self, imgdata):
     super(PlugDImageAnnotator, self).__init__(imgdata)
     self.plug = PlugD()
Beispiel #7
0
 def __init__(self, *args, **kwargs):
     super(HumTemCamera, self).__init__(*args, **kwargs)
     self.plug = PlugD()
 def __init__(self, imgdata):
     super(PlugDImageAnnotator, self).__init__(imgdata)
     self.plug = PlugD()
class ControlCenter(BaseHandler):
    def __init__(self, baseurl, options, lock):
        self.plug = PlugD()
        self.baseurl = baseurl

    def handle(self, req, args):
        if not args or not args[0]:
            req.text_response(self.html())
            return

        target = args[0]
        if target == "icon":
            self.handle_icon(req, args[1:])
        elif target == "css":
            req.text_response(self.css(), encoding="css")
        elif target in ("on", "off", "toggle"):
            self.handle_command(req, args)
        else:
            req.send_error(404)

    def html(self):
        html = os.path.expanduser("~/.plug/toggle_index.html")
        if not os.path.exists(html):
            return ""

        result = StringIO()
        with open(os.path.expanduser("~/.plug/toggle_index.html")) as fh:
            for line in fh:
                if "##PLUG_IMAGES##" in line:
                    self.button_list(result)
                elif "##HUM_TEM##" in line:
                    self.add_environment(result)
                else:
                    result.write(line)
        return result.getvalue()

    def button_list(self, output):
        states = self.plug.status()
        for state in states:
            output.write("<a class=\"plug_button\" href=\"toggle/%s\">" % (state.name))
            output.write("<img class=\"plug_icon\" src=\"icon/{0}\" alt=\"{0}\" /></a>".format(state.name))

    def add_environment(self, output):
        output.write(time.strftime("%H:%M ") + " ".join(hum_tem()))

    def css(self):
        css = os.path.expanduser("~/.plug/toggle_index.css")
        if os.path.exists(css):
            return open(css, 'r').read()
        else:
            return ""

    @needs_args(1)
    def handle_icon(self, req, args):
        target = args.pop(0)
        bare, flags = req.argsplit(args)

        encoding = "PNG" if not "encoding" in flags else flags["encoding"]
        icon = self.icon(urllib.unquote(target), encoding=encoding)
        if icon is None:
            req.send_error(404)
        else:
            req.image_response(icon, encoding=encoding)
            
    def icon(self, name, encoding="PNG"):
        states = self.plug.status()
        wanted = [state for state in states if state.name == name]
        if len(wanted) != 1:
            return None
        return PlugIcon(wanted[0]).to_binary(encoding=encoding)

    @needs_args(2)
    def handle_command(self, req, args):
        command, target = args[:2]
        target = urllib.unquote(target)
        success = self.command(command, target)
        if success:
            req.redirect_response("/%s/" % self.baseurl, text="success")
        else:
            req.send_error(404)

    def command(self, com, target):
        com_table = {
            "on": "enable",
            "off": "disable",
            "toggle": "toggle"
            }
        if com not in com_table:
            return False
        com = com_table[com]

        states = self.plug.status()
        if len([state for state in states if state.name == target]) != 1:
            return False
        result = getattr(self.plug, com)(target)
        if result is None:
            return False
        return True
 def __init__(self, baseurl, options, lock):
     self.plug = PlugD()
     self.baseurl = baseurl
Beispiel #11
0
 def __init__(self, *args, **kwargs):
     super(HumTemCamera, self).__init__(*args, **kwargs)
     self.plug = PlugD()
Beispiel #12
0
 def __init__(self):
     self.plug = PlugD()
 def __init__(self, baseurl, options, lock):
     self.plug = PlugD()
     self.baseurl = baseurl
class ControlCenter(BaseHandler):
    def __init__(self, baseurl, options, lock):
        self.plug = PlugD()
        self.baseurl = baseurl

    def handle(self, req, args):
        if not args or not args[0]:
            req.text_response(self.html())
            return

        target = args[0]
        if target == "icon":
            self.handle_icon(req, args[1:])
        elif target == "css":
            req.text_response(self.css(), encoding="css")
        elif target in ("on", "off", "toggle"):
            self.handle_command(req, args)
        else:
            req.send_error(404)

    def html(self):
        html = os.path.expanduser("~/.plug/toggle_index.html")
        if not os.path.exists(html):
            return ""

        result = StringIO()
        with open(os.path.expanduser("~/.plug/toggle_index.html")) as fh:
            for line in fh:
                if "##PLUG_IMAGES##" in line:
                    self.button_list(result)
                elif "##HUM_TEM##" in line:
                    self.add_environment(result)
                else:
                    result.write(line)
        return result.getvalue()

    def button_list(self, output):
        states = self.plug.status()
        for state in states:
            output.write("<a class=\"plug_button\" href=\"toggle/%s\">" %
                         (state.name))
            output.write(
                "<img class=\"plug_icon\" src=\"icon/{0}\" alt=\"{0}\" /></a>".
                format(state.name))

    def add_environment(self, output):
        output.write(time.strftime("%H:%M ") + " ".join(hum_tem()))

    def css(self):
        css = os.path.expanduser("~/.plug/toggle_index.css")
        if os.path.exists(css):
            return open(css, 'r').read()
        else:
            return ""

    @needs_args(1)
    def handle_icon(self, req, args):
        target = args.pop(0)
        bare, flags = req.argsplit(args)

        encoding = "PNG" if not "encoding" in flags else flags["encoding"]
        icon = self.icon(urllib.unquote(target), encoding=encoding)
        if icon is None:
            req.send_error(404)
        else:
            req.image_response(icon, encoding=encoding)

    def icon(self, name, encoding="PNG"):
        states = self.plug.status()
        wanted = [state for state in states if state.name == name]
        if len(wanted) != 1:
            return None
        return PlugIcon(wanted[0]).to_binary(encoding=encoding)

    @needs_args(2)
    def handle_command(self, req, args):
        command, target = args[:2]
        target = urllib.unquote(target)
        success = self.command(command, target)
        if success:
            req.redirect_response("/%s/" % self.baseurl, text="success")
        else:
            req.send_error(404)

    def command(self, com, target):
        com_table = {"on": "enable", "off": "disable", "toggle": "toggle"}
        if com not in com_table:
            return False
        com = com_table[com]

        states = self.plug.status()
        if len([state for state in states if state.name == target]) != 1:
            return False
        result = getattr(self.plug, com)(target)
        if result is None:
            return False
        return True