def run(self): global image global snapshot global processed_flag global retval global video_writer msg = "starting " + self.name + " thread, url: " + str(self.url) if appVariables.qDebug1.full() == False: appVariables.qDebug1.put(msg) if self.url is None: pass else: try: stream = urllib.urlopen(self.url) print(stream) bytes = '' self._running.set() while True: bytes += stream.read(1024) a = bytes.find('\xff\xd8') b = bytes.find('\xff\xd9') if a != -1 and b != -1: # print('stream') image = bytes[a:b + 2] bytes = bytes[b + 2:] imagecv = np.fromstring(image, dtype=np.uint8) imagecv = cv2.imdecode(imagecv, 1) cv2.putText( imagecv, datetime.datetime.now().strftime("%I:%M:%S.%f"), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) (retval, image_processed_encoded) = cv2.imencode( '.jpg', imagecv) image_processed_encoded = bytearray( image_processed_encoded) # imagecv = bytearray(jpg) if self.q_image.full() == False: self.q_image.put(image_processed_encoded) if self.stopped(): break except: appVariables.print_exception("[camera_remote] ") msg = "exiting " + self.name + " thread" if appVariables.qDebug1.full() == False: appVariables.qDebug1.put(msg) self._running.clear()
def run(self): global video_writer msg = "starting " + self.name + " thread" if self.qDebug1.full() == False: self.qDebug1.put(msg) while True: time.sleep(0.01) try: while self.q_rec.empty() == False: frame = self.q_rec.get(block=False) if video_writer is not None: video_writer.write(frame) else: msg = "video writer not initialized" if self.qDebug1.full() == False: self.qDebug1.put(msg) except: appVariables.print_exception("[RecordingThread]")
def handle(self): try: self.request.setblocking(0) t0 = time.time() self.data = '' self.index = 0 while 1: time.sleep(0.01) t1 = time.time() # self.data = self.request.recv(1024) # self.c = self.request.recv(1) while 1: try: self.c = self.rfile.read(1) except: break self.data += self.c self.index += 1 if ((self.c == '\n') or (self.index >= 50)): self.index = 0 # self.data = self.data.strip() msg = "[simple_tcp_server] recv " + str( self.client_address[0]) + ': ' + str(self.data) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) self.data = '' if t1 - t0 >= 1: t0 = t1 msg = "[simple_tcp_server] send " if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) self.request.send("100,\n") except: appVariables.print_exception("[TCPRequestHandler]")
def run(self): msg = "[ControlSystemsThread] " + 'running' if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) while True: time.sleep(0.5) # msg = "[ControlSystemsThread] " + 'running' # if not appVariables.qDebug1.full(): # appVariables.qDebug1.put(msg) # print('vnodes:') # print(len(appVariables.virtualNodeList)) for knode in range(len(appVariables.virtualNodeList)): vnode = appVariables.virtualNodeList[knode] # relay controller try: # check for parameter changes if not appVariables.virtualNodeListFcn[knode][ 'q_out'].empty(): msg = "[ControlSystemsThread] " + 'parameter change' if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) cmdString = appVariables.virtualNodeListFcn[knode][ 'q_out'].get(block=False) cmdStringArray = cmdString.split(",") cmdArray = [0] * len(cmdStringArray) for i in range(len(cmdStringArray)): try: cmdArray[i] = int(cmdStringArray[i]) except: cmdArray[i] = 0 cmd = cmdArray[0] # execute received commands (user) # get index of command indexofcmd = -1 for icmd in range(len(vnode['sdata'])): if cmd == vnode['sdata'][icmd]['id']: indexofcmd = icmd break if indexofcmd != -1: if cmd > 1 and cmd <= 100: # value appVariables.virtualNodeList[knode]['sdata'][ indexofcmd]['value'] = cmdArray[1] elif cmd >= 101 and cmd <= 200: # binary toggle appVariables.virtualNodeList[knode]['sdata'][ indexofcmd]['value'] = bool( appVariables.virtualNodeList[knode] ['sdata'][indexofcmd]['value']) ^ ( True) elif cmd > 201 and cmd <= 300: # binary trigger commands pass elif cmd >= 301 and cmd <= 400: # enable commands appVariables.virtualNodeList[knode]['sdata'][ indexofcmd]['value'] = 1 elif cmd >= 401 and cmd <= 500: # disable commands appVariables.virtualNodeList[knode]['sdata'][ indexofcmd]['value'] = 0 else: # special cmd if cmd == 1: # id appVariables.virtualNodeList[knode][ 'id'] = cmdArray[1] if cmd == 201: # save msg = "[ControlSystemsThread] " + 'save' if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) write_virtual_nodes( appVariables.virtualNodeList) if vnode['type'] == 1001: # I/O relay input = None output = None input_node = 0 input_channel = 0 output_node = 0 output_channel = 0 id_input = 0 id_output = 0 lo_thd = 0 hi_thd = 0 inverted = False enabled = False for icmd in range(len(vnode['sdata'])): val = vnode['sdata'][icmd]['value'] if vnode['sdata'][icmd]['id'] == 10: input_node = val elif vnode['sdata'][icmd]['id'] == 11: input_channel = val elif vnode['sdata'][icmd]['id'] == 20: output_node = val elif vnode['sdata'][icmd]['id'] == 21: output_channel = val elif vnode['sdata'][icmd]['id'] == 2: id_input = icmd elif vnode['sdata'][icmd]['id'] == 3: id_output = icmd elif vnode['sdata'][icmd]['id'] == 4: lo_thd = val elif vnode['sdata'][icmd]['id'] == 5: hi_thd = val elif vnode['sdata'][icmd]['id'] == 101: enabled = val elif vnode['sdata'][icmd]['id'] == 102: inverted = val # print(id_input) # print(vnode['sdata']) # read input for client in appVariables.clientList: if client['id'] == input_node: channelid = input_channel if channelid < len(client['data']): input = client['data'][channelid] vnode['sdata'][id_input]['value'] = input break # calculate output if input is not None: # control law if input < lo_thd: if inverted: output = 0 else: output = 1 vnode['sdata'][id_output]['value'] = output elif input > hi_thd: if inverted: output = 1 else: output = 0 vnode['sdata'][id_output]['value'] = output # update output, if controller is active (enabled) if enabled and (output is not None): for i in range(len(appVariables.clientList)): if appVariables.clientList[i][ 'id'] == output_node: channelid = output_channel if not appVariables.clientListFcn[i][ 'q_out'].full(): # appVariables.clientListFcn[i]['q_out'].put("51,"+str(channelid)+","+str(output)+",\n") new_data = "51," + str(output) new_data = appVariables.add_checksum( new_data) appVariables.clientListFcn[i][ 'q_out'].put(new_data) break except: appVariables.print_exception("[DataBucketThread]") # print (output) msg = "[ControlSystemsThread] " + 'stopped' if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg)
def run(self): global image_processed_encoded imagecv_processed = None avg = None msg = "starting " + self.name + " thread" if self.qDebug1.full() == False: self.qDebug1.put(msg) while True: time.sleep(0.01) try: if self.q_image_in.empty() == False: imagecv = self.q_image_in.get(block=False) imagecv_processed = imagecv # resize the frame, convert it to grayscale, and blur it gray = cv2.cvtColor(imagecv, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (21, 21), 0) # if the first frame is None, initialize it if avg is None: avg = gray.copy().astype("float") cv2.accumulateWeighted(gray, avg, 0.4) # compute the absolute difference between the current frame and # running average frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(avg)) thresh = cv2.threshold(frameDelta, 10, 255, cv2.THRESH_BINARY)[1] # dilate the thresholded image to fill in holes, then find contours # on thresholded image thresh = cv2.dilate(thresh, None, iterations=2) # TODO: this is for different opencv versions compatibility (image_x, cnts, hierarchy) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # (cnts, hierarchy) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) ncont = 0 # loop over the contours for c in cnts: # if the contour is too small, ignore it ## if cv2.contourArea(c) > 5000 and cv2.contourArea(c) < 25000: if cv2.contourArea(c) > 100: # compute the bounding box for the contour, draw it on the frame, # and update the text (x, y, w, h) = cv2.boundingRect(c) cv2.rectangle(imagecv_processed, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.drawContours(imagecv_processed, cnts, -1, (255, 255, 255), 3) ncont += 1 # cv2.putText(imagecv_processed, datetime.datetime.now().strftime("%A %d %B %Y %I:%M:%S%p"), # (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) cv2.putText( imagecv_processed, datetime.datetime.now().strftime("%I:%M:%S.%f"), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) cv2.putText(imagecv_processed, str(ncont) + ' objects detected', (10, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2) (retval, image_processed_encoded) = cv2.imencode( '.jpg', imagecv_processed) image_processed_encoded = bytearray( image_processed_encoded) if self.q_image_out.full() == False: self.q_image_out.put(image_processed_encoded) if ncont > 0: result = {'n_obj': ncont} if not self.q_result.full(): self.q_result.put(result) except: appVariables.print_exception("[ProcessingThread]")
def run(self): t0=time.time() t0_ws=t0 t02=t0 flag1=False flag5=False # print(appVariables.myList) msg = "[DataBucketThread] " + "running" if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) # this thread gathers data from other modules and dispatches to control threads while True: try: time.sleep(0.01) # get data from video processing if appVariables.appConfig['modules']['pi_camera']: if appVariables.raspicam is not None: video_processing_result = appVariables.raspicam.get_processing_result() # if video_processing_result is not None: # msg = "[DataBucketThread] " + "video processing: " + json.dumps(video_processing_result) # if not appVariables.qDebug1.full(): # appVariables.qDebug1.put(msg) # get data from nodes and prepare for display t1 = time.time() # request data from nodes (polling), each with own sampling rate if t1 - t0 >= 5: t0 = t1 flag5=True if t1 - t02 >= 1: t02 = t1 flag1=True for i in range(len(appVariables.clientList)): dt_poll = 1 # request data only from discovered clients if appVariables.clientList[i]['id']!=0 and appVariables.clientList[i]['sdata']!=False: if 'settings' in appVariables.clientList[i]: dt_poll=appVariables.clientList[i]['settings']['polling'] if (t1-appVariables.clientListFcn[i]['t0_polling'])>=dt_poll: appVariables.clientListFcn[i]['t0_polling']=t1 if not appVariables.clientListFcn[i]['q_out'].full(): appVariables.clientListFcn[i]['q_out'].put("211") # equivalent of dhcp discover, send discover message to connected clients # the clients should return basic info if flag5 or (appVariables.clientList[i]['id']==0 and flag1): # msg = "[DataBucketThread] " + "code 100: " + str(appVariables.clientList[i]['id']) # if not appVariables.qDebug1.full(): # appVariables.qDebug1.put(msg) if not appVariables.clientListFcn[i]['q_out'].full(): appVariables.clientListFcn[i]['q_out'].put("100") # update client with metadata, when client is identified or when client id is changed if appVariables.clientList[i]['id'] != appVariables.clientListFcn[i]['prev_id']: appVariables.clientListFcn[i]['prev_id'] = appVariables.clientList[i]['id'] # get metadata from client model, update client msg = "[DataBucketThread] " + "update client model: " + str(appVariables.clientList[i]['id']) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) for j in range(len(appVariables.clientModelDB)): if appVariables.clientModelDB[j]['id'] == appVariables.clientList[i]['type']: appVariables.clientList[i]['sdata'] = copy.deepcopy( appVariables.clientModelDB[j]['configuration']['metadata']) appVariables.clientList[i]['commands'] = copy.deepcopy( appVariables.clientModelDB[j]['configuration']['commands']) appVariables.clientList[i]['settings'] = copy.deepcopy( appVariables.clientModelDB[j]['configuration']['communication']) appVariables.clientList[i]['info'] = { 'type': appVariables.clientModelDB[j]['type'], 'class': appVariables.clientModelDB[j]['class'] } break # basic i/o client operations for i in range(len(appVariables.clientList)): # send data to app controlled clients # [watering system] if appVariables.clientList[i]['type'] == 201: if not appVariables.wsOutQueue.empty(): cmd_string = appVariables.wsOutQueue.get(block=False) msg = "[DataBucketThread] " + "ws out: " + cmd_string if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) if not appVariables.clientListFcn[i]['q_out'].full(): appVariables.clientListFcn[i]['q_out'].put(cmd_string) # [sound lights] # handle sound data (for led arrays) if appVariables.clientList[i]['type'] == 102: if not appVariables.qAudioData2.empty(): soundData = appVariables.qAudioData2.get(block=False) if soundData['fftString'] is not None: new_data = '1,' + soundData['fftString'] if not appVariables.clientListFcn[i]['q_out'].full(): appVariables.clientListFcn[i]['q_out'].put(new_data) # retrieve and process data from clients if not appVariables.clientListFcn[i]['q_in'].empty(): cdata = appVariables.clientListFcn[i]['q_in'].get(block=False) # print(cdata) # update client with structured data (fill in metadata) if cdata is not None: if cdata['data'][0] == 211 and appVariables.clientList[i]['sdata'] != False: len_mdata = len(appVariables.clientList[i]['sdata']) for j in range(len(cdata['data']) - 1): if j < len_mdata: appVariables.clientList[i]['sdata'][j]['value'] = cdata['data'][appVariables.clientList[i]['sdata'][j]['pos']] appVariables.flags["new_client_data"] = True # retrieve data for special clients (app controlled) if appVariables.clientList[i]['type'] == 201: if not appVariables.queue_ws_app_data.full(): appVariables.queue_ws_app_data.put(cdata['str']) if not appVariables.queue_ws_control.full(): appVariables.queue_ws_control.put(cdata['str']) if cdata is not None and appVariables.clientList[i]['sdata'] != False: # write data to database (log) datatypes = appVariables.clientList[i]['sdata'] has_logged = False for k in range(len(datatypes)): if k < len(cdata['data']): if 'log' in datatypes[k]: dt_log = datatypes[k]['log'] if (dt_log != 0) and (t1 - appVariables.clientListFcn[i]['t0_log'] >= dt_log): has_logged = True cid = datatypes[k]['id'] pos = datatypes[k]['pos'] timestamp = datetime.datetime.now() msg = "[DataBucketThread] " + "db log" + ', id: ' + str( appVariables.clientList[i]['id']) + ', cid: ' + str(cid) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) try: if appVariables.appConfig['mongo']: doc = { "s_id":appVariables.clientList[i]['id'], "s_type":appVariables.clientList[i]['type'], "s_chan":cid, "ts": str(timestamp), "value": cdata['data'][pos] } appVariables.mongomanager.insert("mydb","sensor_data",doc) # add sensor to database or update sensor info query1 = { "s_id":appVariables.clientList[i]['id'] } query2 = { "$set":{ "s_id":appVariables.clientList[i]['id'], "s_type":appVariables.clientList[i]['type'], # "n_chan":len(cdata['data']) "n_chan": len(datatypes) } } appVariables.mongomanager.update("mydb", "sensors", query1, query2, ups=True) elif appVariables.appConfig['sql']: data = [''] appVariables.qDatabaseIn.put(('dataBucketThread', 'SELECT * FROM Sensors WHERE s_id = (?)', (appVariables.clientList[i]['id'],))) while data[0] != 'dataBucketThread': data = appVariables.qDatabaseOut.get(True) # blocking # print(data) data = db_utils.get_array_result(data) if not data or not len(data) > 0: data = [''] appVariables.qDatabaseIn.put(('dataBucketThread2', 'INSERT INTO Sensors (ID, s_id, s_type, n_chan) VALUES (NULL, (?), (?), (?))', (appVariables.clientList[i]['id'], appVariables.clientList[i]['type'], len(datatypes)))) while data[0] != 'dataBucketThread2': data = appVariables.qDatabaseOut.get(True) # blocking data = [''] appVariables.qDatabaseIn.put(('dataBucketThread3', 'INSERT INTO SensorData (ID, s_id, s_type, s_chan, s_unit, ts, value) VALUES(NULL, (?), (?), (?), (?), (?), (?))', (appVariables.clientList[i]['id'], appVariables.clientList[i]['type'], cid, None, str(timestamp), cdata['data'][pos]))) except: appVariables.print_exception("[DataBucketThread]") else: break if has_logged: appVariables.clientListFcn[i]['t0_log'] = t1 break flag1 = False flag5 = False except: appVariables.print_exception("[DataBucketThread]")
def run(self): msg = "[IrrigationControlThread] " + "running" if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) flag_control = 0 time.sleep(5) configOk = self.loadControlSettings() debounce_counter = 0 N_DEBOUNCE_COUNTER = 10 # watering flag shows if program is active while True: try: time.sleep(0.1) if not appVariables.queue_ws_control.empty(): data = appVariables.queue_ws_control.get(block=False) if data != False: str1 = data if len(str1) != 0: string = str1 stringArray = string.split(",") if len(stringArray) > 22: if (int(stringArray[0]) == 211): watering_flag = int(stringArray[22]) if flag_control == 0: if watering_flag == 1: debounce_counter += 1 if debounce_counter > N_DEBOUNCE_COUNTER: debounce_counter = 0 flag_control = 1 else: debounce_counter = 0 elif flag_control == 2: # settings updated, waiting to finish watering program if watering_flag == 0: debounce_counter += 1 if debounce_counter > N_DEBOUNCE_COUNTER: debounce_counter = 0 flag_control = 0 else: debounce_counter = 0 if ((flag_control == 1) or (appVariables.flag_sim['control'] == True)): appVariables.flag_sim['control'] = False flag_control = 2 # wait until program finishes msg = "[IrrigationControlThread] " + "start watering" if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) configOk = self.loadControlSettings() if configOk: date1 = datetime.datetime.now() timestamp = datetime.datetime.now() startdate = date1 - datetime.timedelta(hours=24) enddate = date1.replace(microsecond=0) startdate = startdate.replace(microsecond=0) print([startdate, enddate]) for i in range(len(self.params)): js = self.params[i] sid = js['sensorId'] msg = "[IrrigationControlThread] " + "sid: " + str( sid) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) pipeline = [{ "$match": { "s_id": sid, "s_chan": 1, "ts": { "$gt": str(startdate), "$lt": str(enddate) } } }, { "$group": { "_id": "$s_id", "avg_hum": { "$avg": "$value" } } }] data = appVariables.mongomanager.aggregate_pipeline( "mydb", "sensor_data", pipeline) msg = "[IrrigationControlThread] " + " sensor data " + str( sid) + ": " + json.dumps( data, default=json_util.default) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) if len(data) == 0: continue data = data[0] avg_hum = data['avg_hum'] avg_hum = int(avg_hum) msg = "[IrrigationControlThread] " + "output (average humidity): " + str( avg_hum) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) query = {"s_id": sid} data = appVariables.mongomanager.find_last_records( "mydb", "control_data", query, 1) # result = appVariables.mongomanager.find("mydb", "control_data", query) newdata = { "s_id": sid, "ts": str(timestamp), "uk1": 0, "rk": js['humRef'], "aux": { "uk": 0, "ek1": 0, "ek2": 0, "integral": 0 }, "yk": 0 } appVariables.mongomanager.insert( "mydb", "control_data", newdata) if len(data) == 0: data = newdata else: data = data[0] uk1 = data['aux']['uk'] ek1 = data['aux']['ek1'] ek2 = data['aux']['ek2'] if 'integral' in data['aux']: integral = data['aux']['integral'] else: integral = 0 msg = "[IrrigationControlThread] last control_data: " + json.dumps( data, default=json_util.default) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) if js['enabled']: if js['type'] == 'pid': ek = js['humRef'] - avg_hum umin = js['umin'] umax = js['umax'] # ek1 is ek of previous control step # ek2 is ek1 of previous control step # ek is current error (ref - avg_hum of last period) q0 = self.ctrlSettings[i]['q0'] q1 = self.ctrlSettings[i]['q0'] q2 = self.ctrlSettings[i]['q0'] Kp = self.ctrlSettings[i]['kp'] Ki = self.ctrlSettings[i]['ki'] Kd = self.ctrlSettings[i]['kd'] # uk = uk1 + q0 * ek + q1 * ek1 + q2 * ek2 if Ki != 0: integral = integral + ek * self.Ts derivative = (ek - ek1) / self.Ts uk = Kp * ek + Ki * integral + Kd * derivative if (uk < umin): uk = umin if (uk > umax): uk = umax uk = int(uk) # update values for pid controller # ek2 = ek1 # ek1 = ek query = {"s_id": sid, "ts": str(timestamp)} newdata = { "$set": { "uk1": uk1, "rk": js['humRef'], "aux": { "uk": uk, "ek1": ek, "ek2": ek1, "integral": integral }, "yk": avg_hum } } appVariables.mongomanager.update( "mydb", "control_data", query, newdata, ups=True) msg = "[IrrigationControlThread] " + 'watering time (uk): ' + str( uk) + ' prev watering time (uk1): ' + str( uk1) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) cmd = appVariables.systemSettings['codeTable'][ 'CMD_SET_PLANT_WTIME'] cmdcode = cmd['cmd'] csv = str(cmdcode) + ',' + str( js['potId']) + ',' + str(uk) msg = "[IrrigationControlThread] " + csv if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) cmd_string = csv if not appVariables.wsOutQueue.full(): appVariables.wsOutQueue.put(cmd_string) except: appVariables.print_exception("[IrrigationControlThread]")
def run(self): global TCP_IP, TCP_PORT, BUFFER_SIZE time.sleep(5) msg = "[TCPServerThread] " + "running" if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) try: server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.setblocking(0) server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server.bind((TCP_IP, TCP_PORT)) server.listen(5) except: appVariables.print_exception("[TCPServerThread]") return # Sockets from which we expect to read inputs = [server] # Sockets to which we expect to write outputs = [] # Outgoing message queues (socket:Queue) message_queues = {} appVariables.clientList = [] appVariables.clientInfoList = [] appVariables.clientListFcn = [] connectionDataBuffer = {} t0 = time.time() while inputs: # Wait for at least one of the sockets to be ready for processing # print >> sys.stderr, '\nwaiting for the next event' # time.sleep(0.001) readable, writable, exceptional = select.select( inputs, outputs, inputs, 0.1) t1 = time.time() # Handle inputs for s in readable: if s is server: # A "readable" server socket is ready to accept a connection connection, client_address = s.accept() msg = "[TCPServerThread] " + 'new connection ' + str( connection) + ' from ' + str(client_address) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) connection.setblocking(0) inputs.append(connection) outputs.append(connection) # Give the connection a queue for data we want to send connectionDataBuffer[connection] = {'str': '', 'index': 0} # check if ip is already in connected client list # if true then only replace properties # if false then create a new object clientInList = False for i in range(len(appVariables.clientListFcn)): if appVariables.clientList[i]['ip'] == client_address[ 0]: clientInList = True appVariables.clientList[i]['id'] = 0 appVariables.clientListFcn[i][ 'connection'] = connection appVariables.clientListFcn[i]['t0'] = t1 appVariables.clientListFcn[i]['t0_polling'] = t1 appVariables.clientListFcn[i]['t0_log'] = t1 break if not clientInList: newClientFcn = copy.deepcopy( appVariables.clientModelFcn) newClientFcn['connection'] = connection newClientFcn['q_in'] = Queue(maxsize=10) newClientFcn['q_out'] = Queue(maxsize=10) newClientFcn['t0'] = t1 newClientFcn['t0_polling'] = t1 newClientFcn['t0_log'] = t1 appVariables.clientListFcn.append(newClientFcn) newClient = copy.deepcopy(appVariables.clientModel) newClient['counter_rx'] = 0 newClient['counter_tx'] = 0 appVariables.clientList.append(newClient) appVariables.clientInfoList.append({ 'id': 0, 'ip': 0, 'type': 0 }) # print(appVariables.clientList) else: try: data = s.recv(1) # print(data) if data: # A readable client socket has data # print >> sys.stderr, 'received "%s" from %s' % (data, s.getpeername()) connectionDataBuffer[s]['str'] += data connectionDataBuffer[s]['index'] += 1 if ((data == '\n') or (connectionDataBuffer[s]['index'] >= BUFFER_SIZE)): connectionDataBuffer[s]['index'] = 0 data1 = connectionDataBuffer[s]['str'] connectionDataBuffer[s]['str'] = '' clientNumStr = data1.split(",") # print clientNumStr clientData = [0] * len(clientNumStr) i = 0 for i in range(len(clientNumStr)): try: clientData[i] = int(clientNumStr[i]) except: clientData[i] = 0 for i in range(len( appVariables.clientListFcn)): if appVariables.clientListFcn[i][ 'connection'] == s: appVariables.clientList[i][ 'in'] = data1 appVariables.clientList[i][ 'ip'] = s.getpeername()[0] appVariables.clientInfoList[i][ 'ip'] = s.getpeername()[0] if len(clientData) > 2: if clientData[0] == 100: # msg = "[TCPServerThread] code 100: " + str(clientData[1]) + ", " + str(clientData[2]) # if not appVariables.qDebug1.full(): # appVariables.qDebug1.put(msg) appVariables.clientList[i][ 'id'] = clientData[1] appVariables.clientList[i][ 'type'] = clientData[2] appVariables.clientInfoList[i][ 'id'] = clientData[1] appVariables.clientInfoList[i][ 'type'] = clientData[2] else: appVariables.clientList[i][ 'data'] = clientData if clientData[0] != 100: # use only actual data if not appVariables.clientListFcn[ i]['q_in'].full(): appVariables.clientListFcn[i][ 'q_in'].put({ 'str': data1, 'data': clientData }) appVariables.clientListFcn[i][ 't0'] = t1 appVariables.clientList[i][ 'counter_rx'] = appVariables.clientList[ i]['counter_rx'] + 1 try: if (not appVariables.qTCPIn.full( )) and (appVariables.clientList[i] ['data'][0] != 211): appVariables.qTCPIn.put( '[' + str(appVariables. clientList[i]['id']) + '] ' + data1) except: pass break except: appVariables.print_exception( "[TCPServerThread] read exception: " + ', at socket: ' + str(s) + ' closing socket: ') outputs.remove(s) inputs.remove(s) # Remove connection data from list del connectionDataBuffer[s] for i in range(len(appVariables.clientListFcn)): if appVariables.clientListFcn[i][ 'connection'] == s: del appVariables.clientListFcn[i] del appVariables.clientList[i] break s.close() # Handle outputs for s in writable: for i in range(len(appVariables.clientListFcn)): if appVariables.clientListFcn[i]['connection'] == s: try: # handle external data if not appVariables.clientListFcn[i][ 'q_out'].empty(): new_data = appVariables.clientListFcn[i][ 'q_out'].get(block=False) appVariables.clientList[i][ 'counter_tx'] = appVariables.clientList[i][ 'counter_tx'] + 1 new_data = appVariables.add_checksum(new_data) new_data += '\n' s.send(new_data) try: if (not appVariables.qTCPOut.full()) and ( appVariables.clientList[i]['data'] [0] != 211): appVariables.qTCPOut.put('[' + str( appVariables.clientList[i]['id']) + '] ' + new_data) except: pass except: appVariables.print_exception( "[TCPServerThread] write exception: " + ', at socket: ' + str(s) + ' closing socket: ') outputs.remove(s) inputs.remove(s) s.close() # Remove connection data from list del connectionDataBuffer[s] for i in range(len(appVariables.clientListFcn)): if appVariables.clientListFcn[i][ 'connection'] == s: del appVariables.clientListFcn[i] del appVariables.clientList[i] break break # Handle "exceptional conditions" for s in exceptional: msg = "[TCPServerThread] " + 'handling exceptional condition' + ', at socket: ' + str( s) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) # Stop listening for input on the connection inputs.remove(s) outputs.remove(s) s.close() # Remove message queue del connectionDataBuffer[s] for i in range(len(appVariables.clientListFcn)): if appVariables.clientListFcn[i]['connection'] == s: del appVariables.clientListFcn[i] del appVariables.clientList[i] break # # Handle timeout # if not (readable or writable or exceptional): # print >> sys.stderr, ' timed out, do some other work here' # continue # Handle timeouts (received data) for i in range(len(appVariables.clientListFcn)): s = appVariables.clientListFcn[i]['connection'] if s not in readable and ( t1 - appVariables.clientListFcn[i]['t0']) > 30: appVariables.clientListFcn[i]['t0'] = t1 msg = "[TCPServerThread] " + ' no data, ' + ', at socket: ' + str( s) + ', socket closed' if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) inputs.remove(s) outputs.remove(s) s.close() # Remove message queue del connectionDataBuffer[s] del appVariables.clientListFcn[i] del appVariables.clientList[i] break
def DatabaseManagerProcess(qDatabaseIn, qDatabaseOut, qDebug1, dbfile): def connect(): # init connection # conn = mysql.connector.connect( # host="localhost", # user="******", # passwd="raspberry", # database="smarthome" # ) # # curs = conn.cursor() conn = sqlite3.connect(dbfile, timeout=appVariables.db_timeout) conn.row_factory = sqlite3.Row # This enables column access by name: row['column_name'] curs = conn.cursor() return (conn, curs) stop_flag = 0 while True: time.sleep(0.1) if stop_flag: break if not qDatabaseIn.empty(): try: data = qDatabaseIn.get(False) requestId = data[0] if requestId == -1: stop_flag = 1 break sqlstr = data[1] params = data[2] msg = "[DatabaseManagerProcess] " + requestId + " - " + str( sqlstr) + ' - ' + str(params) if qDebug1.full() == False: qDebug1.put(msg) conn, curs = connect() if params is None: params = () curs.execute(sqlstr, params) # commit the changes data = curs.fetchall() if len(data) == 0: data = False else: data = json.dumps([dict(ix) for ix in data]) #CREATE JSON qDatabaseOut.put((requestId, data)) msg = "[DatabaseManagerProcess] " + "commit" if not qDebug1.full(): qDebug1.put(msg) conn.commit() conn.close() except: appVariables.print_exception("[DatabaseManagerProcess]")
def handle(self): try: # print "Connection from: %s" % str(self.client_address) # request_msg = self.rfile.readline(1024) # msg = "[simple_tcp_server] "+str(request_msg) # if not appVariables.qDebug1.full(): # appVariables.qDebug1.put(msg) # self.wfile.write('211,\n') # self.wfile.flush() self.request.setblocking(0) t0=time.time() self.data='' self.index=0 msg = "[TCPRequestHandler] new connection at " + str(self.client_address[0]) if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) # update client list with connected client clientInList = False for i in range(len(appVariables.clientListFcn)): if appVariables.clientList[i]['ip'] == self.client_address[0]: clientInList = True appVariables.clientList[i]['id'] = 0 appVariables.clientListFcn[i]['t0'] = t0 appVariables.clientListFcn[i]['t0_polling'] = t0 appVariables.clientListFcn[i]['t0_log'] = t0 break if not clientInList: msg = "[TCPRequestHandler] add client " if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) newClientFcn = copy.deepcopy(appVariables.clientModelFcn) newClientFcn['q_in'] = Queue(maxsize=10) newClientFcn['q_out'] = Queue(maxsize=10) newClientFcn['t0'] = t0 newClientFcn['t0_polling'] = t0 newClientFcn['t0_log'] = t0 appVariables.clientListFcn.append(newClientFcn) newClient = copy.deepcopy(appVariables.clientModel) newClient['ip'] = self.client_address[0] newClient['counter_rx'] = 0 newClient['counter_tx'] = 0 appVariables.clientList.append(newClient) appVariables.clientInfoList.append({'id': 0, 'ip': 0, 'type': 0}) else: msg = "[TCPRequestHandler] update client " if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) while 1: time.sleep(0.01) t1=time.time() try: self.data = self.request.recv(1024).decode() except: self.data = None if self.data is not None: # self.data = self.data.strip() # msg = "[TCPServerThread] recv " + str(self.client_address[0]) + ': ' + str(self.data) # if not appVariables.qDebug1.full(): # appVariables.qDebug1.put(msg) # parse client data # print(self.data) clientNumStr = self.data.split(",") # print clientNumStr clientData = [0] * len(clientNumStr) i = 0 for i in range(len(clientNumStr)): try: clientData[i] = int(clientNumStr[i]) except: clientData[i] = 0 # update client list for i in range(len(appVariables.clientListFcn)): if appVariables.clientList[i]['ip'] == self.client_address[0]: appVariables.clientList[i]['in'] = self.data appVariables.clientInfoList[i]['ip'] = self.client_address[0] if len(clientData) > 2: if clientData[0] == 100: # msg = "[TCPServerThread] code 100: " + str(clientData[1]) + ", " + str(clientData[2]) # if not appVariables.qDebug1.full(): # appVariables.qDebug1.put(msg) appVariables.clientList[i]['id'] = clientData[1] appVariables.clientList[i]['type'] = clientData[2] appVariables.clientInfoList[i]['id'] = clientData[1] appVariables.clientInfoList[i]['type'] = clientData[2] else: appVariables.clientList[i]['data'] = clientData if clientData[0] != 100: # use only actual data if not appVariables.clientListFcn[i]['q_in'].full(): appVariables.clientListFcn[i]['q_in'].put( {'str': self.data, 'data': clientData}) appVariables.clientListFcn[i]['t0'] = t1 appVariables.clientList[i]['counter_rx'] = appVariables.clientList[i][ 'counter_rx'] + 1 try: if (not appVariables.qTCPIn.full()) and (appVariables.clientList[i]['data'][0] != 211): appVariables.qTCPIn.put('[' + str(appVariables.clientList[i]['id']) + '] ' + self.data) except: pass self.data='' # end while # handle sending data for i in range(len(appVariables.clientList)): if appVariables.clientList[i]['ip'] == self.client_address[0]: # handle external data if not appVariables.clientListFcn[i]['q_out'].empty(): new_data = appVariables.clientListFcn[i]['q_out'].get(block=False) appVariables.clientList[i]['counter_tx'] = appVariables.clientList[i]['counter_tx'] + 1 new_data = appVariables.add_checksum(new_data) new_data += '\n' self.request.send(new_data.encode()) try: if (not appVariables.qTCPOut.full()) and (appVariables.clientList[i]['data'][0] != 211): appVariables.qTCPOut.put('[' + str(appVariables.clientList[i]['id']) + '] ' + new_data) except: pass # handle timeouts (received data) expired=False for i in range(len(appVariables.clientList)): if appVariables.clientList[i]['ip'] == self.client_address[0]: if (t1 - appVariables.clientListFcn[i]['t0']) > 10: appVariables.clientListFcn[i]['t0'] = t1 msg = "[TCPRequestHandler] " + ' no data ' + ' at ' + str(self.client_address[0]) + '. socket closed' if not appVariables.qDebug1.full(): appVariables.qDebug1.put(msg) del appVariables.clientListFcn[i] del appVariables.clientList[i] del appVariables.clientInfoList[i] expired = True break if expired: break except: appVariables.print_exception("[TCPRequestHandler] exception. closing socket at " + self.client_address[0])