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

Stubbing and config #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions src/conf/conf_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import ConfigParser
import os

class ConfigHelper:
def __init__(self):
dir_path = os.path.dirname(__file__)
self._config_file_location = dir_path+"/rpi-eco-light.ini"

def get_current_config(self):
config = ConfigParser.ConfigParser()
config.read(self._config_file_location)
return config
35 changes: 35 additions & 0 deletions src/conf/rpi-eco-light.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
;'s are for comments

[service]
log_level = DEBUG
update_interval_in_sec = 5
db_path = /opt/eagleowl
max_update_checks = 20

[monitor]
;Can be either 'KW' or 'PENCE'
type = KW
cost_per_hour_in_pence = 13.556

[levels]
;KW Values
;Blue
level_1 = 0.6
;Green
level_2 = 0.74
;Yellow
level_3 = 1.1
;Orange
level_4 = 1.47
;Red

;Pence Values
;;Blue
;level_1 = 8
;;Green
;level_2 = 10
;;Yellow
;level_3 = 15
;;Orange
;level_4 = 20
;;Red
13 changes: 9 additions & 4 deletions src/db_comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@


class DBComms:
def __init__(self, db_location):
def __init__(self, config_helper):
self._logger = logging.getLogger(self.__class__.__name__)
self._db_location = db_location
self._config_helper = config_helper
self._live_location = ".live"
self._last_update_time = None
self._update_checks = 0
self._max_update_checks = 10

def get_current_kw(self):
with open("{}/{}".format(self._db_location,
current_config = self._config_helper.get_current_config()
with open("{}/{}".format(current_config.get('service','db_path'),
self._live_location),
"r") as live_file:
line = live_file.readline()
Expand All @@ -27,7 +28,9 @@ def get_current_kw(self):

def check_comms_status(self):
comms_good = True
temp_last_update_time = time.ctime(os.path.getmtime(self._db_location + "/" + self._live_location))
current_config = self._config_helper.get_current_config()
self._max_update_checks = current_config.get('service','max_update_checks')
temp_last_update_time = time.ctime(os.path.getmtime(current_config.get('service','db_path') + "/" + self._live_location))
if not self._last_update_time:
# not initialised
self._last_update_time = temp_last_update_time
Expand All @@ -43,4 +46,6 @@ def check_comms_status(self):
# We've reached our max attempts to query - something's gone wrong
self._logger.error("Max update checks reached, last update time: %s" % temp_last_update_time)
comms_good = False
else:
self._logger.debug("Value has not been updated in %d queries, max queries before error: %d", self._update_checks, self._max_update_checks)
return comms_good
25 changes: 25 additions & 0 deletions src/db_comms_stub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os
import time
import logging


class DBComms:
def __init__(self, config_helper):
self._logger = logging.getLogger(self.__class__.__name__)
self._config_helper = config_helper
self._live_location = ".live"
self._last_update_time = None
self._update_checks = 0
self._max_update_checks = 10
self._current_kw = 0.3

def get_current_kw(self):
ret_val = self._current_kw
self._current_kw += 0.1
if self._current_kw > 1.8:
self._current_kw = 0.3
return ret_val

def check_comms_status(self):
comms_good = True
return comms_good
57 changes: 36 additions & 21 deletions src/energy_usage_light.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,48 @@
import logging

from db_comms import DBComms
from lighting import Lighting
#from db_comms import DBComms
#from lighting import Lighting
from db_comms_stub import DBComms
from lighting_stub import Lighting

logger = logging.getLogger(__name__)


def kw_to_rgb(kw, pence_per_hour):
def kw_to_rgb(kw, current_config):
"""
Converts Power (KW) to a RGB value
:param kw: power
:param pence_per_hour: the cost in pence per kw per hour
:param current_config: the current configuration file
:return: a dict for RGB values
"""
kw_in_pence = pence_per_hour * kw
logger.debug("KW in pence: %s" % kw_in_pence)
if kw_in_pence >= 20:
logger.debug(">= 20 = RED")
lighting_value = kw
if (current_config.get('monitor','type') == "PENCE"):
lighting_value = current_config.getfloat('monitor','cost_per_hour_in_pence') * kw
logger.debug("KW in pence: %s" % lighting_value)
else:
logger.debug("KW: %s" % lighting_value)

level_1 = current_config.getfloat('levels','level_1')
level_2 = current_config.getfloat('levels','level_2')
level_3 = current_config.getfloat('levels','level_3')
level_4 = current_config.getfloat('levels','level_4')

if lighting_value >= level_4:
logger.debug(">= %.2f = RED",level_4)
rgb_dict = Lighting.RED
elif kw_in_pence >= 15:
logger.debug("< 20 && >= 15 = ORANGE")
elif lighting_value >= level_3:
logger.debug("< %.2f && >= %.2f = ORANGE",level_4, level_3)
rgb_dict = Lighting.ORANGE
elif kw_in_pence >= 10:
logger.debug("< 15 && >= 10 = YELLOW")
elif lighting_value >= level_2:
logger.debug("< %.2f && >= %.2f = YELLOW",level_3,level_2)
rgb_dict = Lighting.YELLOW
elif kw_in_pence >= 8:
logger.debug("< 10 && >= 8 = GREEN")
elif lighting_value >= level_1:
logger.debug("< %.2f && >= %.2f = GREEN",level_2, level_1)
rgb_dict = Lighting.GREEN
elif 0 > kw_in_pence < 8:
logger.debug("< 8 = BLUE")
elif lighting_value < level_1 and lighting_value > 0:
logger.debug("< %.2f = BLUE",level_1)
rgb_dict = Lighting.BLUE
elif kw_in_pence == 0:
elif lighting_value == 0:
logger.debug("==0 = PURPLE (No value yet)")
rgb_dict = Lighting.PURPLE
else:
Expand All @@ -40,15 +52,18 @@ def kw_to_rgb(kw, pence_per_hour):


class EnergyUsageLight(object):
def __init__(self, db_path, pence_per_hour):
self._comms = DBComms(db_path)
def __init__(self, config_helper):
self._config_helper = config_helper
current_config = config_helper.get_current_config()
self._comms = DBComms(config_helper)
self._light = Lighting()
self._pence_per_hour = pence_per_hour

def update(self):
current_config = self._config_helper.get_current_config()

if not self._comms.check_comms_status():
self._light.set_error()
else:
kw = self._comms.get_current_kw()
rgb_dict = kw_to_rgb(kw, self._pence_per_hour)
rgb_dict = kw_to_rgb(kw, current_config)
self._light.set_light(rgb_dict)
53 changes: 53 additions & 0 deletions src/lighting_stub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import time
import logging

class Lighting:
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
YELLOW = (177, 142, 52)
ORANGE = (255, 165, 0)
RED = (255, 0, 0)
PURPLE = (148, 0, 211)

def __init__(self):
self._logger = logging.getLogger(self.__class__.__name__)
self._current_value = self.BLUE
pixels = self._set_whole_grid(self._current_value)

def set_light(self, target_value):
set_to_value = self._current_value
while set_to_value != target_value:
set_to_value = self._fade_between_rgb(set_to_value, target_value,1)
pixels = self._set_whole_grid(set_to_value)
time.sleep(0.01)
self._current_value = set_to_value

def set_error(self):
self._current_value = self.PURPLE
self._logger.error("Light set to error state")

def _set_whole_grid(self, rgb_val):
pixels = []
for _ in range(8):
pixels.append([])
for _ in range(8):
pixels[len(pixels)-1].append(rgb_val)
return pixels

def _fade_between_rgb(self, current_rgb, desired_rgb, change_step=2):
r = 0
g = 0
b = 0
for i in range(0, change_step):
r = self._inc_dec(current_rgb[0], desired_rgb[0])
g = self._inc_dec(current_rgb[1], desired_rgb[1])
b = self._inc_dec(current_rgb[2], desired_rgb[2])
return r, g, b

def _inc_dec(self, curr_val, des_val):
ret_val = curr_val
if curr_val < des_val:
ret_val += 1
elif curr_val > des_val:
ret_val -= 1
return ret_val
16 changes: 8 additions & 8 deletions src/rpi-eco-light.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@
import logging.config

from conf.logging import LOGGING
from conf.conf_helper import ConfigHelper
from energy_usage_light import EnergyUsageLight


DB_PATH = "/opt/eagleowl"
SLEEP_TIME_IN_SECS = 5
COST_PER_HOUR_IN_PENCE = 13.37

logging.config.dictConfig(LOGGING)
logger = logging.getLogger(__name__)

config_helper = ConfigHelper()

def signal_term_handler(signal, frame):
logger.fatal("Handling SIGTERM")
sys.exit(0)


if __name__ == "__main__":
logger.info("Starting RPi ECO Light")
signal.signal(signal.SIGINT, signal_term_handler)
usage_light = EnergyUsageLight(DB_PATH, COST_PER_HOUR_IN_PENCE)
usage_light = EnergyUsageLight(config_helper)
while True:
current_config = config_helper.get_current_config()
LOGGING['handlers']['console']['level'] = current_config.get('service','log_level')
logging.config.dictConfig(LOGGING)

usage_light.update()
#Flush the stdout each time round the loop
sys.stdout.flush()

#Sleep for a bit before the next update
time.sleep(SLEEP_TIME_IN_SECS)
time.sleep(float(current_config.get('service','update_interval_in_sec')))