def __init__(self, conf, redis_address): """ init """ self.interval = 1 print(redis_address) self.red = RedisClient(redis_address) self.influxc = InfluxC(conf['influxdb']) self.dead_threshold = conf['app']['dead_threshold'] self.node_name = conf['app']['node_name'] self.int_tags = conf['app']['int_tags']
class MsgHandler(HandlerBase): """ msg processor for winwin """ def __init__(self, channel): """ channel """ self.channel = channel #self.conf = conf super(MsgHandler, self).__init__() #self.conf = self.g.conf if self.conf['logging']['debug']: #print "%%%" * 20 log.debug('no redis connected') pass else: self.red = RedisClient(self.conf['redis']) self.red.load_script(self.conf['output']['enqueue_script']) def process(self): """ process msg """ rtn = self.red.enqueue(eqpt_no=eqpt_no, timestamp=timestamp, cmd=cmd, rawdata=msgpack.packb(rawdata), data=msgpack.packb(data), measurement=measurement.lower()) log.debug(rtn) def run(self): """ loop """ while True: try: #log.debug("queue size: %d" % self.g.msg_queue.qsize()) ### get msg from msg_queue line = self.get() if line == 'stop it': log.warning("stopped by msg") break log.debug(self.conf[self.channel]) log.debug(line) #msg_queue.task_done() except Exception as ex: log.error(ex)
def __init__(self, plugin): """ channel """ super(DAMHandler, self).__init__(__file__, plugin) log.debug('---' * 25) log.debug('---' * 25) self.red = RedisClient(self.conf['redis']) self.red.load_script(self.conf['output']['enqueue_script'])
def __init__(self): """ init """ #self.conf = conf self.g = SharedQ() ### singleton conf self.conf = self.g.conf if self.conf['logging']['debug']: #print "%%%" * 20 log.debug('no redis connected') pass else: self.red = RedisClient(self.conf['redis']) self.red.load_script(self.conf['output']['enqueue_script'])
def __init__(self, channel): """ channel """ self.channel = channel #self.conf = conf super(MsgHandler, self).__init__() #self.conf = self.g.conf if self.conf['logging']['debug']: #print "%%%" * 20 log.debug('no redis connected') pass else: self.red = RedisClient(self.conf['redis']) self.red.load_script(self.conf['output']['enqueue_script'])
def __init__(self, conf): """ init """ self.conf = conf self.winwin = WinWin(conf['station']) self.influxc = InfluxC(conf['influxdb']) self.red = RedisClient(conf['redis']) self.interval = conf['app']['interval'] self.title_like = conf['app']['title_like'] self.tags = conf['output']['tags'] self.measurement = conf['output']['measurement'] self.red.load_script(conf['output']['enqueue_script']) ### find window self.winwin.get_win_list(self.title_like) self.fields = {} self.text_pos = {}
class Messenger(object): """ messenger """ def __init__(self, conf, redis_address): """ init """ self.interval = 1 print(redis_address) self.red = RedisClient(redis_address) self.influxc = InfluxC(conf['influxdb']) self.dead_threshold = conf['app']['dead_threshold'] self.node_name = conf['app']['node_name'] self.int_tags = conf['app']['int_tags'] def run(self): """ send msg to influxdb """ dead_count = 0 last_alive_time = time.time() while True: if dead_count > 5: dead_json = { 'time': int(time.time()) * 1000000, 'measurement': 'dead_node', 'fields': { 'node_name': self.node_name }, 'tags': { 'alive?': 'no' } } try: self.influxc.send([dead_json]) log.debug('the node is dead now') except Exception as e: log.error(e) try: data_len = self.red.get_len() if data_len > 0: alive_json = { 'time': int(time.time()) * 1000000, 'measurement': 'dead_node', 'fields': { 'node_name': self.node_name }, 'tags': { 'alive?': 'yes' } } self.influxc.send([alive_json]) dead_count = 0 last_alive_time = time.time() for i in range(0, data_len): rdata = self.red.dequeue() data = Messenger.transefer(msgpack.unpackb(rdata[1])) data_handle = self.convert_float( data['data']['fields']) data['data']['fields'] = data_handle # log.debug(data) # string to integer data['data']['time'] = round( float(data['data']['time'])) json_data = [data['data']] log.debug(json_data) try: self.influxc.send(json_data) except Exception as ex: log.error(ex) log.error(traceback.format_exc()) log.debug("re queue...") self.red.re_queue(rdata) time.sleep(6) continue else: log.debug('queue now don\'t have data') now = time.time() threshold = self.dead_threshold * 1 if now - last_alive_time > threshold: dead_count += 1 except Exception as ex: log.error(ex) log.error(traceback.format_exc()) # timer("output") time.sleep(self.interval) @staticmethod def transefer(bytes_dict): """ lua to python3, lua's table will be transefer to python dict, but the key and the value of dict is byte string, and bytes string can't be directly used in send function from influxdb package. :param bytes_dict: a dict whcih key and value is byte string. :return: a user-friendly normal dict. """ a = {} if not isinstance(bytes_dict, dict): return bytes_dict for key, value in bytes_dict.items(): value = Messenger.transefer(value) if isinstance(key, bytes): key = key.decode() if isinstance(value, bytes): value = value.decode() a[key] = value return a def convert_float(self, data): """ convert the int data to float data :param data,allowed_tag :return :data we need """ int_tags = self.int_tags data_handle = {} log.debug(data) for key, value in data.items(): if (key not in int_tags) and (type(value) == int): data_handle[key] = float(value) else: data_handle[key] = value log.debug(data_handle) return data_handle
class DAMHandler(HandlerBase): """ msg processor for Dam""" def __init__(self, plugin): """ channel """ super(DAMHandler, self).__init__(__file__, plugin) log.debug('---' * 25) log.debug('---' * 25) self.red = RedisClient(self.conf['redis']) self.red.load_script(self.conf['output']['enqueue_script']) def process(self, **kwargs): """ process """ rtn = self.red.enqueue(eqpt_no=kwargs['eqpt_no'], timestamp=kwargs['timestamp'], cmd=kwargs['cmd'], rawdata=msgpack.packb(kwargs['rawdata']), data=msgpack.packb(kwargs['data']), measurement=kwargs['measurement']) log.debug(rtn) def run(self): """ loop """ while True: try: ### get msg from msg_queue fields = self.get() for field in fields: timestamp = field['timestamp'] eqpt_no = field['unit'] print eqpt_no # log.debug(button_status) measurement = self.conf['dam_measurement']['measurement'] log.debug(fields) # msg = {'uuid':uid, 'timestamp':timestamp,'type':type, 'channel':self.channel, 'interval':interval, 'payload ':payload} self.process(eqpt_no=eqpt_no, timestamp=int(timestamp * (1000)), cmd='STR', rawdata=field['payload'], data=field['payload'], measurement=measurement) # msg_queue.task_done() print("*" * 50) log.debug(int(timestamp)) print("*" * 50) except Exception as e: log.debug(e)
class Worker(object): """ worker find content in windows """ def __init__(self, conf): """ init """ self.conf = conf self.winwin = WinWin(conf['station']) self.influxc = InfluxC(conf['influxdb']) self.red = RedisClient(conf['redis']) self.interval = conf['app']['interval'] self.title_like = conf['app']['title_like'] self.tags = conf['output']['tags'] self.measurement = conf['output']['measurement'] self.red.load_script(conf['output']['enqueue_script']) ### find window self.winwin.get_win_list(self.title_like) self.fields = {} self.text_pos = {} def build_json(self): """ build json for influxdb """ data = [{"time":int(time.time()), "measurement":self.measurement, \ "tags":self.tags, "fields":self.fields}] return data def build_fields(self): """ build fields for influxdb json """ #log.debug("build_fields") for hwnd in self.winwin.win_list: self.winwin.get_child_list(hwnd) for parent_hwnd in self.winwin.win_dict: #log.debug(hex(hwnd_parent)) #log.debug("##" * 20) for item in self.winwin.win_dict[parent_hwnd]: #log.debug("****"*20) #log.debug("%s-[%s]-%s-%s" %(item[0], hex(item[1]), item[2], item[3])) #log.debug("****"*20) class_name, child_hwnd, parent_title, title = item #log.debug("%s,%s" %(hex(child_hwnd), class_name)) (left, top, right, bottom) = self.winwin.get_rect(child_hwnd) #log.debug("%s,%s,%s,%s" %(left, top, right, bottom)) #log.debug("class name:%s" % class_name) title = self.winwin.get_title(child_hwnd) #log.debug("[%s][%s] parent title:%s" % (hex(child_hwnd), title, parent_title)) if class_name == 'Edit': text = self.winwin.get_value(child_hwnd) #log.debug("text:%s" % text) if text != '': self.fields['edit1'] = text # Current Sequence Statistics elif class_name == 'SysListView32' or class_name == 'ListBox': data = self.winwin.get_list_view_items(parent_hwnd, column_index=1) log.debug(hex(parent_hwnd)) #v = self.winwin.get_parent_info(parent_hwnd) #log.debug("%s - %s" %(hex(v[0]), v[1])) #v = self.winwin.get_parent_info(v[0]) #log.debug("%s - %s" %(hex(v[0]), v[1])) #if v[1] == 'Channel Counters': # self.fields['counter1'] = int(data[0]) # self.fields['counter2'] = int(data[1]) log.debug(data) elif class_name == 'ThunderRT6TextBox' and parent_title == 'Current Sequence Statistics': title = self.winwin.get_value( child_hwnd) #self.winwin.get_title(child_hwnd) log.debug("###############[%s]" % (title)) log.debug("###>%s [%s,%s,%s,%s][%s]" % (title, left, top, right, bottom, left + top + right + bottom)) self.fields['total_time'] = title elif class_name == 'ThunderRT6TextBox' and parent_title == 'Current Status': title = self.winwin.get_title(child_hwnd) log.debug("===>%s [%s,%s,%s,%s][%s]" % (title, left, top, right, bottom, left + top + right + bottom)) self.fields['sequence'] = title elif class_name == 'ProgressBar20WndClass' and parent_title == 'Current Status': pos, range = self.winwin.get_progress(child_hwnd) self.fields['progress'] = pos / range log.debug("%s:%s" % (pos, range)) def enqueue(self): """ put data to queue in redis """ if len(self.fields) > 0: json_data = self.build_json() log.debug(json_data) ### send data to influxdb #influxc.send(json_data) eqpt_no = "abc" timestamp = int(time.time()) cmd = "cmd" measurement = "RPC" rawdata = self.fields data = self.fields log.debug(self.fields) rtn = self.red.enqueue(eqpt_no, timestamp, cmd, msgpack.packb(rawdata), msgpack.packb(data), measurement.lower()) log.debug(rtn) elif len(self.winwin.win_dict) > 0: pass else: log.debug("find window again...") self.winwin.get_win_list(self.title_like) def run(self): """ run the job """ while True: ### init fields each round self.fields = {} try: #log.debug("abc") self.build_fields() log.debug(self.fields) self.enqueue() except ConnectionError as err: log.error(traceback.format_exc()) except NoScriptError as err: log.error(traceback.format_exc()) try: self.red.load_script(self.conf['output']['enqueue_script']) except: pass except AttributeError as err: log.error(traceback.format_exc()) except Exception as ex: log.error(traceback.format_exc()) log.debug("find window again...") self.winwin.get_win_list(self.title_like) time.sleep(self.interval)