import datetime
import random
import random_time
import codecs

import seconds as seconds
f1 = codecs.open(r'./1.txt', 'w', encoding='utf-8')
# f1 = open('test.txt', 'w')
now_time = datetime.datetime.now()
now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")

for i in range(10000):
    # 建立连接模块

    #now_time = datetime.datetime.now()
    now_time = random_time.time_simulate(now_time)
    now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
    f1.write(str(now_time_formal) + "  ")
    f1.write("开始建立连接\n")

    # 模拟连接站点
    '''delta_second_number = random.randint(1, 10)
    delta_second = datetime.timedelta(seconds=delta_second_number)
    now_time = now_time + delta_second'''
    now_time = random_time.time_simulate(now_time)
    now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
    f1.write(str(now_time_formal) + "  ")
    site_list_number = random.randint(0, 3)
    site_list = ["b", "c", "d", "e"]
    site = site_list[site_list_number]
    f1.write("a与"+str(site)+"开始ale建链呼叫\n")
    now_time = random_time.time_simulate(now_time)
    now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
    f1.write(str(now_time_formal) + "  ")
    f1.write("a与"+str(site)+"建链成功\n")

    #模拟使用频率
    frequency_list_number_1 = random.randint(0, 1)
    frequency_list_number_2 = random.randint(0, 1)
    frequency_list_receive_list = range(2000, 30000, 5)
    frequency_list_deliver_list = range(2000, 30000, 5)
    # frequency_list_receive = ["11111", "22222"]
    # frequency_list_deliver = ["33333", "44444"]
    frequency_receive = random.choice(frequency_list_receive_list)
    frequency_deliver = random.choice(frequency_list_deliver_list)
    # frequency_receive = frequency_list_receive[frequency_list_number_1]
    # frequency_deliver = frequency_list_deliver[frequency_list_number_2]
    now_time = random_time.time_simulate(now_time)
    now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
    f1.write(str(now_time_formal) + "  ")
    # f1.write("a与"+str(site)+"通路的接收频率为"+str(frequency_receive) + "kHz\n")
    f1.write("a与" + str(site) + "通路的接收频率为" + str(frequency_receive) + "\n")
    now_time = random_time.time_simulate(now_time)
    now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
    f1.write(str(now_time_formal) + "  ")
    # f1.write("a与"+str(site)+"通路的发送频率为"+str(frequency_deliver) + "kHz\n")
    f1.write("a与" + str(site) + "通路的发送频率为" + str(frequency_deliver) + "\n")

    # 模拟信噪比
    now_time = random_time.time_simulate(now_time)
    now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
    f1.write(str(now_time_formal) + "  ")
    SNR = random.randint(-40, 40)
    f1.write("a与"+str(site)+"通路的信号噪声比为"+str(SNR) + "\n")

    #模拟发送
    action_number = random.randint(1, 3)
    for j in range(action_number):
        rate_action = random.randint(0, 100)
        rate_action2 = random.randint(0, 100)
        if rate_action > 40:
            now_time = random_time.time_simulate(now_time)
            now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
            f1.write(str(now_time_formal) + "  ")
            f1.write("a正在向"+str(site)+"发送文电\n")
            progress = random.randint(10, 100)
            now_time = random_time.time_simulate(now_time)
            now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
            f1.write(str(now_time_formal) + "  ")
            f1.write("a向"+str(site)+ "发送的进度为" + str(progress) + "%\n")
        else:
            now_time = random_time.time_simulate(now_time)
            now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
            f1.write(str(now_time_formal) + "  ")
            f1.write("a正在向" + str(site) + "发起会话\n")
            time = random.randint(2, 10)
            delta_second = datetime.timedelta(seconds=time)
            now_time = now_time + delta_second
            now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
            f1.write(str(now_time_formal) + "  ")
            f1.write("a与" + str(site) + "会话的已发生" + str(time) + "秒\n")

        now_time = random_time.time_simulate(now_time)
        now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
        f1.write(str(now_time_formal) + "  ")

        if rate_action2 > 20:
            f1.write("a与" + str(site) + "发生的此次操作成功\n")
        else:
            f1.write("a与" + str(site) + "发生的此次操作失败\n")


    # 模拟发送成功或者失败
    now_time = random_time.time_simulate(now_time)
    now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
    f1.write(str(now_time_formal) + "  ")
    rate = random.randint(0, 100)
    if rate > 30:
        f1.write("a与"+str(site)+"连接成功\n")
        rate_1 = random.randint(0, 100)
        now_time = random_time.time_simulate(now_time)
        now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
        f1.write(str(now_time_formal) + "  ")
        if rate_1 > 40:
            f1.write("a与"+str(site)+"的通路正在拆链\n")
        else:
            f1.write("a与"+str(site)+"的通路链路质量差触发自动拆链\n")
    else:
        f1.write("a与"+str(site)+"的通路连接失败\n")
        now_time = random_time.time_simulate(now_time)
        now_time_formal = now_time.strftime("%Y-%m-%d %H:%M:%S")
        f1.write(str(now_time_formal) + "  ")
        f1.write("a与"+str(site)+"的通路连接失败触发自动拆链\n")
    f1.write("\n")







