def startServer(serverThread, var): global RUN if RUN == 0: RUN = 1 else: return #readcommands("socket thread", 0.01) thread.start_new_thread(server.readcommands, ("socket thread", var, )) time.sleep(1) log.l("'help' for commands\n\n", log.LEVEL_UI) while RUN: input = raw_input(">") if(input == 'exit'): if server.serversocket is not None: server.serversocket.close() server.RUN = 0 RUN = 0 server.triggerManager.stop() if server.CurrentCMD is not None: server.CurrentCMD.stop() server.CurrentCMD.join() server.CurrentCMD = None led.PIGPIO.stop() if(input == 'help'): help = "\n\nCommand list:" help = help + "\ncc r g b - change color" help = help + "\nclear - clears log" help = help + "\nexit - stops the server end kills process" log.l(help, log.LEVEL_UI) if input == 'clear': configure.cls() if input.startswith('cc'): try: args = input.split(' ') r = float(args[1]) g = float(args[2]) b = float(args[3]) led.changeColor(r, g, b) except: log.l('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1]), log.LEVEL_ERRORS)
def readcommands(threadName, intervall): #print the config parameters configure.cls() configure.printConfig() print '\n... starting server (',intervall,')...\n\n' #globals global ID global serversocket #create an INET, STREAMing socket serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #bind the socket to a public host, # and a well-known port success = False while not success: try: log.l('trying to bind on port '+str(config.SERVER_PORT)+'...', log.LEVEL_UI) serversocket.bind(('', config.SERVER_PORT)) # socket.gethostname() # for getting own address success = True log.l('successfully bound server to port '+str(config.SERVER_PORT)+'...', log.LEVEL_UI) except: log.l('port is already in use. Trying to reconnect in 5 seconds', log.LEVEL_ERRORS) time.sleep(5) #become a server socket serversocket.listen(5) while RUN: try: (clientsocket, address) = serversocket.accept() global CurrentCMD global CurrentFilters global CommandHistory global CommandCount rcvString = '' clientsocket.setblocking(1) # recv() waits til data comes or timeout is reached clientsocket.settimeout(config.CONNECTION_TIMEOUT) # timeout for receiving and sending kilobyte = str(clientsocket.recv(1024)) try: while(kilobyte): rcvString += kilobyte try: json.loads(rcvString) kilobyte = None except: kilobyte = str(clientsocket.recv(1024)) except: pass #flag which is true if the received command comes from a http request isHTTPRequest = False mutex.acquire() try: answer = {} answer['error'] = [] if rcvString.startswith('GET') or rcvString.startswith('OPTIONS') or rcvString.startswith('POST') or rcvString.startswith('PUT'): isHTTPRequest = True indexOfLineBreak = rcvString.find('\n\n') if indexOfLineBreak < 0: indexOfLineBreak = rcvString.find('\r\n\r\n') rcvString = rcvString[indexOfLineBreak:] rcvString = urllib.unquote(rcvString) if log.m(log.LEVEL_SOCKET_COMMUNICATION): log.l('RECEIVED HTTP ('+str(rcvString)+'): '+rcvString+'\n\n') startIndex = rcvString.find('{') endIndex = rcvString.rfind('}') if startIndex >= 0 and endIndex >= 0: rcvString = rcvString[rcvString.find('{'):rcvString.rfind('}')+1] if log.m(log.LEVEL_SOCKET_COMMUNICATION): log.l('RECEIVED Command ('+str(len(rcvString))+'): '+rcvString+'\n\n') else: if log.m(log.LEVEL_SOCKET_COMMUNICATION): log.l('no valid command found in ('+str(len(rcvString))+'): '+rcvString+'\n\n') else: isHTTPRequest = False if log.m(log.LEVEL_SOCKET_COMMUNICATION): log.l('RECEIVED Command ('+str(len(rcvString))+'): '+rcvString+'\n\n') r = json.loads(rcvString) CommandCount += 1 CommandHistory.append(r) #limit command history to the last 10 commands (client request messages) if len(CommandHistory) > 10: CommandHistory = CommandHistory[len(CommandHistory)-10:len(CommandHistory)] # execute commands if isinstance(r, dict) and r.has_key('commands') and len(r['commands']) > 0: try: if log.m(log.LEVEL_COMMANDS): log.l( 'commands: '+ str(len(r['commands']))) if CurrentCMD is not None: CurrentCMD.stop() CurrentCMD.join() CurrentCMD = None ID = ID + 1 CurrentCMD = CommandThread(ID, 'command thread', r) CurrentFilters = [] answer['commands'] = 1 except: answer['error'].append('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1])) answer['commands'] = 0 log.l('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1]), log.LEVEL_ERRORS) #add new filters if a command is running if isinstance(r, dict) and r.has_key('filters') and len(r['filters']) > 0 and CurrentCMD is not None and CurrentCMD.state != constants.CMD_STATE_STOPPED: try: for f in r['filters']: CurrentFilters.append(filters.Filter.createFilter(f)) answer['filters'] = 1 except: answer['error'].append('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1])) answer['filters'] = 0 log.l('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1]), log.LEVEL_ERRORS) #answer request if isinstance(r, dict) and r.has_key('request'): try: req = requests.Request.createRequest(r['request']) answer['request'] = req.execute() except: answer['error'].append('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1])) answer['request'] = None log.l('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1]), log.LEVEL_ERRORS) #add new triggers if isinstance(r, dict) and r.has_key('triggers') and len(r['triggers']) > 0: try: for t in r['triggers']: triggerManager.addTrigger(trigger.Trigger(t)) answer['triggers'] = 1 except: answer['error'].append('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1])) answer['triggers'] = 0 log.l('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1]), log.LEVEL_ERRORS) #starting a new command if a new arrived and could be correctly decoded if answer.has_key('commands') and answer['commands'] == 1: CurrentCMD.start() if log.m(log.LEVEL_SOCKET_COMMUNICATION): log.l('---ANSWER---') log.l(str(answer)) except: log.l('ERROR: ' + str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1]), log.LEVEL_ERRORS) answer['error'].append(str(sys.exc_info()[0]) + ": "+ str(sys.exc_info()[1])) #clientsocket.send(json.dumps(answer, separators=(',',':'))) mutex.release() if isHTTPRequest: answerString = json.dumps(answer, separators=(',',':')) httpResponse = 'HTTP/1.1 200 OK\n' httpResponse = httpResponse + 'Content-Type: application/json;charset=utf-8\n' httpResponse = httpResponse + 'Access-Control-Allow-Origin: *\n' httpResponse = httpResponse + 'Content-Length: '+str(len(answerString))+'\n\n' httpResponse = httpResponse + answerString clientsocket.send(httpResponse) log.l('answer sent (http response)', log.LEVEL_SOCKET_COMMUNICATION) else: clientsocket.send(json.dumps(answer, separators=(',',':'))) log.l('answer sent (normal socket)', log.LEVEL_SOCKET_COMMUNICATION) clientsocket.close() except: log.l('ERROR: ' + str(sys.exc_info()[0])+ ": "+ str(sys.exc_info()[1]), log.LEVEL_ERRORS)