From f1d7232c101258e73417bd672645e109877189b3 Mon Sep 17 00:00:00 2001 From: Senad Uka Date: Wed, 8 Jun 2016 10:34:05 +0200 Subject: [PATCH] network check script --- controller/README.md | 9 +++++++- controller/network_check.py | 3 +++ controller/state/__init__.py | 10 +++++++-- misc/latest_dweets.html | 43 ++++++++++++++++++++++++++++++++++++ misc/latest_dweets.js | 24 ++++++++++++++++++++ 5 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 controller/network_check.py create mode 100644 misc/latest_dweets.html create mode 100644 misc/latest_dweets.js diff --git a/controller/README.md b/controller/README.md index fe099a9..297288a 100644 --- a/controller/README.md +++ b/controller/README.md @@ -3,7 +3,7 @@ ## Installation 1. Go to every subdirectory in drivers directory and follow instructions about installation of drivers -2. edit controller/config/__init__.py and set your controller ID to unique number +2. edit controller/config/__init__.py and set your controller ID to unique number 3. configure cron to run controller.py every 15 minutes as a superuser: ``` @@ -12,3 +12,10 @@ crontab -e -u root */15 * * * * /usr/bin/python /home/pi/projects/tfm/controller/sensors.py "Automatski, Senad Uka" 120 */1 * * * * /usr/bin/python /home/pi/projects/tfm/controller/sync_state.py "Automatski, Senad Uka" 120 ``` + +4. add following lines at the end of /etc/rc.local + +``` +python /home/pi/projects/tfm/controller/lockdown.py +python /home/pi/projects/tfm/controller/dweet.py +``` diff --git a/controller/network_check.py b/controller/network_check.py new file mode 100644 index 0000000..8b48177 --- /dev/null +++ b/controller/network_check.py @@ -0,0 +1,3 @@ +import state + +state.reboot_if_network_down() diff --git a/controller/state/__init__.py b/controller/state/__init__.py index af22f4b..c295f0f 100644 --- a/controller/state/__init__.py +++ b/controller/state/__init__.py @@ -9,6 +9,14 @@ def safely_panic(): changer = Changer(safe_state,safe_state) changer.stop_everything() +def reboot_if_network_down(): + try: + server = Server(config.API_BASE_URL, config.CONTROLLER_ID) + server_state = server.get_state() + print "Got state from server: " + repr(server_state) + except: + print "Problem with the network!" + commands.getoutput('/sbin/shutdown -r +3') def sync(): try: @@ -35,5 +43,3 @@ def sync(): except: print " panicking safely ! " safely_panic() - print " rebooting " - commands.getoutput('/sbin/shutdown -r +3') diff --git a/misc/latest_dweets.html b/misc/latest_dweets.html new file mode 100644 index 0000000..6763dc5 --- /dev/null +++ b/misc/latest_dweets.html @@ -0,0 +1,43 @@ + + + + + + + + + + Get JSON data +
+ + + + diff --git a/misc/latest_dweets.js b/misc/latest_dweets.js new file mode 100644 index 0000000..7ae630c --- /dev/null +++ b/misc/latest_dweets.js @@ -0,0 +1,24 @@ + +$(document).ready(function () { + $('#get-data').click(function () { + var showData = $('#show-data'); + + $.getJSON('https://dweet.io:443/get/dweets/for/5410ab1e-319c-4f14-a2e4-04725df69121', function (data) { + console.log(data); + + var items = data.items.map(function (item) { + return item.key + ': ' + item.value; + }); + + showData.empty(); + + if (items.length) { + var content = '
  • ' + items.join('
  • ') + '
  • '; + var list = $('