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

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

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

"""The Raspberry http thread 

 

Server files using the http protocol 

 

""" 

 

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

 

import logging 

logger = logging.getLogger("janitoo") 

import os, sys 

import threading 

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler 

from SimpleHTTPServer import SimpleHTTPRequestHandler 

from SocketServer import ThreadingMixIn 

from distutils.dir_util import copy_tree 

import shutil 

from pkg_resources import get_distribution, DistributionNotFound, resource_filename, Requirement 

 

from janitoo.thread import JNTBusThread, BaseThread 

from janitoo.options import get_option_autostart 

from janitoo.utils import HADD 

from janitoo.node import JNTNode 

from janitoo.value import JNTValue 

from janitoo.component import JNTComponent 

from janitoo.bus import JNTBus 

 

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

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

#Must be implemented for non-regression 

from janitoo.classes import COMMAND_DESC 

 

COMMAND_WEB_CONTROLLER = 0x1030 

COMMAND_WEB_RESOURCE = 0x1031 

COMMAND_DOC_RESOURCE = 0x1032 

 

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

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

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

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

 

DEPLOY_DIRS = ['css', 'images', 'js'] 

 

def make_thread(options): 

    if get_option_autostart(options, 'http') == True: 

        return HttpThread(options) 

    else: 

        return None 

 

def make_http_resource(**kwargs): 

    return HttpResourceComponent(**kwargs) 

 

class ThreadedHTTPHandler(SimpleHTTPRequestHandler): 

    """ 

    """ 

    def translate_path(self, path): 

        logger.debug("[%s] - Request page for %s", self.__class__.__name__, path) 

        logger.debug("[%s] - Serve files from %s", self.__class__.__name__, self.server.root_directory + path) 

        return SimpleHTTPRequestHandler.translate_path(self, self.server.root_directory + path) 

 

class ThreadedHTTPServer(ThreadingMixIn, HTTPServer): 

    """Handle requests in a separate thread.""" 

    def __init__(self, server_address, RequestHandlerClass, root_directory): 

        HTTPServer.__init__(self, server_address, RequestHandlerClass) 

        self.root_directory = root_directory 

 

class HttpServerThread(BaseThread): 

    """The Rdd cache thread 

 

    Implement a cache. 

 

    """ 

    def __init__(self, section, options={}): 

        """Initialise the cache thread 

 

        Manage a cache for the rrd. 

 

        A timer in a separated thread will pickle the cache to disk every 30 seconds. 

 

        An other thread will update the rrd every hours 

 

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

        :type clientid: str 

        """ 

        self.section = section 

        BaseThread.__init__(self, options=options) 

        self.config_timeout_delay = 1.5 

        self.loop_sleep = 0.005 

        self.host = "localhost" 

        self.port = 8081 

        self._server = None 

 

    def config(self, host="localhost", port=8081): 

        """ 

        """ 

        if host is not None: 

            self.host = host 

        if port is not None: 

            self.port = port 

 

    def pre_loop(self): 

        """Launch before entering the run loop. The node manager is available. 

        """ 

        dirname='.' 

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

            dirname = self.options.data['home_dir'] 

        dirname = os.path.join(dirname, 'public') 

        if not os.path.exists(dirname): 

            os.makedirs(dirname) 

        #~ os.chdir(dirname) 

        if dirname.endswith("/"): 

            dirname = dirname[:-1] 

        logger.debug("[%s] - dirname %s", self.__class__.__name__, dirname) 

        self._server = ThreadedHTTPServer((self.host, self.port), ThreadedHTTPHandler, root_directory=dirname) 

 

    def post_loop(self): 

        """Launch after finishing the run loop. The node manager is still available. 

        """ 

        pass 

 

    def loop(self): 

        """Launch after finishing the run loop. The node manager is still available. 

        """ 

        self._server.serve_forever() 

 

    def config_timeout_callback(self): 

        """Called when configuration is finished. 

        """ 

        BaseThread.config_timeout_callback(self) 

        if self._server is not None: 

            self._server.shutdown() 

            self._server = None 

 

    def stop(self): 

        """Stop the thread 

        """ 

        BaseThread.stop(self) 

        if self._server is not None: 

            self._server.shutdown() 

            self._server = None 

 

    def run(self): 

        """Run the loop 

        """ 

        self._stopevent.clear() 

        #~ self.boot() 

        self.trigger_reload() 

        logger.debug("[%s] - Wait for the thread reload event for initial startup", self.__class__.__name__) 

        while not self._reloadevent.isSet() and not self._stopevent.isSet(): 

            self._reloadevent.wait(0.50) 

        logger.debug("[%s] - Entering the thread loop", self.__class__.__name__) 

        while not self._stopevent.isSet(): 

            self._reloadevent.clear() 

            try: 

                self.pre_loop() 

            except: 

                logger.exception('[%s] - Exception in pre_loop', self.__class__.__name__) 

                self._stopevent.set() 

            while not self._reloadevent.isSet() and not self._stopevent.isSet(): 

                self.loop() 

            try: 

                self.post_loop() 

            except: 

                logger.exception('[%s] - Exception in post_loop', self.__class__.__name__) 

 

class HttpBus(JNTBus): 

    """A pseudo-bus to manage RRDTools 

    """ 

    def __init__(self, **kwargs): 

        """ 

        :param int bus_id: the SMBus id (see Raspberry Pi documentation) 

        :param kwargs: parameters transmitted to :py:class:`smbus.SMBus` initializer 

        """ 

        JNTBus.__init__(self, **kwargs) 

        self._lock =  threading.Lock() 

        self._server = None 

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

            dirname = self.options.data['home_dir'] 

        directory = os.path.join(dirname, 'public') 

        if not os.path.exists(directory): 

            os.makedirs(directory) 

 

        uuid="host" 

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

            node_uuid=self.uuid, 

            help='The host or IP to use for the server', 

            label='Host', 

            default='localhost', 

        ) 

 

        uuid="port" 

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

            node_uuid=self.uuid, 

            help='The port', 

            label='Port', 

            default=8081, 

        ) 

 

        uuid="actions" 

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

            node_uuid=self.uuid, 

            help='The action on the HTTP server', 

            label='Actions', 

            list_items=['start', 'stop', 'reload'], 

            set_data_cb=self.set_action, 

            is_writeonly = True, 

            cmd_class=COMMAND_WEB_CONTROLLER, 

            genre=0x01, 

        ) 

 

    def get_resource_path(self): 

        """Return the resource path 

 

        """ 

        return "%s:%s/%%s" % (self.values["host"].data, self.values["port"].data) 

 

    def check_heartbeat(self): 

        """Check that the component is 'available' 

 

        """ 

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

        if self._server is not None: 

            return self._server.is_alive() 

        return False 

 

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

        """Act on the server 

        """ 

        params = {} 

        if data == "start": 

            if self.mqttc is not None: 

                self.start(self.mqttc) 

        elif data == "stop": 

            self.stop() 

        elif data == "reload": 

            if self._server is not None: 

                self._server.trigger_reload() 

 

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

        JNTBus.start(self, mqttc, trigger_thread_reload_cb) 

        self._server = HttpServerThread("http_server", self.options.data) 

        self._server.config(host=self.values["host"].data, port=self.values["port"].data) 

        self._server.start() 

 

    def stop(self): 

        if self._server is not None: 

            self._server.stop() 

            self._server = None 

        JNTBus.stop(self) 

 

class HttpResourceComponent(JNTComponent): 

    """ A resource ie /rrd """ 

 

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

        """ 

        """ 

        oid = kwargs.pop('oid', 'http.resource') 

        name = kwargs.pop('name', "HTTP resource") 

        product_name = kwargs.pop('product_name', "HTTP resource") 

        product_type = kwargs.pop('product_type', "Software") 

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

        JNTComponent.__init__(self, oid=oid, bus=bus, addr=addr, name=name, 

                product_name=product_name, product_type=product_type, product_manufacturer="Janitoo", **kwargs) 

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

        self.path = path 

        dirname='.' 

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

            dirname = self.options.data['home_dir'] 

        dirname = os.path.join(dirname, "public") 

        dirname = os.path.join(dirname, self.path) 

        self.deploy_resource(dirname) 

 

    def start(self, mqttc): 

        """Start the component. 

 

        """ 

        JNTComponent.start(self, mqttc) 

        return True 

 

    def stop(self): 

        """Stop the component. 

 

        """ 

        JNTComponent.stop(self) 

        return True 

 

    def deploy_resource(self, destination): 

        """ 

        """ 

        pass 

 

    def check_heartbeat_file(self, filename): 

        """Check that the component is 'available' 

 

        """ 

        dirname='.' 

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

            dirname = self.options.data['home_dir'] 

        dirname = os.path.join(dirname, "public", filename) 

        return os.path.exists(dirname) 

 

class BasicResourceComponent(HttpResourceComponent): 

    """ A resource ie /rrd """ 

 

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

        """ 

        """ 

        oid = kwargs.pop('oid', 'http.basic') 

        product_name = kwargs.pop('product_name', "HTTP basic resource") 

        name = kwargs.pop('name', "Http basic resource") 

        HttpResourceComponent.__init__(self, path, oid=oid, bus=bus, addr=addr, name=name, 

                product_name=product_name, **kwargs) 

        uuid="resource" 

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

            node_uuid=self.uuid, 

            help='The http resource: host:port', 

            label='Resource', 

            get_data_cb=self.get_resource, 

            genre=0x01, 

            cmd_class=COMMAND_WEB_RESOURCE, 

        ) 

        #~ config_value = self.values[uuid].create_config_value(help='The resource path', label='resource', type=0x08) 

        #~ self.values[config_value.uuid] = config_value 

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

        self.values[poll_value.uuid] = poll_value 

 

    def get_resource(self, node_uuid, index): 

        """ 

        """ 

        #~ print self._bus.get_resource_path() % self.path 

        return self._bus.get_resource_path() % '%s/' % self.path 

 

    def deploy_resource(self, destination): 

        """ 

        """ 

        for subdir in DEPLOY_DIRS: 

            try: 

                source = os.path.join(self.resource_filename('public'), self.path, subdir) 

                logger.debug('[%s] - public source = %s', self.__class__.__name__, source) 

                if os.path.isdir(source): 

                    if not os.path.exists(os.path.join(destination,subdir)): 

                        os.makedirs(os.path.join(destination,subdir)) 

                    copy_tree(source, os.path.join(destination,subdir), preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=0, verbose=0, dry_run=0) 

            except: 

                logger.exception('[%s] - Exception in deploy_resource', self.__class__.__name__) 

        try: 

            source = os.path.join(self.resource_filename('public'), "html") 

            logger.debug('[%s] - public html source = %s', self.__class__.__name__, source) 

            if os.path.isdir(source): 

                src_files = os.listdir(source) 

                for file_name in src_files: 

                    try: 

                        full_file_name = os.path.join(source, file_name) 

                        if (os.path.isfile(full_file_name)): 

                            shutil.copy(full_file_name, destination) 

                    except: 

                        logger.exception('[%s] - Exception in deploy_resource', self.__class__.__name__) 

        except: 

            logger.exception('[%s] - Exception in deploy_resource', self.__class__.__name__) 

 

 

class DocumentationResourceComponent(HttpResourceComponent): 

    """ A resource ie /rrd """ 

 

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

        """ 

        """ 

        oid = kwargs.pop('oid', 'http.doc') 

        product_name = kwargs.pop('product_name', "HTTP documentation resource") 

        name = kwargs.pop('name', "Http documentation resource") 

        HttpResourceComponent.__init__(self, path=os.path.join('doc',path), oid=oid, bus=bus, addr=addr, name=name, 

                product_name=product_name, **kwargs) 

        uuid="key" 

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

            node_uuid=self.uuid, 

            help='The key of documentation (ie controller.audiovideo.installation, node.audiovideo.samsung_ue46, ...))', 

            label='Key doc.', 

            get_data_cb=self.get_key, 

            is_readonly=True, 

            genre=0x01, 

            cmd_class=COMMAND_DOC_RESOURCE, 

        ) 

        self.configs_instances = self.values[uuid].instances 

        #~ print self.configs_instances 

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

        self.values[poll_value.uuid] = poll_value 

        uuid="resource" 

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

            node_uuid=self.uuid, 

            help='The http documentation : host:port/path', 

            label='Documentation', 

            get_data_cb=self.get_resource, 

            genre=0x01, 

            cmd_class=COMMAND_WEB_RESOURCE, 

        ) 

        config_value = self.values[uuid].create_config_value(help='The resource path', label='resource', type=0x08) 

        self.values[config_value.uuid] = config_value 

        self.values[config_value.uuid].instances = self.configs_instances 

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

        self.values[poll_value.uuid] = poll_value 

 

    def deploy_resource(self, destination): 

        """ 

        """ 

        try: 

            source = self.resource_filename('docs') 

            logger.debug('[%s] - doc source = %s', self.__class__.__name__, source) 

            if os.path.isdir(source): 

                #~ if os.path.isfile(source): 

                copy_tree(source, destination, preserve_mode=1, preserve_times=1, preserve_symlinks=0, update=0, verbose=0, dry_run=0) 

        except: 

            logger.exception('[%s] - Exception in deploy_resource', self.__class__.__name__) 

 

    def get_key(self, node_uuid, index): 

        """ 

        """ 

        pass 

 

    def get_resource(self, node_uuid, index): 

        """ 

        """ 

        #~ print self._bus.get_resource_path() % self.path 

        pass 

 

class HttpThread(JNTBusThread): 

    """The Http thread 

 

    """ 

    def init_bus(self): 

        """Build the bus 

        """ 

        self.section = 'http' 

        self.bus = HttpBus(options=self.options, oid=self.section, product_name="Http server")