def __init__(self): self.message_broker = infrastructure.Messaging() self.logger = get_logger("SensorWorker") self.sleep_time = 10 self._reading_loop_should_run = True self._command_loop_should_run = True self._worker_loop_should_run = True self.sensor_modules = self._import_sensor_modules() self.loaded_sensor_modules = []
def __init__(self): self.message_broker = infrastructure.Messaging() self.logger = logging.get_logger("MonitoringLogger") self.sleep_time = 10 self._shared_queue = queue.Queue(maxsize=100) self.monitoring_topics = None self.monitoring_stages = None self._consumer_loop_is_running = True self._producer_loop_in_running = True self._main_loop_is_running = True self._producer = None self._consumer = None
def __init__(self): super().__init__() self._pubsub = self.client.get_redis().pubsub() self.logger = get_logger("MonitoringMessaging")
class SensorModule(abc.ABC): _config: SensorConfig = None _db: Database = Database() _logger = get_logger("SensorModule") def __init__(self): self._initialize() def _save_config(self): if self._config: self._db.save_config(self._config.id, self._config.__dict__) def _load_config(self): old_config = self._db.load_config(self._config.id) if old_config: self._config = SensorConfig(**old_config) def _initialize(self): self._config = self._configure() self._load_config() # Custom logic to be implemented in sub-classes. def _configure(self) -> SensorConfig: """ Should return an instance of SensorConfig. """ raise NotImplementedError() def _get_data(self) -> typing.List[SensorMeasurement]: """ Performs a read from the sensor. :return: A list of SensorMeasurement. """ raise NotImplementedError() @property def id(self) -> str: return self._config.id @property def name(self) -> str: """ Returns a friendly name for the sensor. """ return self._config.name @property def description(self) -> str: """ Returns the sensor's description. """ return self._config.description @property def is_enabled(self) -> bool: """ Queries if the sensor is enabled or not. """ return self._config.enabled @property def state(self) -> str: """ Queries the state the sensor is in. Valid states are defined in the SensorState enum. """ return self._config.state def get_data(self) -> typing.List[SensorMeasurement]: """ Performs a sensor read and returns the data. """ if not self._config.enabled: raise SensorException( "Invalid operation: performing read on disabled sensor.") return self._get_data() def enable(self): """ Enables the sensor. """ if not self._config.enabled: self._config.enabled = True self._save_config() def disable(self): """ Disables the sensor. """ if self._config.enabled: self._config.enabled = False self._save_config() def set_state(self, sensor_state: SensorState): """ Sets the sensor state. """ self._config.state = sensor_state.value self._save_config()
import nucuhub.monitoring.infrastructure as infrastructure from nucuhub.logging import get_logger from nucuhub.monitoring import ConsumerStage logger = get_logger("MonitoringWorkflows") class DebugWorkflow(ConsumerStage): name = "DebugWorkflow" def process(self, message): logger.debug(f"DebugWorkflow {message}") return False class SensorsWorkflow(ConsumerStage): name = "SensorsWorkflow" firebase_collection = "sensors" def process(self, message): """ Processes message of the type: Example message: {'type': 'message', 'pattern': None, 'channel': b'sensors', 'data': b'test'} :param message: """ type = message.get("type") channel = message.get("channel", "").decode() stop = False if type == "message" and channel == "sensors": data = infrastructure.Messaging.decode_message_data(message) db = infrastructure.Firebase.instance()
def __init__(self): super().__init__() self._pubsub = self.client.get_redis().pubsub() self._pubsub.subscribe("sensors_cmd") self.logger = get_logger("SensorsMessaging")
import json from nucuhub.infrastructure.redis import RedisService from nucuhub.logging import get_logger logger = get_logger("sensors.infrastructure") class RedisBackend: client: RedisService = None def __init__(self): self.client = RedisService.instance() class Messaging(RedisBackend): def __init__(self): super().__init__() self._pubsub = self.client.get_redis().pubsub() self._pubsub.subscribe("sensors_cmd") self.logger = get_logger("SensorsMessaging") def publish(self, data): """ Publishes data to the sensors topic on Redis. """ redis = self.client.get_redis() logger.debug(data) redis.publish("sensors", json.dumps(data)) def get_command(self):
def init_logger(self, name): """ Initializes basic logging. :param name: The logger's name. """ self._logger = get_logger(name)
import redis from nucuhub.config import ApplicationConfig from nucuhub.logging import get_logger logger = get_logger("RedisService") class RedisService: __singleton = None __client = None def __init__(self): """ Don't call this method directly instead use instance. """ pass @classmethod def instance(cls): if cls.__singleton is None: cls.redis_url = ApplicationConfig.REDIS_URL logger.info( f"RedisSingleton: Setting Connection String={cls.redis_url}") cls.__singleton = cls() cls.__client = redis.Redis(cls.redis_url) return cls.__singleton @classmethod def set_singleton(cls, singleton): cls.__singleton = singleton
import pyrebase from nucuhub.config import ApplicationConfig from nucuhub.logging import get_logger logger = get_logger("FirebaseService") class FirebaseConfiguration: api_key = None auth_domain = None database_url = None storage_bucket = None user_email = None user_password = None def __init__( self, api_key=None, auth_domain=None, database_url=None, storage_bucket=None, user_email=None, user_password=None, ): self.api_key = api_key or ApplicationConfig.FIREBASE_API_KEY self.auth_domain = auth_domain or ApplicationConfig.FIREBASE_AUTH_DOMAIN self.database_url = database_url or ApplicationConfig.FIREBASE_DATABASE_URL self.storage_bucket = (storage_bucket or ApplicationConfig.FIREBASE_STORAGE_BUCKET) self.user_email = user_email or ApplicationConfig.FIREBASE_USER_EMAIL