Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Aggregates import arrays_aggregates
- from Aggregates import message_aggregates
- from Decoder import dictionaries
- # specyficzne typy wiadomości
- need_adjustment = ["8_6", "24", "25", "26"] #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 = ["15", "16"]
- def decode_message(message): # funkcja decode_message wywoływana w mainie, dostajemy słownik z polami "type" i "payload"(czyli wiadomość)
- if message["type"] in need_adjustment:
- adjust_message(message)
- mainfields_bitstrings = split_to_fields(message) #to zwraca tablicę, czy mainfields_bitstrings nie trzeba zdefiniować jako tablicę?
- dataParts = decode_bitstrings(mainfields_bitstrings, message)
- if message["type"] in has_array:
- bin_array = extract_array(message) #to samo pytanie co wyżej w komie
- fields_array = decode_array(bin_array, message)
- decoded_message = dictionaries.msg_type_dictionary(message["type"])(dataParts, fields_array)
- else:
- decoded_message = dictionaries.msg_type_dictionary(message["type"])(dataParts)
- return decoded_message
- def adjust_message(message): # if-else po typach i odpowiednie dostosowanie
- if message["type"] == '24':
- check_24 = int(message["message"][38:40])
- if check_24 == 0:
- message["type"] = '24_A'
- if check_24 == 1:
- message["type"] = '24_B'
- if message["type"] == '25' or '26':
- flags = message[38:40] # pole flag adressed i structured
- message["type"] = str(message["type"] + '_' + flags)
- ##TYP 26, trzeba dodać do wyjątków, sprawdzić ile ma ostatni element, a następnie wyekstraktować, ostatnie 20 bitów i append() do dataParts
- if message["type"] == '8_6':
- check_8_6 = int(message["message"][56]) #56 - pole wmo
- if check_8_6 == 0: #pole (bitu) wmo = 0
- message["type"] = '8_6_1'
- if check_8_6 == 1: #pole (bitu) wmo = 1
- message["type"] = '8_6_2'
- def shorten_message(message):
- nowa_krotka = 0 ###
- if message["type"] == "15":
- if len(message["message"]) == 88:
- nowa_krotka = message_aggregates.datapartsBitlengths[message["type"][0:7]]
- if len(message["message"]) == 110:
- nowa_krotka = message_aggregates.datapartsBitlengths[message["type"][0:11]]
- if len(message["message"]) == 160:
- nowa_krotka = message_aggregates.datapartsBitlengths[message["type"]]
- if message["type"] == "16":
- if len(message["message"]) == 92:
- nowa_krotka = message_aggregates.datapartsBitlengths[message["type"][0:7]]
- if len(message["message"]) == 140:
- nowa_krotka = message_aggregates.datapartsBitlengths[message["type"]]
- return nowa_krotka
- # wykorzystanie krotki z długościami - podzielenie długiego ciągu na kilka podciągów
- def split_to_fields(message):
- i = 0
- position = 0
- fragment = []
- if message["type"] in shorter_than_expected:
- field_count = len(shorten_message(message))
- else:
- field_count = len(message_aggregates.datapartsBitlengths[message["type"]])
- while i < field_count:
- field_bits = message_aggregates.datapartsBitlengths[message["type"]][i]
- fragment.append(message["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(bit_strings, message):
- i = 0
- position = 0
- dataParts = []
- #field_count = len(message_aggregates.datapartsBitlengths[message["type"]])
- if message["type"] in shorter_than_expected:
- field_count = len(shorten_message(message))
- else:
- field_count = len(message_aggregates.datapartsBitlengths[message["type"]])
- while i < field_count:
- field_bits = message_aggregates.datapartsBitlengths[message["type"]][i]
- field_type = message_aggregates.datapartsTypes[message["type"]][i]
- ##########################################################################
- if message["type"] != 'x':
- one_decoded_field = dictionaries.field_type_dictionary(bit_strings[i], field_type)
- dataParts.append(one_decoded_field)
- one_decoded_field = dictionaries.field_type_dictionary(bit_strings[i], field_type)
- dataParts.append(one_decoded_field)
- position += field_bits
- i += 1
- return dataParts
- ##########################################################################
- # wykorzystaj krotkę z długościami pól "nietablicowych" (ich sumaryczną długość) wykorzystaj typ wiadomości, np.
- def extract_array(message):
- # 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 = message["type"]
- nr_of_array = dictionaries.array_dictionary[type]
- offset = sum(list(message_aggregates.datapartsBitlengths[type]))
- counter = offset + 1 # +1 bo offset to ostatnia wartość bitu pól nietablicowych
- bits_per_element_of_array = sum(arrays_aggregates.arraysDataBitlengths[nr_of_array]) #też z sum(list()) ?
- length_of_array = len(message["message"]) - offset #różnica całości i części nietablicowej
- while counter < length_of_array: # < (length_of_array+1) ??
- extracted_array[i] = message["message"][counter:(counter + bits_per_element_of_array)]
- counter += bits_per_element_of_array
- i += 1
- return extracted_array
- def decode_array(bin_array, message):
- i = 0
- k = 0
- l = 0
- position = 0
- decoded = []
- type = dictionaries.array_dictionary[message["type"]]
- if message["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])
- while k < field_count:
- decoded_part = field_of_array(type, bin_array, k)
- decoded.append(decoded_part[0])
- if message["type"] == '8_9':
- field_count = len(arrays_aggregates.arraysDataBitlengths[type])
- while i < len(bin_array - 1):
- while k < (field_count - 1):
- if k == 0:
- type_of_report = recognize_what_type(4, bin_array, k, position, type) #position += 4
- decoded_part = field_of_array(type, bin_array, k)
- decoded.append(decoded_part[0]) #position += decoded_part[1]
- k += 1
- field_report_type_count = len(arrays_aggregates.arraysDataBitlengths[type_of_report]) #dla ostatniego pola
- while l < (field_report_type_count - 1):
- decoded_part = field_of_array(type_of_report, bin_array, k)
- decoded.append(decoded_part[0]) #position += decoded_part[1]
- l += 1
- i += 1
- if message["type"] == '6_7' or '8_7':
- while i < len(bin_array - 1):
- type_of_shape = recognize_what_type(3, bin_array, i, position, type)
- field_count = len(arrays_aggregates.arraysDataBitlengths[type_of_shape])
- while k < (field_count - 1):
- decoded_part = field_of_array(type_of_shape, bin_array, k)
- decoded.append(decoded_part[0])
- if type_of_shape == 5:
- for m in range(4):
- while l < 2:
- decoded_part = field_of_array((type_of_shape+1), bin_array, k)
- decoded.append(decoded_part[0])
- l += 1
- if type_of_shape == 5:
- for m in range(4):
- while l < 2:
- decoded_part = field_of_array((type_of_shape + 1), bin_array, k)
- decoded.append(decoded_part[0])
- l += 1
- k += 1
- i += 0
- return decoded
- def recognize_what_type(numb_of_type_bits, array, l, position, type):
- value = ""
- j = 0
- message = array[l]
- while j < numb_of_type_bits:
- value += str(message[position])
- position += 1
- j += 1
- if type == '8_9':
- recognized_type = dictionaries.report_dictionary[value]
- if type == '6_7' or '8_7':
- recognized_type = dictionaries.shape_dictionary[value]
- return recognized_type
- def field_of_array(type, mess, n):
- field_bits = arrays_aggregates.arraysDataBitlengths[type][n]
- field_type = arrays_aggregates.arraysDataTypes[type][n]
- fragment = mess[n]
- endgame_field = dictionaries.field_type_dictionary(fragment, field_type)
- pack = [field_bits, endgame_field] #nie wiem czy jest sens z tym field bits, zastanowię się jak będę przytomna
- return pack
- # def special_type_25()
- # pass
- #
- # def special_type_26()
- # pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement