led.value(1)

# ------------------------------------------------------------------------
# connect to network
# ------------------------------------------------------------------------

timeout = Timeout(10000)

net = network.WLAN(network.STA_IF)
net.active(True)
net.connect(wifi_config.wifi_ssid, wifi_config.wifi_pw)

while not net.isconnected():
    led.value(not led.value())
    utime.sleep_ms(100)
    if timeout.hasExpired():
        machine.reset()

print(net.ifconfig())

# ------------------------------------------------------------------------
# start webserver
# ------------------------------------------------------------------------

# create webserver
server = nbwebserver.WebServer()

rate = 1000

# handler for /led requests
class Request:

    MODE_GOT_CONNECTION = 1
    MODE_GOT_REQUEST = 2
    MODE_GOT_HEADER = 3
    MODE_DONE = 4

    def __init__(self, sock, addr, handlers):
        self._sock = sock
        self._addr = addr
        self._handlers = handlers
        self._reqTimeout = Timeout(2000)
        self._mode = Request.MODE_GOT_CONNECTION
        self._buffer = b''

        self.header = {}
        self.method = None
        self.reqPath = None
        self.query = None

    def _handleRequest(self):
        handler = self._handlers.get(self.reqPath)
        if handler is not None:
            handler(self, Response(self._sock))
        else:
            Response(self._sock).sendResponse(404)

        self._sock.close()
        self._mode = Request.MODE_DONE

    def _parseQuery(self, queryStr):
        q = {}
        pairs = queryStr.split('&')
        for pair in pairs:
            nk = pair.split('=')
            if len(nk) == 2:
                q[nk[0]] = nk[1]
        return q

    def _parseRequestLine(self, reqLine):
        r = reqLine.split()
        if len(r) != 3:
            return Request.MODE_DONE

        self.method = r[0]
        reqParts = r[1].split('?')
        self.reqPath = reqParts[0]
        if len(reqParts) > 1:
            self.query = self._parseQuery(reqParts[1])

        return Request.MODE_GOT_REQUEST

    def _parseLine(self, line):
        print("line ", line)
        if self._mode == Request.MODE_GOT_CONNECTION:
            self._mode = self._parseRequestLine(line)
        elif self._mode == Request.MODE_GOT_REQUEST:
            if len(line) == 0:
                self._mode = Request.MODE_GOT_HEADER
            else:
                h = line.split(":")
                self.header[h[0]] = h[1].strip()

        if self._mode == Request.MODE_GOT_HEADER:
            self._handleRequest()

    def Update(self):
        if self._reqTimeout.hasExpired():
            print("timeout")
            self._mode = Request.MODE_DONE
        else:
            data, ok = _nb_recv(self._sock, 64)
            if ok:
                self._buffer += data
                while True:
                    eol = self._buffer.find(b'\r\n')
                    if eol == -1:
                        break
                    self._parseLine(self._buffer[:eol].decode())
                    self._buffer = self._buffer[eol + 2:]

        return self._mode