Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from Agregates import arrays_aggregates
- from Agregates import message_aggregates
- from Decoder import decode_fragment
- # specyficzne typy wiadomości
- need_adjustment = ["8_6", "24"] #chyba wsio?
- has_array = ['6_2', '6_7', '6_8', '6_9', '6_11', '7', '8_4', '8_7', '8_9', '8_10', '8_15', '20']
- shorter_than_expected = ["16", "17", "14", "15"] #czy na pewno jeszzce to 14 i 15?
- #####
- ##chyba ten słownik z polami "type" i "payload" to types = {"type": type_dec, "message": payload}
- #####
- aggregates_dictionary = {
- '1': message_aggregates.messageType_1,
- '2': message_aggregates.messageType_2,
- '3': message_aggregates.messageType_3,
- '4': message_aggregates.messageType_4,
- '5': message_aggregates.messageType_5,
- '6_1': message_aggregates.messageType_6_1,
- '6_2': message_aggregates.messageType_6_2,
- '6_3': message_aggregates.messageType_6_3,
- '6_4': message_aggregates.messageType_6_4,
- '6_5': message_aggregates.messageType_6_5,
- '6_6': message_aggregates.messageType_6_6,
- '6_7': message_aggregates.messageType_6_7,
- '6_8': message_aggregates.messageType_6_8,
- '6_9': message_aggregates.messageType_6_9,
- '6_10': message_aggregates.messageType_6_10,
- '6_11': message_aggregates.messageType_6_11,
- '6_12': message_aggregates.messageType_6_12,
- '6_13': message_aggregates.messageType_6_13,
- '6_14': message_aggregates.messageType_6_14,
- '6_15': message_aggregates.messageType_6_15,
- '7': message_aggregates.messageType_7,
- '8_1': message_aggregates.messageType_8_1,
- '8_2': message_aggregates.messageType_8_2,
- '8_3': message_aggregates.messageType_8_3,
- '8_4': message_aggregates.messageType_8_4,
- '8_5': message_aggregates.messageType_8_5,
- '8_6_1': message_aggregates.messageType_8_6_1,
- '8_6_2': message_aggregates.messageType_8_6_2,
- '8_7': message_aggregates.messageType_8_7,
- '8_8': message_aggregates.messageType_8_8,
- '8_9': message_aggregates.messageType_8_9,
- '8_10': message_aggregates.messageType_8_10,
- '8_11': message_aggregates.messageType_8_11,
- '8_12': message_aggregates.messageType_8_12,
- '8_13': message_aggregates.messageType_8_13,
- '8_14': message_aggregates.messageType_8_14,
- '8_15': message_aggregates.messageType_8_15,
- '8_16': message_aggregates.messageType_8_16,
- '9': message_aggregates.messageType_9,
- '10': message_aggregates.messageType_10,
- '11': message_aggregates.messageType_4,
- '12': message_aggregates.messageType_12,
- '13': message_aggregates.messageType_13,
- '14': message_aggregates.messageType_14,
- '15': message_aggregates.messageType_15,
- '16': message_aggregates.messageType_16,
- '17': message_aggregates.messageType_17,
- '18': message_aggregates.messageType_18,
- '19': message_aggregates.messageType_19,
- '20': message_aggregates.messageType_20,
- '21': message_aggregates.messageType_21,
- '22': message_aggregates.messageType_22,
- '23': message_aggregates.messageType_23,
- '24_A': message_aggregates.messageType_24_A,
- '24_B': message_aggregates.messageType_24_B,
- '25': message_aggregates.messageType_25,
- '26': message_aggregates.messageType_26,
- '27': message_aggregates.messageType_27
- }
- def decode_message(types): # funkcja decode_message wywoływana w mainie, dostajemy słownik z polami "type" i "payload"(czyli wiadomość)
- if types["type"] in need_adjustment:
- adjust_message(types)
- mainfields_bitstrings = split_to_fields(types) #to zwraca tablicę, czy mainfields_bitstrings nie trzeba zdefiniować jako tablicę?
- dataParts = decode_bitstrings(mainfields_bitstrings, types)
- #pokombinować z tym shorter_than_expected
- if types["type"] in has_array:
- bin_array = extract_array(types) #to samo pytanie co wyżej w komie
- fields_array = decode_array(bin_array, types)
- decoded_message = aggregates_dictionary[types["type"]](dataParts, fields_array)
- else:
- decoded_message = aggregates_dictionary[types["type"]](dataParts)
- def adjust_message(types):
- # if-else po typach i odpowiednie dostosowanie
- if types["type"] == '24': #message["type"] == '24':
- check_24 = int(types["message"][38:40]) #int(message["message"][38:40]) ?
- if check_24 == 0:
- types["type"] = '24_A'
- if check_24 == 1:
- types["type"] = '24_B'
- if types["type"] == '8_6': #message["type"] == '8_6':
- check_8_6 = int(types["message"][38:40]) #to jest przekopiowane z 24, zmienić z tym polem wmo
- if check_8_6 == 0: #pole (bitu) wmo = 0?
- types["type"] = '8_6_1'
- if check_8_6 == 1: #pole (bitu) wmo = 1?
- types["type"] = '8_6_2'
- # wykorzystanie krotki z długościami - podzielenie długiego ciągu na kilka podciągów
- def split_to_fields(types):
- i = 0
- position = 0
- fragment = []
- field_count = len(message_aggregates.datapartsBitlengths[types["type"]])
- while i < field_count:
- field_bits = message_aggregates.datapartsBitlengths[types["type"]][i]
- fragment[i] = types["message"][position:(field_bits + position)]
- position += field_bits
- i += 1
- return fragment
- # wykorzystaj krotkę z typami danych, którą? to powie parametr type
- def decode_bitstrings(fragment, types):
- i = 0
- position = 0
- dataParts = []
- field_count = len(message_aggregates.datapartsBitlengths[types["type"]])
- while i < field_count:
- field_bits = message_aggregates.datapartsBitlengths[types["type"]][i]
- field_type = message_aggregates.datapartsTypes[types["type"]][i]
- one_decoded_field = decode_one_field(field_type, field_bits, fragment, types, position)
- dataParts.append(one_decoded_field)
- position += field_bits
- i += 1
- return dataParts
- def decode_one_field(field_type, field_bits, fragment, types, position): #dodaję types i position, żeby nie wywalało, ale nie wiem czy nie trzeba zmienić 'd'
- decoded_field = 0
- type_dictionary = {
- 'u': decode_fragment.decode_u_int,
- 'e': decode_fragment.decode_u_int,
- 'b': decode_fragment.decode_u_int,
- 'x': "empty", # ?
- 't': decode_fragment.decode_t,
- 's': decode_fragment.decode_s_int,
- 'I1': decode_fragment.decode_IX,
- 'I2': decode_fragment.decode_IX,
- 'I3': decode_fragment.decode_IX,
- 'I4': decode_fragment.decode_IX,
- 'U1': decode_fragment.decode_UX,
- 'U2': decode_fragment.decode_UX,
- 'd': types["message"][position:(field_bits + position)] # zmienić?
- }
- if field_type == 'u' or 'e' or 'b' or 't':
- decoded_field = type_dictionary[field_type](fragment)
- if field_type == 'x':
- decoded_field = "empty"
- if field_type == 's':
- decoded_field = type_dictionary[field_type](fragment, field_bits)
- if field_type == 'I1':
- decoded_field = type_dictionary[field_type](fragment, field_bits, 1)
- if field_type == 'I2':
- decoded_field = type_dictionary[field_type](fragment, field_bits, 2)
- if field_type == 'I3':
- decoded_field = type_dictionary[field_type](fragment, field_bits, 3)
- if field_type == 'I4':
- decoded_field = type_dictionary[field_type](fragment, field_bits, 4)
- if field_type == 'U1':
- decoded_field = type_dictionary[field_type](fragment, 1)
- if field_type == 'U2':
- decoded_field = type_dictionary[field_type](fragment, 2)
- if field_type == 'd':
- decoded_field = type_dictionary[field_type]
- return decoded_field
- array_dictionary = {
- '6_2': 1, '6_7': 29,
- '6_8': 10, '6_9': 11,
- '6_11': 12, '7': 13,
- '8_4': 14, '8_7': 29,
- '8_9': 15, '8_10': 11,
- '8_15': 27, '20': 28
- }
- # wykorzystaj krotkę z długościami pól "nietablicowych" (ich sumaryczną długość) wykorzystaj typ wiadomości, np.
- def extract_array(types):
- # hokus pokus wyciągnij tyle bitów ile trzeba, wyodrębnij bitowe elementy tablicy (np. po 17 bitów) to jeszcze nie jest podział na pola!
- i = 0
- extracted_array = []
- type = types["type"]
- nr_of_array = array_dictionary[type]
- offset = sum(list(message_aggregates.datapartsBitlengths[type]))
- counter = offset # counter = offset+1 ??, bo offset to ostatnia wartość bitu pól nietablicowych <chyba>
- bits_per_element_of_array = sum(arrays_aggregates.arraysDataBitlengths[nr_of_array]) #też z sum(list()) ?
- length_of_array = len(types["message"]) - offset #różnica całości i części nietablicowej
- while counter < length_of_array: # < (length_of_array+1) ??
- extracted_array[i] = types["message"][counter:(counter + bits_per_element_of_array)]
- counter += bits_per_element_of_array
- i += 1
- return extracted_array
- report_dictionary = {
- '0000': 16, '0001': 17, '0010': 18, '0011': 19, '0100': 20, '0101': 21, '0110': 22, '0111': 23, '1000': 24, '1001': 25, '1010': 26
- }
- shape_dictionary = {
- '000': 2, '001': 3, '010': 4, '011': 5, '100': 7, '101': 9
- }
- def decode_array(bin_array, types):
- i = 0
- k = 0
- position = 0
- decoded = []
- type = array_dictionary[types["type"]]
- if types["type"] == '6_2' or '6_8' or '6_9' or '6_11' or '7' or '8_4' or '8_10' or '8_15' or '20':
- field_count = len(arrays_aggregates.arraysDataBitlengths[type])
- decoded = field_of_array(i, field_count, type, bin_array, position)
- if types["type"] == '8_9':
- field_count = len(arrays_aggregates.arraysDataBitlengths[type])
- for n in range(len(bin_array-1)):
- if i == 0:
- recognized_type = recognize_what_type(4, bin_array, position, dict)
- position += 4
- decoded = field_of_array(i, field_count, type, bin_array, position)
- field_report_type_count = len(arrays_aggregates.arraysDataBitlengths[recognized_type]) + 1
- decoded = field_of_array(k, field_report_type_count, type, bin_array, position)
- if types["type"] == '6_7' or '8_7':
- #to już zrobię jutro, bo nie mam siły ;__;
- pass
- return decoded
- def recognize_what_type(numb_of_type_bits, message, position, dict):
- value = ""
- j = 0
- while j < numb_of_type_bits:
- value += str(message[position]) #tu problem, bo bin_array[position] to będzie ciąg bitów, poprawić potem
- position += 1
- recognized_type = dict[value]
- number_of_fields = len(arrays_aggregates.arraysDataBitlengths[recognized_type])
- return recognized_type, number_of_fields
- def field_of_array(n, f_num, type, mess, pos): # endgame_array):
- endgame_array = []
- while n < f_num:
- field_bits = arrays_aggregates.arraysDataBitlengths[type][n]
- field_type = arrays_aggregates.arraysDataTypes[type][n]
- #fragment = mess[pos:(field_bits + pos)]
- fragment = mess[n]
- endgame_array.append(decode_one_field(field_type, field_bits, fragment))
- pos += field_bits
- n += 1
- return endgame_array
Add Comment
Please, Sign In to add comment