Esempio n. 1
0
    def serve(self):
        try:
            self.serve_forever()

        except KeyboardInterrupt:
            print("Shutting down..")
            gpio.all_off()
            self.server_close()
            print("bye!")
            self.shutdown()
Esempio n. 2
0
    def handshake(self):
        data = self.request.recv(1024).strip()
        # data = data.split('\r\n', 1) ==> TypeError: Type str doesn't support the buffer API
        # needs to be: data.split(b'\r\n', 1)
        # print(data)
        # data = data[1]
        # data = data.splitlines()
        # print(data)
        data = data.split(b"\r\n", 1)[1]  # remove the HTTP header, but why?
        # print(data)
        # data = str(data)
        # data = StringIO(data)
        # print(data)
        headers = message_from_bytes(data)  # so that we can use the get method!
        # print(headers)
        upg = headers.get("Upgrade", None)
        # print(">%s<" % upg)
        if upg != "websocket":
            return

        print("Handshaking...")
        # turn all pins off
        gpio.all_off()

        key = headers["Sec-WebSocket-Key"]
        # digest = b64encode(sha1(key + self.magic).hexdigest().decode('hex'))
        # print(key)
        key = key + self.magic
        key = key.encode("ascii")
        digest = sha1(key)
        # print(digest)
        digest = digest.hexdigest()
        # print(digest)
        digest = bytes.fromhex(digest)
        # print(digest)
        digest = b64encode(digest)
        digest = digest.decode("utf8")
        # print(digest)
        response = "HTTP/1.1 101 Switching Protocols\r\n"
        response += "Upgrade: websocket\r\n"
        response += "Connection: Upgrade\r\n"
        # print(response)
        response += "Sec-WebSocket-Accept: "
        response += digest
        response += "\r\n\r\n"
        # print(response)
        response = bytes(response.encode("ascii"))
        # print(response)
        self.handshake_done = self.request.send(response)
Esempio n. 3
0
def put_to_power_sleep():
    voltage = get_battery_voltage()
    current = get_battery_current()
    try:
        if voltage < 2:
            voltage = voltage * 10
            printf(
                "Voltage reading biased: Reading {0} volt and {1} amps".format(
                    voltage, current))
        if voltage < 11.0:
            had_slept = None
            try:
                with open('/media/mmcblk0p1/logs/sleep.log', 'r') as sched_log:
                    had_slept = sched_log.read()
            except:
                pass
            if had_slept:
                all_off(1)
                printf(
                    'Voltage still too low, going back to a long sleep (1 hour). Reading {0} volt and {1} amps'
                    .format(voltage, current))
                call('rm /media/mmcblk0p1/logs/sleep.log', shell=True)
                # call(
                # "bash /media/mmcblk0p1/amigos/bash/sleep {0}".format(59), shell=True)
            else:
                with open('/media/mmcblk0p1/logs/sleep.log',
                          'w+') as sched_log:
                    sched_log.write('1')
                printf(
                    'Voltage too low, going back to 10 minutes sleep. Reading {0} volt and {1} amps'
                    .format(voltage, current))
                # call(
                # "bash /media/mmcblk0p1/amigos/bash/sleep {0}".format(10), shell = True)
        elif voltage > 14.0:
            printf('Voltage is too high. Reading {0} volt and {1} amps'.format(
                voltage, current))
        else:
            printf(
                'Voltage in normal operating range. Reading {0} volt and {1} amps'
                .format(voltage, current))
    except:
        printf('failed to excute put_to_sleep')
        traceback.print_exc(
            file=open("/media/mmcblk0p1/logs/system.log", "a+"))
Esempio n. 4
0
    # print(sys.path)

    try:
        gpio.setup()

        def simpleprint(self, message):
            # print("message %s" % message)
            try:
                actor = int(message.split()[0])
                wert = message.split()[1]
                if 0 <= actor <= 3:
                    # print("actor  %d, wert %d" % (actor, wert))
                    gpio.mag_st(actor, int(wert))

                if 4 <= actor <= 7:
                    if wert == "true":
                        wert = 0
                    else:
                        wert = 1
                    # print("W="+str(wert))
                    gpio.pol(actor - 4, wert)

            except ValueError:
                return

        WebSocketServer(port, simpleprint).serve()

    finally:
        # turn all pins off
        gpio.all_off()