Advertisement
martaczaska

array_decoder_new_version ver 2.

May 25th, 2021
1,421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.26 KB | None | 0 0
  1. from Aggregates import arrays_aggregates
  2. from Aggregates import message_aggregates
  3. from Decoder import dictionaries
  4.  
  5. # specyficzne typy wiadomości
  6. need_adjustment = ["8_6", "24", "25", "26"] #chyba wsio?
  7. has_array = ['6_2', '6_7', '6_8', '6_9', '6_11', '7', '8_4', '8_7', '8_9', '8_10', '8_15', '20']
  8. shorter_than_expected = ["15", "16"]
  9.  
  10.  
  11. def decode_message(message):                # funkcja decode_message wywoływana w mainie, dostajemy słownik z polami "type" i "payload"(czyli wiadomość)
  12.     if message["type"] in need_adjustment:
  13.         adjust_message(message)
  14.  
  15.     mainfields_bitstrings = split_to_fields(message)                  #to zwraca tablicę, czy mainfields_bitstrings nie trzeba zdefiniować jako tablicę?
  16.     dataParts = decode_bitstrings(mainfields_bitstrings, message)
  17.  
  18.     if message["type"] in has_array:
  19.         bin_array = extract_array(message)                            #to samo pytanie co wyżej w komie
  20.         fields_array = decode_array(bin_array, message)
  21.         decoded_message = dictionaries.msg_type_dictionary(message["type"])(dataParts, fields_array)
  22.     else:
  23.         decoded_message = dictionaries.msg_type_dictionary(message["type"])(dataParts)
  24.  
  25.     return decoded_message
  26.  
  27.  
  28. def adjust_message(message):      # if-else po typach i odpowiednie dostosowanie
  29.     if message["type"] == '24':
  30.         check_24 = int(message["message"][38:40])
  31.         if check_24 == 0:
  32.             message["type"] = '24_A'
  33.         if check_24 == 1:
  34.             message["type"] = '24_B'
  35.  
  36.     if message["type"] == '25' or '26':
  37.         flags = message[38:40]          # pole flag adressed i structured
  38.         message["type"] = str(message["type"] + '_' + flags)
  39.  
  40.     ##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
  41.  
  42.     if message["type"] == '8_6':
  43.         check_8_6 = int(message["message"][56])                  #56 - pole wmo
  44.         if check_8_6 == 0:                                     #pole (bitu) wmo = 0
  45.             message["type"] = '8_6_1'
  46.         if check_8_6 == 1:                                     #pole (bitu) wmo = 1
  47.             message["type"] = '8_6_2'
  48.  
  49.  
  50. def shorten_message(message):
  51.     nowa_krotka = 0 ###
  52.     if message["type"] == "15":
  53.         if len(message["message"]) == 88:
  54.             nowa_krotka = message_aggregates.datapartsBitlengths[message["type"][0:7]]
  55.         if len(message["message"]) == 110:
  56.             nowa_krotka = message_aggregates.datapartsBitlengths[message["type"][0:11]]
  57.         if len(message["message"]) == 160:
  58.             nowa_krotka = message_aggregates.datapartsBitlengths[message["type"]]
  59.  
  60.     if message["type"] == "16":
  61.         if len(message["message"]) == 92:
  62.             nowa_krotka = message_aggregates.datapartsBitlengths[message["type"][0:7]]
  63.         if len(message["message"]) == 140:
  64.             nowa_krotka = message_aggregates.datapartsBitlengths[message["type"]]
  65.  
  66.     return nowa_krotka
  67.  
  68. # wykorzystanie krotki z długościami - podzielenie długiego ciągu na kilka podciągów
  69. def split_to_fields(message):
  70.     i = 0
  71.     position = 0
  72.     fragment = []
  73.     if message["type"] in shorter_than_expected:
  74.         field_count = len(shorten_message(message))
  75.     else:
  76.         field_count = len(message_aggregates.datapartsBitlengths[message["type"]])
  77.  
  78.     while i < field_count:
  79.         field_bits = message_aggregates.datapartsBitlengths[message["type"]][i]
  80.         fragment.append(message["message"][position:(field_bits + position)])
  81.         position += field_bits
  82.         i += 1
  83.     return fragment
  84.  
  85.  
  86. # wykorzystaj krotkę z typami danych, którą? to powie parametr type
  87. def decode_bitstrings(bit_strings, message):
  88.     i = 0
  89.     position = 0
  90.     dataParts = []
  91.     #field_count = len(message_aggregates.datapartsBitlengths[message["type"]])
  92.  
  93.     if message["type"] in shorter_than_expected:
  94.         field_count = len(shorten_message(message))
  95.     else:
  96.         field_count = len(message_aggregates.datapartsBitlengths[message["type"]])
  97.  
  98.     while i < field_count:
  99.         field_bits = message_aggregates.datapartsBitlengths[message["type"]][i]
  100.         field_type = message_aggregates.datapartsTypes[message["type"]][i]
  101.  
  102.     ##########################################################################
  103.         if message["type"] != 'x':
  104.             one_decoded_field = dictionaries.field_type_dictionary(bit_strings[i], field_type)
  105.             dataParts.append(one_decoded_field)
  106.  
  107.         one_decoded_field = dictionaries.field_type_dictionary(bit_strings[i], field_type)
  108.         dataParts.append(one_decoded_field)
  109.         position += field_bits
  110.         i += 1
  111.  
  112.     return dataParts
  113.     ##########################################################################
  114.  
  115.  
  116. # wykorzystaj krotkę z długościami pól "nietablicowych" (ich sumaryczną długość) wykorzystaj typ wiadomości, np.
  117. def extract_array(message):
  118.     # 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!
  119.     i = 0
  120.     extracted_array = []
  121.     type = message["type"]
  122.     nr_of_array = dictionaries.array_dictionary[type]
  123.     offset = sum(list(message_aggregates.datapartsBitlengths[type]))
  124.     counter = offset + 1                                             # +1 bo offset to ostatnia wartość bitu pól nietablicowych
  125.     bits_per_element_of_array = sum(arrays_aggregates.arraysDataBitlengths[nr_of_array]) #też z sum(list()) ?
  126.     length_of_array = len(message["message"]) - offset                            #różnica całości i części nietablicowej
  127.  
  128.     while counter < length_of_array:                                            # < (length_of_array+1) ??
  129.         extracted_array[i] = message["message"][counter:(counter + bits_per_element_of_array)]
  130.         counter += bits_per_element_of_array
  131.         i += 1
  132.     return extracted_array
  133.  
  134.  
  135. def decode_array(bin_array, message):
  136.     i = 0
  137.     k = 0
  138.     l = 0
  139.     position = 0
  140.     decoded = []
  141.  
  142.     type = dictionaries.array_dictionary[message["type"]]
  143.     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':
  144.         field_count = len(arrays_aggregates.arraysDataBitlengths[type])
  145.         while k < field_count:
  146.             decoded_part = field_of_array(type, bin_array, k)
  147.             decoded.append(decoded_part[0])
  148.  
  149.     if message["type"] == '8_9':
  150.         field_count = len(arrays_aggregates.arraysDataBitlengths[type])
  151.  
  152.         while i < len(bin_array - 1):
  153.             while k < (field_count - 1):
  154.                 if k == 0:
  155.                     type_of_report = recognize_what_type(4, bin_array, k, position, type)       #position += 4
  156.  
  157.                 decoded_part = field_of_array(type, bin_array, k)
  158.                 decoded.append(decoded_part[0])         #position += decoded_part[1]
  159.                 k += 1
  160.  
  161.             field_report_type_count = len(arrays_aggregates.arraysDataBitlengths[type_of_report])  #dla ostatniego pola
  162.             while l < (field_report_type_count - 1):
  163.                 decoded_part = field_of_array(type_of_report, bin_array, k)
  164.                 decoded.append(decoded_part[0])         #position += decoded_part[1]
  165.                 l += 1
  166.             i += 1
  167.  
  168.     if message["type"] == '6_7' or '8_7':
  169.         while i < len(bin_array - 1):
  170.             type_of_shape = recognize_what_type(3, bin_array, i, position, type)
  171.             field_count = len(arrays_aggregates.arraysDataBitlengths[type_of_shape])
  172.             while k < (field_count - 1):
  173.                 decoded_part = field_of_array(type_of_shape, bin_array, k)
  174.                 decoded.append(decoded_part[0])
  175.  
  176.                 if type_of_shape == 5:
  177.                     for m in range(4):
  178.                         while l < 2:
  179.                             decoded_part = field_of_array((type_of_shape+1), bin_array, k)
  180.                             decoded.append(decoded_part[0])
  181.                             l += 1
  182.  
  183.                 if type_of_shape == 5:
  184.                     for m in range(4):
  185.                         while l < 2:
  186.                             decoded_part = field_of_array((type_of_shape + 1), bin_array, k)
  187.                             decoded.append(decoded_part[0])
  188.                             l += 1
  189.                 k += 1
  190.             i += 0
  191.  
  192.  
  193.     return decoded
  194.  
  195.  
  196. def recognize_what_type(numb_of_type_bits, array, l, position, type):
  197.     value = ""
  198.     j = 0
  199.     message = array[l]
  200.     while j < numb_of_type_bits:
  201.         value += str(message[position])
  202.         position += 1
  203.         j += 1
  204.  
  205.     if type == '8_9':
  206.         recognized_type = dictionaries.report_dictionary[value]
  207.     if type == '6_7' or '8_7':
  208.         recognized_type = dictionaries.shape_dictionary[value]
  209.  
  210.     return recognized_type
  211.  
  212.  
  213. def field_of_array(type, mess, n):
  214.     field_bits = arrays_aggregates.arraysDataBitlengths[type][n]
  215.     field_type = arrays_aggregates.arraysDataTypes[type][n]
  216.     fragment = mess[n]
  217.     endgame_field = dictionaries.field_type_dictionary(fragment, field_type)
  218.     pack = [field_bits, endgame_field]              #nie wiem czy jest sens z tym field bits, zastanowię się jak będę przytomna
  219.  
  220.     return pack
  221.  
  222. # def special_type_25()
  223. #     pass
  224. #
  225. # def special_type_26()
  226. #     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement