class ConfigTest(object): def __init__(self): self.my_ini = MyIni() def test_sms(self): print self.my_ini.get_sms() def test_ping(self): print self.my_ini.get_ping()
class SMSTest(object): def __init__(self): self.my_ini = MyIni() self.sms = SMS(self.my_ini.get_sms()) def get_sms(self): content = u'广东实现' mobiles = ['15819851862'] print self.sms.sms_send(content, mobiles)
class SMSTest(object): def __init__(self): self.my_ini = MyIni() self.sms = SMS(**self.my_ini.get_sms()) def __del__(self): pass def sms_post(self): mobiles = ['15819851862'] content = u'报警测试' print self.sms.sms_send(content, mobiles)
class WatchDog(object): def __init__(self): self.date_flag = arrow.now().replace(hours=-1) self.my_ini = MyIni() self.mobiles_list = self.my_ini.get_mobiles()['number'].split(',') #self.kakou_ini = self.my_ini.get_kakou() #self.sms_ini = self.my_ini.get_sms() self.fxbh_dict = { 'IN': u'进城', 'OT': u'出城', 'WE': u'西向东', 'EW': u'东向西', 'SN': u'南往北', 'NS': u'北往南', 'QT': u'其他' } self.sms = SMS(**self.my_ini.get_sms()) self.kakou = Kakou(**self.my_ini.get_kakou()) self.kkdd_list = [] # 短信发送记录,形如{('441302001', 'IN'): <Arrow [2016-03-02T20:08:58.190000+08:00]>} self.sms_send_dict = {} self.sms_send_time = 7 def __del__(self): pass def get_kkdd_list(self): self.kkdd_list = [] for i in ['441305']: self.kkdd_list += self.kakou.get_kkdd(i) def sms_send_info(self, sms_send_list): """发送短信通知""" t = arrow.now() content = u'[大亚湾卡口报警]\n' for i in sms_send_list: content += u'[{kkdd},{fxbh}]\n'.format( kkdd=i['kkdd'], fxbh=i['fx']) content += u'超过1小时没数据' self.sms.sms_send(content, self.mobiles_list) def check_kakou_count(self): """遍历检测所有卡口方向车流量""" t = arrow.now() # 待发送的卡口列表如[{'kkdd': '东江大桥卡口', 'fx': '进城'}] sms_send_list = [] for i in self.kkdd_list: for fx in i['fxbh_list']: count = self.kakou.get_kakou_count( st=t.replace(hours=-1).format('YYYY-MM-DD HH:mm:ss'), et=t.format('YYYY-MM-DD HH:mm:ss'), kkdd=i['kkdd_id'], fxbh=fx) # 如果车流量为0则发送短信 #print u'{0}{1}:{2}'.format(i['kkdd_name'], fx, count) if count <= 0: last_send_date = self.sms_send_dict.get( (i['kkdd_id'], fx), None) # 没有发送记录的 if last_send_date is None: self.sms_send_dict[(i['kkdd_id'], fx)] = t sms_send_list.append( {'kkdd': i['kkdd_name'], 'fx': self.fxbh_dict[fx]}) continue # 当前时间大于6am,并且当前时间大于历史记录发送时间18小时 if t.datetime.hour > self.sms_send_time and \ t > last_send_date.replace(hours=18): self.sms_send_dict[(i['kkdd_id'], fx)] = t sms_send_list.append( {'kkdd': i['kkdd_name'], 'fx': self.fxbh_dict[fx]}) if sms_send_list: self.sms_send_info(sms_send_list) def run(self): while 1: try: # 当前时间 t = arrow.now() # 每10分钟检查一遍 if t > self.date_flag.replace(minutes=10): self.get_kkdd_list() self.check_kakou_count() self.date_flag = t #print self.sms_send_dict print 'date_flag %s' % t except Exception as e: time.sleep(10) finally: time.sleep(1)
class SMSSender: def __init__(self): self.myini = MyIni() self.sms_conf = self.myini.get_sms() self.kakou_ini = { 'host': '10.47.187.165', 'port': 80 } self.id_flag = self.sms_conf['id_flag'] self.step = 100 self.kakou_status = False # 布控车牌字典形如 {'粤LXX266': {'kkdd': '东江大桥卡口', # 'jgsj': <Arrow [2016-03-04T09:39:45.738000+08:00]>}} self.bkcp_dict = {} def __del__(self): del self.myini def get_cltxmaxid(self): url = 'http://%s:%s/rest_hz_kakou/index.php/%s/kakou/cltxmaxid' % ( self.kakou_ini['host'], self.kakou_ini['port'], 'hcq') try: r = requests.get(url) if r.status_code == 200: return json.loads(r.text) else: self.kakou_status = False raise Exception('url: %s, status: %s, %s' % ( url, r.status_code, r.text)) except Exception as e: self.kakou_status = False raise def get_cltxs(self, _id, last_id): url = 'http://%s:%s/rest_hz_kakou/index.php/%s/kakou/cltxs/%s/%s' % ( self.kakou_ini['host'], self.kakou_ini['port'], 'hcq', _id, last_id) try: r = requests.get(url) if r.status_code == 200: return json.loads(r.text) else: self.kakou_status = False raise Exception('url: %s, status: %s, %s' % ( url, r.status_code, r.text)) except Exception as e: self.kakou_status = False raise def get_bkcp_by_hphm(self, hphm): url = u'http://%s:%s/rest_hz_kakou/index.php/%s/kakou/bkcp?q=%s' % ( self.kakou_ini['host'], self.kakou_ini['port'], 'hcq', hphm) try: r = requests.get(url) if r.status_code == 200: return json.loads(r.text) else: self.kakou_status = False raise Exception('url: %s, status: %s, %s' % ( url, r.status_code, r.text)) except Exception as e: self.kakou_status = False raise def send_sms(self, cltx): bkcp = self.get_bkcp_by_hphm(cltx['hphm']) if bkcp['total_count'] >= 1: try: print u'BKCP: {0}, {1}'.format(cltx['hphm'], cltx['jgsj']) memo = '' if bkcp['items'][0]['memo'] != None: memo = bkcp['items'][0]['memo'] data = { 'mobiles': bkcp['items'][0]['mobiles'].split(','), 'content': u'[惠城区卡口报警]%s,%s,%s,%s.(%s)' % ( cltx['jgsj'], cltx['kkdd'], cltx['fxbh'], cltx['hphm'], memo) } #print 'data: %s' % data helper.sms_post(data) except Exception as e: print (e) def check_is_repeat(self, hphm, kkdd, jgsj): """检查是否重复发送""" # 当前时间 t = arrow.get(jgsj) # 历史记录时间 h = self.bkcp_dict.get(hphm, None) try: # 没有历史记录 if h is None: return False # 卡口地点相同并且绝对时间差小于120秒 if h['kkdd'] == kkdd and abs((h['jgsj'] - t).total_seconds()) < 120: return True return False except Exception as e: raise finally: self.bkcp_dict[hphm] = {'kkdd': kkdd, 'jgsj': t} def loop_get_data(self): self.id_flag = self.get_cltxmaxid()['maxid'] - 100 while 1: try: maxid = self.get_cltxmaxid()['maxid'] if self.id_flag < maxid: #print self.id_flag cltx_dict = self.get_cltxs( self.id_flag, self.id_flag+self.step) if cltx_dict['total_count'] > 0: for i in cltx_dict['items']: if i['clbj'] in set(['B', 'L']): #print 'bkcp:%s,%s'%(i['hphm'], i['jgsj']) #self.send_sms(i) if not self.check_is_repeat( i['hphm'], i['kkdd'], i['jgsj']): self.send_sms(i) if self.id_flag+self.step < maxid: #print 'test' self.id_flag += self.step #self.myini.set_sms(self.id_flag) elif cltx_dict['total_count'] > 0: #print 'my_id:%s' % (cltx_dict['items'][-1]['id']) self.id_flag = cltx_dict['items'][-1]['id'] #self.myini.set_sms(self.id_flag) time.sleep(0.1) else: time.sleep(1) except Exception as e: print (e) time.sleep(5)