コード例 #1
0
def all_receive_data(sensor_ip_list):
    time_tag = get_time_tag()
    old_min = time_tag[14:16]
    file_date = time_tag[0:14]

    all_data = []

    for ii in range(len(sensor_ip_list)):
        all_data.append([])  #initial data list
        all_data[ii] = ''
    while True:
        #    for ii in range(data_time*50/3): # each package have 600 data,sample rate is 10000,so each second it have 50/3 packages
        receive_data, client_address = server_socket.recvfrom(2048)
        data_ip = str(client_address[0])
        #        print len(receive_data)
        if len(receive_data) < 100:
            re_message = decode_data.decode_config_message(receive_data)
            if re_message in 'Sp':
                print("%s are stoped!" % data_ip)
                continue
        if data_ip in sensor_ip_list:
            data_location_num = sensor_ip_list.index(data_ip)
        else:
            print('%s sensor still upload data!' % data_ip)
            continue

        time_tag = get_time_tag()
        new_min = time_tag[14:16]
        if len(receive_data) < 600:
            print("111")
            continue


#        print time_tag
#        print len(receive_data)
        tmpdata = decode_data.decode_data(receive_data)
        # print(type(tmpdata))
        all_data[data_location_num] = all_data[
            data_location_num] + time_tag + tmpdata + '\n'
        if new_min == old_min:
            continue
        else:
            filename = file_date + old_min + '.txt'
            old_min = new_min
            file_date = time_tag[0:14]
            writ_data = copy.deepcopy(all_data)
            thread.start_new_thread(save_file,
                                    (sensor_ip_list, filename, writ_data))

            for ii in range(len(sensor_ip_list)):
                all_data[ii] = ''
コード例 #2
0
def receive_data(sensor_ip, target_str):

    flag = False
    start_time = time.time()
    while True:

        receive_data, client_address = server_socket.recvfrom(1024)
        real_data = decode_data.decode_config_message(receive_data)
        #        print ('%s and message is %s'%(str(client_address[0]), real_data))

        if (cmp(str(client_address[0]), sensor_ip)
                == 0) and target_str in real_data:
            #    print ('%s and message is %s'%(client_address, real_data))
            flag = True
            break
        now_time = time.time()
        if now_time - start_time > 1:
            break

    return flag
コード例 #3
0
def all_receive_data(sensor_ip_list, time_lim):

    time_tag = get_time_tag()
    old_min = time_tag[14:16]
    file_date = time_tag[0:14]
    start_second = int(time.time())

    all_data = []
    plot_data = [[0 * x for x in range(600 * PLOT_NUM)]
                 for j in range(len(sensor_ip_list))]
    plot_count = np.zeros(len(sensor_ip_list))
    plot_data_count = np.zeros(len(sensor_ip_list))
    filter_data = [[0 * x for x in range(600 * PLOT_COUNT)]
                   for j in range(len(sensor_ip_list))]

    for ii in range(len(sensor_ip_list)):
        all_data.append([])  #initial data list
        all_data[ii] = ''

    # initial plot info
    data_location_num = 0

    #thread = threading.Thread(target=myplot, args=(len(sensor_ip_list),))
    #   thread.setDaemon(True)
    #   thread.start()
    p = multiprocessing.Process(target=myplot, args=(len(sensor_ip_list), ))
    p.start()

    server_socket.setblocking(0)
    data_flag = False
    while True:
        time_tag = get_time_tag()
        new_min = time_tag[14:16]

        #time_limit
        now_second = int(time.time())
        if (time_lim > 0) & (now_second - start_second > time_lim):
            sensor_stop(sensor_ip_list)
            #save file
            filename = file_date + new_min + '.txt'
            writ_data = copy.deepcopy(all_data)
            threading.Thread(target=save_file,
                             args=(
                                 sensor_ip_list,
                                 filename,
                                 writ_data,
                             )).start()
            p.terminate()
            break

        # save file
        if new_min != old_min:
            filename = file_date + old_min + '.txt'
            old_min = new_min
            file_date = time_tag[0:14]
            writ_data = copy.deepcopy(all_data)
            #threading.Thread(target=save_file, args=(sensor_ip_list,filename,writ_data,)).start()

            for ii in range(len(sensor_ip_list)):
                all_data[ii] = ''


# receive sensor package
        try:
            receive_data, client_address = server_socket.recvfrom(2048)
            data_flag = True
            time_tag = get_time_tag()
        except IOError as e:
            if e.errno == errno.EWOULDBLOCK:
                data_flag = False
            continue

        t1 = time.clock()
        data_ip = str(client_address[0])

        if len(receive_data) < 100:
            re_message = decode_data.decode_config_message(receive_data)
            if re_message in 'Sp':
                print("%s is stoped!" % data_ip)
            continue
        if data_ip in sensor_ip_list:
            data_location_num = sensor_ip_list.index(data_ip)
        else:
            print('%s sensor still upload data!' % data_ip)
            continue

        de_code_data = decode_data.decode_data(receive_data)
        #        print time_tag
        #        print len(receive_data)

        all_data[data_location_num] = all_data[
            data_location_num] + time_tag + de_code_data + '\n'

        use_data = de_code_data.split('(')[2]
        use_data = use_data.replace(')', '')
        char_list = use_data.split(',')
        int_data = [int(x) for x in char_list]

        filter_data[
            data_location_num] = filter_data[data_location_num] + int_data
        filter_data[data_location_num][0:600] = []
        plot_count[data_location_num] = plot_count[data_location_num] + 1

        # put data into queue
        if plot_count[data_location_num] >= PLOT_COUNT:
            queue[data_location_num].put(filter_data[data_location_num])
            plot_count[data_location_num] = 0

        t2 = time.clock()
        #       print("data process time is %f"%(t2-t1))
        time.sleep(0.01)
コード例 #4
0
def all_receive_data(sensor_ip_list, time_lim):
    time_tag = get_time_tag()
    old_min = time_tag[14:16]
    file_date = time_tag[0:14]
    start_second = int(time.time())

    all_data = []
    plot_data = [[0 * x for x in range(600 * PLOT_NUM)]
                 for j in range(len(sensor_ip_list))]
    plot_count = np.zeros(len(sensor_ip_list))
    plot_data_count = np.zeros(len(sensor_ip_list))
    filter_data = [[0 * x for x in range(600 * PLOT_NUM)]
                   for j in range(len(sensor_ip_list))]
    tmp_filter_data = [[] for j in range(len(sensor_ip_list))]

    for ii in range(len(sensor_ip_list)):
        all_data.append([])  #initial data list
        all_data[ii] = ''

    # initial plot info
    for ii in range(len(sensor_ip_list) + 1):
        plt.figure(ii)
        plt.ylim([0, 45000])

    while True:
        #    for ii in range(data_time*50/3): # each package have 600 data,sample rate is 10000,so each second it have 50/3 packages
        receive_data, client_address = server_socket.recvfrom(2048)
        data_ip = str(client_address[0])
        #        print len(receive_data)
        if len(receive_data) < 100:
            re_message = decode_data.decode_config_message(receive_data)
            if re_message in 'Sp':
                print("%s is stoped!" % data_ip)
            continue
        if data_ip in sensor_ip_list:
            data_location_num = sensor_ip_list.index(data_ip)
        else:
            print('%s sensor still upload data!' % data_ip)
            continue

        time_tag = get_time_tag()
        new_min = time_tag[14:16]
        de_code_data = decode_data.decode_data(receive_data)
        #        print time_tag
        #        print len(receive_data)

        all_data[data_location_num] = all_data[
            data_location_num] + time_tag + de_code_data + '###\n'

        use_data = de_code_data.split('(')[2]
        use_data = use_data.replace(')', '')
        char_list = use_data.split(',')
        int_data = [int(x) for x in char_list]
        #print(use_data)
        #plot_data[data_location_num] = plot_data[data_location_num] + use_data

        plot_data[data_location_num] = plot_data[data_location_num] + int_data
        plot_count[data_location_num] = plot_count[data_location_num] + 1
        plot_data[data_location_num][0:600] = []

        tmp_filter_data[
            data_location_num] = tmp_filter_data[data_location_num] + int_data
        # begin plot
        if plot_count[data_location_num] >= PLOT_COUNT:
            tmp_len = len(tmp_filter_data[data_location_num])
            if tmp_len > 0:
                s = '''
                filtered_sig = scipy.signal.wiener(tmp_filter_data[data_location_num], 1001).tolist()
                tmp_filter_data[data_location_num] = []
                filter_data[data_location_num] = filter_data[data_location_num] + filtered_sig
                print(tmp_len)
                filter_data[data_location_num][0:tmp_len] = []
                plt.figure(data_location_num +1)
                plt.clf()
                plt.ylim([0, 45000])
                plt.plot(filter_data[data_location_num])
                plt.pause(0.001)     
   '''
                filtered_sig = scipy.signal.wiener(
                    plot_data[data_location_num], 501)
                plt.figure(data_location_num + 1)
                plt.clf()
                plt.ylim([0, 45000])
                plt.plot(filtered_sig)
                plt.pause(0.001)

            plt.figure(data_location_num)
            plt.clf()
            plt.ylim([0, 45000])
            plt.plot(plot_data[data_location_num])
            plt.pause(0.001)
            #plt.show()
            plot_count[data_location_num] = 0


# ax.draw()

#time_limit
        now_second = int(time.time())
        if now_second - start_second > time_lim:
            sensor_stop(sensor_ip_list)
            break

        if new_min == old_min:
            continue

        else:
            filename = file_date + old_min + '.txt'
            old_min = new_min
            file_date = time_tag[0:14]
            writ_data = copy.deepcopy(all_data)
            thread.start_new_thread(save_file,
                                    (sensor_ip_list, filename, writ_data))

            for ii in range(len(sensor_ip_list)):
                all_data[ii] = ''