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

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

"""The bus 

 

A physical bus : i2c, 1-wire, ... 

""" 

 

__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 Sébastien GALLET aka bibi21000" 

 

import logging 

logger = logging.getLogger( "janitoo" ) 

 

from pkg_resources import iter_entry_points 

 

from utils import JanitooNotImplemented, HADD 

from janitoo.node import JNTNode 

 

############################################################## 

#Check that we are in sync with the official command classes 

#Must be implemented for non-regression 

from janitoo.classes import COMMAND_DESC 

 

COMMAND_CONTROLLER = 0x1050 

 

assert(COMMAND_DESC[COMMAND_CONTROLLER] == 'COMMAND_CONTROLLER') 

############################################################## 

 

class JNTBus(object): 

    def __init__(self, oid='generic', **kwargs): 

        """Initialise the bus 

 

        :param oid: The oid implemented by the bus. 

        :type oid: str 

        """ 

        self.oid = oid 

        self.factory = {} 

        for entry in iter_entry_points(group='janitoo.components', name=None): 

            if entry.name.startswith('%s.'%self.oid): 

                try: 

                    self.factory[entry.name] = entry.load() 

                except: 

                    logger.exception('Exception when loading entry_point %s',  entry.name) 

        self.components = {} 

        self.values = {} 

        self.cmd_classes = [COMMAND_CONTROLLER] 

        self._trigger_thread_reload_cb = None 

        self.mqttc = None 

        self.options = kwargs.get('options', None) 

        """The options""" 

        self.product_name = kwargs.get('product_name', 'Default product name') 

        """The product name of the node""" 

        self.product_type = kwargs.get('product_type', 'Default product type') 

        """The product type of the node""" 

        self.name = kwargs.get('name', 'Default bus name') 

        """The name""" 

        self.nodeman = None 

        self.value_factory = {} 

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

            try: 

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

            except: 

                logger.exception('Exception when loading entry_point %s',  entry.name) 

 

    def start(self, mqttc, trigger_thread_reload_cb=None): 

        self._trigger_thread_reload_cb = trigger_thread_reload_cb 

        self.mqttc = mqttc 

 

    def stop(self): 

        for compo in self.components.keys(): 

            self.components[compo].stop() 

        self.components = {} 

 

    @property 

    def uuid(self): 

        """Return an uuid for the bus. Must be the same as the section name to retrieve the hadd of the controller 

 

        """ 

        return "%s" % self.oid 

 

    def add_component(self, oid, addr, **kwargs): 

        """Return an uuid for the bus 

 

        """ 

        if addr in self.components: 

            return False 

        if oid not in self.factory: 

            logger.warning("[%s] - Can't find %s in factory", self.__class__.__name__, oid) 

            return None 

        compo = self.factory[oid](addr=addr, bus=self, **kwargs) 

        self.components[addr] = compo 

        return compo 

 

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

        """Create a node associated to this bus 

        """ 

        self.nodeman = nodeman 

        name = kwargs.pop('name', "%s controller"%self.name) 

        self.node = JNTNode(uuid=self.uuid, cmd_classes=self.cmd_classes, hadd=hadd, name="%s controller"%self.name, product_name=self.product_name, product_type=self.product_type, **kwargs) 

        return self.node