Exemple #1
0
    def getJSON(self, compact=False):
        if compact:
            f = 'f'
            v = 'v'
        else:
            f = 'function'
            v = 'value'

        json = {}
        for (bus, value) in BUSLIST.items():
            json[bus] = int(value["enabled"])

        gpios = {}
        if len(self.export) > 0:
            export = self.export
        else:
            export = range(GPIO.GPIO_COUNT)

        for gpio in export:
            gpios[gpio] = {}
            if compact:
                gpios[gpio][f] = GPIO.getFunction(gpio)
            else:
                gpios[gpio][f] = GPIO.getFunctionString(gpio)
            gpios[gpio][v] = int(GPIO.input(gpio))

            if GPIO.getFunction(gpio) == GPIO.PWM:
                (pwmType, value) = GPIO.getPulse(gpio).split(':')
                gpios[gpio][pwmType] = value

        json['GPIO'] = gpios
        return types.jsonDumps(json)
    def getJSON(self):
        json = "{"
        first = True
        for (alt, value) in FUNCTIONS.items():
            if not first:
                json += ", "
            json += '"%s": %d' % (alt, value["enabled"])
            first = False
        
        json += ', "GPIO":{\n'
        first = True
        for gpio in range(GPIO.GPIO_COUNT):
            if not first:
                json += ", \n"

            function = GPIO.getFunctionString(gpio)
            value = GPIO.input(gpio)
                    
            json += '"%d": {"function": "%s", "value": %d' % (gpio, function, value)
            if GPIO.getFunction(gpio) == GPIO.PWM:
                (type, value) = GPIO.getPulse(gpio).split(':')
                json  += ', "%s": %s' %  (type, value)
            json += '}'
            first = False
            
        json += "\n}}"
        return json
Exemple #3
0
    def getJSON(self):
        json = "{"
        first = True
        for (alt, value) in FUNCTIONS.items():
            if not first:
                json += ", "
            json += '"%s": %d' % (alt, value["enabled"])
            first = False
        
        json += ', "GPIO":{\n'
        first = True
        for gpio in range(GPIO.GPIO_COUNT):
            if not first:
                json += ", \n"

            function = GPIO.getFunctionString(gpio)
            value = GPIO.input(gpio)
                    
            json += '"%d": {"function": "%s", "value": %d' % (gpio, function, value)
            if GPIO.getFunction(gpio) == GPIO.PWM:
                (type, value) = GPIO.getPulse(gpio).split(':')
                json  += ', "%s": %s' %  (type, value)
            json += '}'
            first = False
            
        json += "\n}}"
        return json
Exemple #4
0
 def getJSON(self, compact=False):
     if compact:
         f = 'f'
         v = 'v'
     else:
         f = 'function'
         v = 'value'
     
     json = {}
     for (bus, value) in BUSLIST.items():
         json[bus] = int(value["enabled"])
     
     gpios = {}
     if len(self.export) > 0:
         export = self.export
     else:
         export = range(GPIO.GPIO_COUNT)
 
     for gpio in export:
         gpios[gpio] = {}
         if compact:
             gpios[gpio][f] = GPIO.getFunction(gpio)
         else:
             gpios[gpio][f] = GPIO.getFunctionString(gpio)
         gpios[gpio][v] = int(GPIO.input(gpio))
 
         if GPIO.getFunction(gpio) == GPIO.PWM:
             (pwmType, value) = GPIO.getPulse(gpio).split(':')
             gpios[gpio][pwmType] = value
     
     json['GPIO'] = gpios
     return types.jsonDumps(json)
    def do_GET(self, relativePath):
        # JSON full state
        if relativePath == "*":
            return (200, self.getJSON(), M_JSON)
            
        # RPi header map
        elif relativePath == "map":
            json = "%s" % MAPPING[GPIO.BOARD_REVISION]
            json = json.replace("'", '"')
            return (200, json, M_JSON)

        # server version
        elif relativePath == "version":
            return (200, SERVER_VERSION, M_PLAIN)

        # board revision
        elif relativePath == "revision":
            revision = "%s" % GPIO.BOARD_REVISION
            return (200, revision, M_PLAIN)

         # get temperature
        elif relativePath == "TEMP":
            strPath="/home/pi/test/thermostate/lastTemp.txt"
            f = open(strPath)
            strText = f.read()
            return (200, strText, M_PLAIN)


        # Single GPIO getter
        elif relativePath.startswith("GPIO/"):
            (mode, s_gpio, operation) = relativePath.split("/")
            gpio = int(s_gpio)

            value = None
            if operation == "value":
                if GPIO.input(gpio):
                    value = "1"
                else:
                    value = "0"
    
            elif operation == "function":
                value = GPIO.getFunctionString(gpio)
    
            elif operation == "pwm":
                if GPIO.isPWMEnabled(gpio):
                    value = "enabled"
                else:
                    value = "disabled"
                
            elif operation == "pulse":
                value = GPIO.getPulse(gpio)
                
            else:
                return (404, operation + " Not Found", M_PLAIN)
                
            return (200, value, M_PLAIN)

        else:
            return (0, None, None)
    def do_GET(self, relativePath):
        # JSON full state
        if relativePath == "*":
            return (200, self.getJSON(), M_JSON)

        # RPi header map
        elif relativePath == "map":
            json = "%s" % MAPPING[GPIO.BOARD_REVISION]
            json = json.replace("'", '"')
            return (200, json, M_JSON)

        # server version
        elif relativePath == "version":
            return (200, SERVER_VERSION, M_PLAIN)

        # board revision
        elif relativePath == "revision":
            revision = "%s" % GPIO.BOARD_REVISION
            return (200, revision, M_PLAIN)

        # get temperature
        elif relativePath == "TEMP":
            strPath = "/home/pi/test/thermostate/lastTemp.txt"
            f = open(strPath)
            strText = f.read()
            return (200, strText, M_PLAIN)

        # Single GPIO getter
        elif relativePath.startswith("GPIO/"):
            (mode, s_gpio, operation) = relativePath.split("/")
            gpio = int(s_gpio)

            value = None
            if operation == "value":
                if GPIO.input(gpio):
                    value = "1"
                else:
                    value = "0"

            elif operation == "function":
                value = GPIO.getFunctionString(gpio)

            elif operation == "pwm":
                if GPIO.isPWMEnabled(gpio):
                    value = "enabled"
                else:
                    value = "disabled"

            elif operation == "pulse":
                value = GPIO.getPulse(gpio)

            else:
                return (404, operation + " Not Found", M_PLAIN)

            return (200, value, M_PLAIN)

        else:
            return (0, None, None)
Exemple #7
0
    def wildcard(self, compact=False):
        if compact:
            f = "f"
            v = "v"
        else:
            f = "function"
            v = "value"

        values = {}
        print(self.export)
        for i in self.export:
            if compact:
                func = GPIO.getFunction(i)
            else:
                func = GPIO.getFunctionString(i)
            values[i] = {f: func, v: int(GPIO.digitalRead(i))}
        return values
Exemple #8
0
 def wildcard(self, compact=False):
     if compact:
         f = "f"
         v = "v"
     else:
         f = "function"
         v = "value"
         
     values = {}
     print(self.export)
     for i in self.export:
         if compact:
             func = GPIO.getFunction(i)
         else:
             func = GPIO.getFunctionString(i)
         values[i] = {f: func, v: int(GPIO.digitalRead(i))}
     return values
Exemple #9
0
    def writeJSON(self, out):
        json = "{"
        first = True
        for (alt, value) in FUNCTIONS.items():
            if not first:
                json += ", "
            json += '"%s": %d' % (alt, value["enabled"])
            first = False
        
        json += ', "GPIO":{\n'
        first = True
        for gpio in range(GPIO.GPIO_COUNT):
            if not first:
                json += ", \n"

            function = GPIO.getFunctionString(gpio)
            value = GPIO.input(gpio)
                
            json += '"%d": {"function": "%s", "value": %d}' % (gpio, function, value)
            first = False
            
        json += "\n}}"
        out.write(json.encode())
Exemple #10
0
    def do_POST(self, relativePath):
        if relativePath.startswith("GPIO/"):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)
            
            if operation == "value":
                if value == "0":
                    GPIO.output(gpio, GPIO.LOW)
                elif value == "1":
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    return (400, "Bad Value", M_PLAIN)
    
                return (200, value, M_PLAIN)

            elif operation == "function":
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    return (400, "Bad Function", M_PLAIN)

                value = GPIO.getFunctionString(gpio)
                return (200, value, M_PLAIN)

            elif operation == "sequence":
                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                return (200, sequence[-1], M_PLAIN)
                
            elif operation == "pwm":
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)
                
                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"
                
                return (200, result, M_PLAIN)
                
            elif operation == "pulse":
                GPIO.pulse(gpio)
                return (200, "OK", M_PLAIN)
                
            elif operation == "pulseRatio":
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                return (200, value, M_PLAIN)
                
            elif operation == "pulseAngle":
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                return (200, value, M_PLAIN)
                
            else: # operation unknown
                return (404, operation + " Not Found", M_PLAIN)
                
        elif relativePath.startswith("macros/"):
            (mode, fname, value) = relativePath.split("/")
            if fname in self.callbacks:
                callback = self.callbacks[fname]

                if ',' in value:
                    args = value.split(',')
                    result = callback(*args)
                elif len(value) > 0:
                    result = callback(value)
                else:
                    result = callback()
                     
                response = ""
                if result:
                    response = "%s" % result
                return (200, response, M_PLAIN)
                    
            else:
                return (404, fname + " Not Found", M_PLAIN)
                
        else: # path unknowns
            return (0, None, None)
Exemple #11
0
    def do_POST(self):
        if not self.checkAuthentication():
            return

        relativePath = self.path.replace(self.server.context, "")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:];

        if (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)
            
            if (operation == "value"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return;
                if (value == "0"):
                    GPIO.output(gpio, GPIO.LOW)
                elif (value == "1"):
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    self.send_error(400, "Bad Value")
                    return
    
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "function"):
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    self.send_error(400, "Bad Function")
                    return
                value = GPIO.getFunctionString(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "sequence"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return;

                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(sequence[-1].encode())
                
            elif (operation == "pwm"):
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)
                
                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"
                
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(result.encode())
                
            elif (operation == "pulse"):
                GPIO.pulse(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write("OK".encode())
                
            elif (operation == "pulseRatio"):
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())
                
            elif (operation == "pulseAngle"):
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())
                
            else: # operation unknown
                self.send_error(404, operation + " Not Found")
                return
        elif (relativePath.startswith("macros/")):
            (mode, fname, value) = relativePath.split("/")
            if (fname in self.server.callbacks):
                if ',' in value:
                    args = value.split(',')
                else:
                    args = [value]
                callback = self.server.callbacks[fname]
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(callback(*args).encode())
            else:
                self.send_error(404, fname + " Not Found")
                return
                
        else: # path unknowns
            self.send_error(404, "Not Found")
Exemple #12
0
    def do_POST(self):
        if not self.checkAuthentication():
            return

        relativePath = self.path.replace(self.server.context, "")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:]

        if (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)

            if (operation == "value"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return
                if (value == "0"):
                    GPIO.output(gpio, GPIO.LOW)
                elif (value == "1"):
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    self.send_error(400, "Bad Value")
                    return

                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "function"):
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    self.send_error(400, "Bad Function")
                    return
                value = GPIO.getFunctionString(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "sequence"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return

                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(sequence[-1].encode())

            elif (operation == "pwm"):
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)

                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"

                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(result.encode())

            elif (operation == "pulse"):
                GPIO.pulse(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write("OK".encode())

            elif (operation == "pulseRatio"):
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "pulseAngle"):
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(value.encode())

            else:  # operation unknown
                self.send_error(404, operation + " Not Found")
                return
        elif (relativePath.startswith("macros/")):
            (mode, fname, value) = relativePath.split("/")
            if (fname in self.server.callbacks):
                if ',' in value:
                    args = value.split(',')
                else:
                    args = [value]
                callback = self.server.callbacks[fname]
                self.send_response(200)
                self.send_header("Content-type", "text/plain")
                self.end_headers()
                self.wfile.write(callback(*args).encode())
            else:
                self.send_error(404, fname + " Not Found")
                return

        else:  # path unknowns
            self.send_error(404, "Not Found")
Exemple #13
0
    def do_GET(self):
        if not self.checkAuthentication():
            return

        relativePath = self.path.replace(self.server.context, "/")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:]

        # JSON full state
        if relativePath == "*":
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.server.writeJSON(self.wfile)

        elif relativePath == "map":
            json = "%s" % MAPPING[GPIO.BOARD_REVISION]
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.wfile.write(json.replace("'", '"').encode())

        # version
        elif relativePath == "version":
            self.send_response(200)
            self.send_header("Content-type", "text/plain")
            self.end_headers()
            self.wfile.write(SERVER_VERSION.encode())

        # Single GPIO getter
        elif (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation) = relativePath.split("/")
            gpio = int(s_gpio)

            value = None
            if (operation == "value"):
                if GPIO.input(gpio):
                    value = "1"
                else:
                    value = "0"

            elif (operation == "function"):
                value = GPIO.getFunctionString(gpio)

            elif (operation == "pwm"):
                if GPIO.isPWMEnabled(gpio):
                    value = "enabled"
                else:
                    value = "disabled"

            elif (operation == "pulse"):
                value = GPIO.getPulse(gpio)

            else:
                self.send_error(404, operation + " Not Found")
                return

            self.send_response(200)
            self.send_header("Content-type", "text/plain")
            self.end_headers()
            self.wfile.write(value.encode())

        # handle files
        else:
            if relativePath == "":
                relativePath = self.server.index

            realPath = relativePath

            if not os.path.exists(realPath):
                realPath = self.server.docroot + os.sep + relativePath

            if not os.path.exists(realPath):
                self.send_error(404, "Not Found")
                return

            realPath = os.path.realpath(realPath)

            if realPath.endswith(".py"):
                self.send_error(403, "Not Authorized")
                return

            if not (realPath.startswith(self.server.docroot)
                    or realPath.startswith(os.getcwd())):
                self.send_error(403, "Not Authorized")
                return

            if (os.path.isdir(realPath)):
                realPath += os.sep + self.server.index
                if not os.path.exists(realPath):
                    self.send_error(403, "Not Authorized")
                    return

            (type, encoding) = mime.guess_type(realPath)
            f = open(realPath)
            data = f.read()
            f.close()
            self.send_response(200)
            self.send_header("Content-type", type)
            #            self.send_header("Content-length", os.path.getsize(realPath))
            self.end_headers()
            try:
                self.wfile.write(data.encode())
            except UnicodeDecodeError:
                self.wfile.write(data)
    def do_POST(self, relativePath):
        if relativePath.startswith("EGG/"):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            if s_gpio == "1":
                gpio = 7
            elif s_gpio == "2":
                gpio = 8
            elif s_gpio == "3":
                gpio = 9
            elif s_gpio == "4":
                gpio = 10
            elif s_gpio == "5":
                gpio = 11
            elif s_gpio == "6":
                gpio = 22
            elif s_gpio == "7":
                gpio = 23
            elif s_gpio == "8":
                gpio = 24
            else:
                gpio = 25

            if operation == "value":
                if value == "off":
                    GPIO.output(gpio, GPIO.LOW)
                elif value == "on":
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    return (400, "Bad Value", M_PLAIN)
    
                return (200, value, M_PLAIN)

            elif operation == "function":
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    return (400, "Bad Function", M_PLAIN)

                value = GPIO.getFunctionString(gpio)
                return (200, value, M_PLAIN)

            elif operation == "sequence":
                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                return (200, sequence[-1], M_PLAIN)
                
            elif operation == "pwm":
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)
                
                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"
                
                return (200, result, M_PLAIN)
                
            elif operation == "pulse":
                GPIO.pulse(gpio)
                return (200, "OK", M_PLAIN)
                
            elif operation == "pulseRatio":
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                return (200, value, M_PLAIN)
                
            elif operation == "pulseAngle":
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                return (200, value, M_PLAIN)
                
            else: # operation unknown
                return (404, operation + " Not Found", M_PLAIN)
                
        elif relativePath.startswith("macros/"):
            (mode, fname, value) = relativePath.split("/")
            if fname in self.callbacks:
                callback = self.callbacks[fname]

                if ',' in value:
                    args = value.split(',')
                    result = callback(*args)
                elif len(value) > 0:
                    result = callback(value)
                else:
                    result = callback()
                     
                response = ""
                if result:
                    response = "%s" % result
                return (200, response, M_PLAIN)
                    
            else:
                return (404, fname + " Not Found", M_PLAIN)

        elif operation == "saca17":
            GPIO.setFunction(17, GPIO.OUT)
        
        elif operation == "saca171":
            GPIO.output(17,GPIO.HIGH)

        elif operation == "saca170":
            GPIO.output(17,GPIO.LOW)

        elif operation == "saca21":
            GPIO.setFunction(21, GPIO.OUT)
        
        elif operation == "saca211":
            GPIO.output(21,GPIO.HIGH)

        elif operation == "saca210":
            GPIO.output(21,GPIO.LOW)

        elif relativePath.startswith("EGGSOFF"):
            GPIO.output(7, GPIO.LOW)
            GPIO.output(8, GPIO.LOW)
            GPIO.output(9, GPIO.LOW)
            GPIO.output(10, GPIO.LOW)
            GPIO.output(11, GPIO.LOW)
            GPIO.output(22, GPIO.LOW)
            GPIO.output(23, GPIO.LOW)
            GPIO.output(24, GPIO.LOW)
            GPIO.output(25, GPIO.LOW)
            return (200, "ALL EGGS OFF", M_PLAIN)

        elif relativePath.startswith("EGGSON"):
            GPIO.output(7, GPIO.HIGH)
            GPIO.output(8, GPIO.HIGH)
            GPIO.output(9, GPIO.HIGH)
            GPIO.output(10, GPIO.HIGH)
            GPIO.output(11, GPIO.HIGH)
            GPIO.output(22, GPIO.HIGH)
            GPIO.output(23, GPIO.HIGH)
            GPIO.output(24, GPIO.HIGH)
            GPIO.output(25, GPIO.HIGH)
            return (200, "ALL EGGS ON", M_PLAIN)
    

        else: # path unknowns
            return (0, None, None)
    def do_POST(self, relativePath):
        if relativePath.startswith("EGG/"):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            if s_gpio == "1":
                gpio = 7
            elif s_gpio == "2":
                gpio = 8
            elif s_gpio == "3":
                gpio = 9
            elif s_gpio == "4":
                gpio = 10
            elif s_gpio == "5":
                gpio = 11
            elif s_gpio == "6":
                gpio = 22
            elif s_gpio == "7":
                gpio = 23
            elif s_gpio == "8":
                gpio = 24
            else:
                gpio = 25

            if operation == "value":
                if value == "off":
                    GPIO.output(gpio, GPIO.LOW)
                elif value == "on":
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    return (400, "Bad Value", M_PLAIN)

                return (200, value, M_PLAIN)

            elif operation == "function":
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    return (400, "Bad Function", M_PLAIN)

                value = GPIO.getFunctionString(gpio)
                return (200, value, M_PLAIN)

            elif operation == "sequence":
                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                return (200, sequence[-1], M_PLAIN)

            elif operation == "pwm":
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)

                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"

                return (200, result, M_PLAIN)

            elif operation == "pulse":
                GPIO.pulse(gpio)
                return (200, "OK", M_PLAIN)

            elif operation == "pulseRatio":
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                return (200, value, M_PLAIN)

            elif operation == "pulseAngle":
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                return (200, value, M_PLAIN)

            else:  # operation unknown
                return (404, operation + " Not Found", M_PLAIN)

        elif relativePath.startswith("macros/"):
            (mode, fname, value) = relativePath.split("/")
            if fname in self.callbacks:
                callback = self.callbacks[fname]

                if ',' in value:
                    args = value.split(',')
                    result = callback(*args)
                elif len(value) > 0:
                    result = callback(value)
                else:
                    result = callback()

                response = ""
                if result:
                    response = "%s" % result
                return (200, response, M_PLAIN)

            else:
                return (404, fname + " Not Found", M_PLAIN)

        elif operation == "saca17":
            GPIO.setFunction(17, GPIO.OUT)

        elif operation == "saca171":
            GPIO.output(17, GPIO.HIGH)

        elif operation == "saca170":
            GPIO.output(17, GPIO.LOW)

        elif operation == "saca21":
            GPIO.setFunction(21, GPIO.OUT)

        elif operation == "saca211":
            GPIO.output(21, GPIO.HIGH)

        elif operation == "saca210":
            GPIO.output(21, GPIO.LOW)

        elif relativePath.startswith("EGGSOFF"):
            GPIO.output(7, GPIO.LOW)
            GPIO.output(8, GPIO.LOW)
            GPIO.output(9, GPIO.LOW)
            GPIO.output(10, GPIO.LOW)
            GPIO.output(11, GPIO.LOW)
            GPIO.output(22, GPIO.LOW)
            GPIO.output(23, GPIO.LOW)
            GPIO.output(24, GPIO.LOW)
            GPIO.output(25, GPIO.LOW)
            return (200, "ALL EGGS OFF", M_PLAIN)

        elif relativePath.startswith("EGGSON"):
            GPIO.output(7, GPIO.HIGH)
            GPIO.output(8, GPIO.HIGH)
            GPIO.output(9, GPIO.HIGH)
            GPIO.output(10, GPIO.HIGH)
            GPIO.output(11, GPIO.HIGH)
            GPIO.output(22, GPIO.HIGH)
            GPIO.output(23, GPIO.HIGH)
            GPIO.output(24, GPIO.HIGH)
            GPIO.output(25, GPIO.HIGH)
            return (200, "ALL EGGS ON", M_PLAIN)

        else:  # path unknowns
            return (0, None, None)
Exemple #16
0
    def do_POST(self, relativePath):
        if relativePath.startswith("GPIO/"):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)

            if operation == "value":
                if value == "0":
                    GPIO.output(gpio, GPIO.LOW)
                elif value == "1":
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    return (400, "Bad Value", M_PLAIN)

                return (200, value, M_PLAIN)

            elif operation == "function":
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                elif value == "pwm":
                    GPIO.setFunction(gpio, GPIO.PWM)
                else:
                    return (400, "Bad Function", M_PLAIN)

                value = GPIO.getFunctionString(gpio)
                return (200, value, M_PLAIN)

            elif operation == "sequence":
                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                return (200, sequence[-1], M_PLAIN)

            elif operation == "pwm":
                if value == "enable":
                    GPIO.enablePWM(gpio)
                elif value == "disable":
                    GPIO.disablePWM(gpio)

                if GPIO.isPWMEnabled(gpio):
                    result = "enabled"
                else:
                    result = "disabled"

                return (200, result, M_PLAIN)

            elif operation == "pulse":
                GPIO.pulse(gpio)
                return (200, "OK", M_PLAIN)

            elif operation == "pulseRatio":
                ratio = float(value)
                GPIO.pulseRatio(gpio, ratio)
                return (200, value, M_PLAIN)

            elif operation == "pulseAngle":
                angle = float(value)
                GPIO.pulseAngle(gpio, angle)
                return (200, value, M_PLAIN)

            else:  # operation unknown
                return (404, operation + " Not Found", M_PLAIN)

        elif relativePath.startswith("macros/"):
            (mode, fname, value) = relativePath.split("/")
            if fname in self.callbacks:
                callback = self.callbacks[fname]

                if "," in value:
                    args = value.split(",")
                    result = callback(*args)
                elif len(value) > 0:
                    result = callback(value)
                else:
                    result = callback()

                response = ""
                if result:
                    response = "%s" % result
                return (200, response, M_PLAIN)

            else:
                return (404, fname + " Not Found", M_PLAIN)

        else:  # path unknowns
            return (0, None, None)
Exemple #17
0
    def do_POST(self):
        if not self.checkAuthentication():
            return

        relativePath = self.path.replace(self.server.context, "")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:];

        if (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation, value) = relativePath.split("/")
            gpio = int(s_gpio)
            
            if (operation == "value"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return;
                if (value == "0"):
                    GPIO.output(gpio, GPIO.LOW)
                elif (value == "1"):
                    GPIO.output(gpio, GPIO.HIGH)
                else:
                    self.send_error(400, "Bad Value")
                    return
    
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "function"):
                value = value.lower()
                if value == "in":
                    GPIO.setFunction(gpio, GPIO.IN)
                elif value == "out":
                    GPIO.setFunction(gpio, GPIO.OUT)
                else:
                    self.send_error(400, "Bad Function")
                    return
                value = GPIO.getFunctionString(gpio)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(value.encode())

            elif (operation == "sequence"):
                if GPIO.getFunction(gpio) != GPIO.OUT:
                    self.send_error(400, "Not Output")
                    return;

                (period, sequence) = value.split(",")
                period = int(period)
                GPIO.outputSequence(gpio, period, sequence)
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(sequence[-1].encode())
                
            else: # operation unknown
                self.send_error(404, operation + " Not Found")
                return
        elif (relativePath.startswith("macros/")):
            (mode, fname, value) = relativePath.split("/")
            if (fname in self.server.callbacks):
                callback = self.server.callbacks[fname]
                self.send_response(200)
                self.send_header("Content-type", "text/plain");
                self.end_headers()
                self.wfile.write(callback(value).encode())
            else:
                self.send_error(404, fname + " Not Found")
                return
                
        else: # path unknowns
            self.send_error(404, "Not Found")
Exemple #18
0
    def do_GET(self):
        if not self.checkAuthentication():
            return
        
        relativePath = self.path.replace(self.server.context, "/")
        if (relativePath.startswith("/")):
            relativePath = relativePath[1:];

        # JSON full state
        if relativePath == "*":
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.server.writeJSON(self.wfile)
            
        elif relativePath == "map":
            json = "%s" % MAPPING[GPIO.BOARD_REVISION]
            self.send_response(200)
            self.send_header("Content-type", "application/json")
            self.end_headers()
            self.wfile.write(json.replace("'", '"').encode())

        # version
        elif relativePath == "version":
            self.send_response(200)
            self.send_header("Content-type", "text/plain")
            self.end_headers()
            self.wfile.write(SERVER_VERSION.encode())

        # Single GPIO getter
        elif (relativePath.startswith("GPIO/")):
            (mode, s_gpio, operation) = relativePath.split("/")
            gpio = int(s_gpio)

            value = None
            if (operation == "value"):
                value = GPIO.input(gpio)
    
            elif (operation == "function"):
                value = GPIO.getFunctionString(gpio)
    
            else:
                self.send_error(404, operation + " Not Found")
                return
                
            self.send_response(200)
            self.send_header("Content-type", "text/plain");
            self.end_headers()
            self.wfile.write(value.encode())

        # handle files
        else:
            if relativePath == "":
                relativePath = self.server.index
                            
            realPath = relativePath;
            
            if not os.path.exists(realPath):
                realPath = self.server.docroot + os.sep + relativePath
                
            if not os.path.exists(realPath):
                self.send_error(404, "Not Found")
                return

            realPath = os.path.realpath(realPath)
            
            if realPath.endswith(".py"):
                self.send_error(403, "Not Authorized")
                return
            
            if not (realPath.startswith(self.server.docroot) or realPath.startswith(os.getcwd())):
                self.send_error(403, "Not Authorized")
                return
                
            if (os.path.isdir(realPath)):
                realPath += os.sep + self.server.index;
                if not os.path.exists(realPath):
                    self.send_error(403, "Not Authorized")
                    return
                
            (type, encoding) = mime.guess_type(realPath)
            f = open(realPath)
            data = f.read()
            f.close()
            self.send_response(200)
            self.send_header("Content-type", type);
#            self.send_header("Content-length", os.path.getsize(realPath))
            self.end_headers()
            try:
                self.wfile.write(data.encode())
            except UnicodeDecodeError:
                self.wfile.write(data)