def __parse(cls, trans): """ Parses the string data into a meaningful JSON block containing the command coming from the radio This function actually calls the responsible parser depending on the incoming command code :param data: A single transaction string coming from the radio that we have to parse to a meaningful JSON block :type data: str :return: JSON formatted block containing the parsed data :rtype: str """ logger.debug("input data: {0}".format(trans)) result_dic = None for s in cls.parsers: if trans.startswith(s): # if we have parser for the current command... fn = cls.parsers[s] result_dic = getattr(fn, '__func__')(cls, trans) # call the responsible parser break logger.debug(result_dic.__str__()) if result_dic is None: result_dic = dict() DecodedTransaction.insertNotSupported(result_dic, trans) result_json = DecodedTransaction.toJson(result_dic) logger.debug(result_json.__str__()) logger.debug("parsed result: {0}".format(result_json)) return result_json
def decode(cls, data): """ Extracts and decodes the first Icom command found within the supplied buffer. :param data: Series of bytes from which we must extract the incoming command. There is no guarantee that the first byte is the beginning of the transaction (i.e. there might be some trash in the beginning). :type data: array :return: Object containing the transaction and some additional control information :rtype: DecodedTransaction """ trans = bytearray(data) # Find the beginning of the transaction trans_start_index = trans.find(cls.TRANS_START) if trans_start_index == -1: return DecodedTransaction(None, 0) # Find the end of the transaction (must be after trans_start_index) trans_end_index = trans.find(cls.TRANS_END, trans_start_index) if trans_end_index == -1: return DecodedTransaction(None, 0) cmd_idx = trans_start_index + 4 # get the index of the command result_dic = dict() if trans[cmd_idx] == cls.CFM_POSITIVE: # <------------------------- positive confirm DecodedTransaction.insertPositiveCfm(result_dic) elif trans[cmd_idx] == cls.CFM_NEGATIVE: # <------------------------- negative confirm DecodedTransaction.insertNegativeCfm(result_dic) elif trans[cmd_idx] == cls.SEND_FREQ: # <------------------------- frequency freq = cls.__frequency_from_bcd_to_string(trans[(cmd_idx + 1):trans_end_index]) DecodedTransaction.insertFreq(result_dic, freq) elif trans[cmd_idx] == cls.SEND_MODE: # <------------------------- mode mode = cls.__mode_from_byte_to_string(trans[cmd_idx+1]) DecodedTransaction.insertMode(result_dic, mode) else: # <------------------------- not-supported DecodedTransaction.insertNotSupported(result_dic, misc_utils.getListInHex(trans[trans_start_index:trans_end_index+1])) # Convert to JSON string result_json = DecodedTransaction.toJson(result_dic) logger.debug("input bytes: {0}".format(misc_utils.getListInHex(bytearray(data)))) logger.debug("returns: {0}; bytes removed: {1}".format(result_json, trans_end_index+1)) # return the object with the decoded transaction and the amount of bytes that we have read from the supplied buffer(string) return DecodedTransaction(result_json, trans_end_index+1)