Ejemplo n.º 1
0
class Sensors():

    def __init__(self, config):
        self.config = config
        self.httprequest = HTTPRequest(config)

    def list(self):
        response = self.httprequest.request('sensors/list', {})
        return response

    def read(self, id):
        response = self.httprequest.request('sensor/info', {"id": id})
        return response['data']

    def readall(self):
        result = {}
        sensors = self.list()

        for sensor in sensors['sensor']:
            result[sensor['name']] = {}
            sensordata = self.read(sensor['id'])
            for data in sensordata:
                result[sensor['name']][data['name']] = data['value']

        return result
Ejemplo n.º 2
0
class Devices:
    def __init__(self, config):
        self.config = config
        self.httprequest = HTTPRequest(config)

    def list(self):
        response = self.httprequest.request("devices/list", {})
        return response

    def read(self, id):
        response = self.httprequest.request("device/info", {"id": id})
        return response

    def readall(self):
        result = []
        devices = self.list()
        for device in devices["device"]:
            result.append(device)
        return result

    def turnOn(self, id):
        response = self.httprequest.request("device/turnOn", {"id": id})
        return response

    def turnOff(self, id):
        response = self.httprequest.request("device/turnOff", {"id": id})
        return response
Ejemplo n.º 3
0
 def __init__(self, config):
     self.config = config
     self.httprequest = HTTPRequest(config)
Ejemplo n.º 4
0
 def __init__(self, client, WebSocketHandlerClass):
     self.client = client
     self.response = HTTPResponse()
     self.request = HTTPRequest.parse(client)
     self.WebSocketHandlerClass = WebSocketHandlerClass
Ejemplo n.º 5
0
    def parse(self, msg):
        request = HTTPRequest()
        # this array hold the three terms at the first line of the request
        fst_line_key = [''] * 3

        # this part makes sure no spaces occurs before GET and after the fist CRLF
        # if there is space, the function flag the format to be false

        # first line end
        fst_line_end = msg.find('\r\n')
        # retrieve first message
        fst_msg = msg[0:fst_line_end]
        # if there are spaces, format is wrong
        if fst_msg != fst_msg.strip():
            request.format = False
        if len(fst_msg.split(' ')) != 3:
            request.format = False

        # this part makes sure the spacing in the "GET /dir Version" format
        # this part checks if the there is extra space in between the three terms
        after_pos = 0
        for i in range(0, 2):
            space_pos = fst_msg.find(' ', after_pos)
            if fst_msg[space_pos + 1] == ' ':
                request.format = False
            # store the first two keys of the first line of the message
            fst_line_key[i] = fst_msg[after_pos:space_pos]
            after_pos = space_pos + 1

        # find out the third key of the first line of the message
        crlf_pos = msg.find('\r\n', after_pos)
        fst_line_key[2] = msg[after_pos:crlf_pos]
        request.function = fst_line_key[0]

        # start with the second line
        after_pos = crlf_pos + 2

        # handle malformed url
        if fst_line_key[1][0] != '/':
            request.format = False

        if fst_line_key[1] != '/' and fst_line_key[1][-1] == '/':
            request.format = False



        # store the information into Request class
        if fst_line_key[1] == '/':
            request.url = '/index.html'
        else:
            request.url = fst_line_key[1]

        request.version = fst_line_key[2]

        while crlf_pos != -1 and after_pos < len(msg):
            crlf_pos = msg.find('\r\n', after_pos)
            line = msg[after_pos:crlf_pos]
            colon = line.find(':')
            if colon == -1 or line[colon - 1] == ' ' or line[colon + 1] != ' ':
                request.format = False
            else:
                after_pos = crlf_pos + 2

        if msg.find('Host:') != -1:
            pos = msg.find('Host:')
            after_pos = msg.find('\r\n', pos)
            request.host = msg[pos + 6: after_pos]
        else:
            request.format = False

        if msg.find('Connection:') != -1:
            pos = msg.find('Connection:')
            after_pos = msg.find('\r\n', pos)
            request.connection = msg[pos + 12: after_pos]
        return request