def __init__(self, val): """ :param val: a tuple (1,2,3,4,5,6,7,8) :return: """ self.debug = True self.result = {} # The result of the SplitBase self.flag = False if not self.split_list: # if the subclass no override it, raise a valueError exception! raise ValueError("The split_list can't empty!") if val: if self.crc_check: self.message_head_content = val self.crc = val[-1] if is_complete(self.message_head_content, self.crc): self.build_dict(val) else: #ignore this request from terminal device self.debug = False error('No complete data from client!') else: self.build_dict(val) else: error('No validation input!') if self.debug == False and val[2] == 2: self.build_dict(val) self.debug = True self.flag = True
def __init__(self, request, conn, protocol=MSG_ID): global gb_file receiver = request.encode('hex') # 接收到的信息, 转码成hex的字符串, print 'rec_msg:' + receiver self.write(receiver + '\n') #judge data ture_len = len(receiver) data_len = 0 mydata = [] while ture_len != 0: # 解析可能出现的TCP自动组包, 把数据包变成一个数组 data_len = (int('0x' + receiver[44:46], 16) * 256 + int('0x' + receiver[46:48], 16)) * 2 + 50 mydata.append(str(receiver[:data_len]).decode('hex')) receiver = receiver[data_len:] ture_len = len(receiver) for i in range(len(mydata)): # 依次应答终端数据 self.protocol = protocol self.request = mydata[i] self.conn = conn self.request_data = None self.client_tuple_data = None self.hex_format_data = None self.split_instance = None self.msg_key = None self.menu_key = None self.request_dict = None self.PUB = True self.resolution() # 开始对单条数据解析 if self.PUB: # pub是接收到的数据是否合法的标志位 self.distribute() else: error("Can't publish your data!")
def __init__(self, request, conn, protocol=MSG_ID): self.protocol = protocol self.request = request self.conn = conn self.request_data = None self.client_tuple_data = None self.hex_format_data = None self.split_instance = None self.msg_key = None self.menu_key = None # Just a key of urlpatterns Dicts self.request_dict = None self.PUB = True self.resolution() if self.PUB: self.distribute() else: error("Can't publish your data!")
def __init__(self, val): self.convert_to_func_arg = { 'to_word': self.convert_to_WORD_code, 'to_dword': self.convert_to_DWORD_code, 'to_int': self.convert_to_INT_code, 'to_timestamp': self.convert_to_timestamp_code, 'to_hello': self.convert_to_hello_code } for func_key in self.convert_to_function: # Get the static function name convert_to_func = self.convert_to_function[func_key] # Get the static function instance convert_lists = self.convert_to_func_arg[func_key] # Get the target list for deal ! for each_field in convert_lists: # Get each item of the list if each_field in val: src_data = val[each_field] # Get the each value from the user input which key is equal each_field! val[each_field] = convert_to_func(src_data) # convert the result with new value! else: error("The Key Does't match any field of val!")
def __init__(self, val): self.convert_to_func_arg = { 'to_word': self.convert_to_WORD_code, 'to_dword': self.convert_to_DWORD_code, 'to_int': self.convert_to_INT_code, 'to_timestamp': self.convert_to_timestamp_code, 'to_hello': self.convert_to_hello_code } for func_key in self.convert_to_function: # Get the static function name convert_to_func = self.convert_to_function[ func_key] # Get the static function instance convert_lists = self.convert_to_func_arg[ func_key] # Get the target list for deal ! for each_field in convert_lists: # Get each item of the list if each_field in val: src_data = val[ each_field] # Get the each value from the user input which key is equal each_field! val[each_field] = convert_to_func( src_data) # convert the result with new value! else: error("The Key Does't match any field of val!")
def __init__(self, request, conn, protocol=MSG_ID): global f receiver = request.encode('hex') print 'rec_msg:' + receiver f.write(receiver + '\n') self.protocol = protocol self.request = request self.conn = conn self.request_data = None self.client_tuple_data = None self.hex_format_data = None self.split_instance = None self.msg_key = None self.menu_key = None # Just a key of urlpatterns Dicts self.request_dict = None self.PUB = True self.resolution() if self.PUB: self.distribute() else: error("Can't publish your data!")
def __init__(self, val): """ :param val: a tuple (1,2,3,4,5,6,7,8) :return: """ self.debug = True self.result = {} # The result of the SplitBase if not self.split_list: # if the subclass no override it, raise a valueError exception! raise ValueError("The split_list can't empty!") if val: if self.crc_check: self.message_head_content = val[1:-1] self.crc = val[-2] if is_complete(self.message_head_content, self.crc): self.build_dict(val) else: # ignore this request from terminal device self.debug = False error('No complete data from client!') else: self.build_dict(val) else: error('No validation input!')
from conf.settings import DB_TYPE, DB_HOST, DB_USER, DB_PASSWORD, DB_PORT, DB_NAME from visual.visual_decorator import error, info try: import MySQLdb DB_INFO = '%s://%s:%s@%s:%i/%s' % (DB_TYPE, DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME) except ImportError, msg: error(msg) info_msg = "Run 'sudo apt-get install mysql-python' to fixed this!" info(info_msg) DB_INFO = 'sqlite:///:memory:' try: from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Numeric, String, Integer, ForeignKey, Float from sqlalchemy.orm import relationships engine = create_engine(DB_INFO) Base = declarative_base() Base.metadata.bind = engine DBSession = sessionmaker(bind=engine) session = DBSession() # For view import ... except ImportError, msg: error(msg) info_msg = "Run 'pip install sqlalchemy' to fixed this!" info(info_msg)
""" import re import time from shortcuts.template import render from conf.protocols import MSG_ID # A Global Dicts from app.urls import urlpatterns from core.split import SplitBase from visual.visual_decorator import error, warning, info from conf.rest import SOCKET_DESCRIPTOR # A Global Dicts from configs.sys_config import SERVER_LOG_FILE_ZDMON try: import tongue except ImportError, msg: error(msg) info_msg = "Run 'pip install tongue' will fix this!" """ urlpatterns is a function Dicts,and it will automatic looking for the target function according your menu_key is ! """ ## f = open(SERVER_LOG_FILE_ZDMON, 'a') def reflect(flag, request): """ By checking the flag of urlpatterns Dicts,and then reflect will do something for next! :param flag:
def t_login(terminal_request): platrandom = (48, 51, 67, 51, 55, 66, 57, 50, 51, 55, 50, 68, 52, 48, 57, 52) mydatabase = MyDatabase.MyDatabase() select_sql = 'select * from sirun_register where 1 order by dispatch_creation_time desc limit 1' select_data = mydatabase.select_last_data(select_sql) #get data time_login_car = get_B11G_timestamp( terminal_request['client_dispatch_creation_time']) time_pi = (int(time.time()), ) bid = terminal_request['client_bid'] msg_flag = terminal_request['client_msg_flag'] event_creation_time = terminal_request['client_event_creation_time'] dispatch_creation_time = get_B11G_timestamp( terminal_request['client_dispatch_creation_time']) msg_counter = terminal_request['client_msg_conuter'] ser_data_len = terminal_request['client_ser_data_len'] sec_ver = terminal_request['client_sec_ver'] if terminal_request['client_mid'] == (1, ): keytype = terminal_request['client_ser_data'][0:1] tboxsn = terminal_request['client_ser_data'][1:30] vin = terminal_request['client_ser_data'][30:47] tboxrandom = terminal_request['client_ser_data'][47:] try: encryption_key = select_data['callback_number'] except: error('you should register terminal') encryption_key = change_tuple(encryption_key) val = tboxrandom + encryption_key + platrandom md5_data = to_md5_data(val) terminal_request['client_ser_data'] = md5_data + platrandom terminal_request['client_mid'] = (2, ) #save data sirun_car_login = { 'time_ter': time_login_car, 'time_pi': time_pi, 'bid': bid, 'message_flag': msg_flag, 'event_creation_time': event_creation_time, 'dispatch_creation_time': dispatch_creation_time, 'message_counter': msg_counter, 'service_data_length': ser_data_len, 'security_version': sec_ver, 'message_flag': msg_flag, 'key_type': keytype, 't_box_sn': tboxsn, 'vin': vin, 't_box_random': tboxrandom, } datasave(terminal_request, sirun_car_login, SIRUN_TABLE_DICT['login']) if terminal_request['client_mid'] == (3, ): serial_up = terminal_request['client_ser_data'][:16] acess_key = terminal_request['client_ser_data'][16:32] success_key = (0, 0, 0, 0, 0, 0, 0, 0) terminal_request['client_ser_data'] = platrandom + ( 0, ) + success_key + success_key + success_key terminal_request['client_mid'] = (5, ) #save data sirun_car_login = { 'time_ter': time_login_car, 'time_pi': time_pi, 'bid': bid, 'message_flag': msg_flag, 'event_creation_time': event_creation_time, 'dispatch_creation_time': dispatch_creation_time, 'message_counter': msg_counter, 'service_data_length': ser_data_len, 'security_version': sec_ver, 'message_flag': msg_flag, 'serial_up': serial_up, 'acess_key': acess_key, } datasave(terminal_request, sirun_car_login, SIRUN_TABLE_DICT['login']) #get sys time terminal_request['client_dispatch_creation_time'] = get_sys_time_list() #data check code terminal_request['client_ser_data_check'] = ( check(terminal_request['client_ser_data'] + (1, )), ) #data length len_data = len(terminal_request['client_ser_data']) terminal_request['client_ser_data_len'] = (len_data / 100, len_data % 100) #msg result terminal_request['client_result'] = (0, ) template = 'client_fix_header|client_ser_data_check|client_ser_ver|client_bid|client_msg_flag|client_event_creation_time|client_aid|client_mid|client_msg_conuter|client_ser_data_len|client_result|client_sec_ver|client_dispatch_creation_time|client_ser_data' ##### display_sys_time() display_stamp_to_date(time_login_car[0]) #return msg render(terminal_request, template)