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

Config and Enum files #68

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion src/actuator/actuator.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from distutils.command.config import config
from tools.status import Status
import tools.config as config

# Actuator Interface


class Actuator:
"""Actuator Interface class, to be overriden by actuator subclasses."""

MAIN_LISTEN_TOPIC = "pid_update"
MAIN_LISTEN_TOPIC = config.pid_update

def __init__(
self,
Expand Down
3 changes: 2 additions & 1 deletion src/actuator/fans.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from actuator.actuator import Actuator
from pubsub import pub
import RPi.GPIO as GPIO
import tools.config as config


class Fans(Actuator):
Expand All @@ -25,7 +26,7 @@ def __init__(self, *args, **kwargs):
self._fan_out_speed: float = 0.0
pub.subscribe(
self.fan_status_listener,
f"{Actuator.MAIN_LISTEN_TOPIC}.actuator.fans_status",
f"{Actuator.MAIN_LISTEN_TOPIC}.{config.actuator}.{config.fan_status}",
)
GPIO.setup(Fans.FAN_IN_PIN0, GPIO.OUT)
GPIO.setup(Fans.FAN_IN_PIN1, GPIO.OUT)
Expand Down
3 changes: 2 additions & 1 deletion src/actuator/led_lights.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from actuator.actuator import Actuator
from pubsub import pub
import RPi.GPIO as GPIO
import tools.config as config


class LEDLights(Actuator):
Expand All @@ -21,7 +22,7 @@ def __init__(self, *args, **kwargs):
self._brightness = 0
pub.subscribe(
self.light_status_listener,
f"{Actuator.MAIN_LISTEN_TOPIC}.actuator.light_status",
f"{Actuator.MAIN_LISTEN_TOPIC}.{config.actuator}.{config.light_status}",
)
GPIO.setup(LEDLights.LED_PIN, GPIO.OUT)

Expand Down
3 changes: 2 additions & 1 deletion src/actuator/water_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from actuator.actuator import Actuator
from pubsub import pub
import RPi.GPIO as GPIO
import tools.config as config


class Waterpump(Actuator):
Expand All @@ -21,7 +22,7 @@ def __init__(self, *args, **kwargs):
self._is_on = True
pub.subscribe(
self.water_pump_listener,
f"{Actuator.MAIN_LISTEN_TOPIC}.actuator.pump_status",
f"{Actuator.MAIN_LISTEN_TOPIC}.{config.actuator}.{config.water_pump_status}",
)
GPIO.setup(Waterpump.PUMP_PIN, GPIO.OUT)

Expand Down
5 changes: 3 additions & 2 deletions src/analyser/analyser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pubsub import pub
from typing import List
import tools.config as config


class Analyser:
Expand All @@ -14,9 +15,9 @@ def __init__(
self._id = analyser_id
for topic in self._sensor_type_topics:
pub.subscribe(
self.datastream_update_listener, "database_update." + topic
self.datastream_update_listener, config.database_update +"." + topic
)
pub.subscribe(self.analyser_listener, "pid_update." + topic)
pub.subscribe(self.analyser_listener, config.pid_update + "." + topic)

def analyser_listener(self, args, rest=None):
pass
Expand Down
7 changes: 4 additions & 3 deletions src/analyser/brightness_analyser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from pubsub import pub
from analyser.analyser import Analyser
import tools.config as config


class BrightnessAnalyser(Analyser):
Expand All @@ -16,17 +17,17 @@ def analyser_listener(self, args, rest=None):
if brightness > 270:
sensor_data.actuator_value = 0.5
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.light_status", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.light_status}", args=sensor_data
) # Lights 50% on
elif brightness > 270:
sensor_data.actuator_value = 0.2
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.light_status", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.light_status}", args=sensor_data
) # Lights 20% on
else:
sensor_data.actuator_value = 0
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.light_status", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.light_status}", args=sensor_data
)

def analyser_datastream_update_listener(self, args, rest=None):
Expand Down
21 changes: 11 additions & 10 deletions src/analyser/brightness_pid.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
from pubsub import pub
from analyser.analyser import Analyser
from pid.pid import PID

import tools.config as config
from tools.analysis_constants import Analysis_Constants as analysis

class BrightnessPidAnalyser(Analyser):
def __init__(self, *args, **kwargs):
super().__init__(["sensor_data.light_sensor"])
self._p_parameter = 1.2
self._i_parameter = 0.5
self._d_parameter = 0.001
super().__init__([config.sensor_data + "." + config.light_sensor])
self._p_parameter = analysis.BRIGHTNESS_P
self._i_parameter = analysis.BRIGHTNESS_I
self._d_parameter = analysis.BRIGHTNESS_D
self._pid = PID(
self._p_parameter, self._i_parameter, self._d_parameter
)
self._pid.SetPoint = 270
self._pid.SetPoint = analysis.BRIGHTNESS_SETPOINT

def analyser_listener(self, args, rest=None):
MAIN_PUBSUB_TOPIC = "pid_update" # TODO move to enum/config file
MAIN_PUBSUB_TOPIC = config.pid_update # TODO move to enum/config file
brightness = args.sensor_value
sensor_data = args
feedback = brightness
Expand All @@ -27,11 +28,11 @@ def analyser_listener(self, args, rest=None):
sensor_data.actuator_value = output

pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.light_status", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.light_status}", args=sensor_data
)

def datastream_update_listener(self, args, rest=None):
MAIN_PUBSUB_TOPIC = "database_update"
MAIN_PUBSUB_TOPIC = config.database_update
brightness = args.sensor_value
sensor_data = args
feedback = brightness
Expand All @@ -42,5 +43,5 @@ def datastream_update_listener(self, args, rest=None):
output = max(0, min(output, 100))
sensor_data.actuator_value = output
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.light_status", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.light_status}", args=sensor_data
)
20 changes: 11 additions & 9 deletions src/analyser/humidity_pid.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from pubsub import pub
from analyser.analyser import Analyser
from pid.pid import PID
import tools.config as config
from tools.analysis_constants import Analysis_Constants as analysis


class HumidityPidAnalyser(Analyser):
def __init__(self, *args, **kwargs):
super().__init__(["sensor_data.humidity_sensor"])
self._p_parameter = 1.2
self._i_parameter = 0.5
self._d_parameter = 0.001
super().__init__([config.sensor_data + "." + config.humidity_sensor])
self._p_parameter = analysis.HUMIDITY_P
self._i_parameter = analysis.HUMIDITY_I
self._d_parameter = analysis.HUMIDITY_D
self._pid = PID(
self._p_parameter, self._i_parameter, self._d_parameter
)
self._pid.SetPoint = 55
self._pid.SetPoint = analysis.HUMIDITY_SETPOINT

def analyser_listener(self, args, rest=None):
MAIN_PUBSUB_TOPIC = "pid_update" # TODO move to enum/config file
MAIN_PUBSUB_TOPIC = config.pid_update # TODO move to enum/config file
humidity = args.sensor_value
sensor_data = args
feedback = humidity
Expand All @@ -27,11 +29,11 @@ def analyser_listener(self, args, rest=None):
sensor_data.actuator_value = output

pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.fans_status", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.fans_status}", args=sensor_data
)

def datastream_update_listener(self, args, rest=None):
MAIN_PUBSUB_TOPIC = "database_update"
MAIN_PUBSUB_TOPIC = config.database_update
humidity = args.sensor_value
sensor_data = args
feedback = humidity
Expand All @@ -44,5 +46,5 @@ def datastream_update_listener(self, args, rest=None):

print("DB update: ", sensor_data)
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.fans_status", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.fans_status}", args=sensor_data
)
23 changes: 12 additions & 11 deletions src/analyser/moisture_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
from analyser.analyser import Analyser
from pid.pid import PID
import time

import tools.config as config
from tools.analysis_constants import Analysis_Constants as analysis

class MoisturePidAnalyser(Analyser):
def __init__(self, *args, **kwargs):
super().__init__(["sensor_data.soil_moisture_sensor"])
self._p_parameter = 1.2
self._i_parameter = 0.5
self._d_parameter = 0.001
super().__init__([config.sensor_data + "." + config.soil_moisture_sensor])
self._p_parameter = analysis.MOISTURE_P
self._i_parameter = analysis.MOISTURE_I
self._d_parameter = analysis.MOISTURE_D
self._pid = PID(
self._p_parameter, self._i_parameter, self._d_parameter
)
self._pid.SetPoint = 50
self._pid.SetPoint = analysis.MOISTURE_SETPOINT

def analyser_listener(self, args, rest=None):
MAIN_PUBSUB_TOPIC = "pid_update" # TODO move to enum/config file
MAIN_PUBSUB_TOPIC = config.pid_update # TODO move to enum/config file
soilmoisture = round(((args.sensor_value * 3300) / 1024), 0)
sensor_data = args
feedback = soilmoisture
Expand All @@ -29,21 +30,21 @@ def analyser_listener(self, args, rest=None):
# monitor/on/off state)
clock = 5
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.water_pump_status", args=1.0
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.water_pump_status}", args=1.0
) # pump on
time.sleep(output * clock)
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.water_pump_status", args=0
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.water_pump_status}", args=0
) # pump off
time.sleep(clock - (output * clock))

def datastream_update_listener(self, args, rest=None):
MAIN_PUBSUB_TOPIC = "database_update"
MAIN_PUBSUB_TOPIC = config.database_update
sensor_data = args
output = (100 - self._pid.output) / 100
sensor_data.actuator_value = output
# TODO either change to send the on off status (will be inaccurate)
# , or see issue #55
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.water_pump_status", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.water_pump_status}", args=sensor_data
)
11 changes: 6 additions & 5 deletions src/analyser/water_level_analyser.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from pubsub import pub
from analyser.analyser import Analyser
import tools.config as config


class WaterLevelAnalyser(Analyser):
def __init__(self, *args, **kwargs):
super().__init__(["sensor_data.water_level_sensor"])
super().__init__([config.sensor_data + "." + config.water_level_sensor])

def analyser_listener(self, args, rest=None):
MAIN_PUBSUB_TOPIC = "pid_update" # TODO move to enum/config file
MAIN_PUBSUB_TOPIC = config.pid_update # TODO move to enum/config file
water_level = round(((args.sensor_value * 3300) / 1024), 0)
sensor_data = args
if water_level < 50:
Expand All @@ -20,13 +21,13 @@ def analyser_listener(self, args, rest=None):
-1
) # Set actuator to -1 to avoid null values in DB
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.water_level", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.water_level}", args=sensor_data
)

def datastream_update_listener(self, args, rest=None):
MAIN_PUBSUB_TOPIC = "database_update"
MAIN_PUBSUB_TOPIC = config.database_update
sensor_data = args
sensor_data.actuator_value = -1
pub.sendMessage(
f"{MAIN_PUBSUB_TOPIC}.actuator.water_level", args=sensor_data
f"{MAIN_PUBSUB_TOPIC}.{config.actuator}.{config.water_level}", args=sensor_data
)
13 changes: 7 additions & 6 deletions src/data_streamer/azure_database_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from dotenv import load_dotenv
import os
import pyodbc
import tools.config as config


class AzureDatabaseManager(DatabaseManager):
sensor_data_topic = "database_update.actuator"
sensor_data_topic = config.database_update + "." + config.actuator

def __init__(self, database_path: str = "test.db"):
super().__init__("azure_db")
Expand Down Expand Up @@ -111,7 +112,7 @@ def get_current_time_iso_cut():
db.create_sensor_data_table()
print(get_current_time_iso_cut())
pub.sendMessage(
"actuator",
config.actuator,
args=SensorData(
get_current_time_iso_cut(), -1, "test_sensor_type", -999, 50
),
Expand All @@ -120,25 +121,25 @@ def get_current_time_iso_cut():
val = 0
for x in range(3):
pub.sendMessage(
"actuator",
config.actuator,
args=SensorData(
get_current_time_iso_cut(), 1, "brightness", 900 + val, 50
),
)
pub.sendMessage(
"actuator",
config.actuator,
args=SensorData(
get_current_time_iso_cut(), 2, "humidity", 55 + val, 50
),
)
pub.sendMessage(
"actuator",
config.actuator,
args=SensorData(
get_current_time_iso_cut(), 3, "temperature", 20 + val, 50
),
)
pub.sendMessage(
"actuator",
config.actuator,
args=SensorData(
get_current_time_iso_cut(), 4, "water level", 15 + val, 50
),
Expand Down
4 changes: 2 additions & 2 deletions src/data_streamer/database_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sqlite3
from pubsub import pub

import tools.config as config

class DatabaseManager:
sensor_data_topic = "database_update.actuator"
Expand Down Expand Up @@ -98,7 +98,7 @@ def __repr__(self) -> str:
with DatabaseManager() as db:
db.create_sensor_data_table()
pub.sendMessage(
"actuator",
config.actuator,
args=SensorData(time(), -1, "test_sensor_type", -999, 50),
)
print(db)
Expand Down
4 changes: 2 additions & 2 deletions src/data_streamer/iot_hub_streamer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from pubsub import pub
from datetime import datetime
import os

import tools.config as config

class IoTHubStreamer:
sensor_data_topic = "database_update.actuator"
sensor_data_topic = config.database_update + "." + config.actuator
MSG_TEXT = (
'{{"Timestamp": {timestamp},'
'"SensorID": {sensor_id},'
Expand Down
Loading