Commit 6b246594 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Device - EmulatedDriver:

- first try of decorating device driver
parent de520e7c
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ from apscheduler.executors.pool import ThreadPoolExecutor
from apscheduler.job import Job
from apscheduler.jobstores.memory import MemoryJobStore
from apscheduler.schedulers.background import BackgroundScheduler
from common.perf_eval_method_wrapper.Decorator import MetricsPool, meter_method
from common.type_checkers.Checkers import chk_float, chk_length, chk_string, chk_type
from device.service.database.KpiSampleType import ORM_KpiSampleTypeEnum, grpc_to_enum__kpi_sample_type
from device.service.driver_api._Driver import (
@@ -122,6 +123,8 @@ def do_sampling(
    value     = abs(0.95 * waveform + 0.05 * noise)
    out_samples.put_nowait((timestamp, resource_key, value))

METRICS_POOL = MetricsPool(labels={'driver': 'emulated'})

class EmulatedDriver(_Driver):
    def __init__(self, address : str, port : int, **settings) -> None: # pylint: disable=super-init-not-called
        self.__lock = threading.Lock()
@@ -170,10 +173,12 @@ class EmulatedDriver(_Driver):
        self.__scheduler.shutdown()
        return True

    @meter_method(METRICS_POOL)
    def GetInitialConfig(self) -> List[Tuple[str, Any]]:
        with self.__lock:
            return dump_subtree(self.__initial)

    @meter_method(METRICS_POOL)
    def GetConfig(self, resource_keys : List[str] = []) -> List[Tuple[str, Union[Any, None, Exception]]]:
        chk_type('resources', resource_keys, list)
        with self.__lock:
@@ -197,6 +202,7 @@ class EmulatedDriver(_Driver):
                results.extend(dump_subtree(resource_node))
            return results

    @meter_method(METRICS_POOL)
    def SetConfig(self, resources : List[Tuple[str, Any]]) -> List[Union[bool, Exception]]:
        chk_type('resources', resources, list)
        if len(resources) == 0: return []
@@ -231,6 +237,7 @@ class EmulatedDriver(_Driver):
                results.append(True)
        return results

    @meter_method(METRICS_POOL)
    def DeleteConfig(self, resources : List[Tuple[str, Any]]) -> List[Union[bool, Exception]]:
        chk_type('resources', resources, list)
        if len(resources) == 0: return []
@@ -268,6 +275,7 @@ class EmulatedDriver(_Driver):
                results.append(True)
        return results

    @meter_method(METRICS_POOL)
    def SubscribeState(self, subscriptions : List[Tuple[str, float, float]]) -> List[Union[bool, Exception]]:
        chk_type('subscriptions', subscriptions, list)
        if len(subscriptions) == 0: return []
@@ -305,6 +313,7 @@ class EmulatedDriver(_Driver):
                results.append(True)
        return results

    @meter_method(METRICS_POOL)
    def UnsubscribeState(self, subscriptions : List[Tuple[str, float, float]]) -> List[Union[bool, Exception]]:
        chk_type('subscriptions', subscriptions, list)
        if len(subscriptions) == 0: return []