app.py 41.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
# coding=utf-8
#!/usr/bin/evn python
import flask
from flask import request,redirect,url_for,jsonify
#from werkzeug import secure_filename
import json,os,re,time,io,datetime
import requests
import ConfigParser
import sys

from werkzeug.utils import secure_filename

from log_module import logger

from code_convert import convert_code,wenan_code_huawei_code_CSYS,wenan_code_huawei_code_CLPP,wenan_code_huawei_code_CLLX\
    ,wenan_code_huawei_code_CDLX,wenan_code_huawei_code_event,wenan_code_huawei_code_HPYS,wenan_code_huawei_code_HPZL\
    ,wenan_code_huawei_code_WFDM,wenan_code_huawei_code_SJLX

from date_format import strtime_format_msec,strtime_format_nomsec

from tid_model import tid_maker,tid_end_dt

import videoconvert as videoutil
from upload_video_ftp import ftpconnect,uploadfile

##调试写文件
def debug_writeFile(data,file_name):
    #print 'wirte %s data--------------' %(data['event_type'])
    with open(file_name, 'w') as fp:
        fp.write(json.dumps(data, indent=4))


##配置文件
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.read(r'/opt/thrid_data.conf')
#config.read(r'thrid_data.conf')

hostIP = config.get('hostIP','hostIP')
service_port = config.get('hostIP','port')
illegal_url = config.get('illegal','url')
event_url = config.get('event','url')
t_flow_url = config.get('t_flow','url')
vehicle_url = config.get('vehicle','url')
ftpServerIp = config.get('ftp', 'ftpServerIP')
ftpUserName = config.get('ftp', 'ftpUserName')
ftpPassWord = config.get('ftp', 'ftpPassWord')



###替换字典key方法

def replace_Dictkey(source_dict,key_dict):
    #print 'replace_dictkey old_key is %r------------------' %key_dict
    for old_key in key_dict:
        #print('old_key is :%s' %old_key)
        if source_dict.has_key(old_key):
            source_dict[key_dict[old_key]] = source_dict.pop(old_key)
        else:
            print('source_dict not key:%s' %old_key)

#删除字段中字段
def del_dictKey(sour_dict,dict_keyList):
    for dict_key in dict_keyList:
        if sour_dict.has_key(dict_key):
            del sour_dict[dict_key]
        else:
            print 'sour_dict no key :%s' %dict_key

vehicle_event_oldKey = ['lane','illegal','version','vehicle','device','model','ID','speed','location','pics','RefinedFeature','driver']#,'start_dt','end_dt'
#交通卡口和违法数据重构
def make_vehicle(event_data_dict):
        #debug_writeFile(event_data_dict, 'make_vehicle_Data_to_json.txt')
    #try:
        #采集方式
        #del event_data_dict['CJFX']
        event_data_dict['GCSJ'] = event_data_dict.pop('TJSD')
        # #设备编号
        # event_data_dict['SBBH'] = event_data_dict['device'].pop('code')
        # #方向类型
        # event_data_dict['FXLX'] = event_data_dict['location']['direction'].pop('code')
        #车道号
        #event_data_dict['CDH'] = event_data_dict['lane'].pop('number')
        #抓拍方向
        event_data_dict['ZPFX'] = event_data_dict.pop('model')
        if event_data_dict['ZPFX'] == '0':
            event_data_dict['ZPFX'] = '2'


        if event_data_dict['illegal']['state'] == 0:
            print '***---过车数据---****'
            #转换成过车卡口数据
            # 车身body类
            # 车辆型号---子品牌?---当前结构里没有子品牌
            if event_data_dict['vehicle'].has_key('body'):
                if event_data_dict['vehicle']['body'].has_key('sublogo'):
                    event_data_dict['CLXH'] = event_data_dict['vehicle']['body']['sublogo']['name']


                if 'logo' in event_data_dict['vehicle']['body'].keys():
                    event_data_dict['CLPP'] = event_data_dict['vehicle']['body']['logo']['code']
                else:
                     event_data_dict['CLPP'] = '0'
                
                print event_data_dict['CLPP'],'888888888888888888------'
                # 年款
                #event_data_dict['CLNK'] = 'unknown'
                # 车身颜色
                if 'color' in event_data_dict['vehicle']['body']:
                    event_data_dict['CSYS'] = event_data_dict['vehicle']['body']['color']['code']
                else:
                    event_data_dict['CSYS'] = '5'

                # 车辆类型
                event_data_dict['CLLX'] = event_data_dict['vehicle']['body']['type'].pop('code')
                # 二次识别-号牌类plate
                # plate_keys = {'text': 'HPHM1', 'type_code': 'HPZL1', 'color_code': 'HPYS1'}
                # replace_Dictkey(event_data_dict['vehicle']['plate'], plate_keys)



            event_data_dict['HPHM'] = event_data_dict['vehicle']['plate'].pop('text')
            if event_data_dict['HPHM'] == u'无牌':
                event_data_dict['HPHM'] = '-'
            event_data_dict['HPZL'] = event_data_dict['vehicle']['plate'].pop('type_code')
            if event_data_dict['HPZL'] in ['31', '32', '00']:
                event_data_dict['HPZL'] = convert_code(event_data_dict['HPZL'], wenan_code_huawei_code_HPZL)

            #event_data_dict['HPZL1'] = convert_code(event_data_dict['HPZL1'], wenan_code_huawei_code_HPZL)
            event_data_dict['HPYS'] = event_data_dict['vehicle']['plate'].pop('color_code')
            #event_data_dict['HPYS1'] = convert_code(event_data_dict['HPYS1'],wenan_code_huawei_code_HPYS)
            if event_data_dict['ZPSL'] > 1:
                event_data_dict['ZP1'] = event_data_dict['pics'][0].pop('pic_base64')
                event_data_dict['ZP2'] = event_data_dict['pics'][1].pop('pic_base64')
            elif event_data_dict['ZPSL'] == 1:
                event_data_dict['ZP1'] = event_data_dict['pics'][0].pop('pic_base64')
                event_data_dict['ZP2'] = event_data_dict['ZP1']
            else:
                print("!!! No picture")

            del event_data_dict['ZPSL']
            del event_data_dict['start_dt']
            del event_data_dict['end_dt']

            ##转换号牌code
            event_data_dict['HPYS'] = convert_code(event_data_dict['HPYS'],wenan_code_huawei_code_HPYS)
            #debug_writeFile(event_data_dict,'makevehile.log')
        else:
            #转成违法数据
            print 'illegal state is :%s' %(event_data_dict['illegal']['state']),'***---违法数据---***'

            ####*****将相应的违法转换成事件----逆行、拥堵、缓行、变道,非机动车逆行
            #illegal_eventCode = ['06','01','40','26','61','21','19']
            illegal_eventCode = ['shijianweidinyi']
            if event_data_dict['illegal'].has_key('code'):
                #添加违法视频名(短时录像)字段
                if event_data_dict.has_key('video'):
                    if event_data_dict['video'].has_key('unid'):
                        if(event_data_dict['video']['unid']) > 0:
                            event_data_dict['WFSPWJM'] = event_data_dict['video'].pop('unid')

                if event_data_dict['illegal']['code'] in illegal_eventCode:
                    print '违法转事件***********************:%s',event_data_dict['illegal']['code']
                    #make_event_data(event_data_dict)---有重复处理字段报错-暂时不调用该函数
                    #debug_writeFile(event_data_dict,'illegal_eventCode.txt')
                    # 车道类型
                    event_data_dict['CDLX'] = event_data_dict['lane'].pop('code')
                    # if event_data.has_key('CDLX'):
                    #     event_data['CDLX'] = convert_code(event_data['CDLX'], wenan_code_huawei_code_CDLX)
                    # 二次识别-号牌类plate
                    # plate_keys = {'text': 'HPHM', 'type_code': 'HPZL'}
                    # replace_Dictkey(event_data_dict['vehicle']['plate'], plate_keys)
                    # debug_writeFile(event_data_dict,'debug.log')
                    #
                    # if event_data_dict['HPZL'] in ['51', '52', '00']:
                    #     event_data_dict['HPZL'] = convert_code(event_data_dict['HPZL'], wenan_code_huawei_code_HPZL)
                    #event_data_dict['HPZL'] = convert_code(event_data_dict['HPZL'], wenan_code_huawei_code_HPZL)
                    # 发生地点
                    event_data_dict['FSDD'] = event_data_dict['location'].pop('name')
                    if not event_data_dict['FSDD']:
                        print('FSDD','no adress....')
                        event_data_dict['FSDD'] = 'no adress'
                    # 开始结束时间
                    event_data_keys = {'start_dt': 'FSSJ', 'end_dt': 'JSSJ'}
                    replace_Dictkey(event_data_dict, event_data_keys)
                    # 事件
                    event_data_dict['SJLX'] = event_data_dict['illegal'].pop('code')
                    # 图片
                    event_data_dict['ZP1'] = event_data_dict['pics'][0].pop('pic_base64')
                    #event_data_dict['ZPLJ1'] = 'no url'
                    # 视频字段
                    event_data_dict['KSSP'] = 'no video'

                    illegal_event_delkey = ['GCSJ',"KKBH","ZPFX","ZPSL","CJFX"]
                    del_dictKey(event_data_dict,illegal_event_delkey)

                    ##转换事件code
                    event_data_dict['SJLX'] = convert_code(event_data_dict['SJLX'],wenan_code_huawei_code_event)

                else:
                    if event_data_dict['vehicle'].has_key('body'):
                        print('违法转换.....')
                        #####交通违法
                        #debug_writeFile(event_data_dict,'weifa.txt')
                        # 车道类型
                        event_data_dict['CDLX'] = event_data_dict['lane'].pop('code')
                        #event_data_dict['CDLX'] = convert_code(event_data_dict['CDLX'], wenan_code_huawei_code_CDLX)
                        # # 二次识别-号牌类plate
                        # plate_keys = {'text': 'HPHM', 'type_code': 'HPZL'}
                        # replace_Dictkey(event_data_dict['vehicle']['plate'], plate_keys)
                        event_data_dict['HPHM'] = event_data_dict['vehicle']['plate'].pop('text')
                        if event_data_dict['HPHM'] == u'无牌':
                            event_data_dict['HPHM'] = '-'
                        event_data_dict['HPZL'] = event_data_dict['vehicle']['plate'].pop('type_code')

                        if event_data_dict['HPZL'] in ['31','32','00']:
                            event_data_dict['HPZL'] = convert_code(event_data_dict['HPZL'], wenan_code_huawei_code_HPZL)
                        # 车辆类型
                        event_data_dict['CLLX'] = event_data_dict['vehicle']['body']['type'].pop('code')
                        # #违法地行政区划分
                        # event_data_dict['XZQH'] = '430801'
                        # #违法地点
                        # event_data_dict['WFDD'] = event_data_dict['location'].pop('name')
                        # if event_data_dict['WFDD'] == "":
                        #     event_data_dict['WFDD'] ='null adress'
                        event_data_keys = {'start_dt':'WFKSSJ','end_dt':'WFJSSJ'}
                        replace_Dictkey(event_data_dict, event_data_keys)
                        #违法代码
                        event_data_dict['WFDM'] = event_data_dict['illegal'].pop('code')
                        if event_data_dict['WFDM'] in [wenan_code[0] for wenan_code in wenan_code_huawei_code_WFDM]:
                            event_data_dict['WFDM'] = convert_code(event_data_dict['WFDM'], wenan_code_huawei_code_WFDM)
                        #区间距离--无
                        event_data_dict['QJJL'] = '0001'
                        #区间用时
                        event_data_dict['QJYS'] = "1"
                        # #设备经度
                        # event_data_dict['SBJD'] ='114.041816'
                        # #设备纬度
                        # event_data_dict['SBWD'] = '27.634384'
                        #图片
                        names = locals()
                        print '照片数量:%d' %(event_data_dict['ZPSL'])
                        for i in range(event_data_dict['ZPSL']):
                            names['ZP%d' % (i + 1)] = i
                            # print('pic***',event_data_dict['pics'][i].pop('pic_base64'))
                            event_data_dict.setdefault('ZP%d' %(i + 1),event_data_dict['pics'][i].pop('pic_base64'))
                        illegal_delkey = ["FXLX","KKBH","ZPFX","GCSJ"]
                        del_dictKey(event_data_dict,illegal_delkey)


        del_dictKey(event_data_dict, vehicle_event_oldKey)
        #debug_writeFile(event_data_dict, 'Vehicle_makeDatajson.txt')
        return True
    #except Exception as e:
        print e,'---***except***--- in vehicle'
        return False



#交通流量数据重构
Tflow_dictNewKey = {'queue_length':'PJPDCD',
                    'distance':'PJCSJJ',
                    'velocity':'PJSD',
                    'sample_num':'TXCLS',
                    'sample_dura':'TJZQS',
                    'road':'CDH',
                    'space_occupy':'CDKJZYL',
                    'road_type':'CDLX',
                    'dist_time':'PJCTSJ'
                    #'direction':'FXLX'
                    }

def make_tflow(event_data_dict):
    try:
        # global event_data
        # event_data = {}
        #
        # print type(event_data_dict)
        # print type(event_data)
        # 方向类型
        event_data_dict['FXLX'] = event_data_dict['direction'].pop('code')
        del event_data_dict['direction']
        #只需要机动车流量
        if event_data_dict.has_key('big_vehicle'):
            #处理机动车--大车数据
            for bigV_data in event_data_dict['big_vehicle']:
                #print data,'data----------',data['queue_length'],type(data)
                #替换字典的key值
                #data['PJPDCD'] = data.pop('queue_length')
                replace_Dictkey(bigV_data,Tflow_dictNewKey)
                del bigV_data['velocity_unit']
                del bigV_data['occupy']
                # if 'space_occupy' in bigV_data:
                #     del bigV_data['space_occupy']
        #处理机动车--小车数据
        if event_data_dict.has_key('small_vehicle'):
            for smallV_data in event_data_dict['small_vehicle']:
                replace_Dictkey(smallV_data, Tflow_dictNewKey)
                del smallV_data['velocity_unit']
                del smallV_data['occupy']
                # if 'space_occupy' in smallV_data:
                #     del smallV_data['space_occupy']

        #del event_data_dict['big_vehicle']

        if event_data_dict.has_key('xcycle'):
            del event_data_dict['event_data_dict']['xcycle']
        if event_data_dict.has_key('pedestrian'):
            del event_data_dict['event_data_dict']['pedestrian']
        if event_data_dict.has_key('bus'):
            del event_data_dict['bus']

        del event_data_dict['device']

        if event_data_dict.has_key('location'):
            del event_data_dict['location']

        event_data_dict['TJSD'] = strtime_format_nomsec(event_data_dict['TJSD'])
        #event_data_dict['CDH'] = int(event_data_dict['CDH'])
        #debug_writeFile(event_data_dict, 'Tflow_makeData_event.txt')
        ##流量数据按车道号发出去
        bigC = event_data_dict.pop('big_vehicle')
        smallC = event_data_dict.pop('small_vehicle')


        #global event_data
        tflow_data_list = []
        #合并大小车车流量
        for (b_data,s_data) in zip(bigC,smallC):
            #event_data_dict = dict(event_data_dict, **s_data)
            event_data_dict['TXCLS'] = s_data.pop('TXCLS') + b_data.pop('TXCLS')
            #######这的问题导致数据结构不对------
            new_event_data = dict(event_data_dict, **s_data)
            tflow_data_list.append(new_event_data)
        # del event_data_dict['big_vehicle']
        # del event_data_dict['small_vehicle']
        # debug_writeFile(event_data_dict,'Tflow_makeData.txt')
        #debug_writeFile(tflow_data_list, 'Tflow_makeData_list.txt')
        return [True,tflow_data_list]
    except Exception as e:
        print e ,'---***except***---except***--- in Tflow'
        return [False,[]]


def make_pflow(event_data_dict):
    try:
        if event_data_dict.has_key('aux_dev_info'):
            del event_data_dict['aux_dev_info']
        event_data_dict  = event_data_dict['event_data_dict']
        if event_data_dict.has_key('xcycle'):
            xcycle = event_data_dict['xcycle']
            for region in xcycle:
                if region.has_key('regionid'):
                    del region['regionid']
                if region.has_key('regionname'):
                    del region['regionname']
                if region.has_key('sample_num_in'):
                    region['in_num'] = region['sample_num_in']
                    del region['sample_num_in']
                if region.has_key('sample_num_out'):
                    region['out_num'] = region['sample_num_out']
                    del region['sample_num_out']
        if event_data_dict.has_key('pedestrian'):
            pedestrian = event_data_dict['pedestrian']
            for region in pedestrian:
                if region.has_key('regionid'):
                    del region['regionid']
                if region.has_key('regionname'):
                    del region['regionname']
                if region.has_key('sample_num_in'):
                    region['in_num'] = region['sample_num_in']
                    del region['sample_num_in']
                if region.has_key('sample_num_out'):
                    region['out_num'] = region['sample_num_out']
                    del region['sample_num_out']
        # with open('PflowData.txt', 'w') as fp:
        #     #print 'pFlow......'
        #     fp.write(json.dumps(event_data_dict, indent=4))
        return True
    except Exception as e:
        print e,'---***except***---in Pflow'
        return False

event_data_oldKey = ["lane","pics","device","TJSD","plate_text","laneNO","direction","direction_name","locate_id","laneType","device_id","device_name","locate_name"]
def make_event_data(event_data_dict):
    try:
        # 设备编号
        #debug_writeFile(event_data_dict,'make_event_data.txt')

        #event_data_dict['SBBH'] = event_data_dict['device'].pop('code')
        if len(event_data_dict['SBBH']) < 18:
            print('SBBH','lenth less 18')
            event_data_dict['SBBH'] = "123456789987654321"
        # 方向类型
        #event_data_dict['FXLX'] = event_data_dict['location']['direction'].pop('code')
        # 车道号
        # event_data_dict['CDH'] = event_data_dict['lane'].pop('number')
        # if event_data_dict['CDH'] == 0:
        #     event_data_dict['CDH'] = 1
        # 车道类型
        event_data_dict['CDLX'] = event_data_dict['lane'].pop('code')

        # # 发生地点
        # event_data_dict['FSDD'] = event_data_dict['location'].pop('name')
        # if event_data_dict['FSDD'] == "":
        #     print('FSDD', 'no adress....')
        #     event_data_dict['FSDD'] = 'no adress'
        # 开始结束时间
        event_data_keys = {'start_dt': 'FSSJ', 'end_dt': 'JSSJ'}
        replace_Dictkey(event_data_dict, event_data_keys)

        event_data_dict['FSSJ'] = strtime_format_nomsec(event_data_dict['FSSJ'])
        event_data_dict['JSSJ'] = strtime_format_nomsec(event_data_dict['JSSJ'])
        # event_data['FSSJ'] = strtime_format_nomsec(event_data['FSSJ'])
        # event_data['JSSJ'] = strtime_format_nomsec(event_data['JSSJ'])
        # # 事件类型
        # event_data_dict['SJLX'] = event_data_dict['illegal'].pop('code')
        # 图片
        event_data_dict['ZP1'] = event_data_dict['pics'][0].pop('pic_base64')
        #视频字段
        del_dictKey(event_data_dict, event_data_oldKey)
        #debug_writeFile(event_data_dict, 'make_event_data_end.txt')
        #time.sleep(10)
        return True
    except Exception as e:
        print e, '---***except***---in event_data'
        return False


ped_xcy_event_oldKey = ['lane','illegal','pedestrian','driver','device','location','pics']
#行人事件数据重构
def make_pedestrian(event_data_dict):
    try:
        make_event_data(event_data_dict)
        del_dictKey(event_data_dict,ped_xcy_event_oldKey)
        # names = locals()
        # for i in range(1, event_data_dict['ZPSL'] + 1):
        #     # 先定义,后引用避免错误
        #     names['ZP%d' % i] = ''
        #     event_data_dict[names['ZP%d' % i]] = event_data_dict['pics'][i - 1].pop('pic_base64')

        #debug_writeFile(event_data_dict, 'Ped_makeDatajson.txt')
        print('***---行人事件-Ped---***')
        return True
    except Exception as e:
        print e,'---***except***---in Pedestrian'
        return False


#周界事件数据重构
def make_behavior(event_data_dict):
    try:
        event_type = event_data_dict['event_type']
        new_event_type = event_type
        if '26' == event_type:
            new_event_type = '1'
        if '25' == event_type:
            new_event_type = '2'
        if '23' == event_type or '24' == event_type:
            new_event_type = '3'
        event_data_dict['event_type'] = new_event_type
        return True
    except Exception as e:
        print e,'---***except***--- in Behavior'
        return False

#非机动车-骑自行车
def make_xcycle(event_data_dict):
    try:
        make_event_data(event_data_dict)
        del_dictKey(event_data_dict, ped_xcy_event_oldKey)

        #debug_writeFile(event_data_dict, 'Xcy_makeDatajson.txt')
        print('***---非机动车事件-Xcy---***')
        return True
    except Exception as e:
        print e,'---***except***--- in Xcycle'
        return False


bigDist_delkeyList = ['aux_dev_info',"dev_unid",'event_refid','source_type',"subtask_id",
                    "task_id","task_type","vchan_refid","vdev_unid","thread_id","event_cate",'event_type'] #"event_cate"

app = flask.Flask(__name__)


# 接收数据
@app.route('/api/v1/recv/events', methods=["POST"])
def recv_data():
    status_code = 200
    message = '{"Code":200, "Detail":"ok"}'
    headers = {'Content-Type':'application/json'}
    finally_send_data_list = []
    #global event_data
    #global tflow_data_list
    data = request.get_data()
    json_2_dict = json.loads(data)

    if json_2_dict.has_key('video'):
        if json_2_dict['video'] != None:
            if json_2_dict['video'][0].has_key('unid'):
                if (json_2_dict['video'][0]['unid']) > 0:
                    WFSPID = json_2_dict['video'][0].pop('unid')
                    WFSPID = WFSPID[:-3] + '-' + WFSPID[-3:]
                    json_2_dict['WFSPWJM'] = WFSPID + '.' + json_2_dict['video'][0].pop('format')
    if json_2_dict['event_cate'] == 'behavior':
        print('behavior-----')
        #event_code = 24
        SJLX = json_2_dict.pop('event_type')
        json_2_dict['SJLX'] = SJLX

        # if json_2_dict['event_type'] == event_code:
        #     print(event_code)

    # elif json_2_dict['event_type'] == 'pedestrian':
    #     is_send = make_pedestrian(json_2_dict)
    #     debug_writeFile(json_2_dict, 'Ped_send_Datajson.txt')
    # elif json_2_dict['event_type'] == 'xcycle':
    #     is_send = make_xcycle(json_2_dict)
    # debug_writeFile(json_2_dict,'Xcy_send_Datajson.txt')
    # elif json_2_dict['event_cate'] == 'behavior':
    # elif json_2_dict.get('event_cate','default--') == 'behavior':###避免无值出现的异常
    #     is_send = make_behavior(event_data)



    #debug_writeFile(json_2_dict, 'recv_sourceData_to_json.txt')
    if json_2_dict.has_key('event_data'):
        #event_data = json_2_dict['event_data']
        event_data = json_2_dict.pop('event_data')
        #debug_writeFile(event_data,'event_data_source.txt')
        if json_2_dict.has_key('WFSPWJM'):
            event_data['WFSPWJM'] = json_2_dict['WFSPWJM']



    # 统计起始时间
    if json_2_dict.has_key('event_dt'):
        event_data['TJSD'] = json_2_dict.pop('event_dt')
    # 设备编号
    if event_data.has_key('device'):
        event_data['SBBH'] = event_data['device'].pop('code')
        if len(event_data['SBBH']) < 18:
            print('SBBH', 'lenth less 18')
            event_data['SBBH'] = "123456789987654321"

    # 车道号
    if event_data.has_key('lane'):
        if event_data['lane'].has_key('number'):
            event_data['CDH'] = event_data['lane'].pop('number')
        if event_data['CDH'] == 0:
            event_data['CDH'] = 1
        #event_data['CDH'] = event_data['lane'].pop('number')
    # 方向类型---交通流量方向类型封装的不一样,特别处理
    if event_data.has_key('location'):
        # event_data_dict['FXLX'] = event_data_dict.pop('direction')
        if event_data['location'].has_key('direction'):
            event_data['FXLX'] = event_data['location']['direction'].pop('code')
        del event_data['location']
        # else:
        #     event_data['FXLX'] = event_data['direction'].pop('code')
        #     del event_data['direction']
        #     #del event_data['location']

    is_send = False
    #print '************', '数据类型:',json_2_dict['event_type'], '***********'

    #交通过车-违法-事件
    if json_2_dict.has_key('event_type'):
        if json_2_dict['event_type'] == 'vehicle':
            event_data['ZPSL'] = len(json_2_dict['pics'])
            event_data['pics'] = json_2_dict.pop('pics')

            # 卡口编号
            #event_data['KKBH'] = '1234567890'

            is_send = make_vehicle(event_data)
            #将event_data字段合并到上一级
            #json_2_dict = dict(json_2_dict,**json_2_dict.pop(event_data))

            # 转换车辆品牌的code
            if event_data.has_key('CLPP'):
                print('CLPP-----1',event_data['CLPP'])
                event_data['CLPP'] = convert_code(event_data['CLPP'], wenan_code_huawei_code_CLPP)
                print('CLPP------2',event_data['CLPP'])
            # 转换车身颜色-----已转换国标,不需要转换
            # if event_data.has_key('CSYS1'):
            #     event_data['CSYS1'] = convert_code(event_data['CSYS1'], wenan_code_huawei_code_CSYS)

            #车辆类型转换code----国标
            if event_data.has_key('CLLX'):
                print 'CLLX-----1',event_data['CLLX']
                event_data['CLLX'] = convert_code(event_data['CLLX'], wenan_code_huawei_code_CLLX)
                print 'CLLX-----2',event_data['CLLX']

            if event_data.has_key('CDLX'):
                if isinstance(event_data['CDLX'],basestring):
                    event_data['CDLX'] = convert_code(event_data['CDLX'], wenan_code_huawei_code_CDLX)
                else:
                    print("event_data['CDLX'] is not string type")
                    event_data['CDLX'] = convert_code(str(event_data['CDLX']), wenan_code_huawei_code_CDLX)


            if is_send:
                if event_data.has_key('GCSJ'):
                    event_data['GCSJ'] = strtime_format_msec(event_data['GCSJ'])

                    # uuid
                    event_data['GCXH'] = tid_end_dt(event_data['GCSJ'])
                    # 采集方式---无
                    event_data['CJFX'] = '11'
                    event_data = [event_data]
                    print '交通过车数据........'
                    #print(json.dumps(event_data, ensure_ascii=False))
                    debug_writeFile(event_data,'guoche.log')
                    #交通过车
                    if vehicle_url != "":
                        try:
                            r = requests.post(url=vehicle_url,headers=headers,data=json.dumps(event_data))
                            print "send trans restult---------:", r.status_code,r.content
                            status_code = r.status_code
                            message = r.text
                        except Exception as e:
                            status_code = 'unreachable'
                            print "send trans restult---------:unreachable"
                        log_vehicle.debug("send trans restult---------:%s" %status_code)
                elif event_data.has_key('WFDM'):
                    # 采集方式---无
                    event_data['CJFX'] = '8'
                    #交通违法
                    print '交通违法数据......'
                    # 若是危险品车辆的话CLLX码转成114
                    if event_data['WFDM'] == '13440':
                        event_data['CLLX'] = '114'
                    WFKSSJ = event_data['WFKSSJ']
                    WFJSSJ = event_data['WFJSSJ']
                    CDH = event_data['CDH']
                    HPHM = event_data['HPHM']
                    SBBH = event_data['SBBH']
                    WFDM = event_data['WFDM']

                    event_data['WFKSSJ'] = strtime_format_nomsec(event_data['WFKSSJ'])
                    event_data['WFJSSJ'] = strtime_format_nomsec(event_data['WFJSSJ'])
                    event_data = [event_data]
                    #finally_send_data_list.append(event_data)
                    #print(json.dumps(event_data,ensure_ascii=False))
                    #debug_writeFile(event_data,'weifa.log')

                    if illegal_url != "":
                        try:
                            r = requests.post(url=illegal_url,headers=headers,data=json.dumps(event_data))
                            print "send illegal restult---------:", r.status_code
                            status_code = r.status_code
                            message = r.text
                        except Exception as e:
                            status_code = 'unreachable'
                            print "send illegal restult---------:unreachable"
                        log_illegal.debug("WFKSSJ:%s------HPHM:%s******SBBH:%s------WFDM:%s------CDH:%d------status_code%r" %(WFKSSJ,HPHM,SBBH,WFDM,CDH,status_code))
                # elif event_data.has_key('SJLX'):
                #     #交通事件
                #     event_data['FSSJ'] = strtime_format_nomsec(event_data['FSSJ'])
                #     event_data['JSSJ'] = strtime_format_nomsec(event_data['JSSJ'])
                #     #debug_writeFile(event_data,'t_event.log')
                #     #finally_send_data_list.append(event_data)
                #     if event_url != "":
                #         try:
                #             r = requests.post(url=event_url,headers=headers,data=json.dumps(event_data))
                #             print "send event restult---------:", r.status_code
                #             status_code = r.status_code
                #         except Exception as e:
                #             status_code = 'unreachable'
                #             print "send event restult---------:unreachable"
        # elif json_2_dict['event_type'] == 'pflow':
        #     is_send = make_pflow(event_data)
        elif json_2_dict['event_type'] == 'tflow':
            print('交通流量.....')
            # 将流量数据按车道复制多份
            #debug_writeFile(event_data,'make_befor.txt')
            t_flow_return = make_tflow(event_data)
            is_send = t_flow_return[0]
            tflow_data_list = t_flow_return[1]



            ##接口发送tflow数据
            if is_send:
                #debug_writeFile(tflow_data_list, 'make_end.txt')
                # event_data['TJSD'] = strtime_format_nomsec(event_data['TJSD'])
                # debug_writeFile(tflow_data_list,'aaaabbb.txt')
                for tflow in tflow_data_list:
                    if isinstance(tflow['CDLX'], basestring):
                        tflow['CDLX'] = convert_code(tflow['CDLX'], wenan_code_huawei_code_CDLX)
                    else:
                        print('road is not stringtype...')
                        tflow['CDLX'] = convert_code(str(tflow['CDLX']), wenan_code_huawei_code_CDLX)
                    tflow['CDH'] = int(tflow['CDH'])
                    tflow['PJPDCD'] = int(tflow['PJPDCD'])
                    tflow['PJSD'] = round(tflow['PJSD'], 1)
                    tflow['CDKJZYL'] = round(tflow['CDKJZYL'], 1)
                    tflow['PJCSJJ'] = round(tflow['PJCSJJ'], 1)
                debug_writeFile(tflow_data_list, 'tflow_data_list.txt')
                if t_flow_url != "":
                    try:
                        r = requests.post(url=t_flow_url,headers=headers,data=json.dumps(tflow_data_list,ensure_ascii=False))
                        print "send tflow restult---------:",r.status_code
                        status_code = r.status_code
                        message = r.text
                    except Exception as e:
                        status_code = 'unreachable'
                        print "send t_flow restult---------:unreachable"
                    log_flow.debug("send tflow restult---------:%s" %status_code)
            #重置空流量列表
            #tflow_data_list[:] = []
            #将流量数据按车道复制多份
            # i = 1
            # for tflow_data in tflow_data_list:
            #     if tflow_data.has_key('location'):
            #         del tflow_data['location']
            #     event_data = tflow_data
            #     #转换成列表,一次发送多条
            #     #finally_send_data_list.append(tflow_data)
            #     # debug_writeFile(event_data,'tflow-event_data%d.txt' %i)
            #
            #
            #     ##接口发送tflow数据
            #     if is_send:
            #         debug_writeFile(event_data,'tflow_%d.log' %i)
            #         if t_flow_url != "":
            #             try:
            #                 r = requests.post(url=t_flow_url,headers=headers,data=json.dumps(event_data,ensure_ascii=False))
            #                 print "send tflow restult---------:",r.status_code
            #                 status_code = r.status_code
            #             except Exception as e:
            #                 status_code = 'unreachable'
            #                 print "send t_flow restult---------:unreachable"
            #     i = i + 1
        #处理事件
    if json_2_dict.has_key('SJLX'):
        print('交通事件........')
        event_data['pics'] = json_2_dict.pop('pics')
        event_data['SJLX'] = json_2_dict['SJLX']
        #print('SJLX ---IS ------%s' %(event_data['SJLX']))
        #转化事件类型为第三方code
        event_data['SJLX'] = convert_code(event_data['SJLX'],wenan_code_huawei_code_SJLX)
        #debug_writeFile(event_data, 'event-data2.txt')
        if event_data.has_key('WFSPWJM'):
            event_data['KSSPWJM'] = event_data.pop('WFSPWJM')
        else:
            print('No video! Plesse turn on shot video.')
        if event_data.has_key("direction_id"):
            del event_data["direction_id"]
        if event_data.has_key("direction"):
            #event_data['FXLX'] = event_data.pop("direction_id")
            event_data['FXLX'] = event_data['direction'].pop('code')
            del event_data['direction']
        is_send = make_event_data(event_data)
        #print('SJLX ---IS ----2--%s' %(event_data['SJLX']))
        event_data = [event_data]
        debug_writeFile(event_data, 'event-data.txt')

        if is_send:
            if event_url != "":
                try:
                    r = requests.post(url=event_url, headers=headers, data=json.dumps(event_data))
                    print "send event restult---------:", r.status_code
                    status_code = r.status_code
                    message = r.text
                except Exception as e:
                    status_code = 'unreachable'
                    print "send event restult---------:unreachable"
                log_event.debug("send event restult---------:%s" %status_code)


    # elif json_2_dict['event_type'] == 'pedestrian':
    #     is_send = make_pedestrian(json_2_dict)
    #     debug_writeFile(json_2_dict, 'Ped_send_Datajson.txt')
    # elif json_2_dict['event_type'] == 'xcycle':
    #     is_send = make_xcycle(json_2_dict)
        #debug_writeFile(json_2_dict,'Xcy_send_Datajson.txt')
    #elif json_2_dict['event_cate'] == 'behavior':
    # elif json_2_dict.get('event_cate','default--') == 'behavior':###避免无值出现的异常
    #     is_send = make_behavior(event_data)

    # 删除不需要的字段
    #del_dictKey(json_2_dict, bigDist_delkeyList)

    #将event_data字段的内容放到上级
    #event_data = dict(event_data, **json_2_dict)
    # try:
    #     json_2_dict = dict(json_2_dict,**json_2_dict.pop('event_data'))
    # except Exception as e:
    #     print e,'dict----json_2_dict------***'
    #     json_2_dict = dict({}, **event_data)

    #存储相应数据,供调试使用
    #debug_writeFile(event_data, 'Finally_send_Datajson.txt')
    print('is_send is---- :%s' % is_send)
    # if is_send == True:
    #     print('is_send -------------------Ture')
    #     requests.post('http://192.168.88.189:20090/api/v1/tx1_box/test', json.dumps(json_2_dict))
    # print json_2_dict['event_data_dict']['driver']['face'][0]['state']
    print type(event_data),'finally data type.......'
    result = {}

    if status_code == 'unreachable':
        thrid_code = 404
        detail = 'unreachable'
    else:
        thrid_code = status_code
        detail = json.loads(message)['Detail']

    result.setdefault('code', thrid_code)
    result.setdefault('message', detail)
    return json.dumps(result)

# @app.route('/api/v1/recv/mac', methods=["GET"])
# def get_mac():
#     mac = request.remote_addr


##接收短时录像并上传
@app.route('/api/v1/web/videos/upload', methods=["POST"])
def uploadVideosByRefid():
    video = request.files['file']
    refid =request.form.get('refid')
    format = request.form.get('format')
    SBBH = request.form.get('location_id')
    # print ('refid=%s' % refid)
    # print ('format=%s' % format)
    # print ('video.name=%s' % video.filename)

    #filepath = os.path.join ( "static", "video", video.filename )  # 视频的路径:SBBH/FXLX/年月日
    video_path = video_dir + '/' + video.filename
    try:
        video.save(video_path)
        #print('save video path : %s' %video_path)
        #outVideosPathList = videoU.sav_videobyfile (video)
        # outVideosPath = outVideosPathList[0]
        # md5_code = outVideosPathList[1]
        # ftp上传
        # ftp = ftpconnect('192.168.9.62','ftpuser','yl')
        # ftp = ftpconnect('43.123.24.151', 'hw', '123456')
        # ftp = ftpconnect('43.123.24.151', 'sf', 'sf123456')
        ftp = ftpconnect(ftpServerIp, ftpUserName, ftpPassWord)
        if ftp:
            # ftp.mkd('sbbh')
            # ftp.cwd('sbbh')
            # ftp.mkd('ymd')
            # ftp.cwd('ymd')
            # print(ftp.pwd(),'0000000')
            # remote_storege_path = ftp.pwd() + '/' + 'test/test/testdir' ##ftp只能一次创建一个层目录
            ##SBBH/日期
            if len(SBBH) <= 0:
                SBBH = 'SBBH--weipeizhi'
            dt = datetime.datetime.now().strftime('%Y%m%d')

            # ftp.cwd('/home/uftp/testdir')

            # file_handle = open('testVideo.file',"rb")
            # ftp.storbinary("STOR %s" %(refid + format),file_handle,1024)
            # ftp.storlines('STOR %s' %video.filename,video)###文本文件方式视频文件ftp传输后播放有问题
            # ftp.close()
            refid = refid[:-3] + '-' + refid[-3:]

            upload_flag = uploadfile(ftp, video_path, SBBH, dt, refid, format)
            if upload_flag:
                result = {}
                #result.setdefault('refid', refid)
                result.setdefault('code', 200)
                result.setdefault('unid', video_path)
                result.setdefault('message','Upload Succeeded!')
                return json.dumps(result)
            else:
                result = {}
                # result.setdefault('refid', refid)
                result.setdefault('code', 500)
                result.setdefault('unid', video_path)
                result.setdefault('message', 'Upload Failed!')
                return json.dumps(result)
        else:
            os.remove(video_path)
            result = {}
            #result.setdefault('refid', refid)
            result.setdefault('code', 530)
            result.setdefault('unid', video_path)
            result.setdefault('message', 'Login Failed!')
            return json.dumps(result)
    except Exception as e:
        log_ftp.debug('upload videofile failed ------%s--%s--error:%s' %(refid,video.filename,e))
        result = {}
        #result.setdefault('refid', refid)
        result.setdefault('code', 500)
        result.setdefault('unid', video_path)
        result.setdefault('message', 'Receive Failed!')
        return json.dumps(result)
    

if __name__ == '__main__':
    #event_data = {}
    #tflow_data_list = []
    log_vehicle = logger('data_info_vehicle.log')
    log_illegal = logger('data_info_illegal.log')
    log_event = logger('data_info_event.log')
    log_flow = logger('data_info_flow.log')
    log_ftp = logger('data_info_ftp.log')
    #videoU = videoutil.VideoUtil(r'D:\ftp_video_test')
    video_dir = r'/data/www/uploadvideo'
    #print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1514784806820))
    print time.strftime ( '%Y-%m-%d %H:%M:%S', time.localtime ( time.time () ) )
    timeStamp = 1514784806820
    #timeStamp = 1544498480153
    print (int ( round ( time.time() * 1000 ) ))  # 毫秒级时间戳
    #dateArray = datetime.datetime.utcfromtimestamp ( timeStamp )
    #print dateArray
    otherStyleTime = time.strftime ( "%Y-%m-%d %H:%M:%S", time.localtime(timeStamp/1000))
    print otherStyleTime
    app.run (
        #host='172.18.37.108',
        host = hostIP,
        port = service_port,
        debug=False,threaded=True)