Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge current work into master #18

Merged
merged 33 commits into from
Jun 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
682afed
Added a few network control commands
May 24, 2017
68385d2
added nmcli scripts for client and ap wifi
May 24, 2017
0a4e0aa
added initial dataflash log upload to cloud
May 24, 2017
3556f85
dfsync - cleaning up the module
May 25, 2017
1ac2101
dflogger - make user home path general
May 25, 2017
1dc8c3a
added dflogger to config and rsync error captuer (for logging)
May 27, 2017
3a3a3d7
added messages which will be used by the webserver
May 27, 2017
563a4b0
auto create the folder/s that we later-on assume exist.
davidbuzz May 27, 2017
ebf93b5
prevent the rsync/ssh non-interactive UI from being blocked while it …
davidbuzz May 27, 2017
6297f06
Merge pull request #11 from davidbuzz/df_sync_wip
davidbuzz May 27, 2017
3f714d3
when writing config file, write it to the correct name.
davidbuzz May 27, 2017
b14fa44
Improve the way modules with lots of confurable options can write the…
davidbuzz May 27, 2017
5d74a72
Merge pull request #12 from davidbuzz/df_sync_wip
davidbuzz May 27, 2017
47466f1
add basic cmd-line help
davidbuzz May 27, 2017
5bce5f4
Merge pull request #13 from davidbuzz/df_sync_wip
davidbuzz May 27, 2017
b0893a9
no message
davidbuzz May 27, 2017
cf6b9f0
Merge pull request #14 from davidbuzz/df_sync_wip
davidbuzz May 27, 2017
c449dfd
bugfix... after adding the -o StrictHostKeyChecking=no to the ssh com…
davidbuzz May 27, 2017
827fa5a
Merge pull request #15 from davidbuzz/df_sync_wip
davidbuzz May 27, 2017
a9a3c11
resolve conflicts
SamuelDudley May 27, 2017
b1eae0a
support for Creating an Identity file on-disk from the browser, and t…
davidbuzz May 27, 2017
77505fc
fix typo
davidbuzz May 27, 2017
f4ec3ec
Merge pull request #16 from davidbuzz/df_sync_wip
davidbuzz May 27, 2017
bab81e3
fix for identity file not being encoded properly.
davidbuzz May 27, 2017
87bdea2
Merge pull request #17 from davidbuzz/df_sync_wip
davidbuzz May 27, 2017
5e11542
dfsync - display public key and rework module structure a little
SamuelDudley May 27, 2017
72dce69
Added initial stab at logging support
Jun 7, 2017
0ba1e3f
Update to dfsync module. changes to support interface with apsync.cloud
Jun 7, 2017
51d32b0
Modify requests_utils so that by default the user is not verfied
SamuelDudley Jun 7, 2017
5a6c0be
dfsync module working with apsync.cloud
SamuelDudley Jun 7, 2017
66bf169
cleanup
davidbuzz Jun 8, 2017
231e05b
dfsync now fully working :)
SamuelDudley Jun 8, 2017
f318eb1
Merge remote-tracking branch 'github/df_sync_wip' into df_sync_wip
SamuelDudley Jun 8, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions APSyncFramework/APSync.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import time, select, sys, signal, os, shlex
import multiprocessing, threading, setproctitle
import traceback
import traceback, logging
from logging.handlers import RotatingFileHandler

from APSyncFramework.modules.lib import APSync_module
from APSyncFramework.utils.common_utils import PeriodicEvent, pid_exists, wait_pid
from APSyncFramework.utils.json_utils import json_unwrap_with_target
from APSyncFramework.utils.file_utils import mkdir_p

# global for signals only.
apsync_state = []
Expand All @@ -12,7 +15,24 @@ class APSync(object):
def __init__(self):
self.modules = []
self.should_exit = False

self.begin_logging()

def begin_logging(self):
logFormatter = logging.Formatter("%(asctime)s [%(levelname)-5.5s] %(message)s")
rootLogger = logging.getLogger()
rootLogger.setLevel(1)
log_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'logs')
mkdir_p(log_dir)
fileHandler = RotatingFileHandler("{0}/{1}.log".format(log_dir, 'APSync'), maxBytes = 100000, backupCount = 5)
fileHandler.setFormatter(logFormatter)
fileHandler.setLevel(1) # used to control what is printed to console
rootLogger.addHandler(fileHandler)

consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
consoleHandler.setLevel(1) # used to control what is printed to console
rootLogger.addHandler(consoleHandler)

@property
def loaded_modules(self):
return [module_instance.name for (module_instance,module) in self.modules]
Expand Down Expand Up @@ -150,17 +170,19 @@ def main_loop(self):
pid = os.getpid()
signal.signal(signal.SIGINT, self.signal_handler)
signal.signal(signal.SIGTERM, self.signal_handler)
self.load_module('mavlink', start_now=True)
# self.load_module('mavlink', start_now=True)
self.load_module('webserver', start_now=True)
self.load_module('dfsync', start_now=True)

# TODO: make the input thread optional (this can be replaced by the web UI)
self.input_loop_queue = multiprocessing.Queue()
self.inject_data_queue = multiprocessing.Queue()

input_loop_thread = threading.Thread(target=self.input_loop, args = (lock, apsync_state, self.input_loop_queue))
input_loop_thread.daemon = True
input_loop_thread.start()

queue_handling_thread = threading.Thread(target=self.queue_handling, args = (lock, self.event, apsync_state,))
queue_handling_thread = threading.Thread(target=self.queue_handling, args = (lock, self.event, apsync_state, self.inject_data_queue))
queue_handling_thread.daemon = True
queue_handling_thread.start()

Expand All @@ -174,6 +196,11 @@ def main_loop(self):
cmd = args[0]
if cmd == 'module':
self.cmd_module(args[1:])
if (cmd == 'help') or (cmd == '?'):
print "try one of these:\n\nmodule reload webserver\nmodule reload mavlink\nmodule list\nor paste some json if you are game\n\n"
if (line[0] == '{' and line[-1] == '}'):
# assume the line is json
self.inject_json(line)
for event in periodic_events:
event.trigger()

Expand All @@ -194,14 +221,15 @@ def input_loop(self, lock, apsync_state, out_queue):
line = line.strip() # remove leading and trailing whitespace
out_queue.put_nowait(line)

def queue_handling(self, lock, event, apsync_state):
def queue_handling(self, lock, event, apsync_state, inject_data_queue):
setproctitle.setproctitle("APSync")

while not self.should_exit:
if event.is_set():
modules = self.modules
module_names = self.loaded_modules
out_queues = [i.out_queue for (i,m) in modules]
out_queues.append(inject_data_queue) # used to pass targeted data from the cmd line to modules
in_queues = [i.in_queue for (i,m) in modules]
queue_file_discriptors = [q._reader for q in out_queues]
event.clear()
Expand All @@ -222,6 +250,19 @@ def queue_handling(self, lock, event, apsync_state):
where = (idx for idx,(i,m) in enumerate(self.modules) if i.name==data['name']).next()
(i,m) = self.modules[where]
i.last_ping = data
elif target == 'logging':
if data['level'].upper() == 'CRITICAL':
logging.critical(data['msg'])
elif data['level'].upper() == 'ERROR':
logging.error(data['msg'])
elif data['level'].upper() == 'WARNING':
logging.warning(data['msg'])
elif data['level'].upper() == 'INFO':
logging.info(data['msg'])
elif data['level'].upper() == 'DEBUG':
logging.debug(data['msg'])
else:
pass
else:
idx = module_names.index(target)
in_queues[idx].put(data)
Expand All @@ -245,7 +286,18 @@ def shlex_quotes(self, value):
lex.whitespace_split = True
lex.commenters = ''
return list(lex)

def inject_json(self, json_data):
'''pass json data to the queue handler thread
e.g.: { "_target":"webserver", "data":{"json_data":{"command":"sendIdentityRequest","replyto":"getIdentityResponse"}} }'''

try:
(target,data,priority) = json_unwrap_with_target(json_data)
print('\nTARGET:\t\t{0}\nPRIORITY:\t{1}\nDATA:\n{2}'.format(target,priority,data))
self.inject_data_queue.put_nowait(json_data)
except Exception as e:
print('Something went wrong while trying to unwrap your json:\n{0}'.format(json_data))

def cmd_module(self, args):
'''module commands'''
usage = "usage: module <list|load|reload|unload>"
Expand Down
1 change: 1 addition & 0 deletions APSyncFramework/conf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/WebConfigServer.json
6 changes: 5 additions & 1 deletion APSyncFramework/conf/WebConfigServer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@
"connection": "tcp:127.0.0.1:5763",
"dialect": "ardupilotmega",

"webserver_port": "4443"
"webserver_port": "4443",

"cloudsync_user": "apsync",
"cloudsync_port": "2221",
"cloudsync_address": "www.mavcesium.io"
}
1 change: 1 addition & 0 deletions APSyncFramework/logs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/APSync.log
Loading