Skip to content

Commit

Permalink
Merge pull request #256 from kb1lqc/issue244
Browse files Browse the repository at this point in the history
Issue244: Adding in Dynamic Proxy, Telemetry, and SimpleUI URLs
  • Loading branch information
kb1lqc authored Aug 16, 2017
2 parents ed6c657 + 0c5760f commit db30f08
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 23 deletions.
2 changes: 1 addition & 1 deletion etc/faraday/proxy.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ CALLSIGN=REPLACEME
NODEID=REPLACEME
COM=REPLACEME
BAUDRATE=115200
TIMEOUT=5
TIMEOUT=5
1 change: 1 addition & 0 deletions etc/faraday/telemetry.sample.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ UNITS=1
NUMBER=0
UNIT0CALL=REPLACEME
UNIT0ID=REPLACEME
PROXYHOST=127.0.0.1
6 changes: 4 additions & 2 deletions faraday/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def uart_worker(modem, getDicts, postDicts, units, log):
except StandardError as e:
logger.error(e)

# Slow down while loop to something reasonable
# Slow down while loop to something reasonable
time.sleep(0.01)


Expand Down Expand Up @@ -682,6 +682,7 @@ def proxy():
raw bytes.
"""
if request.method == "POST":

try:
data = request.get_json(force=False) # Requires HTTP JSON header
port = request.args.get("port")
Expand Down Expand Up @@ -845,6 +846,7 @@ def proxy():
return json.dumps({"error": str(e)}), 400
# Return data from queue to RESTapi
# If data is in port queu, turn it into JSON and return

try:
if (len(getDicts[callsign + "-" + str(nodeid)][port]) > 0):
data = []
Expand Down Expand Up @@ -1128,7 +1130,7 @@ def main():
t.start()

try:
# Start the flask server on localhost:8000
# Start the flask server on host:port
proxyHost = proxyConfig.get("FLASK", "host")
proxyPort = proxyConfig.getint("FLASK", "port")
except ConfigParser.Error as e:
Expand Down
23 changes: 17 additions & 6 deletions faraday/proxyio/faradaybasicproxyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(self, port=8000, logger=None):

#Functions

def POST(self, local_device_callsign, local_device_id, uart_port, data):
def POST(self, host, local_device_callsign, local_device_id, uart_port, data):
"""
The POST function is a python function that interacts with the Faraday RESTful API and "POSTS" (puts into) data into the transmit queue. Data provided to this function will be transmitted
to a local Faraday device over UART (as specified by the arguments) over the intended Faraday transport layer "Service Port."
Expand Down Expand Up @@ -83,16 +83,24 @@ def POST(self, local_device_callsign, local_device_id, uart_port, data):
payload = {'data': [b64_data]}

#POST data to UART service port
status = requests.post("http://127.0.0.1:" + str(self.FLASK_PORT) + "/?" + "callsign=" + str(local_device_callsign).upper() + '&port=' + str(uart_port) + '&' + 'nodeid=' + str(local_device_id), json=payload) #Sends Base64 config flash update packet to Faraday
url = 'http://{0}:{1}/?port={2}&callsign={3}&nodeid={4}'.format(host, self.FLASK_PORT, uart_port, local_device_callsign, local_device_id)
try:
status = requests.post(url, json=payload) #Sends Base64 config flash update packet to Faraday

except requests.ConnectionError as e:
self._logger.error(e)
self._logger.error(url)
return ''

#Return
return status

def GET(self, local_device_callsign, local_device_id, uart_service_number, limit=None):
def GET(self, host, local_device_callsign, local_device_id, uart_service_number, limit=None):
"""
This function returns a dictionary of all data packets waiting a Flask API interface queue as specified by the supplied
UART Port (Service Number).
:param host: Hostname or IP address of server to query
:param local_device_callsign: Callsign of the local Faraday device to direct the data to (allows multiple local units)
:param local_device_id: Callsign ID number of the local Faraday device to direct the data to (allows multiple local units)
:param uart_service_number: Intended Faraday transport layer service port to direct the supplied data to
Expand All @@ -108,18 +116,21 @@ def GET(self, local_device_callsign, local_device_id, uart_service_number, limit
The example below retrieves two data packets waiting from the "Telemetry" UART port (Port 5 in this example).
>>> faraday_1 = faradaybasicproxyio.proxyio()
>>> faraday_1.GET("KB1LQD", 1, FARADAY_TELEMETRY_UART_PORT)
>>> faraday_1.GET(host, "KB1LQD", 1, FARADAY_TELEMETRY_UART_PORT)
[{u'data': u'AwBhS0IxTFFEBXsDBgdLQjFMUUQwME4GBzkpFhIACeAHMzM1Mi40MjAxTjExODIyLjYwNDhXMzQuNjIwMDBNMC4yNzAyMC45MgAXYAjdCKoICQe8B/sIFgAAAB4K/gAAHCAAAAAARgYHS0IxTFFEAAAABgcTKRYSABZf',
u'port': 5},
{u'data': u'AwBhS0IxTFFEBXsDBgdLQjFMUUQFewMGBxIqFhIACeAHMzM1Mi40MjAzTjExODIyLjYwNDdXMzQuNTIwMDBNMC4yNzAyMC45MAAXYAjeCKoICQe5B/oIGAAAAB4LAwAAHCAAAAAAAABGBgdLQjFMUUQAAAAGBxMpFhT/',
u'port': 5}]
"""
url = 'http://127.0.0.1:' + str(self.FLASK_PORT) + "/" + "?port=" + str(uart_service_number) + "&callsign=" + str(local_device_callsign) + "&nodeid=" + str(local_device_id)

# here is where I want to use a value passed to the function for a IP/hostname
url = 'http://{0}:{1}/?port={2}&callsign={3}&nodeid={4}'.format(host, self.FLASK_PORT, uart_service_number, local_device_callsign, local_device_id)

# If limit is provided, check that it's positive and add to url
# TODO: Does this actually do much?
if limit is not None:
if int(limit) >= 0:
url = url + "&limit=" + str(limit)
url = "{0}&limit={1}".format(url, str(limit))

try:
response = requests.get(url) #calling IP address directly is much faster than localhost lookup
Expand Down
38 changes: 30 additions & 8 deletions faraday/simpleui.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@


# Command line input
parser = argparse.ArgumentParser(description='SimpleUI application provides a simple user interface for Faraday radios at http://localhost/')
parser = argparse.ArgumentParser(description='SimpleUI application provides a simple user interface for Faraday radios at http://localhost/ or other defined web address')
parser.add_argument('--init-config', dest='init', action='store_true', help='Initialize SimpleUI configuration file')
parser.add_argument('--callsign', help='Set Local SimpleUI callsign for data display')
parser.add_argument('--nodeid', help='Set Local SimpleUI nodeid for data display')
Expand Down Expand Up @@ -158,6 +158,8 @@ def simpleui():
try:
callsign = simpleuiConfig.get("SIMPLEUI", "CALLSIGN").upper()
nodeid = simpleuiConfig.getint("SIMPLEUI", "NODEID")
telemetryHost = simpleuiConfig.get("TELEMETRY", "HOST")
telemetryPort = simpleuiConfig.get("TELEMETRY", "PORT")
if callsign == "REPLACEME":
raise ConfigParser.Error("Please configure SimpleUI --callsign and --nodeid")

Expand All @@ -173,6 +175,8 @@ def simpleui():
else:
#Return HTML/Javascript template
return render_template('index.html',
host=telemetryHost,
port=telemetryPort,
callsign=callsign,
nodeid=nodeid)

Expand All @@ -182,8 +186,16 @@ def simpleui():
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")
try:
callsign = simpleuiConfig.get("SIMPLEUI", "LOCALCALLSIGN").upper()
nodeid = simpleuiConfig.getint("SIMPLEUI", "LOCALNODEID")

except ValueError as e:
logger.error(e)

# Return to simple user interface page after commanding
host = simpleuiConfig.get("FLASK", "HOST")
return redirect("http://{0}/".format(host), code=302)

if request.form["IO"] == "LED1 ON":
logger.debug("Local {0}-{1} LED1 commanded ON".format(callsign, nodeid))
Expand Down Expand Up @@ -289,9 +301,17 @@ def simpleui():
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")
try:
# Obtain remote station from config file, check form data for intended command
remotecallsign = simpleuiConfig.get("SIMPLEUI", "REMOTECALLSIGN").upper()
remotenodeid = simpleuiConfig.getint("SIMPLEUI", "REMOTENODEID")

except ValueError as e:
logger.error(e)

# Return to simple user interface page after commanding
host = simpleuiConfig.get("FLASK", "HOST")
return redirect("http://{0}/".format(host), code=302)

if request.form["IO"] == "LED1R ON":
logger.debug("Remote {0}-{1} LED1 commanded ON".format(remotecallsign, remotenodeid))
Expand Down Expand Up @@ -453,10 +473,12 @@ def simpleui():
remotenodeid))

# Send POST command for remote station control
faraday_1.POST(callsign, nodeid, faraday_1.CMD_UART_PORT, command)
proxyHost = simpleuiConfig.get("PROXY", "HOST")
faraday_1.POST(proxyHost, callsign, nodeid, faraday_1.CMD_UART_PORT, command)

# Return to simple user interface page after commanding
return redirect("http://localhost/", code=302)
host = simpleuiConfig.get("FLASK", "HOST")
return redirect("http://{0}/".format(host), code=302)


@app.errorhandler(404)
Expand Down
12 changes: 9 additions & 3 deletions faraday/static/simpleui.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ function getTelemetry(){
// using JQuery to place updated values onto the webpage

try{


// Build URL for Telemetry db query, then retrieve data
var url = "http://localhost:8001/?limit=1&callsign=" +
// TODO: update for dynamic URL

var url = "http://" +
host +
":" +
port +
"/?limit=1&callsign=" +
callsign + // jshint ignore:line
"&nodeid=" +
"&nodeid=" +
nodeid; // jshint ignore:line
$.getJSON(url,function(data){
try{
Expand Down
8 changes: 6 additions & 2 deletions faraday/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
parser.add_argument('--nodeid', type=int, help='Set Faraday node ID in Proxy to connect to')
parser.add_argument('--unit', type=int, default=0, help='Specify Faraday unit to configure')
parser.add_argument('--start', action='store_true', help='Start Telemetry server')
parser.add_argument('--proxyhost', help='Set hostname/IP of Proxy to connect to')

# Telemetry database options
parser.add_argument('--database', help='Set Telemetry database name')
Expand Down Expand Up @@ -157,6 +158,8 @@ def configureTelemetry(args):
config.set('TELEMETRY', unit + 'CALL', args.callsign)
if args.nodeid is not None:
config.set('TELEMETRY', unit + 'ID', args.nodeid)
if args.proxyhost is not None:
config.set('TELEMETRY', 'proxyhost', args.proxyhost)

#Configure Telemetry databases
if args.database is not None:
Expand Down Expand Up @@ -247,6 +250,7 @@ def telemetry_worker(config):
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
Expand All @@ -263,7 +267,7 @@ def telemetry_worker(config):
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))
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
Expand Down Expand Up @@ -1059,7 +1063,7 @@ def main():
threads.append(t)
t.start()

# Start the flask server on localhost:8001
# Start the flask server on host:port
try:
telemetryHost = telemetryConfig.get("FLASK", "HOST")
telemetryPort = telemetryConfig.getint("FLASK", "PORT")
Expand Down
4 changes: 3 additions & 1 deletion faraday/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
</script>
<script type="text/javascript">
// Obtain callsign and node ID for Telemetry application from python
var host = "{{host}}";
var port = "{{port}}";
var callsign = "{{callsign}}";
var nodeid = "{{nodeid}}";
</script>
<script type=text/javascript
src="../static/simpleui.js">
</script>
</body>
</html>
</html>

0 comments on commit db30f08

Please sign in to comment.