From 6ae519f78efaf69da765c7d5c4936f864f248e7b Mon Sep 17 00:00:00 2001 From: Marcel Otte Date: Sun, 15 Oct 2017 22:45:17 +0200 Subject: [PATCH] introduced service class --- config.json | 40 +++++++++++++++++++++++++++++++++++++ src/service.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++ src/temperature.py | 3 ++- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/service.py diff --git a/config.json b/config.json index 47ac94d..f8fa4a0 100644 --- a/config.json +++ b/config.json @@ -1,12 +1,14 @@ { "sensors": { "0":{ + "name": "inside", "BCM": 20, "GPIO": 20, "header-pin": 38, "pre-resistance": 9770.0 }, "1":{ + "name": "outside", "BCM": 26, "GPIO": 26, "header-pin": 37, @@ -82,5 +84,43 @@ }, "pwm-defaults":{ "frequency": 25000 + }, + "activated-channels": [ 0, 1], + "interval": 1.0, + "use_difference": false, + "thresholds-for": "inside", + "thresholds": { + "off":{ + "temp": 25, + "diff": 5, + "fan-up": false, + "fan-low": false, + "pwm-up": 0.0, + "pwm-low": 0.0 + }, + "33%": { + "temp": 35, + "diff": 15, + "fan-up": false, + "fan-low": true, + "pwm-up": 33.0, + "pwm-low": 33.0 + }, + "66%": { + "temp": 45, + "diff": 25, + "fan-up": true, + "fan-low": true, + "pwm-up": 100.0, + "pwm-low": 66.0 + }, + "100%": { + "temp": 100, + "diff": 50, + "fan-up": true, + "fan-low": true, + "pwm-up": 100.0, + "pwm-low": 100.0 + } } } \ No newline at end of file diff --git a/src/service.py b/src/service.py new file mode 100644 index 0000000..91ec9a4 --- /dev/null +++ b/src/service.py @@ -0,0 +1,49 @@ +from config import Config +import time +from threading import Thread +from temperature import Temperature +import graphitesend +from lowlevel import LowLevel + + +class Service(object): + def __init__(self): + self.__cfg = Config() + + pass + + def run(self): + interval = self.__cfg.get_data()["interval"] + t = time.time() + while True: + t = time.time() + time.sleep(t + interval - time.time()) + thread = Thread(self.work) + thread.start() + pass + + def work(self): + # get sensor data + channels = self.__cfg.get_data()["activated-channels"] + results = [] + for c in channels: + results.append(Temperature(c).read()) + + # check thresholds + thresholds = self.__cfg.get_data()["thresholds"] + sensors = self.__cfg.get_sensors_dict() + channel = -1 + for k in sensors: + if sensors[k]["name"] == self.__cfg.get_data()["thresholds-for"]: + channel = int(k) + if k < 0: + raise RuntimeError("fuck.") + for threshold in thresholds: + if results[channel] < threshold["temp"]: + LowLevel().lower_fan(threshold["fan-low"]) + LowLevel().lower_fan_pwm(threshold["pwm-low"]) + LowLevel().upper_fan(threshold["fan-up"]) + LowLevel().upper_fan_pwm(threshold["pwm-up"]) + break + pass + diff --git a/src/temperature.py b/src/temperature.py index 7baf858..888a9f8 100644 --- a/src/temperature.py +++ b/src/temperature.py @@ -4,7 +4,8 @@ from decimal import * from lowlevel import LowLevel from config import Config -class Temperature: + +class Temperature(object): def __init__(self, channel): self.__channel = channel self.__cfg = Config()