Esempio n. 1
0
def main():
    print("\x1b[1;34;40m delay:{d} [RELAY: {r}]\x1b[0m".format(
        d=my_delay[index_my_delay], r=topic))
    files = analysis_5_file()

    for i in range(len(files)):
        name = files[i]
        data = get_data(name)
        if data['_command']['delay'] != my_delay[index_my_delay]:
            raise Exception("Errore delay: ", data['_command']['delay'])
        print(
            "\x1b[1;34;40m addr:{a}, delay:{d} n_mex:{n} [RELAY: {r}]\x1b[0m".
            format(a=data['_command']['addr'],
                   d=data['_command']['delay'],
                   n=data['_command']['n_mex'],
                   r=topic))
        manage_statistics(i, data, name)
        if runs:
            plot_points(data, i)
        print("--------------")
        time.sleep(1)

    summary()
    if runs:
        my.save_json_data_elegant(path=file_name, data=my_dictionary)
        manage_plot()
    else:
        my.print_data_as_json(my_dictionary)
        plot_mean()
Esempio n. 2
0
def main():
    # path = my.get_argument()
    my_data = dict()

    try:
        my_data = my.open_file_and_return_data(path=path)
        # my.save_json_data_elegant(path=path_1, data=my_data)
        print('\x1b[0;33;40m' + " Preprocessing completed! " + '\x1b[0m')
    except Exception as e:
        print(e)
        return

    try:
        my_data = resolve_errors_preprocessing(data=my_data)
        # my.save_json_data_elegant(path=path_1, data=my_data)
        print('\x1b[0;33;40m' + " Error Preprocessing resoluted! " + '\x1b[0m')
    except Exception as e:
        print(e)
        return

    try:
        my_data, checks = second_analysis(data=my_data)
        # my.save_json_data_elegant(path=path_1, data=my_data)
        print('\x1b[0;33;40m' + " Second Analysis completed! " + '\x1b[0m')
    except Exception as e:
        print(e)
        return

    try:
        my_data = third_analysis(data=my_data)
        print('\x1b[0;33;40m' + " Third Analysis completed! " + '\x1b[0m')
        my.save_json_data_elegant(path=path_1, data=my_data)
    except Exception as e:
        print(e)
Esempio n. 3
0
def save_dictionary():
    path_directory = "./test_bis/test_" + str(
        dt.datetime.strftime(dt.datetime.now(), "%Y_%m_%d"))
    directory = my.define_directory(directory=path_directory)
    path = directory + '/test_' + dt.datetime.now().strftime(
        "%y_%m_%d-%H_%M_%S") + '.json'
    print(path)
    my.save_json_data_elegant(path=path, data=data)
Esempio n. 4
0
def main():
    # TODO usare solo quando ttl è uguale a 7, 6, 5
    if True:
        change_ttl_from_7_to_3(path=path)
        time.sleep(1)
    my_data = preprocessing(path=path)
    my.save_json_data_elegant(path=preprocessing_path, data=my_data)

    time.sleep(1)

    my_data = first_analysis(path=path_get_preprocessing)
    my.save_json_data_elegant(path=analysis_path, data=my_data)
Esempio n. 5
0
def remove_double():
    source_path = my.path_media + file_name[3:-5] + "_analysis.json"
    data = my.open_file_and_return_data(path=source_path)
    double = False
    for k, v in data['_mex'].items():
        if len(v) == 7:
            double = True
            x = list()
            for e in v:
                if 'ble' in e:
                    x.append(e)
                elif 'type_mex' in e:
                    if e['type_mex'] == 'S' or e['type_mex'] == 'R':
                        x.append(e)
            data['_mex'][k] = x

    if double:
        my.save_json_data_elegant(path=analysis_path_2, data=data)
Esempio n. 6
0
def preprocessing(data):
    print("- {}".format(preprocessing.__name__))
    wrong_mex = dict()
    time_change = list()
    mex_correct = list()
    end_sent = ""
    for i, mex in enumerate(data['messages']):
        string = mex['message_id']
        string = string.split(',')
        if re.match(regular_expression_mex, mex['message_id']):
            # MEX string
            mex_correct.append({
                'mex_id': int(string[1]),
                'type_mex': string[0],
                'ttl': string[2],
                'time': mex['time']
            })
        elif re.match(regular_expression_time, mex['message_id']):
            # DELAY check WIFI string
            time_change.append({
                'type_mex': string[0],
                'delay': int(string[1]),
                'time': mex['time']
            })
        elif re.match(regular_expression_final, mex['message_id']):
            end_sent = {'type_mex': string[0], 'time': mex['time']}
        else:
            # wrong saving
            match = re.findall(find_all_matches, mex['message_id'])
            wrong_mex[i] = {
                'datetime': mex['time'],
                'valid_mex_found': match,
                'string': mex['message_id']
            }

    new_wrong_mex = dict()
    for index in wrong_mex:
        try:
            string = wrong_mex[index]['string']
            datetime = wrong_mex[index]['datetime']
            if len(wrong_mex[index]['valid_mex_found']) > 0:
                for m in wrong_mex[index]['valid_mex_found']:
                    string = string.replace(m, '')
                    m1 = m.split(',')
                    if re.match(regular_expression_mex, m):
                        mex_correct.append({
                            'mex_id': int(m1[1]),
                            'type_mex': m1[0],
                            'ttl': m1[2],
                            'time': datetime
                        })
                    elif re.match(regular_expression_time, m):
                        time_change.append({
                            'type_mex': m1[0],
                            'delay': int(m1[1]),
                            'time': datetime
                        })
                    elif re.match(regular_expression_final, m):
                        end_sent = {'type_mex': m1[0], 'time': datetime}
                if len(string) > 0:
                    new_wrong_mex[index] = {
                        'datetime': datetime,
                        'string': string
                    }
            else:
                new_wrong_mex[index] = {'datetime': datetime, 'string': string}
        except Exception:
            print("Erore index: ", index)

    # 1. creo una lista contenente tutte le strings con k propedeutici (23,24,25)
    # 2. appena trovo un numero che non è immediatamente consecutivo agli elementi in key_list, analizzo la string creata
    # 3. cerco tutti i possibili match con (mex, time, etc) e procedo a sistemare il tutto
    string = ""
    time_list = list()
    key_list = list()
    for k, v in new_wrong_mex.items():
        if key_list:
            if abs(k - key_list[-1]) != 1:
                match = re.findall(find_all_matches, string)
                i = 0
                for m in match:
                    string = string.replace(m, '')
                    m1 = m.split(',')
                    if re.match(regular_expression_mex, m):
                        mex_correct.append({
                            'mex_id': int(m1[1]),
                            'type_mex': m1[0],
                            'ttl': m1[2],
                            'time': time_list[i]
                        })
                    elif re.match(regular_expression_time, m):
                        time_change.append({
                            'type_mex': m1[0],
                            'delay': int(m1[1]),
                            'time': time_list[i]
                        })
                    elif re.match(regular_expression_final, m):
                        end_sent = {'type_mex': m1[0], 'time': time_list[i]}
                    i += 1
                key_list.clear()
                time_list.clear()
        if string == "":
            string = str(v['string'])
            if v['datetime'] == "2020-02-01 09:16:59.943322":
                print(string)
        else:
            string += str(v['string'])
            if v['datetime'] == "2020-02-01 09:16:59.943322":
                print(string)
        key_list.append(k)
        time_list.append(v['datetime'])

    # TODO PER GESTIRE L'ULTIMO ELEMENTO NELLA LISTA DEGLI ERRORI
    if string != "":
        match = re.findall(find_all_matches, string)
        i = 0
        for m in match:
            string = string.replace(m, '')
            m1 = m.split(',')
            if re.match(regular_expression_mex, m):
                mex_correct.append({
                    'mex_id': int(m1[1]),
                    'type_mex': m1[0],
                    'ttl': m1[2],
                    'time': time_list[i]
                })
            elif re.match(regular_expression_time, m):
                time_change.append({
                    'type_mex': m1[0],
                    'delay': int(m1[1]),
                    'time': time_list[i]
                })
            elif re.match(regular_expression_final, m):
                end_sent = {'type_mex': m1[0], 'time': time_list[i]}
            i += 1

    # TODO Sort given list of dictionaries by date
    mex_correct.sort(
        key=lambda x: dt.datetime.strptime(x['time'], '%Y-%m-%d %H:%M:%S.%f'))
    time_change.sort(
        key=lambda x: dt.datetime.strptime(x['time'], '%Y-%m-%d %H:%M:%S.%f'))

    # PRINT MEX
    start = dt.datetime.strptime(mex_correct[0]['time'],
                                 '%Y-%m-%d %H:%M:%S.%f')
    end_sent_ = dt.datetime.strptime(end_sent['time'], '%Y-%m-%d %H:%M:%S.%f')
    end_test = dt.datetime.strptime(mex_correct[-1]['time'],
                                    '%Y-%m-%d %H:%M:%S.%f')

    diff_ = end_sent_ - start
    h2, m2, s2 = my.convert_timedelta(diff_)
    diff = end_test - start
    h1, m1, s1 = my.convert_timedelta(diff)

    print("Start: {}".format(start))
    print("End_sent: {}".format(end_sent_))
    print("End_test: {}".format(end_test))
    print("Sent: {}:{}.{} [mm:s.us]".format(m2, s2, diff_.total_seconds()))
    print("Test: {}:{}.{} [mm:s.us]".format(m1, s1, diff.total_seconds()))

    # Salvare Preprocessing
    t1 = str(h2) + ":" + str(m2) + "." + str(s2) + " [h:m.s]"
    t2 = str(h1) + ":" + str(m1) + "." + str(s1) + " [h:m.s]"
    my_data = dict()
    my_data['_command'] = data['_command']
    my_data['_info'] = {
        'start': start,
        'end_sent': end_sent_,
        'end_test': end_test,
        'time_send': t1,
        'time_test': t2
    }
    my_data['_mex'] = mex_correct
    my_data['_time'] = time_change

    # TODO saving
    print("Saving Preprocessing File")
    my.save_json_data_elegant(path=preprocessing_path, data=my_data)

    hash_table = dict()
    length = len(mex_correct)
    for i in range(length):
        k = mex_correct[i]['mex_id']
        if k in hash_table:
            hash_table[k].append(i)
        else:
            hash_table[k] = list()
            hash_table[k].append(i)

    word_info = dict()
    info_statistic = {
        'send_ble': 0,
        'receive_ble': 0,
        'error_ble': 0,
        'lost_ble': 0,
        'double_sent': 0,
        'send_wifi': 0,
        'receive_wifi': 0,
        'error_wifi': 0,
        'lost_wifi': 0,
        'sent_received': 0
    }
    ttl_receive = 3 - int(data['_command']['relay'])
    list_wait = list()
    error_1 = list()
    error_2 = list()
    diff_ble = list()
    latency_ble = list()
    diff_wifi = list()
    latency_wifi = list()
    hash_json_data = dict()
    double = list()
    set_1 = set()
    set_2 = set()
    for k, v in hash_table.items():
        for i in v:
            word_info, info_statistic, double = my.look_into_it(
                word_info, info_statistic, mex_correct[i], double)
            if mex_correct[i]['type_mex'] == "O":
                set_1.add(mex_correct[i]['mex_id'])
            if not k in hash_json_data:
                hash_json_data[k] = []
            hash_json_data[k].append(mex_correct[i])
        # BLE
        if 'receive_ble' in word_info:
            if not int(word_info['send_ble']['ttl']) == 3:
                print('\x1b[1;31;40m' + ' Error analysis_ttl --> send_: ' +
                      word_info['send_ble']['time'] + '\x1b[0m')
                error_1.append({'k': k, 'time': word_info['send_ble']['time']})
            if not int(word_info['receive_ble']['ttl']) == ttl_receive:
                print('\x1b[1;31;40m' + ' Error analysis_ttl --> receive_: ' +
                      word_info['receive_ble']['time'] + '\x1b[0m')
                error_2.append({'k': k, 'time': word_info['send_ble']['time']})

            sent = dt.datetime.strptime(word_info['send_ble']['time'],
                                        '%Y-%m-%d %H:%M:%S.%f')
            receive_wifi = dt.datetime.strptime(
                word_info['receive_ble']['time'], '%Y-%m-%d %H:%M:%S.%f')
            difference = receive_wifi - sent
            diff_ble.append(abs(difference.total_seconds()))
            latency_ble.append(abs(difference.total_seconds()) / 2)
            info_statistic['sent_received'] += 1
            hash_json_data[k].append({
                'ble': {
                    'send_time': sent,
                    'status_time': receive_wifi,
                    'difference': abs(difference.total_seconds()),
                    'latency': (abs(difference.total_seconds()) / 2)
                }
            })
        elif 'error_ble' in word_info:
            info_statistic['send_ble'] -= 1
        else:
            info_statistic['lost_ble'] += 1
        # WIFI
        if 'send_wifi' in word_info:
            if 'receive_wifi' in word_info:
                sent = dt.datetime.strptime(word_info['send_wifi']['time'],
                                            '%Y-%m-%d %H:%M:%S.%f')
                receive_wifi = dt.datetime.strptime(
                    word_info['receive_wifi']['time'], '%Y-%m-%d %H:%M:%S.%f')
                difference = receive_wifi - sent
                diff_wifi.append(abs(difference.total_seconds()))
                latency_wifi.append(abs(difference.total_seconds()) / 2)
                info_statistic['sent_received'] += 1
                hash_json_data[k].append({
                    'wifi': {
                        'send_time': sent,
                        'status_time': receive_wifi,
                        'difference': abs(difference.total_seconds()),
                        'latency': (abs(difference.total_seconds()) / 2)
                    }
                })
                set_2.add(word_info['receive_wifi']['mex_id'])
            elif 'error_wifi' in word_info:
                info_statistic['send_wifi'] -= 1
            else:
                info_statistic['lost_wifi'] += 1
            # waiting
            send_ble = dt.datetime.strptime(word_info['send_ble']['time'],
                                            '%Y-%m-%d %H:%M:%S.%f')
            send_wifi = dt.datetime.strptime(word_info['send_wifi']['time'],
                                             '%Y-%m-%d %H:%M:%S.%f')
            wait = abs(send_wifi - send_ble)
            list_wait.append(wait.total_seconds())
            if 'receive_wifi' in word_info:
                receive_wifi = dt.datetime.strptime(
                    word_info['receive_wifi']['time'], '%Y-%m-%d %H:%M:%S.%f')
                diff_1 = (receive_wifi - send_ble).total_seconds() / 2
                diff_2 = wait.total_seconds() + (
                    (receive_wifi - send_wifi).total_seconds() / 2)
                hash_json_data[k].append({
                    'ble_wifi': {
                        'send_ble': send_ble,
                        'send_wifi': send_wifi,
                        'receive_wifi': receive_wifi,
                        'wait': wait.total_seconds(),
                        'latency_1': diff_1,
                        'latency_2': diff_2
                    }
                })
            else:
                hash_json_data[k].append({
                    'ble_wifi': {
                        'send_ble': send_ble,
                        'send_wifi': send_wifi,
                        'wait': wait.total_seconds()
                    }
                })

        if 'receive_ble' in word_info and 'send_wifi' in word_info:
            info_statistic['double_sent'] += 1
        word_info.clear()

    if len(double) != 0:
        for k in double:
            print(hash_json_data[k])
        raise Exception(
            'Indici Duplicati, esempio R,34,2 - R,34,1 --> Rimuovere R,34,2')

    print("---------------")
    print("[relay: {} -> ttl: {}]".format(data['_command']['relay'],
                                          ttl_receive))
    print("Changing Delay: {}".format(len(time_change)))
    print("---------------")

    if len(error_1) > 0 or len(error_2) > 0:
        raise Exception('\x1b[1;31;40m' + ' Error TTL: sent: ' +
                        str(len(error_1)) + ' --> received: ' +
                        str(len(error_2)) + ' \x1b[0m')

    print("Media DIff BLE: {}s".format(statistics.mean(diff_ble)))
    print("Media Latency BLE: {}s".format(statistics.mean(latency_ble)))
    print("Media Diff WIFI: {}s".format(statistics.mean(diff_wifi)))
    print("Media Latency WIFI: {}s".format(statistics.mean(latency_wifi)))
    print("Media Waiting BLE -> WIFI: {}s".format(statistics.mean(list_wait)))

    info_statistic['valid_mex'] = info_statistic[
        'sent_received'] - info_statistic['double_sent']
    print("Total Send -> [{}]".format(data['_command']['n_mex']))
    print("S (ble) -> {}".format(info_statistic['send_ble']))
    print("R (ble) -> {}".format(info_statistic['receive_ble']))
    print("E (ble) -> {}".format(info_statistic['error_ble']))  # not_sent_ble
    print("Lost BLE: {}".format(info_statistic['lost_ble']))
    print("I (wifi) -> {}".format(info_statistic['send_wifi']))
    print("O (wifi) -> {}".format(info_statistic['receive_wifi']))
    print("W (wifi) -> {}".format(
        info_statistic['error_wifi']))  # not_sent_wifi
    print("Lost WiFi: {}".format(info_statistic['lost_wifi']))
    print("Double Sent -> {}".format(info_statistic['double_sent']))
    print("Sent & Received -> {}".format(info_statistic['sent_received']))
    print("Valid Mex -> {}".format(info_statistic['valid_mex']))

    # Check statistics
    if data['_command']['n_mex'] - info_statistic['send_ble'] - info_statistic[
            'error_ble'] != 0:
        raise Exception('\x1b[1;31;40m Error BLE: mex number [T-S-E] \x1b[0m')
    elif info_statistic['send_ble'] - info_statistic[
            'receive_ble'] != info_statistic['lost_ble']:
        raise Exception('\x1b[1;31;40m Error BLE: mex number [S-R] \x1b[0m')
    elif info_statistic['send_wifi'] - info_statistic[
            'lost_wifi'] != info_statistic['receive_wifi']:
        print(set_2.difference(set_1))
        print(set_1.difference(set_2))
        raise Exception('\x1b[1;31;40m Error WIFI: mex number [I-O] \x1b[0m')
    elif info_statistic['valid_mex'] > data['_command']['n_mex']:
        raise Exception('\x1b[1;31;40m Error MEX: Valid Mex \x1b[0m')
    else:
        print('\x1b[1;31;42m Check OK \x1b[0m')
    my_data_2 = dict()
    my_data_2['_command'] = my_data['_command']
    my_data_2['_info'] = my_data['_info']
    my_data_2['_info_2'] = {
        'mean': {
            'ble_mean_diff': statistics.mean(diff_ble),
            'ble_mean_latency': statistics.mean(latency_ble),
            'wifi_mean_diff': statistics.mean(diff_wifi),
            'wifi_mean_latency': statistics.mean(latency_wifi),
            'wait_mean': statistics.mean(list_wait)
        },
        'mex_': {
            'ble': {
                'send_ble': info_statistic['send_ble'],
                'receive_ble': info_statistic['receive_ble'],
                'not_send_ble': info_statistic['error_ble'],
                'lost_ble': info_statistic['lost_ble']
            },
            'wifi': {
                'send_wifi': info_statistic['send_wifi'],
                'receive_wifi': info_statistic['receive_wifi'],
                'not_send_wifi': info_statistic['error_wifi'],
                'lost_wifi': info_statistic['lost_wifi']
            },
            'double_sent': info_statistic['double_sent'],
            'valid_mex': info_statistic['valid_mex'],
            'sent_received_total': info_statistic['sent_received']
        }
    }
    my_data_2['_time'] = my_data['_time']
    my_data_2['_mex'] = hash_json_data
    print("Saving Analysis File")
    my.save_json_data_elegant(path=analysis_path, data=my_data_2)
Esempio n. 7
0
def preprocessing(data):
    print("- {}".format(preprocessing.__name__))
    wrong_mex = dict()
    time_change = list()
    mex_correct = list()
    status_change = list()
    for i, mex in enumerate(data['messages']):
        string = mex['message_id']
        string = string.split(',')
        if re.match(regular_expression_mex, mex['message_id']):
            # MEX string
            mex_correct.append({
                'mex_id': int(string[1]),
                'type_mex': string[0],
                'ttl': string[2],
                'time': mex['time']
            })
        elif re.match(regular_expression_time, mex['message_id']):
            # DELAY check WIFI string
            time_change.append({
                'type_mex': string[0],
                'delay': int(string[1]),
                'time': mex['time']
            })
        elif re.match(regular_expression_final, mex['message_id']):
            status_change.append({
                'type_mex': string[0],
                'case': int(string[1]),
                'status': int(string[2]),
                'time': mex['time']
            })
        else:
            # wrong saving
            match = re.findall(find_all_matches, mex['message_id'])
            wrong_mex[i] = {
                'datetime': mex['time'],
                'valid_mex_found': match,
                'string': mex['message_id']
            }

    new_wrong_mex = dict()
    for index in wrong_mex:
        try:
            string = wrong_mex[index]['string']
            datetime = wrong_mex[index]['datetime']
            if len(wrong_mex[index]['valid_mex_found']) > 0:
                for m in wrong_mex[index]['valid_mex_found']:
                    string = string.replace(m, '')
                    m1 = m.split(',')
                    if re.match(regular_expression_mex, m):
                        mex_correct.append({
                            'mex_id': int(m1[1]),
                            'type_mex': m1[0],
                            'ttl': m1[2],
                            'time': datetime
                        })
                    elif re.match(regular_expression_time, m):
                        time_change.append({
                            'type_mex': m1[0],
                            'delay': int(m1[1]),
                            'time': datetime
                        })
                    elif re.match(regular_expression_final, m):
                        status_change.append({
                            'type_mex': m1[0],
                            'case': int(m1[1]),
                            'status': int(m1[2]),
                            'time': datetime
                        })
                if len(string) > 0:
                    new_wrong_mex[index] = {
                        'datetime': datetime,
                        'string': string
                    }
            else:
                new_wrong_mex[index] = {'datetime': datetime, 'string': string}
        except Exception:
            print("Erore index: ", index)

    # 1. creo una lista contenente tutte le strings con k propedeutici (23,24,25)
    # 2. appena trovo un numero che non è immediatamente consecutivo agli elementi in key_list, analizzo la string creata
    # 3. cerco tutti i possibili match con (mex, time, etc) e procedo a sistemare il tutto
    string = ""
    time_list = list()
    key_list = list()
    for k, v in new_wrong_mex.items():
        if key_list:
            if abs(k - key_list[-1]) != 1:
                match = re.findall(find_all_matches, string)
                i = 0
                for m in match:
                    string = string.replace(m, '')
                    m1 = m.split(',')
                    if re.match(regular_expression_mex, m):
                        mex_correct.append({
                            'mex_id': int(m1[1]),
                            'type_mex': m1[0],
                            'ttl': m1[2],
                            'time': time_list[i]
                        })
                    elif re.match(regular_expression_time, m):
                        time_change.append({
                            'type_mex': m1[0],
                            'delay': int(m1[1]),
                            'time': time_list[i]
                        })
                    elif re.match(regular_expression_final, m):
                        status_change.append({
                            'type_mex': m1[0],
                            'case': int(m1[1]),
                            'status': int(m1[2]),
                            'time': time_list[i]
                        })
                    i += 1
                key_list.clear()
                time_list.clear()
        if string == "":
            string = str(v['string'])
            if v['datetime'] == "2020-02-01 09:16:59.943322":
                print(string)
        else:
            string += str(v['string'])
            if v['datetime'] == "2020-02-01 09:16:59.943322":
                print(string)
        key_list.append(k)
        time_list.append(v['datetime'])

    # TODO PER GESTIRE L'ULTIMO ELEMENTO NELLA LISTA DEGLI ERRORI
    if string != "":
        match = re.findall(find_all_matches, string)
        i = 0
        for m in match:
            string = string.replace(m, '')
            m1 = m.split(',')
            if re.match(regular_expression_mex, m):
                mex_correct.append({
                    'mex_id': int(m1[1]),
                    'type_mex': m1[0],
                    'ttl': m1[2],
                    'time': time_list[i]
                })
            elif re.match(regular_expression_time, m):
                time_change.append({
                    'type_mex': m1[0],
                    'delay': int(m1[1]),
                    'time': time_list[i]
                })
            elif re.match(regular_expression_final, m):
                status_change.append({
                    'type_mex': m1[0],
                    'case': int(m1[1]),
                    'status': int(m1[2]),
                    'time': time_list[i]
                })
            i += 1

    # TODO Sort given list of dictionaries by date
    mex_correct.sort(
        key=lambda x: dt.datetime.strptime(x['time'], '%Y-%m-%d %H:%M:%S.%f'))
    time_change.sort(
        key=lambda x: dt.datetime.strptime(x['time'], '%Y-%m-%d %H:%M:%S.%f'))
    status_change.sort(
        key=lambda x: dt.datetime.strptime(x['time'], '%Y-%m-%d %H:%M:%S.%f'))

    my_data = dict()
    my_data['_command'] = data['_command']
    my_data['_mex'] = mex_correct
    my_data['_time'] = time_change
    my_data['_status'] = status_change

    # TODO saving
    print("Saving Preprocessing File")
    my.save_json_data_elegant(path=preprocessing_path, data=my_data)

    hash_table = dict()
    length = len(mex_correct)
    for i in range(length):
        k = mex_correct[i]['mex_id']
        if k in hash_table:
            hash_table[k].append(i)
        else:
            hash_table[k] = list()
            hash_table[k].append(i)

    word_info = dict()
    info_statistic = {
        'send_ble': 0,
        'receive_ble': 0,
        'error_ble': 0,
        'lost_ble': 0,
        'double_sent': 0,
        'send_wifi': 0,
        'receive_wifi': 0,
        'error_wifi': 0,
        'lost_wifi': 0,
        'sent_received': 0
    }
    ttl_receive = 3 - int(data['_command']['relay'])
    list_wait = list()
    error_1 = list()
    error_2 = list()
    diff_ble = list()
    latency_ble = list()
    diff_wifi = list()
    latency_wifi = list()
    hash_json_data = dict()
    double_set = set()
    received_ble_set = set()
    sent_wifi_set = set()
    received_wifi_set = set()
    double = list()
    for k, v in hash_table.items():
        for i in v:
            word_info, info_statistic, double = my.look_into_it(
                word_info, info_statistic, mex_correct[i], double)
            if not k in hash_json_data:
                hash_json_data[k] = []
            hash_json_data[k].append(mex_correct[i])
        # BLE
        if 'receive_ble' in word_info:
            received_ble_set.add(int(k))
            # check TTL BLE
            if not int(word_info['send_ble']['ttl']) == 3:
                print('\x1b[1;31;40m' + ' Error analysis_ttl --> send_: ' +
                      word_info['send_ble']['time'] + '\x1b[0m')
                error_1.append({'k': k, 'time': word_info['send_ble']['time']})
            if not int(word_info['receive_ble']['ttl']) == ttl_receive:
                print('\x1b[1;31;40m' + ' Error analysis_ttl --> receive_: ' +
                      word_info['receive_ble']['time'] + '\x1b[0m')
                error_2.append({'k': k, 'time': word_info['send_ble']['time']})

            sent = dt.datetime.strptime(word_info['send_ble']['time'],
                                        '%Y-%m-%d %H:%M:%S.%f')
            receive = dt.datetime.strptime(word_info['receive_ble']['time'],
                                           '%Y-%m-%d %H:%M:%S.%f')
            difference = abs(receive - sent)
            diff_ble.append(difference.total_seconds())
            latency_ble.append(difference.total_seconds() / 2)
            info_statistic['sent_received'] += 1
            hash_json_data[k].append({
                'ble': {
                    'send_time': sent,
                    'status_time': receive,
                    'difference': difference.total_seconds(),
                    'latency': (difference.total_seconds() / 2)
                }
            })
        elif 'error_ble' in word_info:
            info_statistic['send_ble'] -= 1
        else:
            info_statistic['lost_ble'] += 1
        # WIFI
        if 'send_wifi' in word_info:
            sent_wifi_set.add(int(k))
            if 'receive_wifi' in word_info:
                received_wifi_set.add(int(k))
                sent = dt.datetime.strptime(word_info['send_wifi']['time'],
                                            '%Y-%m-%d %H:%M:%S.%f')
                receive = dt.datetime.strptime(
                    word_info['receive_wifi']['time'], '%Y-%m-%d %H:%M:%S.%f')
                difference = abs(receive - sent)
                diff_wifi.append(difference.total_seconds())
                latency_wifi.append(difference.total_seconds() / 2)
                info_statistic['sent_received'] += 1
                hash_json_data[k].append({
                    'wifi': {
                        'send_time': sent,
                        'status_time': receive,
                        'difference': difference.total_seconds(),
                        'latency': (difference.total_seconds() / 2)
                    }
                })
            elif 'error_wifi' in word_info:
                info_statistic['send_wifi'] -= 1
            else:
                info_statistic['lost_wifi'] += 1

        # waiting
        if 'send_ble' in word_info and 'send_wifi' in word_info:
            # t0 -> S BLE
            # t1 -> S WiFi
            # t2 -> R WiFi
            # (t2-t1)/2
            send_ble = dt.datetime.strptime(word_info['send_ble']['time'],
                                            '%Y-%m-%d %H:%M:%S.%f')
            send_wifi = dt.datetime.strptime(word_info['send_wifi']['time'],
                                             '%Y-%m-%d %H:%M:%S.%f')
            wait = abs(send_wifi - send_ble)
            list_wait.append(wait.total_seconds())

            if 'receive_wifi' in word_info:
                rc_wifi = dt.datetime.strptime(
                    word_info['receive_wifi']['time'], '%Y-%m-%d %H:%M:%S.%f')
                diff_1 = abs(rc_wifi - send_ble)  # latency_ble+wifi
                diff_2 = abs(rc_wifi - send_wifi)  # latency_wifi
                hash_json_data[k].append({
                    'ble_wifi': {
                        'send_ble':
                        send_ble,
                        'send_wifi':
                        send_wifi,
                        'receive_wifi':
                        rc_wifi,
                        'wait':
                        wait.total_seconds(),
                        'latency_1': (diff_1.total_seconds() / 2),
                        'latency_2':
                        (wait.total_seconds() + (diff_2.total_seconds() / 2))
                    }
                })
            else:
                print('packet_lost: ', k)

        if 'receive_ble' in word_info and 'send_wifi' in word_info:
            double_set.add(int(k))
            info_statistic['double_sent'] += 1
        word_info.clear()

    if len(double) != 0:
        for k in double:
            print(hash_json_data[k])
        raise Exception(
            'Indici Duplicati, esempio R,34,2 - R,34,1 --> Rimuovere R,34,2')

    if len(error_1) > 0 or len(error_2) > 0:
        raise Exception('\x1b[1;31;40m' + ' Error TTL: sent: ' +
                        str(len(error_1)) + ' --> received: ' +
                        str(len(error_2)) + ' \x1b[0m')

    print("Media DIff BLE: {}s".format(statistics.mean(diff_ble)))
    print("Media Latency BLE: {}s".format(statistics.mean(latency_ble)))
    print("Media Diff WIFI: {}s".format(statistics.mean(diff_wifi)))
    print("Media Latency WIFI: {}s".format(statistics.mean(latency_wifi)))
    print("Media Waiting BLE -> WIFI: {}s".format(statistics.mean(list_wait)))

    info_statistic['valid_mex'] = info_statistic[
        'sent_received'] - info_statistic['double_sent']
    print("Total Send -> [{}]".format(data['_command']['n_mex']))
    print("S (ble) -> {}".format(info_statistic['send_ble']))
    print("R (ble) -> {}".format(info_statistic['receive_ble']))
    print("E (ble) -> {}".format(info_statistic['error_ble']))  # not_sent_ble
    print("Lost BLE: {}".format(info_statistic['lost_ble']))
    print("I (wifi) -> {}".format(info_statistic['send_wifi']))
    print("O (wifi) -> {}".format(info_statistic['receive_wifi']))
    print("W (wifi) -> {}".format(
        info_statistic['error_wifi']))  # not_sent_wifi
    print("Lost WiFi: {}".format(info_statistic['lost_wifi']))
    print("Double Sent -> {}".format(info_statistic['double_sent']))
    print("Sent & Received -> {}".format(info_statistic['sent_received']))
    print("Valid Mex -> {}".format(info_statistic['valid_mex']))

    print("Intesezione: Received BLE Sent Wifi {}".format(
        received_ble_set.intersection(sent_wifi_set)))
    print("double: {}".format(double_set))

    my_data_2 = dict()
    my_data_2['_command'] = my_data['_command']
    my_data_2['_time'] = my_data['_time']
    my_data_2['_status'] = my_data['_status']
    my_data_2['_mex'] = hash_json_data
    print("Saving Analysis File")
    my.save_json_data_elegant(path=analysis_path, data=my_data_2)
Esempio n. 8
0
def call_fourth_analysis_and_save(path_s):
    data = my.open_file_and_return_data(code=0, path=path_2)
    data = clean_data(data=data, approach="packets")  # packets or time
    my.save_json_data_elegant(path=path_s, data=data)
Esempio n. 9
0
def call_third_analysis_and_save(path_s):
    data = my.open_file_and_return_data(code=0, path=path_2)
    data = third_analysis(data=data)
    my.save_json_data_elegant(path=path_s, data=data)
Esempio n. 10
0
def call_second_analysis_and_save(path_s):
    data = my.open_file_and_return_data(code=0, path=path_1)
    data, users = second_analysis(data=data)
    my.save_json_data_elegant(path=path_s, data=data)
Esempio n. 11
0
def call_error_resolution_and_save(path_s):
    data = my.open_file_and_return_data(code=0, path=path_1)
    data = resolve_errors_preprocessing(data=data)
    my.save_json_data_elegant(path=path_s, data=data)
Esempio n. 12
0
def call_preprocessing_and_save(path, path_1):
    my_data = preprocessing(path=path)
    my.save_json_data_elegant(path=path_1, data=my_data)