class MyHttpHandler(BaseHTTPRequestHandler): def __init__(self): # 连接数据库 self.conn = ConnectMysql() # 如果表不存在则创建表 self.conn.create_table() def do_POST(self): length = int(self.headers['Content-Length']) readdata = self.rfile.read(length).decode('utf-8') post_data = json.loads(readdata) cur_thread = threading.current_thread() # 解析post信息 # print(cur_thread.name) # print(post_data) # post_num == # # # if post_num == 0: # 原始数据输入 #单行指纹输入,输入信息都应来自于post数据 flag = self.conn.insert_data(model, addr, phoneIP, 1, strx, stry, direction, line_time, mac, ap) if flag == -1: data = json.dumps({'result': 'false'}) else: data = json.dumps({'result': 'true'}) elif post_num == 1: # 请求指纹更新结果 g = GuiContent() # 数据处理 # 此部分可以预先进行,只要在所有原始数据都保存完成后, # 放在此处的话每次调用都是产生的重复数据 # 可能需要传入地址信息 g.data_process(ap_mac, x, y) # 指纹更新 # 第二个参数为增量训练的模型位置 ap_m = g.fingerprint_update(ap_mac, 10, x, y) data = json.dumps({'result': ap_m}) else: data = json.dumps({'result': {'###'}}) # # # enc = "UTF-8" encoded = ''.join(data).encode(enc) f = io.BytesIO() f.write(encoded) f.seek(0) self.send_response(200) self.send_header("Content-type", "text/html; charset=%s" % enc) self.send_header("Content-Length", str(len(encoded))) self.end_headers() shutil.copyfileobj(f, self.wfile)
class SaveData: def __init__(self): # 连接数据库 self.conn = ConnectMysql() # 如果表不存在则创建表 self.conn.create_table() # 完整数据保存 def data_save(self, path, file, phone_model): with open(path + file, 'r') as file_read: file_a = file.split('.') file_b = file_a[0].split('-') addr = "C204" phoneIP = phone_model model = file_b[0] coordinate = file_b[1] # 注意x,y的先后 strx = coordinate[len(coordinate) - 1] stry = coordinate[0:(len(coordinate) - 1)] direction = file_b[2] line_datas = [] while True: lines = file_read.readline() # 读取整行数据 if not lines: break line_datas.append(lines.split()) if int(model) == 0: # 模型0为底图采集,只保存中间三分之一数据 one = int(len(line_datas) / 3) line_datas = line_datas[one:one * 2] for line_data in line_datas: line_time = line_data.pop(0) line_time = line_time + " " + line_data.pop(0) mac = [] ap = [] if line_data: for x in range(0, len(line_data), 2): mac.append(line_data[x]) ap.append(line_data[x + 1]) flag = self.conn.insert_data(model, addr, phoneIP, 1, strx, stry, direction, line_time, mac, ap) if flag == -1: return line_time return 1 # 读取数据 def data_read(self): data = self.conn.select_data() return data #关闭连接 def close_connect(self): self.conn.close_conn()
class MyHttpHandler(BaseHTTPRequestHandler): def __init__(self): # 连接数据库 self.conn = ConnectMysql() # 如果表不存在则创建表 self.conn.create_table() def do_POST(self): length = int(self.headers['Content-Length']) readdata = self.rfile.read(length).decode('utf-8') post_data = json.loads(readdata) cur_thread = threading.current_thread() # 解析post信息 # print(cur_thread.name) # print(post_data) # post_num == # # # if post_num == 0: # 原始数据输入 #单行指纹输入,输入信息都应来自于post数据 flag = self.conn.insert_data(addr, signal_type, strx, stry, line_time, uploading_device, ap_mac, ap_name, ap_value) if flag == -1: data = json.dumps({'result': 'false'}) else: data = json.dumps({'result': 'true'}) elif post_num == 1: # 请求指纹更新结果 # ap_m = 从指纹数据库获取最新指纹库并进行格式整理 data = json.dumps({'result': ap_m}) else: data = json.dumps({'result': {'错误请求'}}) # # # enc = "UTF-8" encoded = ''.join(data).encode(enc) f = io.BytesIO() f.write(encoded) f.seek(0) self.send_response(200) self.send_header("Content-type", "text/html; charset=%s" % enc) self.send_header("Content-Length", str(len(encoded))) self.end_headers() shutil.copyfileobj(f, self.wfile)
class SaveData: def __init__(self): # 连接数据库 self.conn = ConnectMysql() # 如果表不存在则创建表 self.conn.create_table() # 完整数据保存 def data_save(self, path, type_t, addr, coo_x, coo_y, room_device): if os.path.getsize(path): #判断文件是否为空 with open(path, 'r') as file_read: model = 0 flag = 1 line_datas = [] read = csv.reader(file_read) for i in read: line_datas.append(i) name = line_datas[0][1:] # print(name) mac = line_datas[1][1:] for x in range(2, len(line_datas)): line_data = line_datas[x] line_timet = line_data.pop(0) line_time = line_timet[:-3] line_time = time.strftime("%y-%m-%d %H:%M:%S", time.localtime(int(line_time))) ap = line_data flag_ap = -1 for a in ap: if int(a) != -200: # 当一行全部都是无效数据时数据库会报错,因此提前检验一次 flag_ap = 1 if flag_ap == 1: flag = self.conn.insert_data(model, addr, type_t, coo_x, coo_y, line_time, mac, name, ap, room_device) if flag == -1: return line_timet return 1 #关闭连接 def close_connect(self): self.conn.close_conn()
class SaveData: def __init__(self): # 连接数据库 self.conn = ConnectMysql() # 如果表不存在则创建表 self.conn.create_table() # 完整数据保存 def complete_data_save(self, path, file): with open(path + file, 'r') as file_read: file_a = file.split('.') file_b = file_a[0].split('-') addr = file_b[0] strx = file_b[1] # 注意x,y的先后 stry = file_b[2] phoneIP = strx + ', ' + stry # ##phoneIP为采集中缺失的参数,暂时用位置和代替 while True: lines = file_read.readline() # 读取整行数据 if not lines: break line_data = lines.split() line_time = line_data.pop(0) line_time = line_time + " " + line_data.pop(0) mac = [] ap = [] if line_data: for x in range(0, len(line_data), 2): mac.append(line_data[x]) ap.append(line_data[x + 1]) flag = self.conn.insert_data(addr, phoneIP, 1, strx, stry, line_time, mac, ap) if flag == -1: return line_time return 1 #底图数据,只保存中间三分之一 def initial_data_save(self, path, file): with open(path + file, 'r') as file_read: file_a = file.split('.') file_b = file_a[0].split('-') addr = file_b[0] strx = file_b[1] # 注意x,y的先后 stry = file_b[2] phoneIP = strx + ', ' + stry # ##phoneIP为采集中缺失的参数,暂时用位置和代替 line_datas = [] while True: lines = file_read.readline() # 读取整行数据 if not lines: break line_datas.append(lines.split()) one = int(len(line_datas) / 3) line_datas = line_datas[one:one * 2] for line_data in line_datas: line_time = line_data.pop(0) line_time = line_time + " " + line_data.pop(0) mac = [] ap = [] if line_data: for x in range(0, len(line_data), 2): mac.append(line_data[x]) ap.append(line_data[x + 1]) flag = self.conn.insert_data(addr, phoneIP, 1, strx, stry, line_time, mac, ap) if flag == -1: return line_time return 1 def close_connect(self): self.conn.close_conn()
class SaveData: def __init__(self): # 连接数据库 self.conn = ConnectMysql() # 如果表不存在则创建表 self.conn.create_table() # 完整数据保存 def data_save(self,path,file,phone_model,tt_time): print(path) time_period = tt_time; with open(path + file, 'r') as file_read: file_a = file.split('.') file_b = file_a[0].split('-') addr = "C204" phoneIP = phone_model coordinate = file_b[1] # 注意x,y的先后 strx = coordinate[len(coordinate) - 1] stry = coordinate[0:(len(coordinate) - 1)] direction = file_b[2] model = file_b[0] line_datas = [] while True: lines = file_read.readline() # 读取整行数据 if not lines: break line_datas.append(lines.split()) line_time_t = [] for line_data in line_datas: line_time_t.append(line_data[0] + " " + line_data[1]) for time_p in time_period: model = time_p[0] if time_p[1] in line_time_t and time_p[2] in line_time_t:#数据采集出了比较大的问题,只有部分数据完整,只保存完整的数据 print(model+" " +str(1)) line_datas_t = line_datas[line_time_t.index(time_p[1]):line_time_t.index(time_p[2])] if int(model) == 0: # 模型0为底图采集 for line_data in line_datas_t: line_time = line_data.pop(0) line_time = line_time + " " + line_data.pop(0) mac = [] ap = [] if line_data: for x in range(0,len(line_data),2): mac.append(line_data[x]) ap.append(line_data[x+1]) flag = self.conn.insert_data(model,addr,phoneIP,1,strx,stry,direction,line_time,mac,ap) if flag == -1: return line_time else: #其他数据分为四组 one = int(len(line_datas_t)/4) for x in range(1,5): o_line_datas = line_datas_t[one*(x-1):one*x] for line_data in o_line_datas: line_time = line_data.pop(0) line_time = line_time + " " + line_data.pop(0) mac = [] ap = [] if line_data: for y in range(0,len(line_data),2): mac.append(line_data[y]) ap.append(line_data[y+1]) flag = self.conn.insert_data(str(x)+""+str(model),addr,phoneIP,1,strx,stry,direction,line_time,mac,ap) if flag == -1: return line_time return 1 # 读取数据 def data_read(self): data = self.conn.read_data() return data #关闭连接 def close_connect(self): pass self.conn.close_conn()