def __init__(self, sock, fd, address, serverport, debug=0): """ 初始化 socket client 实例 Args: sock (socket): _socket实例 fd (int): file descriptor address (tuple): (ip,port) serverport (int): 本地监听端口 debug (int): 是否输出调试信息 """ global UDPCLIENTS self.sock = sock t = _time.time() self.fileno = fd self.last_send_time = t self.last_recv_time = t self.address = address self.ip_uint = ip2int(address[0]) self.ip_int64 = ip2int(address[0], 1) self.temp_recv_buffer = '' self.nothing_to_send = 1 self.server_port = serverport self.debug = debug self.attributes = set() if self.fileno > -1: UDPCLIENTS[self.fileno] = self
def add_eventlog(self, event_id, user_id, remark): strsql = 'insert into uas.events_log (event_id, event_time, user_id,event_ip,event_remark) \ values ("{0}","{1}","{2}","{3}","{4}")'.format( event_id, int(time.time()), user_id, mx.ip2int(self.request.remote_ip), remark) libiisi.m_sql.run_exec(strsql) del strsql
def __init__(self, sock, fd, address, serverport, debug=0): """ 初始化 socket client 实例 Args: sock (socket): _socket实例 fd (int): file descriptor address (tuple): (ip,port) serverport (int): 本地监听端口 debug (int): 是否输出调试信息 """ global SEND_QUEUE, CLIENTS self.sock = sock if self.sock is not None: self.sock.setblocking(0) t = _time.time() # def list p = [] self.fileno = fd self.last_send_time = t self.last_recv_time = t self.connect_time = t self.clientid = -1 self.attributes = set() self.name = '' self.ka = binascii.unhexlify('3a-53-3b-a0'.replace("-", '')) self.recognition = -1 # 1-tml,2-data,3-client,4-sdcmp,5-fwdcs,6-upgrade self.wait4send = None self.guardtime = 0 self.address = address self.ip_uint = ip2int(address[0]) self.ip_int64 = ip2int(address[0], 1) self.temp_recv_buffer = '' self.nothing_to_send = 1 self.server_port = serverport self.gps = (0.0, 0.0) # 经度 纬度 self.debug = debug if self.fileno > -1: CLIENTS[self.fileno] = self SEND_QUEUE[self.fileno] = PriorityQueue() register(self.fileno, READ) self.onSessionConnect()
def post(self): legal, rqmsg, msg = yield self.check_arguments(msgws.rqQueryDataEvents(), msgws.QueryDataEvents(), use_scode=1) try: if legal: strsql = "SELECT e.event_id,u.user_name,s.event_name,e.event_remark,e.event_time,e.event_ip from uas.events_log as e \ LEFT JOIN uas.events_info as s ON e.event_id=s.event_id \ LEFT JOIN uas.user_info as u ON e.user_id=u.user_id WHERE e.event_ip!=0 ORDER BY e.event_time desc, u.user_id" record_total, buffer_tag, paging_idx, paging_total, cur = yield self.mydata_collector( strsql, need_fetch=1, buffer_tag=msg.head.paging_buffer_tag, paging_idx=msg.head.paging_idx, paging_num=msg.head.paging_num) if record_total is None: msg.head.if_st = 45 else: msg.head.paging_record_total = record_total msg.head.paging_buffer_tag = buffer_tag msg.head.paging_idx = paging_idx msg.head.paging_total = paging_total for d in cur: env = msgws.QueryDataEvents.DataEventsView() env.events_id = int(d[0]) env.user_name = d[1] env.events_msg = '{0}'.format(d[2]) env.dt_happen = int(d[3]) env.remote_ip = mx.ip2int(d[4]) msg.data_events_view.extend([env]) del env msg.head.if_st = 1 msg.head.if_msg = "select events Success" except Exception as ex: msg.head.if_st = 0 msg.head.if_msg = str(ex.message) self.write(mx.convertProtobuf(msg)) self.finish() del msg, rqmsg
def sendServerMsg(msg, cmd): servermsg = initRtuProtobuf(cmd, [-1], [mx.ip2int('127.0.0.1')]) if len(msg) > 0: servermsg.syscmds.logger_msg = msg return mx.convertProtobuf(servermsg)
def initRtuJson(mod, src, ver, tver, tra, cmd, ip, port, addr, data, cid="1", scid="", sim="", ret=1): """ 列表转json :param mod: 1-系统指令,2-数传指令,3-SQL指令,4-错误数据 :param src: 1-通讯服务,2-数据服务,3-客户端,4-串口采集(光照度,GPS) :param ver: 1-内部协议版本v1.0 :param tver: 1-终端协议版本v1.0 :param tra: 1-数据通过模块直接传输,2-数据通过485传输 :param cmd: 单位.设备.指令 :param ip: 目的ip :param port: 目的端口 :param addr: 终端地址,列表格式 :param data: 传输数据,字符串格式 :param cid: 集中器地址,列表格式 :param scid: 控制器地址,列表格式 :param sim: 手机卡号,字符串格式 :param ret: return state: 1 正常 101 终端不在线 102 超时 """ j = {} if int(mod) == 1: j = { "head": { "mod": int(mod), "src": int(src), "ver": int(ver), "tver": int(tver), "tra": int(tra), "ret": int(ret), "cmd": cmd, }, } elif int(mod) == 2 or int(mod) == 4: j = { "head": { "mod": int(mod), "src": int(src), "ver": int(ver), "tver": int(tver), "tra": int(tra), "ret": int(ret), "cmd": cmd, }, "args": { "ip": [mx.ip2int(ip)], "port": int(port), "addr": [int(a) for a in str(addr).split(",")] }, "data": data } if len(sim) > 0: j["args"]["sim"] = sim if len(cid) > 0: # [int(a) for a in str(cid).split(",")] j["args"]["cid"] = int(cid) else: pass return j