def channelStart(self, channel): try: if channel in self.process_map : print channel , self.process_map[channel].is_alive() if not self.dataSession_map[channel].State(): del self.process_map[channel] else : self.putTask(channel,{'MsgType' : 3, 'data' : {'state' : 0}}) while self.dataSession_map[channel].State(): print channel, "is alive" time.sleep(3) del self.process_map[channel] if channel in self.thread_map : if self.thread_map[channel].is_alive: self.thread_map[channel].stop() del self.thread_map[channel] except Exception as e: print "channelStart Error : " , e if channel in self.channel_map : session = data_session(channel, self.channel_map[channel], 9600, 0.5, lambda self, data : handlers.MsgDict[data['MsgType']](self,data)) self.dataSession_map[channel] = session p = multiprocessing.Process(target=session) self.process_map[channel] = p p.start() th = MyThread(session) self.thread_map[channel] = th th.start()
@author: sanhe ''' import handlers import SmartServer import torndb import sys reload(sys) sys.setdefaultencoding('utf8') from data_session import data_session def handleConnect(sock): client = SmartServer.AsyncClient(sock, handlers.handleData) SmartServer.SmartServer.client_map[id(client)] = client if __name__ == "__main__" : session = data_session( u'空调外机通道' , '/dev/ttyUSB0', 9600, 0.5, lambda self, data : handlers.MsgDict[data['MsgType']](self,data)) # session.start() for value in session.getDevice(u'室外北区红外').getDataDict().values(): print value.getDataItem() conn = torndb.Connection("127.0.0.1:3306", "smarthome", user = "******", password = "******") sql = 'select link_key,link_type,link_para1 from conf_links where conf_name = %s' sql = 'select conf_name,description from infrared_confs' data_links = None try : data_links = conn.query(sql) except Exception as e: print e print data_links conn.close() # SmartServer.SmartServer().Init('172.16.1.13', 8888, handleConnect) # SmartServer.SmartServer().Start()