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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

# -*- coding: utf-8 -*- 

"""The component 

 

An I2C device, ... 

""" 

 

__license__ = """ 

    This file is part of Janitoo. 

 

    Janitoo is free software: you can redistribute it and/or modify 

    it under the terms of the GNU General Public License as published by 

    the Free Software Foundation, either version 3 of the License, or 

    (at your option) any later version. 

 

    Janitoo is distributed in the hope that it will be useful, 

    but WITHOUT ANY WARRANTY; without even the implied warranty of 

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

    GNU General Public License for more details. 

 

    You should have received a copy of the GNU General Public License 

    along with Janitoo. If not, see <http://www.gnu.org/licenses/>. 

 

""" 

__author__ = 'Sébastien GALLET aka bibi21000' 

__email__ = 'bibi21000@gmail.com' 

__copyright__ = "Copyright © 2013-2014-2015-2016 Sébastien GALLET aka bibi21000" 

 

import logging 

logger = logging.getLogger(__name__) 

from pkg_resources import resource_filename, Requirement, iter_entry_points 

 

from janitoo.utils import JanitooNotImplemented, HADD 

from janitoo.node import JNTNode 

from janitoo.options import JNTOptions 

 

class JNTComponent(object): 

    def __init__(self, oid='generic.generic', bus=None, addr=None, **kwargs): 

        """Initialise the component 

 

        :param oid: The oid implemented by the component. 

        :type oid: str 

        """ 

        self.name = kwargs.pop('name', 'Generic component') 

        self.product_name = kwargs.pop('product_name', 'Software generic component') 

        self.product_type = kwargs.pop('product_type', 'Software component') 

        self.product_manufacturer = kwargs.pop('product_manufacturer', 'Janitoo') 

        self.oid = oid 

        self._bus = bus 

        self._addr = addr 

        self.values = {} 

        self.cmd_classes = [] 

        self.node = None 

        self.mqttc = None 

        self.options = kwargs.get('options', {}) 

        if type(self.options) == type(dict()): 

            self.options = JNTOptions(self.options) 

        if self._bus is None: 

            self.value_factory = {} 

            for entrypoint in iter_entry_points(group = 'janitoo.values'): 

                self.value_factory[entrypoint.name] = entrypoint.load() 

        else: 

            self.value_factory = self._bus.value_factory 

 

    @property 

    def uuid(self): 

        """Return an uuid for the component 

 

        """ 

        return "%s" % (self._addr) 

 

    def loop(self, stopevent): 

        """loop 

 

        """ 

        pass 

 

    def check_heartbeat(self): 

        """Check that the component is 'available' 

 

        """ 

        if self._bus is not None: 

            return self._bus.check_heartbeat() 

        return False 

 

    def start(self, mqttc): 

        """Start the component. Can be used to start a thread to acquire data. 

 

        """ 

        self.mqttc = mqttc 

        for value in self.values: 

            self.values[value].start() 

        return True 

 

    def stop(self): 

        """Stop the component. 

 

        """ 

        for value in self.values: 

            self.values[value].stop() 

        return True 

 

    def create_node(self, hadd, **kwargs): 

        """Create a node associated to this component 

        """ 

        cb_check_hearbeat = self.check_heartbeat 

        try: 

            ret = cb_check_hearbeat() 

        except NotImplementedError: 

            cb_check_hearbeat = None 

        self.node = JNTNode(uuid=self.uuid, cmd_classes=self.cmd_classes, hadd=hadd, 

                name=self.name, product_name=self.product_name, product_type=self.product_type, product_manufacturer=self.product_manufacturer, 

                check_hearbeat_cb=cb_check_hearbeat, oid=self.oid, **kwargs) 

        return  self.node 

 

    #~ def value_poll_get(self, node_uuid, index, prefix=''): 

        #~ """ 

        #~ """ 

        #~ value_id = '%s_%s'%(prefix,'poll') 

        #~ temp_poll = self._bus.nodeman.options.get_option("%s"%node_uuid, value_id) 

        #~ if temp_poll is not None: 

            #~ try: 

                #~ self.node.values[value_id].poll_delay = int(temp_poll) 

            #~ except ValueError: 

                #~ logger.exception('Exception when retrieving poll %s', value_id) 

        #~ #print "%s" % self.node.values 

        #~ return self.node.values[value_id].poll_delay 

 

    #~ def value_poll_set(self, node_uuid, index, value, prefix=''): 

        #~ """ 

        #~ """ 

        #~ try: 

            #~ value_id = '%s_%s'%(prefix,'poll') 

            #~ self.node.values[value_id].poll_delay = int(value) 

            #~ self._bus.nodeman.add_poll(self.node.values[value_id]) 

            #~ self._bus.nodeman.options.set_option("%s"%node_uuid, value_id, '%s'%self.node.values[value_id].poll_delay) 

        #~ except ValueError: 

                #~ logger.exception('Exception when setting poll %s', value_id) 

 

    def get_bus_value(self, value_uuid): 

        '''Retrieve a bus's private value. Take care of exported buses 

        This is the preferred way to retrieve a value of the bus 

        ''' 

        #~ logger.debug('_bus %s'%self._bus) 

        if self._bus is not None: 

            return self._bus.get_bus_value(value_uuid) 

        return None 

 

    def resource_filename(self, path='public'): 

        """Needed to publish static files 

        """ 

        return resource_filename(Requirement.parse(self.get_package_name().split('.')[0]), path) 

 

    def get_package_name(self): 

        """Return the name of the package. Needed to publish static files 

 

        **MUST** be copy paste in every extension that publish statics files 

        """ 

        return __package__