# Imports - Faraday Specific from faraday.proxyio import faradaybasicproxyio from faraday.proxyio import faradaycommands from faraday.proxyio import telemetryparser #Variables local_device_callsign = 'REPLACEME' # Should match the connected Faraday unit as assigned in Proxy configuration local_device_node_id = 0 # Should match the connected Faraday unit as assigned in Proxy configuration #Start the proxy server after configuring the configuration file correctly #Setup a Faraday IO object faraday_1 = faradaybasicproxyio.proxyio() faraday_cmd = faradaycommands.faraday_commands() faraday_parser = telemetryparser.TelemetryParse() ############ ## Telemetry ############ def get_telemetry(): #Flush old data from UART service port faraday_1.FlushRxPort(local_device_callsign, local_device_node_id, faraday_1.TELEMETRY_PORT) #Command UART Telemetry Update NOW faraday_1.POST(local_device_callsign, local_device_node_id, faraday_1.CMD_UART_PORT, faraday_cmd.CommandLocalUARTFaradayTelemetry()) #Wait up to 1 second for the unit to respond to the command. NOTE: GETWait will return ALL packets received if more than 1 packet (likley not in THIS case) rx_telem_data = faraday_1.GETWait(local_device_callsign, local_device_node_id, faraday_1.TELEMETRY_PORT, 1) #Will block and wait for given time until a packet is recevied
def telemetry_worker(config): """ Interface Faraday Proxy to obtain telemetry This function interfaces the Proxy application via its RESTful interface. It is a one-way operation as it makes no sense to POST data to proxy for telemetry to a specific unit with this application. This function runs in an infinite loop continually querying for data. :param config: telemetry.ini ConfigParser object :return: Nothing """ logger.info('Starting telemetry_worker thread') # initialize variables stations = {} count = 0 # Initialize proxy object proxy = faradaybasicproxyio.proxyio() # Initialize Faraday parser faradayParser = telemetryparser.TelemetryParse() # Add logger? try: # Pragmatically create descriptors for each Faraday connected to Proxy count = config.getint("TELEMETRY", "UNITS") except ConfigParser.Error as e: # Error reading in configs so get stuck in infinite loop indicating problem while True: logger.error("ConfigParse.Error: " + str(e)) time.sleep(1) for num in range(count): try: callsign = config.get("TELEMETRY", "UNIT" + str(num) + "CALL").upper() nodeid = config.get("TELEMETRY", "UNIT" + str(num) + "ID") proxyHost = str(config.get("TELEMETRY", "proxyhost")) except ConfigParser.Error as e: # Error reading in configs so get stuck in infinite loop indicating problem while True: logger.error("ConfigParse.Error: " + str(e)) time.sleep(1) stations["UNIT" + str(num) + "CALL"] = callsign stations["UNIT" + str(num) + "ID"] = nodeid telemetryDicts[str(callsign) + str(nodeid)] = deque([], maxlen=1000) # Check for data on telemetry port with infinite loop. while True: for radio in range(count): callsign = stations["UNIT" + str(num) + "CALL"] nodeid = stations["UNIT" + str(num) + "ID"] data = proxy.GET(proxyHost, str(callsign), str(nodeid), int(proxy.TELEMETRY_PORT)) if type(data) is dict: # A dict means something is wrong with GET, print error JSON logger.info(data["error"]) elif data is None: # No data is available from Proxy logger.debug("telemetryworker data GET response = None") else: # Iterate through each packet and unpack into dictionary logger.debug("Proxy data: " + repr(data)) for item in data: try: # Decode BASE64 JSON data packet into unPackedItem = proxy.DecodeRawPacket(item["data"]) # Unpack packet into datagram elements datagram = faradayParser.UnpackDatagram( unPackedItem, False) # Extract the payload length from payload since padding could be used telemetryData = faradayParser.ExtractPaddedPacket( datagram["PayloadData"], faradayParser.packet_3_len) # Unpack payload and return a dictionary of telemetry, return tuple and dictionary parsedTelemetry = faradayParser.UnpackPacket_3( telemetryData, False) except ValueError as e: logger.error("ValueError: " + str(e)) except IndexError as e: logger.error("IndexError: " + str(e)) except KeyError as e: logger.error("KeyError: " + str(e)) else: # Successful decode, open database and save telemetry workerDB = openDB() sqlInsert(workerDB, parsedTelemetry) telemetryDicts[str(callsign) + str(nodeid)].append(parsedTelemetry) time.sleep(1) # Slow down main while loop