from typing import Optional

from django.core.cache import caches

from devices.models.sensor import ArmaSensor
from devices.services.sensor.enums import SystemMessage
from devices.services.sensor.rabbitmq import SensorManagement


class SensorService:

    def __init__(self, sensor: ArmaSensor):
        self.sensor = sensor

    def get_status(self) -> Optional[dict]:
        sensor_status = caches['redis'].get(f'sensor_{self.sensor.pk}_status') or {'status': 'offline'}
        return sensor_status

    def get_interfaces(self):
        """Get all interfaces from sensor"""

        actually_interfaces = SensorManagement().send_message(
            sensor=self.sensor, message_type=SystemMessage.interfaces, wait_response=True)
        return actually_interfaces
