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 Nut Janitoo helper 

 

""" 

 

__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 os, sys 

from datetime import datetime, timedelta 

from nut2 import PyNUTClient 

import threading 

 

from janitoo.thread import JNTBusThread 

from janitoo.options import get_option_autostart 

from janitoo.utils import HADD, HADD_SEP, json_dumps, json_loads 

from janitoo.node import JNTNode 

from janitoo.value import JNTValue, value_config_poll 

from janitoo.bus import JNTBus 

from janitoo.component import JNTComponent 

 

OID = 'nut' 

 

def make_thread(options, force=False): 

    if get_option_autostart(options, OID) == True or force: 

        return NutThread(options) 

    else: 

        return None 

 

def make_ups(**kwargs): 

    return NutUps(**kwargs) 

 

class NutThread(JNTBusThread): 

    """The Nut thread& 

 

    """ 

    def init_bus(self): 

        """Build the bus 

        """ 

        self.section = OID 

        self.bus = JNTBus(options=self.options, oid=self.section, product_name="NUT controller") 

 

class NutUps(JNTComponent): 

    """ 

    This class abstracts a roowifi and gives attributes for telemetry data, 

    as well as methods to command the robot 

    """ 

    def __init__(self, bus=None, addr=None, **kwargs): 

        JNTComponent.__init__(self, 

            oid = kwargs.pop('oid', '%s.ups'%OID), 

            bus = bus, 

            addr = addr, 

            name = kwargs.pop('name', "NUT Ups"), 

            product_name = kwargs.pop('product_name', "NUT Ups"), 

            **kwargs) 

        self._lock =  threading.Lock() 

        self._battery_charge = -1.0 

        self._battery_voltage = -1.0 

        self._battery_runtime = -1.0 

        self._battery_chemistry = "unknown" 

        self._status = "unknown" 

        self._ups_stats_last = False 

        self._ups_stats_next_run = datetime.now() + timedelta(seconds=10) 

        logger.debug("[%s] - __init__ node uuid:%s", self.__class__.__name__, self.uuid) 

 

        uuid="ip_ping" 

        self.values[uuid] = self.value_factory['ip_ping'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='Ping the nut server', 

            label='Ping', 

            default='127.0.0.1' 

        ) 

        config_value = self.values[uuid].create_config_value(help='The IP of the NUT server', label='IP',) 

        self.values[config_value.uuid] = config_value 

        poll_value = self.values[uuid].create_poll_value() 

        self.values[poll_value.uuid] = poll_value 

 

        uuid="username" 

        self.values[uuid] = self.value_factory['config_string'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='Username to connect the nut server', 

            label='Username', 

        ) 

 

        uuid="upsname" 

        self.values[uuid] = self.value_factory['config_string'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='Ups name on the nut server', 

            label='UPS', 

        ) 

 

        uuid="password" 

        self.values[uuid] = self.value_factory['config_password'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='Password to connect the nut server', 

            label='Password', 

        ) 

 

        uuid="port" 

        self.values[uuid] = self.value_factory['config_integer'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='Port to connect the nut server', 

            label='Port', 

            default=3493, 

        ) 

 

        uuid="battery_voltage" 

        self.values[uuid] = self.value_factory['sensor_voltage'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The voltage of the battery', 

            label='Voltage', 

            get_data_cb=self.get_battery_voltage, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=90) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid="battery_charge" 

        self.values[uuid] = self.value_factory['sensor_percent'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The charge of the battery', 

            label='Charge', 

            get_data_cb=self.get_battery_charge, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=90) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid="battery_chemistry" 

        self.values[uuid] = self.value_factory['sensor_string'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The chemistry of the battery', 

            label='Chemistry', 

            get_data_cb=self.get_battery_chemistry, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=1800) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid="battery_runtime" 

        self.values[uuid] = self.value_factory['sensor_integer'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The left runtime of the battery', 

            label='Runtime', 

            units='Seconds', 

            get_data_cb=self.get_battery_runtime, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=90) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid="status" 

        self.values[uuid] = self.value_factory['sensor_string'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The status of the UPS', 

            label='Status', 

            get_data_cb=self.get_status, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=60) 

        self.values[poll_value.uuid] = poll_value 

 

    def check_heartbeat(self): 

        """Check that the component is 'available' 

 

        """ 

        #~ print "it's me %s : %s" % (self.values['upsname'].data, self._ups_stats_last) 

        return self._ups_stats_last 

 

    def get_ups_stats(self): 

        """ 

        """ 

        ret = False 

        if self._ups_stats_next_run < datetime.now(): 

            locked = self._lock.acquire(False) 

            if locked == True: 

                try: 

                    client = PyNUTClient(host=self.values['ip_ping_config'].data,login=self.values['username'].data, password=self.values['password'].data, port=self.values['port'].data) 

                    res = client.list_vars(self.values['upsname'].data) 

                    self._battery_charge = res['battery.charge'] 

                    self._battery_voltage = res['battery.voltage'] 

                    self._battery_runtime = res['battery.runtime'] 

                    self._battery_chemistry = res['battery.chemistry'] 

                    self._status = res['ups.status'] 

                    self.node.product_name = res['ups.model'] 

                    self.node.product_type = res['ups.serial'] 

                    self.node.product_manufacturer = res['ups.mfr'] 

                    self._ups_stats_last = True 

                    ret = True 

                except: 

                    logger.exception("[%s] - Exception catched in get_ups_stats", self.__class__.__name__) 

                    self._ups_stats_last = False 

                finally: 

                    self._lock.release() 

                    logger.debug("And finally release the lock !!!") 

                if self.values['ip_ping_poll'].data>0: 

                    self._ups_stats_next_run = datetime.now() + timedelta(seconds=self.values['ip_ping_poll'].data) 

            return ret 

        return False 

 

    def get_battery_charge(self, node_uuid, index): 

        """Return the battery charge 

        """ 

        self.get_ups_stats() 

        return self._battery_charge 

 

    def get_battery_voltage(self, node_uuid, index): 

        """Return the battery voltage 

        """ 

        self.get_ups_stats() 

        return self._battery_voltage 

 

    def get_battery_chemistry(self, node_uuid, index): 

        """Return the battery chemistry 

        """ 

        self.get_ups_stats() 

        return self._battery_chemistry 

 

    def get_battery_runtime(self, node_uuid, index): 

        """Return the battery runtime 

        """ 

        self.get_ups_stats() 

        return self._battery_runtime 

 

    def get_status(self, node_uuid, index): 

        """Return the status 

        """ 

        self.get_ups_stats() 

        return self._status