def __init__(self): """ The message application object contains all the functions, definitions, and state machines needed to implement a bare-bones text message application using the Faraday command application "experimental RF Packet Forward" functionality." """ # Identification Variables self.local_device_callsign = 'kb1lqc' self.local_device_node_id = 1 # Initialize objects self.faraday_Rx = faradaybasicproxyio.proxyio() self.faraday_Rx_SM = MsgStateMachineRx() # Initialize variables # Frame Definitions (Should be combined later with TX?) self.pkt_datagram_frame = struct.Struct('1B 41s') # Fixed self.pkt_start = struct.Struct('9s 3B') # Fixed self.pkt_data = struct.Struct('2B 39s') # Variable Data Length self.pkt_end = struct.Struct('1B') # Fixed
def __init__(self, local_callsign, local_callsign_id): """ The message application object contains all the functions, definitions, and state machines needed to implement a bare-bones text message application using the Faraday command application "experimental RF Packet Forward" functionality." """ # Identification Variables self.local_device_callsign = str(local_callsign).upper() self.local_device_node_id = int(local_callsign_id) self.remote_callsign = '' #str(destination_callsign).upper() self.remote_id = 0 #int(destination_id) # Initialize objects self.faraday_1 = faradaybasicproxyio.proxyio() self.faraday_cmd = faradaycommands.faraday_commands() # Initialize variables self.destination_callsign = '' self.destination_id = 0 self.command = ''
# Add Faraday library to the Python path. sys.path.append(os.path.join(os.path.dirname(__file__), '../../../..')) # 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())
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
#!/usr/bin/env python import os import sys import time # Add Faraday library to the Python path. sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) # Imports - Faraday Specific from faraday.proxyio import faradaybasicproxyio from faraday.proxyio import faradaycommands #Setup a Faraday IO object faraday_1 = faradaybasicproxyio.proxyio() #default proxy port faraday_cmd = faradaycommands.faraday_commands() callsign = 'REPLACEME' node_id = 0 while True: #Turn LED 1 ON (GREEN) print "Turning LED 1 ON" command = faraday_cmd.CommandLocalGPIOLED1On() faraday_1.POST(callsign, node_id, faraday_1.CMD_UART_PORT, command) time.sleep(0.5) #Turn LED 1 OFF print "Turning LED 1 OFF" command = faraday_cmd.CommandLocalGPIOLED1Off() faraday_1.POST(callsign, node_id, faraday_1.CMD_UART_PORT, command)
from faraday.proxyio import deviceconfig # Global Constants UART_PORT_APP_COMMAND = 2 # Start logging after importing modules logging.config.fileConfig('loggingConfig.ini') logger = logging.getLogger('deviceconfiguration') # Load Telemetry Configuration from telemetry.ini file # Should have common file for apps... deviceConfig = ConfigParser.RawConfigParser() deviceConfig.read('deviceconfiguration.ini') # Initialize proxy object proxy = faradaybasicproxyio.proxyio() # Initialize faraday command module faradayCmd = faradaycommands.faraday_commands() # Initialize Flask microframework app = Flask(__name__) @app.route('/', methods=['GET', 'POST']) def unitconfig(): """ This function is called when the RESTful API GET or POST call is made to the '/' of the operating port. Querying a GET will command the local and queried unit's device configuration in Flash memory and return the information as a JSON dictionary. Issuing a POST will cause the local .INI file configuration to be loaded into the respective units Flash memory device configuration.
def simpleui(): """ Provides a simple user interface with python and javascript Uses Flask to return the user interface template file when a GET request is made while this function checks form data for appropriate commands intended to be sent to a local/remote station. Once completed the user is redirected to the main simpleui page. """ if request.method == "GET": # Obtain telemetry station from config file callsign = simpleuiconfig.get("SIMPLEUI", "CALLSIGN").upper() nodeid = simpleuiconfig.getint("SIMPLEUI", "NODEID") #Return HTML/Javascript template return render_template('index.html', callsign=callsign, nodeid=nodeid) if request.method == "POST": # Start the proxy server after configuring the configuration file correctly # Setup a Faraday IO object faraday_1 = faradaybasicproxyio.proxyio() # default proxy port faraday_cmd = faradaycommands.faraday_commands() # Obtain local station from config file, check form data for intended command callsign = simpleuiconfig.get("SIMPLEUI", "LOCALCALLSIGN").upper() nodeid = simpleuiconfig.getint("SIMPLEUI", "LOCALNODEID") if request.form["IO"] == "LED1 ON": logger.debug("Local {0}-{1} LED1 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIOLED1On() if request.form["IO"] == "LED1 OFF": logger.debug("Local {0}-{1} LED1 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIOLED1Off() if request.form["IO"] == "LED2 ON": logger.debug("Local {0}-{1} LED2 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIOLED2On() if request.form["IO"] == "LED2 OFF": logger.debug("Local {0}-{1} LED2 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIOLED2Off() if request.form["IO"] == "GPIO0 ON": logger.debug("Local {0}-{1} GPIO0 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( gpioallocations.DIGITAL_IO_0, 0, 0, 0, 0, 0) if request.form["IO"] == "GPIO0 OFF": logger.debug("Local {0}-{1} GPIO0 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, 0, gpioallocations.DIGITAL_IO_0, 0, 0) if request.form["IO"] == "GPIO1 ON": logger.debug("Local {0}-{1} GPIO1 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( gpioallocations.DIGITAL_IO_1, 0, 0, 0, 0, 0) if request.form["IO"] == "GPIO1 OFF": logger.debug("Local {0}-{1} GPIO1 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, 0, gpioallocations.DIGITAL_IO_1, 0, 0) if request.form["IO"] == "GPIO2 ON": logger.debug("Local {0}-{1} GPIO2 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( gpioallocations.DIGITAL_IO_2, 0, 0, 0, 0, 0) if request.form["IO"] == "GPIO2 OFF": logger.debug("Local {0}-{1} GPI2 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, 0, gpioallocations.DIGITAL_IO_2, 0, 0) if request.form["IO"] == "GPIO3 ON": logger.debug("Local {0}-{1} GPIO3 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, gpioallocations.DIGITAL_IO_3, 0, 0, 0, 0) if request.form["IO"] == "GPIO3 OFF": logger.debug("Local {0}-{1} GPIO3 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, 0, 0, gpioallocations.DIGITAL_IO_3, 0) if request.form["IO"] == "GPIO4 ON": logger.debug("Local {0}-{1} GPIO4 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, gpioallocations.DIGITAL_IO_4, 0, 0, 0, 0) if request.form["IO"] == "GPIO4 OFF": logger.debug("Local {0}-{1} GPIO4 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, 0, 0, gpioallocations.DIGITAL_IO_4, 0) if request.form["IO"] == "GPIO5 ON": logger.debug("Local {0}-{1} GPIO5 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, gpioallocations.DIGITAL_IO_5, 0, 0, 0, 0) if request.form["IO"] == "GPIO5 OFF": logger.debug("Local {0}-{1} GPIO5 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, 0, 0, gpioallocations.DIGITAL_IO_5, 0) if request.form["IO"] == "GPIO6 ON": logger.debug("Local {0}-{1} GPIO6 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, gpioallocations.DIGITAL_IO_6, 0, 0, 0, 0) if request.form["IO"] == "GPIO6 OFF": logger.debug("Local {0}-{1} GPIO6 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, 0, 0, gpioallocations.DIGITAL_IO_6, 0) if request.form["IO"] == "GPIO7 ON": logger.debug("Local {0}-{1} GPIO7 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, gpioallocations.DIGITAL_IO_7, 0, 0, 0, 0) if request.form["IO"] == "GPIO7 OFF": logger.debug("Local {0}-{1} GPIO7 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, 0, 0, gpioallocations.DIGITAL_IO_7, 0) if request.form["IO"] == "GPIO8 ON": logger.debug("Local {0}-{1} GPIO8 commanded ON".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, gpioallocations.DIGITAL_IO_8, 0, 0, 0) if request.form["IO"] == "GPIO8 OFF": logger.debug("Local {0}-{1} GPIO8 commanded OFF".format( callsign, nodeid)) command = faraday_cmd.CommandLocalGPIO( 0, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_8) if request.form["IO"] == "MOSFET": logger.debug("Local {0}-{1} MOSFET commanded to fire".format( callsign, nodeid)) command = faraday_cmd.CommandLocalHABActivateCutdownEvent() if request.form["IO"] == "HABTIMERRESET": logger.debug("Local {0}-{1} HAB timer reset commanded".format( callsign, nodeid)) command = faraday_cmd.CommandLocalHABResetAutoCutdownTimer() if request.form["IO"] == "HABDISABLETIMER": logger.debug("Local {0}-{1} HAB timer disable commanded".format( callsign, nodeid)) command = faraday_cmd.CommandLocalHABDisableAutoCutdownTimer() if request.form["IO"] == "HABTIMERIDLE": logger.debug("Local {0}-{1} HAB timer idle commanded".format( callsign, nodeid)) command = faraday_cmd.CommandLocalHABResetCutdownIdle() # Obtain remote station from config file, check form data for intended command remotecallsign = simpleuiconfig.get("SIMPLEUI", "REMOTECALLSIGN").upper() remotenodeid = simpleuiconfig.getint("SIMPLEUI", "REMOTENODEID") if request.form["IO"] == "LED1R ON": logger.debug("Remote {0}-{1} LED1 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIOLED1On(remotecallsign, remotenodeid)) if request.form["IO"] == "LED1R OFF": logger.debug("Remote {0}-{1} LED1 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIOLED1Off(remotecallsign, remotenodeid)) # Below here does not work if request.form["IO"] == "LED2R ON": logger.debug("Remote {0}-{1} LED2 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIOLED2On(remotecallsign, remotenodeid)) if request.form["IO"] == "LED2R OFF": logger.debug("Remote {0}-{1} LED2 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIOLED2Off(remotecallsign, remotenodeid)) if request.form["IO"] == "GPIO0R ON": logger.debug("Remote {0}-{1} GPIO0 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, gpioallocations.DIGITAL_IO_0, 0, 0, 0, 0, 0)) if request.form["IO"] == "GPIO0R OFF": logger.debug("Remote {0}-{1} GPIO0 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, gpioallocations.DIGITAL_IO_0, 0, 0)) if request.form["IO"] == "GPIO1R ON": logger.debug("Remote {0}-{1} GPIO1 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, gpioallocations.DIGITAL_IO_1, 0, 0, 0, 0, 0)) if request.form["IO"] == "GPIO1R OFF": logger.debug("Remote {0}-{1} GPIO1 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, gpioallocations.DIGITAL_IO_1, 0, 0)) if request.form["IO"] == "GPIO2R ON": logger.debug("Remote {0}-{1} GPIO2 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, gpioallocations.DIGITAL_IO_2, 0, 0, 0, 0, 0)) if request.form["IO"] == "GPIO2R OFF": logger.debug("Remote {0}-{1} GPIO2 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, gpioallocations.DIGITAL_IO_2, 0, 0)) if request.form["IO"] == "GPIO3R ON": logger.debug("Remote {0}-{1} GPIO3 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, gpioallocations.DIGITAL_IO_3, 0, 0, 0, 0)) if request.form["IO"] == "GPIO3R OFF": logger.debug("Remote {0}-{1} GPIO3 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_3, 0)) if request.form["IO"] == "GPIO4R ON": logger.debug("Remote {0}-{1} GPIO4 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_4, 0)) if request.form["IO"] == "GPIO4R OFF": logger.debug("Remote {0}-{1} GPIO4 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_4, 0)) if request.form["IO"] == "GPIO5R ON": logger.debug("Remote {0}-{1} GPIO5 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_5, 0)) if request.form["IO"] == "GPIO5R OFF": logger.debug("Remote {0}-{1} GPIO5 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_5, 0)) if request.form["IO"] == "GPIO6R ON": logger.debug("Remote {0}-{1} GPIO6 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_6, 0)) if request.form["IO"] == "GPIO6R OFF": logger.debug("Remote {0}-{1} GPIO6 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_6, 0)) if request.form["IO"] == "GPIO7R ON": logger.debug("Remote {0}-{1} GPIO7 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_7, 0)) if request.form["IO"] == "GPIO7R OFF": logger.debug("Remote {0}-{1} GPIO7 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_7, 0)) if request.form["IO"] == "GPIO8R ON": logger.debug("Remote {0}-{1} GPIO8 commanded ON".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_8)) if request.form["IO"] == "GPIO8R OFF": logger.debug("Remote {0}-{1} GPIO8 commanded OFF".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteGPIO(remotecallsign, remotenodeid, 0, 0, 0, 0, 0, gpioallocations.DIGITAL_IO_8)) if request.form["IO"] == "MOSFETR": logger.debug("Remote {0}-{1} MOSFET commanded to fire".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteHABActivateCutdownEvent( remotecallsign, remotenodeid)) if request.form["IO"] == "HABTIMERRESETR": logger.debug("Remote {0}-{1} HAB timer commanded to reset".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteHABResetAutoCutdownTimer( remotecallsign, remotenodeid)) if request.form["IO"] == "HABDISABLETIMERR": logger.debug("Remote {0}-{1} HAB timer disable commanded".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteHABDisableAutoCutdownTimer( remotecallsign, remotenodeid)) if request.form["IO"] == "HABTIMERIDLER": logger.debug("Remote {0}-{1} HAB timer idle commanded".format( remotecallsign, remotenodeid)) command = faraday_cmd.CommandLocal( 9, faraday_cmd.CommandRemoteHABResetCutdownIdle( remotecallsign, remotenodeid)) # Send POST command for remote station control faraday_1.POST(callsign, nodeid, faraday_1.CMD_UART_PORT, command) # Return to simple user interface page after commanding return redirect("http://localhost/", code=302)