class DeviceTest(object): def __init__(self): self.my_ini = MyIni() print self.my_ini.get_device() self.dev = Device(**self.my_ini.get_device()) def test_get_device(self): """上传卡口数据""" #print self.dev.get_device_list() #print self.dev.get_device_list(type=2) #print self.dev.get_device_list(type=3) print self.dev.get_device_by_ip('127.0.0.1') print self.dev.get_device_check(num=20, type=2) print self.dev.get_device_check(num=10, type=3) #assert r['headers'] == 201 def test_set_device(self): data = [{ 'ip': '127.0.0.1', 'status': True }, { 'ip': '127.0.0.2', 'status': False }] r = self.dev.set_device(data) print r
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()
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
def __init__(self): self.myini = MyIni() self.hbc_conf = self.myini.get_hbc() self.kakou_ini = {'host': '10.47.187.165', 'port': 80} self.cgs_ini = { 'host': '10.47.222.45', 'port': 8080, 'username': '******', 'password': '******' } self.hbc_ini = { 'host': '127.0.0.1', 'port': 5000, 'username': '******', 'password': '******' } #self.cgs2_ini = {'host': '10.47.222.45', 'port': 8081} self.id_flag = self.hbc_conf['id_flag'] self.step = self.hbc_conf['step'] self.kkdd = self.hbc_conf['kkdd'] self.city = self.hbc_conf['city'] self.city_name = self.hbc_conf['city_name'] self.kakou_status = False self.cgs_status = False self.hbc_status = False # 黄标车集合 dict self.gdhbc_dict = {} # 号牌颜色字典 dict self.hpys_dict = { 'WT': u'白底黑字', 'YL': u'黄底黑字', 'BU': u'蓝底白字', 'BK': u'黑底白字', 'QT': u'其他' } # 机号字典 dict self.jh_dict = {} # 设备代号 dict self.sbdh_dict = {} # 方向编号 dict self.fxbh_dict = { 'NS': u'北向南', 'SN': u'南向北', 'EW': u'东向西', 'WE': u'西向东', 'IN': u'东向西', 'OT': u'西向东' } self.hbc_img_path = self.hbc_conf[ 'hbc_img_path'] #u'd://videoandimage' self.wz_img_path = self.hbc_conf['wz_img_path'] self.hbc_img_dict = {}
def __init__(self): self.myini = MyIni() self.hbc_conf = self.myini.get_hbc() self.kakou_ini = {'host': '10.47.187.165', 'port': 80} self.cgs_ini = {'host': '10.47.222.45', 'port': 8080, 'username':'******', 'password': '******', 'token': 'eyJhbGciOiJIUzI1NiIsImV4cCI6MTQ0MzI1NjcyMiwiaWF0IjoxNDQzMjQ5NTIyfQ.eyJzY29wZSI6WyJzY29wZV9nZXQiLCJoemhiY19nZXQiXSwidWlkIjoyM30.Qga6zksBXBu8Aq9zVBb7tsR_vQFI4A7IfzdgMvGEfrw'} self.hbc_ini = {'host': '10.47.222.45', 'port': 8081, 'username':'******', 'password': '******', 'token': ''} self.id_flag = self.hbc_conf['id_flag'] self.step = self.hbc_conf['step'] self.kkdd = self.hbc_conf['kkdd'] self.kakou_status = False self.cgs_status = False self.hbc_status = False # 黄标车集合 self.hzhbc_set = set() self.hpys5 = {'WT':u'白底黑字','YL':u'黄底黑字', 'BU':u'蓝底白字','BK':u'黑底白字','QT':u'其他'} self.floder2 = { '441302101': u'462', '441302102': u'461', '441302103': u'458', '441302104': u'460', '441302105': u'459', '441302106': u'457' } self.fxbh3 = { 'NS': u'北向南', 'SN': u'南向北', 'EW': u'东向西', 'WE': u'西向东', 'IN': u'东向西', 'OT': u'西向东' } self.city = { '441302': 'hcq', '441303': 'hy', '441322': 'bl' '441323': 'hd', '441324': 'lm', '441333': 'dyw' } self.img_file = 'd://videoandimage'
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 PingTest(object): def __init__(self): self.my_ini = MyIni() self.p = Ping(**self.my_ini.get_ping()) def test_ping(self): """上传卡口数据""" r = self.p.get_ping('10.44.240.6') print r
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
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 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)
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 __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 __init__(self): self.myini = MyIni() self.hbc_conf = self.myini.get_hbc() self.kakou_ini = {'host': '10.47.187.165', 'port': 80} self.cgs_ini = {'host': '10.47.222.45', 'port': 8080, 'username': '******', 'password': '******'} self.hbc_ini = {'host': '127.0.0.1', 'port': 5000, 'username': '******', 'password': '******'} #self.cgs2_ini = {'host': '10.47.222.45', 'port': 8081} self.id_flag = self.hbc_conf['id_flag'] self.step = self.hbc_conf['step'] self.kkdd = self.hbc_conf['kkdd'] self.city = self.hbc_conf['city'] self.city_name = self.hbc_conf['city_name'] self.kakou_status = False self.cgs_status = False self.hbc_status = False # 黄标车集合 dict self.gdhbc_dict = {} # 号牌颜色字典 dict self.hpys_dict = { 'WT': u'白底黑字', 'YL': u'黄底黑字', 'BU': u'蓝底白字', 'BK': u'黑底白字', 'QT': u'其他' } # 机号字典 dict self.jh_dict = {} # 设备代号 dict self.sbdh_dict = {} # 方向编号 dict self.fxbh_dict = { 'NS': u'北向南', 'SN': u'南向北', 'EW': u'东向西', 'WE': u'西向东', 'IN': u'东向西', 'OT': u'西向东' } self.hbc_img_path = self.hbc_conf['hbc_img_path']#u'd://videoandimage' self.wz_img_path = self.hbc_conf['wz_img_path'] self.hbc_img_dict = {}
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 __init__(self): self.my_ini = MyIni() self.sms = SMS(**self.my_ini.get_sms())
def __init__(self): self.my_ini = MyIni()
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)
def __init__(self): self.my_ini = MyIni() print self.my_ini.get_device() self.dev = Device(**self.my_ini.get_device())
class HbcCompare(object): def __init__(self): self.myini = MyIni() self.hbc_conf = self.myini.get_hbc() self.kakou_ini = {'host': '10.47.187.165', 'port': 80} self.cgs_ini = { 'host': '10.47.222.45', 'port': 8080, 'username': '******', 'password': '******' } self.hbc_ini = { 'host': '127.0.0.1', 'port': 5000, 'username': '******', 'password': '******' } #self.cgs2_ini = {'host': '10.47.222.45', 'port': 8081} self.id_flag = self.hbc_conf['id_flag'] self.step = self.hbc_conf['step'] self.kkdd = self.hbc_conf['kkdd'] self.city = self.hbc_conf['city'] self.city_name = self.hbc_conf['city_name'] self.kakou_status = False self.cgs_status = False self.hbc_status = False # 黄标车集合 dict self.gdhbc_dict = {} # 号牌颜色字典 dict self.hpys_dict = { 'WT': u'白底黑字', 'YL': u'黄底黑字', 'BU': u'蓝底白字', 'BK': u'黑底白字', 'QT': u'其他' } # 机号字典 dict self.jh_dict = {} # 设备代号 dict self.sbdh_dict = {} # 方向编号 dict self.fxbh_dict = { 'NS': u'北向南', 'SN': u'南向北', 'EW': u'东向西', 'WE': u'西向东', 'IN': u'东向西', 'OT': u'西向东' } self.hbc_img_path = self.hbc_conf[ 'hbc_img_path'] #u'd://videoandimage' self.wz_img_path = self.hbc_conf['wz_img_path'] self.hbc_img_dict = {} #self.hbc_img = helper.hbc_img() def get_gdhbc_by_hphm(self, hphm, hpzl): headers = {'content-type': 'application/json'} url = u'http://{0[host]}:{0[port]}/hbc/{hphm}/{hpzl}'.format( self.cgs_ini, hphm=hphm, hpzl=hpzl) try: r = requests.get(url, headers) if r.status_code == 200: return json.loads(r.text) elif r.status_code == 404: return None else: self.cgs_status = False raise Exception('url: {url}, status: {code}, {text}'.format( url=url, code=r.status_code, text=r.text)) except Exception as e: self.cgs_status = False raise def get_gdhbc_all(self): headers = {'content-type': 'application/json'} url = 'http://{0[host]}:{0[port]}/hbc_all'.format(self.cgs_ini) try: r = requests.get(url, headers) if r.status_code == 200: items = json.loads(r.text)['items'] print 'hbc_num:%s' % len(items) for i in items: self.gdhbc_dict[(i['hphm'], i['hpzl'])] = i['ccdjrq'] else: self.cgs_status = False raise Exception('url: {url}, status: {code}, {text}'.format( url=url, code=r.status_code, text=r.text)) except Exception as e: self.cgs_status = False raise def get_cltxs(self): last_id = self.id_flag + self.step url = 'http://%s:%s/rest_hz_kakou/index.php/%s/kakou/cltxs/%s/%s' % ( self.kakou_ini['host'], self.kakou_ini['port'], self.city, self.id_flag, 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_cltxmaxid(self): url = 'http://%s:%s/rest_hz_kakou/index.php/%s/kakou/cltxmaxid' % ( self.kakou_ini['host'], self.kakou_ini['port'], 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: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.kakou_status = False raise def get_kkdd(self): """获取卡口地点代码""" url = 'http://%s:%s/kkdd/%s' % (self.hbc_ini['host'], self.hbc_ini['port'], self.kkdd) headers = {'content-type': 'application/json'} try: r = requests.get(url, headers=headers) if r.status_code == 200: items = json.loads(r.text)['items'] for i in items: self.jh_dict[i['kkdd_id']] = i['cf_id'] self.sbdh_dict[i['kkdd_id']] = i['sbdh'] else: self.hbc_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.hbc_status = False raise def get_hbc_img(self): """获取违章黄标车路标图片""" url = 'http://%s:%s/wzimg/%s' % (self.hbc_ini['host'], self.hbc_ini['port'], self.kkdd) headers = {'content-type': 'application/json'} try: r = requests.get(url, headers=headers) if r.status_code == 200: base_path = u'%s\%s' % (self.wz_img_path, self.kkdd) helper.makedirs(base_path) for i in json.loads(r.text)['items']: filename = u'%s\%s_%s.jpg' % (base_path, i['kkdd_id'], i['fxbh_code']) # 如果图片不存在则抓图 if not os.path.exists(filename): helper.get_url_img(i['img_url'], filename) self.hbc_img_dict[(i['kkdd_id'], i['fxbh_code'])] = filename else: self.hbc_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.hbc_status = False raise def check_hbc_img_by_hphm(self, date, hphm): headers = {'content-type': 'application/json'} url = 'http://%s:%s/hbc/img/%s/%s/%s' % ( self.hbc_ini['host'], self.hbc_ini['port'], date, hphm, self.kkdd) try: r = requests.get(url) if r.status_code == 200: return json.loads(r.text) else: self.hbc_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.hbc_status = False raise def check_hbc(self, hphm, hpzl): """检测是否黄标车""" ccdjrq = self.gdhbc_dict.get((hphm, hpzl), None) if not ccdjrq: return False h = self.get_gdhbc_by_hphm(hphm, hpzl) if not h: return False if arrow.get(ccdjrq).date() != arrow.get(h['ccdjrq']).date(): return False return True def add_hbc(self, data): """添加黄标车信息""" url = 'http://%s:%s/hbc' % (self.hbc_ini['host'], self.hbc_ini['port']) headers = {'content-type': 'application/json'} try: r = requests.post(url, headers=headers, data=json.dumps(data)) if r.status_code == 201: return json.loads(r.text) else: self.hbc_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.hbc_status = False raise def cmpare_hbc(self, i): """根据车辆信息比对黄标车""" # 判断车牌是否需要黄标车查询 f_hphm = helper.fix_hphm(i['hphm'], i['hpys_code']) if f_hphm['hpzl'] == '00': return # 是否在黄标车集合里面 if not self.check_hbc(f_hphm['hphm'], f_hphm['hpzl']): return jgsj = arrow.get(i['jgsj']) # print u'黄表车: %s, 号牌颜色: %s' % (i['hphm'], i['hpys']) hbc_img = self.check_hbc_img_by_hphm(jgsj.format('YYYY-MM-DD'), i['hphm']) imgpath = '' if hbc_img['total_count'] == 0: try: path = u'%s/%s/违章图片目录' % (self.hbc_img_path, jgsj.format(u'YYYY年MM月DD日')) # 图片名称 name = u'机号%s车道A%s%sR454DOK3T%sC%sP%s驶向%s违章黄标车违反禁令标志' % ( self.jh_dict[i['kkdd_id']], i['cdbh'], jgsj.format(u'YYYY年MM月DD日HH时mm分ss秒'), f_hphm['cpzl'], self.hpys_dict[i['hpys_code']], i['hphm'], self.fxbh_dict.get(i['fxbh_code'], u'其他')) # 水印内容 text = u'违法时间:%s 违法地点:%s%s\n违法代码:13441 违法行为:违章黄标车 设备编号:%s\n防伪码:%s' % ( i['jgsj'], self.city_name, i['kkdd'], self.sbdh_dict[i['kkdd_id']], helper.get_sign()) # 违章路标图片 wz_img = self.hbc_img_dict.get((i['kkdd_id'], i['fxbh_code']), None) ## if wz_img is not None: ## wz_img = u'hbc_img/'+wz_img imgpath = img_builder.get_img_by_url(i['imgurl'], path, name, text, wz_img) except Exception as e: logger.error('url: %s' % i['imgurl']) logger.error(e) imgpath = '' data = { 'jgsj': i['jgsj'], 'hphm': i['hphm'], 'kkdd_id': i['kkdd_id'], 'hpys_code': i['hpys_code'], 'fxbh_code': i['fxbh_code'], 'cdbh': i['cdbh'], 'imgurl': i['imgurl'], 'imgpath': imgpath } # 添加黄标车信息到数据库 self.add_hbc(data) def fetch_data(self): """获取卡口车辆信息""" maxid = self.get_cltxmaxid()['maxid'] if maxid <= self.id_flag: # 没有新的数据 返回1 time.sleep(1) return 1 carinfo = self.get_cltxs() if carinfo['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) time.sleep(1) # print 'id_flag: %s' % self.id_flag return 0 elif carinfo['total_count'] < self.step: time.sleep(1) # 遍历列表并比对是否黄标车 for i in carinfo['items']: if i['kkdd_id'] is not None: self.cmpare_hbc(i) self.id_flag = carinfo['items'][-1]['id'] self.myini.set_hbc(self.id_flag) print '{city}_id_flag: {flag}'.format(city=self.city, flag=self.id_flag) return -1 def main_loop(self): # 时间戳标记 time_flag = time.time() # 加载初始化数据 init_flag = False while 1: if not init_flag: try: # 获取黄标车数据 self.get_gdhbc_all() self.cgs_status = True # 获取卡口地点数据 self.get_kkdd() self.hbc_status = True init_flag = True print 'Init Finish' except Exception as e: logger.error(e) time.sleep(1) elif self.kakou_status and self.cgs_status and self.hbc_status: try: # 当前时间大于时间戳标记时间2小时则更新黄标车数据 if time.time() - time_flag > 7200: self.get_gdhbc_all() time_flag = time.time() self.fetch_data() except Exception as e: logger.error(e) time.sleep(1) else: try: if not self.kakou_status: self.get_cltxmaxid() self.kakou_status = True if not self.cgs_status: self.get_gdhbc_by_hphm(u'粤L12345', '02') self.cgs_status = True if not self.hbc_status: self.check_hbc_img_by_hphm('2015-09-26', u'粤L12345') self.hbc_status = True except Exception as e: #print (e) time.sleep(1) del hbc
from hbc import app, views from ini_conf import MyIni if __name__ == '__main__': my_ini = MyIni('my_ini.conf') s_ini = my_ini.get_sys() h_ini = my_ini.get_hzhbc() app.config['SECRET_KEY'] = s_ini['secret_key'] app.config['EXPIRES'] = s_ini['expires'] app.config['WHITE_LIST_OPEN'] = s_ini['white_list_open'] app.config['WHITE_LIST'] = set(s_ini['white_list'].split(',')) app.config['SQLALCHEMY_BINDS'] = { 'hz_hbc': 'mysql://%s:%s@%s:%s/%s' % (h_ini['username'], h_ini['password'], h_ini['host'], h_ini['port'], h_ini['db']) } app.run(port=8098, threaded=True)
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)
def test_set_id_flag(): mi = MyIni() mi.set_id(709394)
class HbcCompare(object): def __init__(self): self.myini = MyIni() self.hbc_conf = self.myini.get_hbc() self.kakou_ini = {'host': '10.47.187.165', 'port': 80} self.cgs_ini = {'host': '10.47.222.45', 'port': 8080, 'username':'******', 'password': '******', 'token': 'eyJhbGciOiJIUzI1NiIsImV4cCI6MTQ0MzI1NjcyMiwiaWF0IjoxNDQzMjQ5NTIyfQ.eyJzY29wZSI6WyJzY29wZV9nZXQiLCJoemhiY19nZXQiXSwidWlkIjoyM30.Qga6zksBXBu8Aq9zVBb7tsR_vQFI4A7IfzdgMvGEfrw'} self.hbc_ini = {'host': '10.47.222.45', 'port': 8081, 'username':'******', 'password': '******', 'token': ''} self.id_flag = self.hbc_conf['id_flag'] self.step = self.hbc_conf['step'] self.kkdd = self.hbc_conf['kkdd'] self.kakou_status = False self.cgs_status = False self.hbc_status = False # 黄标车集合 self.hzhbc_set = set() self.hpys5 = {'WT':u'白底黑字','YL':u'黄底黑字', 'BU':u'蓝底白字','BK':u'黑底白字','QT':u'其他'} self.floder2 = { '441302101': u'462', '441302102': u'461', '441302103': u'458', '441302104': u'460', '441302105': u'459', '441302106': u'457' } self.fxbh3 = { 'NS': u'北向南', 'SN': u'南向北', 'EW': u'东向西', 'WE': u'西向东', 'IN': u'东向西', 'OT': u'西向东' } self.city = { '441302': 'hcq', '441303': 'hy', '441322': 'bl' '441323': 'hd', '441324': 'lm', '441333': 'dyw' } self.img_file = 'd://videoandimage' def cgs_token(self): url = 'http://%s:%s/token' % (self.cgs_ini['host'], self.cgs_ini['port']) headers = {'content-type': 'application/json'} data = {'username': self.cgs_ini['username'], 'password': self.cgs_ini['password']} try: r = requests.post(url, headers=headers, data=json.dumps(data)) if r.status_code == 200: self.cgs_ini['token'] = json.loads(r.text)['access_token'] #print json.loads(r.text)['access_token'] else: self.cgs_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.cgs_status = False raise def hbc_token(self): url = 'http://%s:%s/token' % (self.hbc_ini['host'], self.hbc_ini['port']) headers = {'content-type': 'application/json'} data = {'username': self.hbc_ini['username'], 'password': self.hbc_ini['password']} try: r = requests.post(url, headers=headers, data=json.dumps(data)) if r.status_code == 201: self.hbc_ini['token'] = json.loads(r.text)['access_token'] else: self.hbc_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.hbc_status = False raise def get_hzhbc_all(self): headers = {'content-type': 'application/json', 'access_token': self.cgs_ini['token']} url = 'http://%s:%s/hzhbc' % (self.cgs_ini['host'], self.cgs_ini['port']) try: r = requests.get(url, headers) if r.status_code == 200: items = json.loads(r.text)['items'] print 'hbc_num:%s' % len(items) for i in items: self.hzhbc_set.add((i['hphm'], i['hpzl'])) else: self.cgs_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.cgs_status = False raise def get_cltxs(self): last_id = self.id_flag + self.step url = 'http://%s:%s/rest_hz_kakou/index.php/%s/kakou/cltxs/%s/%s' %(self.kakou_ini['host'], self.kakou_ini['port'], self.city.get(self.kkdd, 'hz'), self.id_flag, 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
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)
from cgs import app, views from ini_conf import MyIni if __name__ == '__main__': debug_logging(u'logs/error.log') access_logging(u'logs/access.log') my_ini = MyIni('my_ini.conf') s_ini = my_ini.get_sys() h_ini = my_ini.get_hzhbc() v_ini = my_ini.get_gdvehicle() app.config['SECRET_KEY'] = s_ini['secret_key'] app.config['EXPIRES'] = s_ini['expires'] app.config['WHITE_LIST_OPEN'] = s_ini['white_list_open'] app.config['WHITE_LIST'] = set(s_ini['white_list'].split(',')) app.config['SQLALCHEMY_BINDS'] = { 'cgs': 'mysql://%s:%s@%s:%s/%s' % (v_ini['username'], v_ini['password'], v_ini['host'], v_ini['port'], v_ini['db']), 'hbc': 'mysql://%s:%s@%s:%s/%s' % (h_ini['username'], h_ini['password'], h_ini['host'], h_ini['port'], h_ini['db']) } app.run(host='0.0.0.0', port=5000, threaded=True)
def __init__(self): self.my_ini = MyIni() self.p = Ping(**self.my_ini.get_ping())
def __init__(self): self.my_ini = MyIni() self.kakou = Kakou(**self.my_ini.get_kakou())
from sms import app from ini_conf import MyIni if __name__ == '__main__': my_ini = MyIni() mysql_ini = my_ini.get_mysql() sys_ini = my_ini.get_sys() app.config['SECRET_KEY'] = sys_ini['secret_key'] app.config['WHITE_LIST_OPEN'] = sys_ini['while_list_open'] app.config['WHITE_LIST'] = sys_ini['while_list'].split(',') app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://%s:%s@%s:%s/%s' % ( mysql_ini['user'], mysql_ini['pwd'], mysql_ini['host'], mysql_ini['port'], mysql_ini['db']) app.config['SMS_WSDL_PARAMS'] = my_ini.get_webservice() del my_ini app.run(host=sys_ini['host'], port=sys_ini['port'], threaded=True)
class HbcCompare(object): def __init__(self): self.myini = MyIni() self.hbc_conf = self.myini.get_hbc() self.kakou_ini = {'host': '10.47.187.165', 'port': 80} self.cgs_ini = {'host': '10.47.222.45', 'port': 8080, 'username': '******', 'password': '******'} self.hbc_ini = {'host': '127.0.0.1', 'port': 5000, 'username': '******', 'password': '******'} #self.cgs2_ini = {'host': '10.47.222.45', 'port': 8081} self.id_flag = self.hbc_conf['id_flag'] self.step = self.hbc_conf['step'] self.kkdd = self.hbc_conf['kkdd'] self.city = self.hbc_conf['city'] self.city_name = self.hbc_conf['city_name'] self.kakou_status = False self.cgs_status = False self.hbc_status = False # 黄标车集合 dict self.gdhbc_dict = {} # 号牌颜色字典 dict self.hpys_dict = { 'WT': u'白底黑字', 'YL': u'黄底黑字', 'BU': u'蓝底白字', 'BK': u'黑底白字', 'QT': u'其他' } # 机号字典 dict self.jh_dict = {} # 设备代号 dict self.sbdh_dict = {} # 方向编号 dict self.fxbh_dict = { 'NS': u'北向南', 'SN': u'南向北', 'EW': u'东向西', 'WE': u'西向东', 'IN': u'东向西', 'OT': u'西向东' } self.hbc_img_path = self.hbc_conf['hbc_img_path']#u'd://videoandimage' self.wz_img_path = self.hbc_conf['wz_img_path'] self.hbc_img_dict = {} #self.hbc_img = helper.hbc_img() def get_gdhbc_by_hphm(self, hphm, hpzl): headers = { 'content-type': 'application/json' } url = u'http://{0[host]}:{0[port]}/hbc/{hphm}/{hpzl}'.format( self.cgs_ini, hphm=hphm, hpzl=hpzl) try: r = requests.get(url, headers) if r.status_code == 200: return json.loads(r.text) elif r.status_code == 404: return None else: self.cgs_status = False raise Exception('url: {url}, status: {code}, {text}'.format( url=url, code=r.status_code, text=r.text)) except Exception as e: self.cgs_status = False raise def get_gdhbc_all(self): headers = { 'content-type': 'application/json' } url = 'http://{0[host]}:{0[port]}/hbc_all'.format(self.cgs_ini) try: r = requests.get(url, headers) if r.status_code == 200: items = json.loads(r.text)['items'] print 'hbc_num:%s' % len(items) for i in items: self.gdhbc_dict[(i['hphm'], i['hpzl'])] = i['ccdjrq'] else: self.cgs_status = False raise Exception('url: {url}, status: {code}, {text}'.format( url=url, code=r.status_code, text=r.text)) except Exception as e: self.cgs_status = False raise def get_cltxs(self): last_id = self.id_flag + self.step url = 'http://%s:%s/rest_hz_kakou/index.php/%s/kakou/cltxs/%s/%s' % ( self.kakou_ini['host'], self.kakou_ini['port'], self.city, self.id_flag, 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_cltxmaxid(self): url = 'http://%s:%s/rest_hz_kakou/index.php/%s/kakou/cltxmaxid' % ( self.kakou_ini['host'], self.kakou_ini['port'], 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: %s, status: %s, %s' % ( url, r.status_code, r.text)) except Exception as e: self.kakou_status = False raise def get_kkdd(self): """获取卡口地点代码""" url = 'http://%s:%s/kkdd/%s' % ( self.hbc_ini['host'], self.hbc_ini['port'], self.kkdd) headers = { 'content-type': 'application/json' } try: r = requests.get(url, headers=headers) if r.status_code == 200: items = json.loads(r.text)['items'] for i in items: self.jh_dict[i['kkdd_id']] = i['cf_id'] self.sbdh_dict[i['kkdd_id']] = i['sbdh'] else: self.hbc_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.hbc_status = False raise def get_hbc_img(self): """获取违章黄标车路标图片""" url = 'http://%s:%s/wzimg/%s' % ( self.hbc_ini['host'], self.hbc_ini['port'], self.kkdd) headers = { 'content-type': 'application/json' } try: r = requests.get(url, headers=headers) if r.status_code == 200: base_path = u'%s\%s' % (self.wz_img_path, self.kkdd) helper.makedirs(base_path) for i in json.loads(r.text)['items']: filename = u'%s\%s_%s.jpg' % (base_path, i['kkdd_id'], i['fxbh_code']) # 如果图片不存在则抓图 if not os.path.exists(filename): helper.get_url_img(i['img_url'], filename) self.hbc_img_dict[(i['kkdd_id'], i['fxbh_code'])] = filename else: self.hbc_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.hbc_status = False raise def check_hbc_img_by_hphm(self, date, hphm): headers = { 'content-type': 'application/json' } url = 'http://%s:%s/hbc/img/%s/%s/%s' % ( self.hbc_ini['host'], self.hbc_ini['port'], date, hphm, self.kkdd) try: r = requests.get(url) if r.status_code == 200: return json.loads(r.text) else: self.hbc_status = False raise Exception('url: %s, status: %s, %s' % (url, r.status_code, r.text)) except Exception as e: self.hbc_status = False raise def check_hbc(self, hphm, hpzl): """检测是否黄标车""" ccdjrq = self.gdhbc_dict.get((hphm, hpzl), None) if not ccdjrq: return False h = self.get_gdhbc_by_hphm(hphm, hpzl) if not h: return False if arrow.get(ccdjrq).date() != arrow.get(h['ccdjrq']).date(): return False return True def add_hbc(self, data): """添加黄标车信息""" url = 'http://%s:%s/hbc' % ( self.hbc_ini['host'], self.hbc_ini['port']) headers = { 'content-type': 'application/json' } try: r = requests.post(url, headers=headers, data=json.dumps(data)) if r.status_code == 201: return json.loads(r.text) else: self.hbc_status = False raise Exception('url: %s, status: %s, %s' % ( url, r.status_code, r.text)) except Exception as e: self.hbc_status = False raise def cmpare_hbc(self, i): """根据车辆信息比对黄标车""" # 判断车牌是否需要黄标车查询 f_hphm = helper.fix_hphm(i['hphm'], i['hpys_code']) if f_hphm['hpzl'] == '00': return # 是否在黄标车集合里面 if not self.check_hbc(f_hphm['hphm'], f_hphm['hpzl']): return jgsj = arrow.get(i['jgsj']) # print u'黄表车: %s, 号牌颜色: %s' % (i['hphm'], i['hpys']) hbc_img = self.check_hbc_img_by_hphm( jgsj.format('YYYY-MM-DD'), i['hphm']) imgpath = '' if hbc_img['total_count'] == 0: try: path = u'%s/%s/违章图片目录' % ( self.hbc_img_path, jgsj.format(u'YYYY年MM月DD日')) # 图片名称 name = u'机号%s车道A%s%sR454DOK3T%sC%sP%s驶向%s违章黄标车违反禁令标志' % ( self.jh_dict[i['kkdd_id']], i['cdbh'], jgsj.format(u'YYYY年MM月DD日HH时mm分ss秒'), f_hphm['cpzl'], self.hpys_dict[i['hpys_code']], i['hphm'], self.fxbh_dict.get(i['fxbh_code'], u'其他')) # 水印内容 text = u'违法时间:%s 违法地点:%s%s\n违法代码:13441 违法行为:违章黄标车 设备编号:%s\n防伪码:%s' % ( i['jgsj'], self.city_name, i['kkdd'], self.sbdh_dict[i['kkdd_id']], helper.get_sign()) # 违章路标图片 wz_img = self.hbc_img_dict.get((i['kkdd_id'], i['fxbh_code']), None) ## if wz_img is not None: ## wz_img = u'hbc_img/'+wz_img imgpath = img_builder.get_img_by_url( i['imgurl'], path, name, text, wz_img) except Exception as e: logger.error('url: %s' % i['imgurl']) logger.error(e) imgpath = '' data = { 'jgsj': i['jgsj'], 'hphm': i['hphm'], 'kkdd_id': i['kkdd_id'], 'hpys_code': i['hpys_code'], 'fxbh_code': i['fxbh_code'], 'cdbh': i['cdbh'], 'imgurl': i['imgurl'], 'imgpath': imgpath } # 添加黄标车信息到数据库 self.add_hbc(data) def fetch_data(self): """获取卡口车辆信息""" maxid = self.get_cltxmaxid()['maxid'] if maxid <= self.id_flag: # 没有新的数据 返回1 time.sleep(1) return 1 carinfo = self.get_cltxs() if carinfo['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) time.sleep(1) # print 'id_flag: %s' % self.id_flag return 0 elif carinfo['total_count'] < self.step: time.sleep(1) # 遍历列表并比对是否黄标车 for i in carinfo['items']: if i['kkdd_id'] is not None: self.cmpare_hbc(i) self.id_flag = carinfo['items'][-1]['id'] self.myini.set_hbc(self.id_flag) print '{city}_id_flag: {flag}'.format(city=self.city, flag=self.id_flag) return -1 def main_loop(self): # 时间戳标记 time_flag = time.time() # 加载初始化数据 init_flag = False while 1: if not init_flag: try: # 获取黄标车数据 self.get_gdhbc_all() self.cgs_status = True # 获取卡口地点数据 self.get_kkdd() self.hbc_status = True init_flag = True print 'Init Finish' except Exception as e: logger.error(e) time.sleep(1) elif self.kakou_status and self.cgs_status and self.hbc_status: try: # 当前时间大于时间戳标记时间2小时则更新黄标车数据 if time.time() - time_flag > 7200: self.get_gdhbc_all() time_flag = time.time() self.fetch_data() except Exception as e: logger.error(e) time.sleep(1) else: try: if not self.kakou_status: self.get_cltxmaxid() self.kakou_status = True if not self.cgs_status: self.get_gdhbc_by_hphm(u'粤L12345', '02') self.cgs_status = True if not self.hbc_status: self.check_hbc_img_by_hphm('2015-09-26', u'粤L12345') self.hbc_status = True except Exception as e: #print (e) time.sleep(1) del hbc
def __init__(self): self.my_ini = MyIni() self.sms = SMS(self.my_ini.get_sms())
from sms import app from ini_conf import MyIni if __name__ == '__main__': my_ini = MyIni() mysql_ini = my_ini.get_mysql() sys_ini = my_ini.get_sys() app.config['SECRET_KEY'] = sys_ini['secret_key'] app.config['WHITE_LIST_OPEN'] = sys_ini['while_list_open'] app.config['WHITE_LIST'] = sys_ini['while_list'].split(',') app.config['SQLALCHEMY_DATABASE_URI']= 'mysql://%s:%s@%s:%s/%s' % ( mysql_ini['user'], mysql_ini['pwd'], mysql_ini['host'], mysql_ini['port'], mysql_ini['db']) app.config['SMS_WSDL_PARAMS'] = my_ini.get_webservice() del my_ini app.run(host=sys_ini['host'], port=sys_ini['port'], threaded=True)
def __init__(self): self.my_ini = MyIni() self.kakou = Kakou(self.my_ini.get_kakou())
from car_recg import app from car_recg.recg_ser import RecgServer from ini_conf import MyIni if __name__ == '__main__': rs = RecgServer() rs.main() my_ini = MyIni() sys_ini = my_ini.get_sys_conf() app.config['THREADS'] = sys_ini['threads'] app.config['MAXSIZE'] = sys_ini['threads'] * 16 app.run(host='0.0.0.0', port=sys_ini['port'], threaded=True) del rs del my_ini