adding untracked files

This commit is contained in:
Senad Uka
2016-01-24 09:15:14 +01:00
parent 85d41bc69b
commit f7061ab3da
8 changed files with 180 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
def sync():
server = Server(API_BASE_URL, CONTROLLER_ID)
local = File(STATE_FILE)
server_state = server.get_state()
if local.present():
local.load()
else:
local.data = server_state
local.save()
local_state = local.data
changer = Changer(local_state, remote_state)
current_state = changer.process_change()
server.post_state(current_state)

View File

@@ -0,0 +1,39 @@
import RPi.GPIO as GPIO
import config
class Changer(object):
def __init__(self, local_state, remote_state):
self.local_state = local_state
self.remote_state = remote_state
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(GPIO_PIN_VALVE, GPIO.OUT)
self.states = {
'opening': self.open_valve,
'closing': self.close_valve,
'open': self.open_valve,
'closed': self.close_valve
}
def process_change(self):
self.validate_states()
if self.local_state['out_valve'] != self.remote_state['out_valve']:
change = self.states.get(self.remote_state['out_valve'], None )
if change is not None:
change()
return self.local_state
def open_valve():
GPIO.output(GPIO_PIN_VALVE, GPIO.HIGH)
self.local_state['out_valve'] = 'open'
def close_valve():
GPIO.output(GPIO_PIN_VALVE, GPIO.LOW)
self.local_state['out_valve'] = 'closed'
def validate_states(self):
if self.local_state is None or self.remote_state is None:
raise ClassNotReadyException("Both local and remote states must be present!")
# TODO: add detailed validation

View File

@@ -0,0 +1,14 @@
class ClassNotReadyException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)
class ErrorCommunicatingWithServerException(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)

27
controller/state/file.py Normal file
View File

@@ -0,0 +1,27 @@
import json
import os.path
class File(object):
"Holds controller state in the file"
def __init__(self, filename=None):
self.filename = filename
def present(self):
os.path.isfile(self.filename)
def load(self):
if self.filename is None:
raise ClassNotReadyException("Filename not set!")
with open(self.filename) as input_file:
self.data = json.load(input_file)
def save(self):
if self.filename is None:
raise ClassNotReadyException("Filename not set!")
if self.data is None:
raise ClassNotReadyException("Data not loaded!")
with open(self.filename, 'w') as out_file:
json.dump(self.data, out_file)

View File

@@ -0,0 +1,33 @@
import json
import requests
class Server(object):
"Gets state from server and sends it to the server after change"
def __init__(self, url_base=None, controller_id=None):
self.url_base = url_base
self.controller_id = controller_id
def get_state(self):
result = requests.get(self.full_url('state/%s') % self.controller_id)
return handle_response(result)
def post_state(self, local_state):
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!")
if self.url_base is None:
raise ClassNotReadyException("URL base not set!")
return self.url_base + '/' + action
def handle_response(response):
if response.status_code != 200:
raise ErrorCommunicatingWithServerException("Response not 200!")
else:
return response.json()