from FaradayIO import faradaybasicproxyio from FaradayIO import faradaycommands from FaradayIO import telemetryparser from FaradayIO import cc430radioconfig import time #Variables local_device_callsign = 'REPLACEME' # Should match the connected Faraday unit as assigned in Proxy configuration local_device_node_id = REPLACEME # 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())
def telemetry_worker(config): """ Interface Faraday Proxy to obtain telemtry 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. """ logger.info('Starting telemetry_worker thread') # initialize variables stations = {} # Initialize proxy object proxy = faradaybasicproxyio.proxyio() # Initialize Faraday parser faradayParser = telemetryparser.TelemetryParse() # Add logger? # Open configuration file dbFilename = config.get("DATABASE", "FILENAME") # Pragmatically create descriptors for each Faraday connected to Proxy count = config.getint("TELEMETRY", "UNITS") for num in range(count): callsign = config.get("TELEMETRY", "UNIT" + str(num) + "CALL").upper() nodeid = config.get("TELEMETRY", "UNIT" + str(num) + "ID") 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, if True place into deque while (1): for radio in range(count): callsign = stations["UNIT" + str(num) + "CALL"] nodeid = stations["UNIT" + str(num) + "ID"] data = proxy.GET(str(callsign), str(nodeid), int(proxy.TELEMETRY_PORT)) # Iterate through each packet and unpack into dictionary if data != None: 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: sqlInsert(parsedTelemetry) telemetryDicts[str(callsign) + str(nodeid)].append(parsedTelemetry) time.sleep(1) # should slow down