martaczaska

array_decoder_new_version

May 23rd, 2021 (edited)
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.85 KB | None | 0 0
  1. import math
  2. from Agregates import arrays_aggregates
  3. from Agregates import message_aggregates
  4. from Decoder import decode_fragment
  5.  
  6.  
  7. # specyficzne typy wiadomości
  8. need_adjustment = ["8_6", "24"] #chyba wsio?
  9. has_array = ['6_2', '6_7', '6_8', '6_9', '6_11', '7', '8_4', '8_7', '8_9', '8_10', '8_15', '20']
  10. shorter_than_expected = ["16", "17", "14", "15"] #czy na pewno jeszzce to 14 i 15?
  11.  
  12. #####
  13. ##chyba ten słownik z polami "type" i "payload" to types = {"type": type_dec, "message": payload}
  14. #####
  15. aggregates_dictionary = {
  16.             '1': message_aggregates.messageType_1,
  17.             '2': message_aggregates.messageType_2,
  18.             '3': message_aggregates.messageType_3,
  19.             '4': message_aggregates.messageType_4,
  20.             '5': message_aggregates.messageType_5,
  21.             '6_1': message_aggregates.messageType_6_1,
  22.             '6_2': message_aggregates.messageType_6_2,
  23.             '6_3': message_aggregates.messageType_6_3,
  24.             '6_4': message_aggregates.messageType_6_4,
  25.             '6_5': message_aggregates.messageType_6_5,
  26.             '6_6': message_aggregates.messageType_6_6,
  27.             '6_7': message_aggregates.messageType_6_7,
  28.             '6_8': message_aggregates.messageType_6_8,
  29.             '6_9': message_aggregates.messageType_6_9,
  30.             '6_10': message_aggregates.messageType_6_10,
  31.             '6_11': message_aggregates.messageType_6_11,
  32.             '6_12': message_aggregates.messageType_6_12,
  33.             '6_13': message_aggregates.messageType_6_13,
  34.             '6_14': message_aggregates.messageType_6_14,
  35.             '6_15': message_aggregates.messageType_6_15,
  36.             '7': message_aggregates.messageType_7,
  37.             '8_1': message_aggregates.messageType_8_1,
  38.             '8_2': message_aggregates.messageType_8_2,
  39.             '8_3': message_aggregates.messageType_8_3,
  40.             '8_4': message_aggregates.messageType_8_4,
  41.             '8_5': message_aggregates.messageType_8_5,
  42.             '8_6_1': message_aggregates.messageType_8_6_1,
  43.             '8_6_2': message_aggregates.messageType_8_6_2,
  44.             '8_7': message_aggregates.messageType_8_7,
  45.             '8_8': message_aggregates.messageType_8_8,
  46.             '8_9': message_aggregates.messageType_8_9,
  47.             '8_10': message_aggregates.messageType_8_10,
  48.             '8_11': message_aggregates.messageType_8_11,
  49.             '8_12': message_aggregates.messageType_8_12,
  50.             '8_13': message_aggregates.messageType_8_13,
  51.             '8_14': message_aggregates.messageType_8_14,
  52.             '8_15': message_aggregates.messageType_8_15,
  53.             '8_16': message_aggregates.messageType_8_16,
  54.             '9': message_aggregates.messageType_9,
  55.             '10': message_aggregates.messageType_10,
  56.             '11': message_aggregates.messageType_4,
  57.             '12': message_aggregates.messageType_12,
  58.             '13': message_aggregates.messageType_13,
  59.             '14': message_aggregates.messageType_14,
  60.             '15': message_aggregates.messageType_15,
  61.             '16': message_aggregates.messageType_16,
  62.             '17': message_aggregates.messageType_17,
  63.             '18': message_aggregates.messageType_18,
  64.             '19': message_aggregates.messageType_19,
  65.             '20': message_aggregates.messageType_20,
  66.             '21': message_aggregates.messageType_21,
  67.             '22': message_aggregates.messageType_22,
  68.             '23': message_aggregates.messageType_23,
  69.             '24_A': message_aggregates.messageType_24_A,
  70.             '24_B': message_aggregates.messageType_24_B,
  71.             '25': message_aggregates.messageType_25,
  72.             '26': message_aggregates.messageType_26,
  73.             '27': message_aggregates.messageType_27
  74.         }
  75.  
  76. def decode_message(types):                # funkcja decode_message wywoływana w mainie, dostajemy słownik z polami "type" i "payload"(czyli wiadomość)
  77.     if types["type"] in need_adjustment:
  78.         adjust_message(types)
  79.  
  80.     mainfields_bitstrings = split_to_fields(types)                  #to zwraca tablicę, czy mainfields_bitstrings nie trzeba zdefiniować jako tablicę?
  81.     dataParts = decode_bitstrings(mainfields_bitstrings, types)
  82.  
  83.     #pokombinować z tym shorter_than_expected
  84.  
  85.     if types["type"] in has_array:
  86.         bin_array = extract_array(types)                            #to samo pytanie co wyżej w komie
  87.         fields_array = decode_array(bin_array, types)
  88.         decoded_message = aggregates_dictionary[types["type"]](dataParts, fields_array)
  89.     else:
  90.         decoded_message = aggregates_dictionary[types["type"]](dataParts)
  91.  
  92.  
  93. def adjust_message(types):
  94.     # if-else po typach i odpowiednie dostosowanie
  95.     if types["type"] == '24':                                 #message["type"] == '24':
  96.         check_24 = int(types["message"][38:40])               #int(message["message"][38:40]) ?
  97.         if check_24 == 0:
  98.             types["type"] = '24_A'
  99.         if check_24 == 1:
  100.             types["type"] = '24_B'
  101.  
  102.     if types["type"] == '8_6':                                 #message["type"] == '8_6':
  103.         check_8_6 = int(types["message"][38:40])               #to jest przekopiowane z 24, zmienić z tym polem wmo
  104.         if check_8_6 == 0:                                     #pole (bitu) wmo = 0?
  105.             types["type"] = '8_6_1'
  106.         if check_8_6 == 1:                                     #pole (bitu) wmo = 1?
  107.             types["type"] = '8_6_2'
  108.  
  109.  
  110. # wykorzystanie krotki z długościami - podzielenie długiego ciągu na kilka podciągów
  111. def split_to_fields(types):
  112.     i = 0
  113.     position = 0
  114.     fragment = []
  115.     field_count = len(message_aggregates.datapartsBitlengths[types["type"]])
  116.  
  117.     while i < field_count:
  118.         field_bits = message_aggregates.datapartsBitlengths[types["type"]][i]
  119.         fragment[i] = types["message"][position:(field_bits + position)]
  120.  
  121.         position += field_bits
  122.         i += 1
  123.     return fragment
  124.  
  125.  
  126. # wykorzystaj krotkę z typami danych, którą? to powie parametr type
  127. def decode_bitstrings(fragment, types):
  128.     i = 0
  129.     position = 0
  130.     dataParts = []
  131.     field_count = len(message_aggregates.datapartsBitlengths[types["type"]])
  132.  
  133.     while i < field_count:
  134.         field_bits = message_aggregates.datapartsBitlengths[types["type"]][i]
  135.         field_type = message_aggregates.datapartsTypes[types["type"]][i]
  136.         one_decoded_field = decode_one_field(field_type, field_bits, fragment, types, position)
  137.         dataParts.append(one_decoded_field)
  138.         position += field_bits
  139.         i += 1
  140.  
  141.     return dataParts
  142.  
  143.  
  144. 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'
  145.     decoded_field = 0
  146.     type_dictionary = {
  147.         'u': decode_fragment.decode_u_int,
  148.         'e': decode_fragment.decode_u_int,
  149.         'b': decode_fragment.decode_u_int,
  150.         'x': "empty",  # ?
  151.         't': decode_fragment.decode_t,
  152.         's': decode_fragment.decode_s_int,
  153.         'I1': decode_fragment.decode_IX,
  154.         'I2': decode_fragment.decode_IX,
  155.         'I3': decode_fragment.decode_IX,
  156.         'I4': decode_fragment.decode_IX,
  157.         'U1': decode_fragment.decode_UX,
  158.         'U2': decode_fragment.decode_UX,
  159.         'd': types["message"][position:(field_bits + position)]  # zmienić?
  160.     }
  161.  
  162.     if field_type == 'u' or 'e' or 'b' or 't':
  163.         decoded_field = type_dictionary[field_type](fragment)
  164.     if field_type == 'x':
  165.         decoded_field = "empty"
  166.     if field_type == 's':
  167.         decoded_field = type_dictionary[field_type](fragment, field_bits)
  168.     if field_type == 'I1':
  169.         decoded_field = type_dictionary[field_type](fragment, field_bits, 1)
  170.     if field_type == 'I2':
  171.         decoded_field = type_dictionary[field_type](fragment, field_bits, 2)
  172.     if field_type == 'I3':
  173.         decoded_field = type_dictionary[field_type](fragment, field_bits, 3)
  174.     if field_type == 'I4':
  175.         decoded_field = type_dictionary[field_type](fragment, field_bits, 4)
  176.     if field_type == 'U1':
  177.         decoded_field = type_dictionary[field_type](fragment, 1)
  178.     if field_type == 'U2':
  179.         decoded_field = type_dictionary[field_type](fragment, 2)
  180.     if field_type == 'd':
  181.         decoded_field = type_dictionary[field_type]
  182.  
  183.     return decoded_field
  184.  
  185.  
  186. array_dictionary = {
  187.         '6_2': 1,       '6_7': 29,
  188.         '6_8': 10,      '6_9': 11,
  189.         '6_11': 12,     '7': 13,
  190.         '8_4': 14,      '8_7': 29,
  191.         '8_9': 15,      '8_10': 11,
  192.         '8_15': 27,     '20': 28
  193. }
  194.  
  195. # wykorzystaj krotkę z długościami pól "nietablicowych" (ich sumaryczną długość) wykorzystaj typ wiadomości, np.
  196. def extract_array(types):
  197.     # 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!
  198.     i = 0
  199.     extracted_array = []
  200.     type = types["type"]
  201.     nr_of_array = array_dictionary[type]
  202.     offset = sum(list(message_aggregates.datapartsBitlengths[type]))
  203.     counter = offset                                                            # counter = offset+1 ??, bo offset to ostatnia wartość bitu pól nietablicowych <chyba>
  204.     bits_per_element_of_array = sum(arrays_aggregates.arraysDataBitlengths[nr_of_array]) #też z sum(list()) ?
  205.     length_of_array = len(types["message"]) - offset                            #różnica całości i części nietablicowej
  206.  
  207.     while counter < length_of_array:                                            # < (length_of_array+1) ??
  208.         extracted_array[i] = types["message"][counter:(counter + bits_per_element_of_array)]
  209.         counter += bits_per_element_of_array
  210.         i += 1
  211.     return extracted_array
  212.  
  213.  
  214. report_dictionary = {
  215.         '0000': 16, '0001': 17, '0010': 18, '0011': 19, '0100': 20, '0101': 21, '0110': 22, '0111': 23, '1000': 24, '1001': 25, '1010': 26
  216.     }
  217.  
  218.  
  219. shape_dictionary = {
  220.     '000': 2, '001': 3, '010': 4, '011': 5, '100': 7, '101': 9
  221. }
  222.  
  223.  
  224. def decode_array(bin_array, types):
  225.     i = 0
  226.     k = 0
  227.     position = 0
  228.     decoded = []
  229.  
  230.     type = array_dictionary[types["type"]]
  231.     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':
  232.         field_count = len(arrays_aggregates.arraysDataBitlengths[type])
  233.         decoded = field_of_array(i, field_count, type, bin_array, position)
  234.     if types["type"] == '8_9':
  235.         field_count = len(arrays_aggregates.arraysDataBitlengths[type])
  236.         for n in range(len(bin_array-1)):
  237.             if i == 0:
  238.                 recognized_type = recognize_what_type(4, bin_array, position, dict)
  239.             position += 4
  240.             decoded = field_of_array(i, field_count, type, bin_array, position)
  241.         field_report_type_count = len(arrays_aggregates.arraysDataBitlengths[recognized_type]) + 1
  242.         decoded = field_of_array(k, field_report_type_count, type, bin_array, position)
  243.     if types["type"] == '6_7' or '8_7':
  244.         #to już zrobię jutro, bo nie mam siły ;__;
  245.         pass
  246.  
  247.     return decoded
  248.  
  249.  
  250. def recognize_what_type(numb_of_type_bits, message, position, dict):
  251.     value = ""
  252.     j = 0
  253.     while j < numb_of_type_bits:
  254.         value += str(message[position])   #tu problem, bo bin_array[position] to będzie ciąg bitów, poprawić potem
  255.         position += 1
  256.     recognized_type = dict[value]
  257.     number_of_fields = len(arrays_aggregates.arraysDataBitlengths[recognized_type])
  258.  
  259.     return recognized_type, number_of_fields
  260.  
  261.  
  262. def field_of_array(n, f_num, type, mess, pos): # endgame_array):
  263.     endgame_array = []
  264.     while n < f_num:
  265.         field_bits = arrays_aggregates.arraysDataBitlengths[type][n]
  266.         field_type = arrays_aggregates.arraysDataTypes[type][n]
  267.         #fragment = mess[pos:(field_bits + pos)]
  268.         fragment = mess[n]
  269.         endgame_array.append(decode_one_field(field_type, field_bits, fragment))
  270.         pos += field_bits
  271.         n += 1
  272.  
  273.     return endgame_array
Add Comment
Please, Sign In to add comment