Files
old-tfm/controller/state/__init__.py

47 lines
1.3 KiB
Python
Raw Normal View History

2016-01-24 09:18:14 +01:00
import config
2016-01-24 09:24:05 +01:00
from state.server import Server
from state.changer import Changer
from state.file import File
2016-06-08 09:16:44 +02:00
import commands
2016-01-24 09:15:14 +01:00
2016-06-08 08:24:43 +02:00
def safely_panic():
safe_state = {}
changer = Changer(safe_state,safe_state)
2016-06-08 08:31:42 +02:00
changer.stop_everything()
2016-01-24 09:15:14 +01:00
2016-06-08 10:34:05 +02:00
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')
2016-01-24 09:37:02 +01:00
2016-06-08 08:24:43 +02:00
def sync():
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
changer = Changer(local_state, server_state)
current_state = changer.process_change()
2016-10-08 18:26:11 +02:00
print "posting :" + repr(current_state)
local.data = current_state
2016-06-08 08:24:43 +02:00
server.post_state(current_state)
2016-10-08 18:26:11 +02:00
local.save()
2016-06-08 08:24:43 +02:00
except:
2016-06-08 08:31:42 +02:00
print " panicking safely ! "
2016-06-08 08:31:58 +02:00
safely_panic()