def do_GET(request): # """Respond to a GET request.""" if not processURLPath(request.path): return resource = request.path.split("/")[1].upper() state = request.path.split("/")[2].upper() print "HTTP_SERVER: Resource - " + resource if resource == "TEMP": request.send_response(200) request.send_header("Content-type", "text/plain") request.end_headers() request.wfile.write(iotUtils.LAST_TEMP) elif resource == "BULB": iotUtils.switchBulb(state) print "HTTP_SERVER: Requested Switch State - " + state elif resource == "SONAR": request.send_response(200) request.send_header("Content-type", "text/plain") request.end_headers() request.wfile.write(iotUtils.LAST_DISTANCE) print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
def on_message(mqttClient, userdata, msg): print("MQTT_LISTENER: " + msg.topic + " " + str(msg.payload)) print 'MQTT_LISTENER: Message Received by Device' print("MQTT_LISTENER: " + msg.topic + " --> " + str(msg.payload)) request = str(msg.payload) resource = request.split(":")[0].upper() state = request.split(":")[1].upper() print "MQTT_LISTENER: Resource- " + resource print "MQTT_LISTENER: State- " + state if resource == "BULB": iotUtils.switchBulb(state=state)
def main(): HOST_NAME = iotUtils.getDeviceIP() server_class = BaseHTTPServer.HTTPServer while True: try: httpd = server_class((HOST_NAME, SERVER_PORT), MyHandler) print "HTTP_SERVER: " + time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, SERVER_PORT) httpd.serve_forever() except (KeyboardInterrupt, Exception) as e: print "HTTP_SERVER: Exception in HttpServerThread (either KeyboardInterrupt or Other)" print ("HTTP_SERVER: " + str(e)) iotUtils.switchBulb("OFF") httpd.server_close() print "HTTP_SERVER: " + time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, SERVER_PORT) print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' pass
def on_message(client, userdata, msg): print(msg.topic+" "+str(msg.payload)) request = str(msg.payload) resource = request.split(":")[0].upper() state = request.split(":")[1].upper() print "Resource: " + resource if resource == "TEMP": pass #request.send_response(200) #request.send_header("Content-type", "text/plain") #request.end_headers() #request.wfile.write(LAST_TEMP) # return elif resource == "BULB": iotUtils.switchBulb(state)
def do_GET(request): # """Respond to a GET request.""" if not processURLPath(request.path): return print request.path.split("/")[1].upper() resource = request.path.split("/")[1].upper() state = request.path.split("/")[2].upper() print "HTTP_SERVER: Resource - " + resource if resource == "TEMPERATURE": request.send_response(200) request.send_header('Content-Type', 'application/json') request.send_header('Authorization', 'Bearer ' + iotUtils.AUTH_TOKEN) request.end_headers() request.wfile.write(iotUtils.LAST_TEMP) elif resource == "BULB": iotUtils.switchBulb(state) print "HTTP_SERVER: Requested Switch State - " + state
def on_message(client, userdata, msg): print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'MQTT_LISTENER: Message Received by Device' print("MQTT_LISTENER: " + msg.topic + " --> " + str(msg.payload)) request = str(msg.payload) resource = request.split(":")[0].upper() state = request.split(":")[1].upper() print "MQTT_LISTENER: Resource- " + resource if resource == "TEMP": pass #request.send_response(200) #request.send_header("Content-type", "text/plain") #request.end_headers() #request.wfile.write(LAST_TEMP) # return elif resource == "BULB": iotUtils.switchBulb(state)
def on_message(client, userdata, msg): print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' print 'MQTT_LISTENER: Message Received by Device' print( "MQTT_LISTENER: " + msg.topic + " --> " + str(msg.payload) ) request = str(msg.payload) resource = request.split(":")[0].upper() state = request.split(":")[1].upper() print "MQTT_LISTENER: Resource- " + resource if resource == "TEMP": pass #request.send_response(200) #request.send_header("Content-type", "text/plain") #request.end_headers() #request.wfile.write(LAST_TEMP) # return elif resource == "BULB": iotUtils.switchBulb(state)
def main(): HOST_NAME = iotUtils.getDeviceIP() HTTP_SERVER_PORT = iotUtils.getHTTPServerPort() server_class = BaseHTTPServer.HTTPServer while True: try: httpd = server_class((HOST_NAME, HTTP_SERVER_PORT), OnRequestListener) print "HTTP_SERVER: " + time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, HTTP_SERVER_PORT) httpd.serve_forever() except (KeyboardInterrupt, Exception) as e: print "HTTP_SERVER: Exception in HttpServerThread (either KeyboardInterrupt or Other)" print ("HTTP_SERVER: " + str(e)) if running_mode.RUNNING_MODE == "N": iotUtils.switchBulb("OFF") else : iotUtils.switchBulb("OFF") httpd.server_close() print "HTTP_SERVER: " + time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, HTTP_SERVER_PORT) print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' pass