#!python
#encoding:utf8
import socket,time
import threading
import socketserver
import os,time,binascii
from datetime import datetime
# ******************************************************************************************************************************
def zero_reset():
flag_reset = 0
while True:
hour = time.strftime('%H',time.localtime(time.time()))
if hour == '00' and flag_reset < 3:
msg = binascii.a2b_hex("33CCFF")
msg_send(client_addr,client_socket,msg)
flag_reset += 1
print(msg)
#*****************************
temp = time.strftime('%H:%M:%S',time.localtime(time.time()))
with open("C:\\Users\\Administrator\\Desktop\\database\\data\\2017-2-11\\test.txt","a+") as text:
text.write(str(temp) + " reset completed!")
#******************
elif hour == '01' and flag_reset != 0:
flag_reset = 0
else:
continue
# ******************************************************************************************************************************
client_addr = []
client_socket = []
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
allow_reuse_address = False#允许端口复用
def setup(self):
ip = self.client_address[0].strip() # 获取客户端的ip
port = self.client_address[1] # 获取客户端的port
print(ip+":"+str(port)+" is connect!")
client_addr.append(self.client_address) # 保存到队列中
client_socket.append(self.request) # 保存套接字socket
def handle(self):
while True: # while循环
# data = str(self.request.recv(1024), 'ascii')
#data = binascii.a2b_hex(self.request.recv(1024))
data = self.request.recv(8192)
if data: # 判断是否接收到数据
data = binascii.b2a_hex(data)
cur_thread = threading.current_thread()
# response = bytes("{}: {}".format(cur_thread.name, data), 'ascii')
response = bytes("{}: {}".format(cur_thread.name, data))
# self.request.sendall(response)
with open("C:\\Users\\Administrator\\Desktop\\database\\data\\2017-2-11\\receive01.txt","w") as txt_recv:
txt_recv.write(data)
# print(response)
print(data+"\n")
def finish(self):
print("client is disconnect!")
client_addr.remove(self.client_address)
client_socket.remove(self.request)
# ********************************************************************************************************************
#***********************************************
#send
def msg_send(client_addr,client_socket,msg):
print("**************************** %d DTU is connecting!******************************"%len(client_addr))
if client_addr:
for client in client_socket:
client.sendall(msg)
#txt read
def txt_read(filename):
temp = []
data = []
temp_path = 'C:\\Users\\Administrator\\Desktop\\database\\data\\2017-2-11'
file_path = temp_path + "\\" + filename + ".txt"
with open(file_path,'r') as file:
for line in file.readlines():
line = line.strip('\n')
temp.append(line)
data = temp
temp = []
return data
#empty txt file
def txt_empty(filename):
temp_path = 'C:\\Users\\Administrator\\Desktop\\database\\data\\2017-2-11'
file_path = temp_path + "\\" + filename + ".txt"
file = open(file_path,'w')
file.close()
def txt_write(content,filename):
temp_path = 'C:\\Users\\Administrator\\Desktop\\database\\data\\2017-2-11'
file_path = temp_path + "\\" + filename + ".txt"
with open(file_path,'w') as file:
# for row in content:
file.write(content)
#检查是否收到新的内容
def check_recei(filename):
temp = []
temp = txt_read(filename)
# txt_empty("receive01")
if len(temp) == 0:
return "no feedback"
else:
return "received"
#检查时间是否经过interval
def check_time(next_time):
now_time = time.time()*1000
if now_time >= next_time:
return True
else:
return False
#循环检查时间
def check_time_loop(next_time):
while True:
flag_time = check_time(next_time)
if flag_time:
flag_send += 1
break
else:
continue
#等待间隔时间
def wait_interval(interval,next_time,current_content):
flag_send = 0
while True:
flag_id = check_id(current_content)
flag_time = check_time(next_time)
if flag_time:
break
else:
if flag_id == "match" and flag_send == 0:
insert([],flag_id)
flag_send = 1 #已发送
else:
continue
continue
if flag_send == 1:
return "match"
else:
return flag_id
#等待interval 且 返回flag_id
def wait(interval,current_content):
temp_next = (time.time() + interval)*1000
flag_sent = 0 #未发送标签
while True:
flag_id = check_id(current_content)
flag_time = check_time(temp_next)
if flag_time:
break
else:
if flag_id == "match" and flag_sent == 0:
insert([],flag_id)
flag_sent = 1 #已发送
else:
continue
continue
if flag_sent:
return "match"
else:
return flag_id
#仅等待
def just_wait(next_time):
while True:
flag_time = check_time(next_time)
if flag_time:
break
else:
continue
#生成发送信息
def id_gener(node_id_num = 100,net_id_num = 5):
trans_info = []
net_id = [i+1 for i in range(net_id_num)]
node_id = [i+1 for i in range(node_id_num)]
for i,x in enumerate(net_id):#net_id
for j,y in enumerate(node_id):#node_id
x_hex = hex(x)#transfer into hex
y_hex = hex(y)
if y < 16:# format: xx xx xx
trans_info.append(("0" + y_hex.split("x")[1] + "0" + x_hex.split("x")[1] + "ff"))
else:
trans_info.append((y_hex.split("x")[1] + "0" + x_hex.split("x")[1] + "ff"))
return trans_info
# **************************************************************************************************
# 单一网络测试
def id_gener2(node_id_num = 100,net_id = 2):
trans_info = []
node_id = [i+1 for i in range(node_id_num)]
for j,y in enumerate(node_id):#node_id
x_hex = hex(net_id)
y_hex = hex(y)
if y < 16:# format: xx xx xx
trans_info.append(("0" + y_hex.split("x")[1] + "0" + x_hex.split("x")[1] + "ff"))
else:
trans_info.append((y_hex.split("x")[1] + "0" + x_hex.split("x")[1] + "ff"))
return trans_info
# **************************************************************************************************
#检查node、net_id
'''
request_recei:收到的终端request txt文件名
request_send:待发送 request txt文件名
recei_file:zigbee返回的 txt文件名
send_file:发送给zigbee txt文件名
'''
def check_id(current_content,recei_file = "receive01",request_recei = "request"):
flag_id = []
flag_recei = check_recei(recei_file)
flag_current = compare(current_content)
flag_request = check_request(request_recei)
if flag_request == "request":
flag_id = "request"
else:
if flag_recei == "received":
if flag_current == "match":