Merge pull request #22 from senaduka/local_safe_autonomy

Local safe autonomy
This commit is contained in:
2016-06-08 11:15:04 +02:00
57 changed files with 3040 additions and 20 deletions

View File

@@ -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:
```
@@ -11,4 +11,12 @@ crontab -e -u root
#enter these lines
*/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
*/30 * * * * /usr/bin/python /home/pi/projects/tfm/controller/network_check.py
```
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
```

6
controller/dweet.py Normal file
View File

@@ -0,0 +1,6 @@
import config
import commands
command = """curl -H "Content-Type: application/json" -X POST -d '{"message": "I am aliveeee (again!)", "controller_id": %s}' https://dweet.io:443/dweet/quietly/for/5410ab1e-319c-4f14-a2e4-04725df69121""" % config.CONTROLLER_ID
print commands.getoutput(command)

3
controller/lockdown.py Normal file
View File

@@ -0,0 +1,3 @@
import state
state.safely_panic()

View File

@@ -0,0 +1,3 @@
import state
state.reboot_if_network_down()

View File

@@ -2,26 +2,44 @@ import config
from state.server import Server
from state.changer import Changer
from state.file import File
import commands
def safely_panic():
safe_state = {}
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():
server = Server(config.API_BASE_URL, config.CONTROLLER_ID)
local = File(config.STATE_FILE)
server_state = server.get_state()
try:
server = Server(config.API_BASE_URL, config.CONTROLLER_ID)
local = File(config.STATE_FILE)
server_state = server.get_state()
if local.present():
local.load()
print "local present: " + repr(local.data)
else:
local.data = server_state
print "local not present, server: " + repr(local.data)
local.save()
local_state = local.data
if local.present():
local.load()
print "local present: " + repr(local.data)
else:
local.data = server_state
print "local not present, server: " + repr(local.data)
local.save()
changer = Changer(local_state, server_state)
current_state = changer.process_change()
local_state = local.data
changer = Changer(local_state, server_state)
current_state = changer.process_change()
server.post_state(current_state)
server.post_state(current_state)
print " everything ok, canceling shutdown "
commands.getoutput('/sbin/shutdown -c')
except:
print " panicking safely ! "
safely_panic()

View File

@@ -24,6 +24,10 @@ class Changer(object):
'closed': self.close_in_valve
}
def stop_everything(self):
self.close_in_valve()
self.close_out_valve()
def safe_remote_state(self, key):
if key in ['out_valve', 'in_valve']:
return self.remote_state.get(key, 'closed')

View File

@@ -16,8 +16,6 @@ class Server(object):
result = requests.post(self.full_url('state/%s') % self.controller_id, local_state)
return handle_response(result)
def full_url(self, action):
if self.controller_id is None:
raise ClassNotReadyException("Controller id not set!")