Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Copyright (c) 2019-2020 ETH Zurich, SIS ID and HVL D-ITET 

2# 

3""" 

4Module with base classes for communication protocols. 

5""" 

6 

7from abc import ABC, abstractmethod 

8from threading import RLock 

9from typing import Type 

10 

11from ..configuration import ConfigurationMixin, EmptyConfig 

12 

13 

14class CommunicationProtocol(ConfigurationMixin, ABC): 

15 """ 

16 Communication protocol abstract base class. 

17 

18 Specifies the methods to implement for communication protocol, as well as 

19 implements some default settings and checks. 

20 """ 

21 

22 def __init__(self, config) -> None: 

23 """ 

24 Constructor for CommunicationProtocol. Takes a configuration dict or 

25 configdataclass as the single parameter. 

26 

27 :param config: Configdataclass or dictionary to be used with the default 

28 config dataclass. 

29 """ 

30 

31 super().__init__(config) 

32 

33 #: Access lock to use with context manager when 

34 #: accessing the communication protocol (thread safety) 

35 self.access_lock = RLock() 

36 

37 @abstractmethod 

38 def open(self): 

39 """ 

40 Open communication protocol 

41 """ 

42 pass # pragma: no cover 

43 

44 @abstractmethod 

45 def close(self): 

46 """ 

47 Close the communication protocol 

48 """ 

49 pass # pragma: no cover 

50 

51 def __enter__(self): 

52 self.open() 

53 return self 

54 

55 def __exit__(self, exc_type, exc_val, exc_tb): 

56 self.close() 

57 

58 

59class NullCommunicationProtocol(CommunicationProtocol): 

60 """ 

61 Communication protocol that does nothing. 

62 """ 

63 

64 def open(self) -> None: 

65 """ 

66 Void open function. 

67 """ 

68 pass 

69 

70 def close(self) -> None: 

71 """ 

72 Void close function. 

73 """ 

74 pass 

75 

76 @staticmethod 

77 def config_cls() -> Type[EmptyConfig]: 

78 """ 

79 Empty configuration 

80 

81 :return: EmptyConfig 

82 """ 

83 return EmptyConfig