def test_get_ini(): mi = MyIni() assert 'host' in mi.get_kakou() assert 'port' in mi.get_kakou() assert 'id_flag' in mi.get_kakou() assert 'city' in mi.get_kakou() assert 'host' in mi.get_union() assert 'port' in mi.get_union()
def ini_test(): myini = MyIni() try: print myini.get_kakou() print myini.get_hbc() print myini.get_mysql() print myini.get_syst() except ConfigParser.NoOptionError as e: print e
class KakouTest(object): def __init__(self): self.my_ini = MyIni() self.kakou = Kakou(self.my_ini.get_kakou()) def get_maxid(self): print self.kakou.get_maxid() def get_cltxs(self): print self.kakou.get_cltxs(123,1235) def get_bkcp_by_hphm(self): print self.kakou.get_bkcp_by_hphm(u'粤L12345')
class KakouTest(object): def __init__(self): self.my_ini = MyIni() self.kakou = Kakou(**self.my_ini.get_kakou()) def get_maxid(self): print self.kakou.get_maxid() def get_vehicle_by_id(self): print self.kakou.get_vehicle_by_id(140) def get_kkdd_by_id(self): print self.kakou.get_kkdd_by_id(441302001)
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 FetchData(object): def __init__(self): self.myini = MyIni() self.kakou_conf = self.myini.get_kakou() self.hbc_conf = self.myini.get_hbc() self.kakou_ini = { 'host': self.kakou_conf['host'], 'port': self.kakou_conf['port'] } self.hbc_ini = { 'host': self.hbc_conf['host'], 'port': self.hbc_conf['port'] } self.id_flag = self.kakou_conf['id_flag'] self.step = self.kakou_conf['id_step'] self.kkdd = self.kakou_conf['kkdd'] self.city = self.kakou_conf['city'] self.kakou_status = False self.hbc_status = False def __del__(self): del self.myini def kakou_post(self, carinfo): """上传卡口数据""" headers = {'content-type': 'application/json'} url = 'http://{0[host]}:{0[port]}/hbc'.format(self.hbc_ini) data = {'carinfo': carinfo} try: r = requests.post(url, headers=headers, data=json.dumps(data)) if r.status_code == 200 or r.status_code == 429: return r else: self.hbc_status = False raise Exception('url: {url}, status: {code}, {text}'.format( url=url, code=r.status_code, text=r.text)) except Exception as e: self.hbc_status = False raise def que_get(self): """查看队列情况""" url = 'http://{0[host]}:{0[port]}/que'.format(self.hbc_ini) try: r = requests.post(url, headers=headers, data=json.dumps(data)) if r.status_code == 200: return r else: self.hbc_status = False raise Exception('url: {url}, status: {code}, {text}'.format( url=url, code=r.status_code, text=r.text)) except Exception as e: self.hbc_status = False raise def get_cltxs(self, id_flag, step=100): #last_id = self.id_flag + self.step url = 'http://{0[host]}:{0[port]}/rest_hz_kakou/index.php/{1}/kakou/cltxs/{2}/{3}'.format( self.kakou_ini, self.city, id_flag, id_flag+step) try: r = requests.get(url) if r.status_code == 200: return json.loads(r.text) else: self.kakou_status = False raise Exception('url: {url}, status: {code}, {text}'.format( url=url, code=r.status_code, text=r.text)) except Exception as e: self.kakou_status = False raise def get_cltxmaxid(self): """获取最大cltx表id值""" url = 'http://{0[host]}:{0[port]}/rest_hz_kakou/index.php/{1}/kakou/cltxmaxid'.format( self.kakou_ini, self.city) try: r = requests.get(url) if r.status_code == 200: return json.loads(r.text) else: self.kakou_status = False raise Exception('url: {url}, status: {code}, {text}'.format( url=url, code=r.status_code, text=r.text)) except Exception as e: self.kakou_status = False raise def fetch_data(self): """获取卡口车辆信息""" maxid = self.get_cltxmaxid()['maxid'] if maxid <= self.id_flag: # 没有新的数据 返回1 return info = self.get_cltxs() if info['total_count'] == 0: if self.id_flag + self.step < maxid: self.id_flag += self.step else: self.id_flag = maxid self.myini.set_hbc(self.id_flag) return # 过滤无效车牌 def data_valid(i)