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

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

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

"""The thread 

 

A thread that handle a bus, ... ie i2e, onewire, 

 

It also handle the controller for the janitoo protocol 

 

How do what : 

 

The tread : 

- hold the mqttc 

- ask the nodeman to boot : 

   - get an HADD for the controller 

   - get configuration for the controller and start the i2c bus, the onewire bus, ..... 

   - get an HADD for each nodes 

   - get configuration of the node and start it : ie the lcd03 of i2c, the cpu of the rapsy, ... 

 

Reloading configration: 

- inside the run loop of the thread so need to kill it and re-create a new one : only possible in the server. 

   The server (=the rapsy server) can do it but it should be accessible on mqtt. 

""" 

 

__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" 

 

# Set default logging handler to avoid "No handler found" warnings. 

import logging 

logger = logging.getLogger(__name__) 

 

import uuid as muuid 

#We must NOT subsitute % in value for alembic (database section) 

import ConfigParser 

from ConfigParser import RawConfigParser 

from datetime import datetime, timedelta 

 

from janitoo.utils import JanitooNotImplemented, HADD, HADD_SEP, CADD 

from janitoo.mqtt import MQTTClient 

 

class JNTOptions(object): 

    def __init__(self, options=None): 

        """The options 

 

        :param options: The options used to start the worker. 

        :type clientid: str 

        """ 

        #retrieve parameters in file 

        if options is not None: 

            self.data = options 

        else: 

            self.data = {} 

        self._cache = {} 

 

    def load(self): 

        """Load system section from file 

 

        :param options: The options used to start the worker. 

        :type clientid: str 

        """ 

        #retrieve parameters in file 

        system = self.get_options('system') 

        self.data.update(system) 

 

    def get_options(self, section): 

        """Retrieve options from a section 

        """ 

        #~ print self.data['conf_file'] 

        #~ print "section ", section, " in cache", section in self._cache 

        #~ print "data ", self.data 

        if section is None: 

            return {} 

        if section in self._cache: 

            return self._cache[section] 

        try: 

            if 'conf_file' in self.data and self.data['conf_file'] is not None: 

                config = RawConfigParser() 

                config.read([self.data['conf_file']]) 

                #~ print "items ", config.items(section) 

                self._cache[section] = dict(config.items(section)) 

                #~ print self._cache[section] 

                return self._cache[section] 

        except: 

            logger.exception("Catched exception") 

        return {} 

 

    def get_option(self, section, key, default = None): 

        """Retrieve options from a section 

        """ 

        #print self.data['conf_file'] 

        if section in self._cache and key in self._cache[section]: 

            return self._cache[section][key] 

        if section not in self._cache: 

            self.get_options(section) 

        if section in self._cache and key in self._cache[section]: 

            return self._cache[section][key] 

        try: 

            if 'conf_file' in self.data and self.data['conf_file'] is not None: 

                config = RawConfigParser() 

                config.read([self.data['conf_file']]) 

                opt = config.get(section, key) 

                if default is None: 

                    self._cache[section][key] = opt 

                    return self._cache[section][key] 

                else: 

                    if type(default) == type(0): 

                        try: 

                            self._cache[section][key] = int(opt) 

                            return self._cache[section][key] 

                        except: 

                            logger.exception("[%s] - Exception when converting option to integer : [%s] %s = %s", self.__class__.__name__, section, key, opt) 

                            return None 

                    elif type(default) == type(0.0): 

                        try: 

                            self._cache[section][key] = float(opt) 

                            return self._cache[section][key] 

                        except: 

                            logger.exception("[%s] - Exception when converting option to float : [%s] %s = %s", self.__class__.__name__, section, key, opt) 

                            return None 

                    self._cache[section][key] = opt 

                    return self._cache[section][key] 

        except ConfigParser.NoOptionError: 

            return default 

        except ConfigParser.NoSectionError: 

        #~ except ValueError: 

            return None 

        return None 

 

    def set_option(self, section, key, value): 

        """Retrieve options from a section 

        """ 

        if section not in self._cache: 

            self.get_options(section) 

        if 'conf_file' in self.data and self.data['conf_file'] is not None: 

            config = RawConfigParser() 

            config.read([self.data['conf_file']]) 

            if config.has_section(section) == False: 

                config.add_section(section) 

            self._cache[section][key] = value 

            config.set(section, key, "%s"%value) 

            with open(self.data['conf_file'], 'wb') as configfile: 

                config.write(configfile) 

                return True 

        return False 

 

    def set_options(self, section, data): 

        """Retrieve options from a section 

        """ 

        if section not in self._cache: 

            self.get_options(section) 

        #print self.data['conf_file'] 

        if 'conf_file' in self.data and self.data['conf_file'] is not None: 

            config = RawConfigParser() 

            config.read([self.data['conf_file']]) 

            if config.has_section(section) == False: 

                config.add_section(section) 

            if section not in  self._cache: 

                 self._cache[section] = {} 

            for key in data: 

                self._cache[section][key] = data[key] 

                config.set(section, key, "%s"%data[key]) 

            with open(self.data['conf_file'], 'wb') as configfile: 

                config.write(configfile) 

                return True 

        return False 

 

    def remove_options(self, section, data): 

        """Retrieve options from a section 

        """ 

        #print self.data['conf_file'] 

        if 'conf_file' in self.data and self.data['conf_file'] is not None: 

            config = RawConfigParser() 

            config.read([self.data['conf_file']]) 

            for key in data: 

                config.remove_option(section, key) 

                if section in self._cache and key in self._cache[section]: 

                    del self._cache[section][key] 

            with open(self.data['conf_file'], 'wb') as configfile: 

                config.write(configfile) 

                return True 

        return False 

 

    def remove_options(self, section): 

        """Remove a n entire section 

        """ 

        #print self.data['conf_file'] 

        if 'conf_file' in self.data and self.data['conf_file'] is not None: 

            config = RawConfigParser() 

            config.read([self.data['conf_file']]) 

            config.remove_section(section) 

            with open(self.data['conf_file'], 'wb') as configfile: 

                config.write(configfile) 

            if section in self._cache: 

                del self._cache[section] 

            return True 

        return False 

 

    def get_options_key(self, section, key, strict=False): 

        """Retrieve options which started with a key from a section 

        """ 

        #print self.data['conf_file'] 

        res = {} 

        options = self.get_options(section) 

        debi = len(key) 

        for okey in options.keys(): 

            if (strict == True and okey == key) or okey.startswith(key): 

                res[okey[debi:]] = options[okey] 

        return res 

 

    def get_settings(self, section): 

        """Retrieve settings from a section 

        """ 

        return self.get_options_key(section, "settings.") 

 

    def get_component_settings(self, section, component): 

        """Retrieve component's configuration from a section 

        """ 

        return self.get_options_key("%s.%s"%(section,component), "settings.") 

 

 

def get_option_autostart(options, section): 

    """Retrieve auto_start option from a section 

    """ 

    #print self.data['conf_file'] 

    if 'conf_file' in options and options['conf_file'] is not None: 

        config = RawConfigParser() 

        config.read([options['conf_file']]) 

        try: 

            return config.getboolean(section, 'auto_start') 

        except ConfigParser.NoOptionError: 

            return False 

        except ConfigParser.NoSectionError: 

            return False 

    return False 

 

def string_to_bool(data): 

    """Convert a string to bool 

    """ 

    if type(data) == type(True): 

        return data 

    data = data.strip().upper() 

    if data == "0" or data == "FALSE" or data == 'OFF' or data == 'NO': 

        return False 

    return True