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

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

"""The value 

 

""" 

 

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

 

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

import os 

import logging 

logger = logging.getLogger("janitoo") 

 

from janitoo.classes import GENRE_DESC, VALUE_DESC 

from janitoo.utils import json_dumps 

from janitoo.value import JNTValue 

from janitoo.value_factory import JNTValueFactoryEntry 

 

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

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

#Must be implemented for non-regression 

from janitoo.classes import COMMAND_DESC 

 

COMMAND_CONFIGURATION = 0x0070 

COMMAND_SENSOR_BINARY = 0x0030 

COMMAND_SENSOR_MULTILEVEL = 0x0031 

 

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

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

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

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

 

def make_config_string(**kwargs): 

    return JNTValueConfigString(**kwargs) 

 

def make_config_password(**kwargs): 

    return JNTValueConfigPassword(**kwargs) 

 

def make_config_integer(**kwargs): 

    return JNTValueConfigInteger(**kwargs) 

 

def make_config_float(**kwargs): 

    return JNTValueConfigFloat(**kwargs) 

 

def make_config_boolean(**kwargs): 

    return JNTValueConfigBoolean(**kwargs) 

 

def make_config_array(**kwargs): 

    return JNTValueConfigArray(**kwargs) 

 

def make_config_list(**kwargs): 

    return JNTValueConfigList(**kwargs) 

 

class JNTValueConfigGeneric(JNTValueFactoryEntry): 

    """ 

    """ 

    def __init__(self, **kwargs): 

        """ 

        """ 

        get_data_cb = kwargs.pop('get_data_cb', self._get_data) 

        set_data_cb = kwargs.pop('set_data_cb', self._set_data) 

        cmd_class = kwargs.pop('cmd_class', COMMAND_CONFIGURATION) 

        genre = kwargs.pop('genre', 0x03) 

        is_readonly = kwargs.pop('is_readonly', False) 

        is_writeonly = kwargs.pop('is_writeonly', False) 

        JNTValueFactoryEntry.__init__(self, 

            get_data_cb=get_data_cb, set_data_cb=set_data_cb, 

            cmd_class=COMMAND_CONFIGURATION, 

            genre=genre, 

            is_readonly=is_readonly, is_writeonly=is_writeonly, 

            **kwargs) 

 

    def _set_data(self, node_uuid, index, data): 

        """ 

        """ 

        if index not in self.instances: 

            self.instances[index] = {} 

        try: 

            if index not in self.instances or self.instances[index] != data: 

                self.instances[index]['data'] = data 

                self.options.set_option(node_uuid, '%s_%s'%(self.uuid, index), '%s'%data) 

        except: 

            self.instances[index]['data'] = None 

            logger.exception('Exception when writing %s_%s for node %s'%(self.uuid, index, node_uuid)) 

 

    def _get_data(self, node_uuid, index): 

        """ 

        """ 

        if index not in self.instances: 

            self.instances[index] = {} 

        if 'data' not in self.instances[index]: 

            self.instances[index]['data'] = None 

        if self.instances[index]['data'] is None: 

            try: 

                self.instances[index]['data'] = self.options.get_option(node_uuid, '%s_%s'%(self.uuid, index)) 

            except: 

                logger.exception('Exception when retrieving %s_%s for node %s'%(self.uuid, index, node_uuid)) 

        return self.instances[index]['data'] 

 

class JNTValueConfigString(JNTValueConfigGeneric): 

    def __init__(self, entry_name="config_string", **kwargs): 

        """ 

        """ 

        help = kwargs.pop('help', 'A string') 

        label = kwargs.pop('label', 'String') 

        index = kwargs.pop('index', 0) 

        JNTValueConfigGeneric.__init__(self, entry_name=entry_name, help=help, label=label, 

            index=index, type=0x08, **kwargs) 

 

class JNTValueConfigList(JNTValueConfigGeneric): 

    def __init__(self, entry_name="config_list", **kwargs): 

        """ 

        """ 

        help = kwargs.pop('help', 'A string') 

        label = kwargs.pop('label', 'String') 

        list_items = kwargs.pop('list_items', ['value1', 'value2']) 

        index = kwargs.pop('index', 0) 

        JNTValueConfigGeneric.__init__(self, entry_name=entry_name, help=help, label=label, 

            index=index, type=0x05, list_items=list_items, **kwargs) 

 

class JNTValueConfigPassword(JNTValueConfigGeneric): 

    def __init__(self, entry_name="config_password", **kwargs): 

        """ 

        """ 

        help = kwargs.pop('help', 'A password') 

        label = kwargs.pop('label', 'Password') 

        index = kwargs.pop('index', 0) 

        JNTValueConfigGeneric.__init__(self, entry_name=entry_name, help=help, label=label, 

            index=index, type=0x14, **kwargs) 

 

class JNTValueConfigBoolean(JNTValueConfigGeneric): 

    def __init__(self, entry_name="config_boolean", **kwargs): 

        """ 

        """ 

        help = kwargs.pop('help', 'A boolean') 

        label = kwargs.pop('label', 'Bool') 

        index = kwargs.pop('index', 0) 

        get_data_cb = kwargs.pop('get_data_cb', self._get_data_bool) 

        JNTValueConfigGeneric.__init__(self, entry_name=entry_name, help=help, label=label, 

            get_data_cb=get_data_cb, 

            index=index, type=0x01, **kwargs) 

 

    def _get_data_bool(self, node_uuid, index): 

        """ 

        """ 

        try: 

            data = self._get_data(node_uuid, index) 

            if data is not None: 

                self.instances[index]['data'] = bool(data) 

            else: 

                self.instances[index]['data'] = None 

        except: 

            logger.exception('Exception when retrieving %s_%s for node %s'%(self.uuid, index, node_uuid)) 

        return self.instances[index]['data'] 

 

class JNTValueConfigInteger(JNTValueConfigGeneric): 

    def __init__(self, entry_name="config_integer", **kwargs): 

        """ 

        """ 

        help = kwargs.pop('help', 'A float') 

        label = kwargs.pop('label', 'Float') 

        index = kwargs.pop('index', 0) 

        get_data_cb = kwargs.pop('get_data_cb', self._get_data_integer) 

        JNTValueConfigGeneric.__init__(self, entry_name=entry_name, help=help, label=label, 

            get_data_cb=get_data_cb, 

            index=index, type=0x04, **kwargs) 

 

    def _get_data_integer(self, node_uuid, index): 

        """ 

        """ 

        try: 

            data = self._get_data(node_uuid, index) 

            if data is not None: 

                self.instances[index]['data'] = int(data) 

            else: 

                self.instances[index]['data'] = None 

        except: 

            logger.exception('Exception when retrieving %s_%s for node %s'%(self.uuid, index, node_uuid)) 

        return self.instances[index]['data'] 

 

class JNTValueConfigFloat(JNTValueConfigGeneric): 

    def __init__(self, entry_name="config_integer", **kwargs): 

        """ 

        """ 

        help = kwargs.pop('help', 'An integer') 

        label = kwargs.pop('label', 'integer') 

        index = kwargs.pop('index', 0) 

        get_data_cb = kwargs.pop('get_data_cb', self._get_data_float) 

        JNTValueConfigGeneric.__init__(self, entry_name=entry_name, help=help, label=label, 

            get_data_cb=get_data_cb, 

            index=index, type=0x04, **kwargs) 

 

    def _get_data_float(self, node_uuid, index): 

        """ 

        """ 

        try: 

            data = self._get_data(node_uuid, index) 

            if data is not None: 

                self.instances[index]['data'] = float(data) 

            else: 

                self.instances[index]['data'] = None 

        except: 

            logger.exception('Exception when retrieving %s_%s for node %s'%(self.uuid, index, node_uuid)) 

        return self.instances[index]['data'] 

 

class JNTValueConfigArray(JNTValueConfigGeneric): 

    def __init__(self, entry_name="config_array", **kwargs): 

        """ 

        """ 

        help = kwargs.pop('help', 'An array of strings separated by |') 

        label = kwargs.pop('label', 'Array') 

        index = kwargs.pop('index', 0) 

        get_data_cb = kwargs.pop('get_data_cb', self._get_data_float) 

        JNTValueConfigGeneric.__init__(self, entry_name=entry_name, help=help, label=label, 

            get_data_cb=get_data_cb, 

            index=index, type=0x16, **kwargs) 

 

    def _get_data_list(self, node_uuid, index): 

        """ 

        """ 

        try: 

            data = self._get_data(node_uuid, index) 

            if data is not None: 

                self.instances[index]['data'] = data.split('|') 

            else: 

                self.instances[index]['data'] = None 

        except: 

            logger.exception('Exception when retrieving %s_%s for node %s'%(self.uuid, index, node_uuid)) 

        return self.instances[index]['data']