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

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

767

768

769

770

771

772

773

774

775

776

777

778

779

780

781

782

783

784

785

786

787

788

789

790

791

792

793

794

795

796

797

798

799

800

801

802

803

804

805

806

807

808

809

810

811

812

813

814

815

816

817

818

819

820

821

822

823

824

825

826

827

828

829

830

831

832

833

834

835

836

837

838

839

840

841

842

843

844

845

846

847

848

849

850

851

852

853

854

855

856

857

858

859

860

861

862

863

864

865

866

867

868

869

870

871

872

873

874

875

876

877

878

879

880

881

882

883

884

885

886

887

888

889

890

891

892

893

894

895

896

897

898

899

900

901

902

903

904

905

906

907

908

909

910

911

912

913

914

915

916

917

918

919

920

921

922

923

924

925

926

927

928

929

930

931

932

933

934

935

936

937

938

939

940

941

942

943

944

945

946

947

948

949

950

951

952

953

954

955

956

957

958

959

960

961

962

963

964

965

966

967

968

969

970

971

972

973

974

975

976

977

978

979

980

981

982

983

984

985

986

987

988

989

990

991

992

993

994

995

996

997

998

999

1000

1001

1002

1003

1004

1005

1006

1007

1008

1009

1010

1011

1012

1013

1014

1015

1016

1017

1018

1019

1020

1021

1022

1023

1024

1025

1026

1027

1028

1029

1030

1031

1032

1033

1034

1035

1036

1037

1038

1039

1040

1041

1042

1043

1044

1045

1046

1047

1048

1049

1050

1051

1052

1053

1054

1055

1056

1057

1058

1059

1060

1061

1062

1063

1064

1065

1066

1067

1068

1069

1070

1071

1072

1073

1074

1075

1076

1077

1078

1079

1080

1081

1082

1083

1084

1085

1086

1087

1088

1089

1090

1091

1092

1093

1094

1095

1096

1097

1098

1099

1100

1101

1102

1103

1104

1105

1106

1107

1108

1109

1110

1111

1112

1113

1114

1115

1116

1117

1118

1119

1120

1121

1122

1123

1124

1125

1126

1127

1128

1129

1130

1131

1132

1133

1134

1135

1136

1137

1138

1139

1140

1141

1142

1143

1144

1145

1146

1147

1148

1149

1150

1151

1152

1153

1154

1155

1156

1157

1158

1159

1160

1161

1162

1163

1164

1165

1166

1167

1168

1169

1170

1171

1172

1173

1174

1175

1176

1177

1178

1179

1180

1181

1182

1183

1184

1185

1186

1187

1188

1189

1190

1191

1192

1193

1194

1195

1196

1197

1198

1199

1200

1201

1202

1203

1204

1205

1206

1207

1208

1209

1210

1211

1212

1213

1214

1215

1216

1217

1218

1219

1220

1221

1222

1223

1224

1225

1226

1227

1228

1229

1230

1231

1232

1233

1234

1235

1236

1237

1238

1239

1240

1241

1242

1243

1244

1245

1246

1247

1248

1249

1250

1251

1252

1253

1254

1255

1256

1257

1258

1259

1260

1261

1262

1263

1264

1265

1266

1267

1268

1269

1270

1271

1272

1273

1274

1275

1276

1277

1278

1279

1280

1281

1282

1283

1284

1285

1286

1287

1288

1289

1290

1291

1292

1293

1294

1295

1296

1297

1298

1299

1300

1301

1302

1303

1304

1305

1306

1307

1308

1309

1310

1311

1312

1313

1314

1315

1316

1317

1318

1319

1320

1321

1322

1323

1324

1325

1326

1327

1328

1329

1330

1331

1332

1333

1334

1335

1336

1337

1338

1339

1340

1341

1342

1343

1344

1345

1346

1347

1348

1349

1350

1351

1352

1353

1354

1355

1356

1357

1358

1359

1360

1361

1362

1363

1364

1365

1366

1367

1368

1369

1370

1371

1372

1373

1374

1375

1376

1377

1378

1379

1380

1381

1382

1383

1384

1385

1386

1387

1388

1389

1390

1391

1392

1393

1394

1395

1396

1397

1398

1399

1400

1401

1402

1403

1404

1405

1406

1407

1408

1409

1410

1411

1412

1413

1414

1415

1416

1417

1418

1419

1420

1421

1422

1423

1424

1425

1426

1427

1428

1429

1430

1431

1432

1433

1434

1435

1436

1437

1438

1439

1440

1441

1442

1443

1444

1445

1446

1447

1448

1449

1450

1451

1452

1453

1454

1455

1456

1457

1458

1459

1460

1461

1462

1463

1464

1465

1466

1467

1468

1469

1470

1471

1472

1473

1474

1475

1476

1477

1478

1479

1480

1481

1482

1483

1484

1485

1486

1487

1488

1489

1490

1491

1492

1493

1494

1495

1496

1497

1498

1499

1500

1501

1502

1503

1504

1505

1506

1507

1508

1509

1510

1511

1512

1513

1514

1515

1516

1517

1518

1519

1520

1521

1522

1523

1524

1525

1526

1527

1528

1529

1530

1531

1532

1533

1534

1535

1536

1537

1538

1539

1540

1541

1542

1543

1544

1545

1546

1547

1548

1549

1550

1551

1552

1553

1554

1555

1556

1557

1558

1559

1560

1561

1562

1563

1564

1565

1566

1567

1568

1569

1570

1571

1572

1573

1574

1575

1576

1577

1578

1579

1580

1581

1582

1583

1584

1585

1586

1587

1588

1589

1590

1591

1592

1593

1594

1595

1596

1597

1598

1599

1600

1601

1602

1603

1604

1605

1606

1607

1608

1609

1610

1611

1612

1613

1614

1615

1616

1617

1618

1619

1620

1621

1622

1623

1624

1625

1626

1627

1628

1629

1630

1631

1632

1633

1634

1635

1636

1637

1638

1639

1640

1641

1642

1643

1644

1645

1646

1647

1648

1649

1650

1651

1652

1653

1654

1655

1656

1657

1658

1659

1660

1661

1662

1663

1664

1665

1666

1667

1668

1669

1670

1671

1672

1673

1674

1675

1676

1677

1678

1679

1680

1681

1682

1683

1684

1685

1686

1687

1688

1689

1690

1691

1692

1693

1694

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

"""The Node 

 

about the pollinh mechanism : 

- simplest way to do it : define a poll_thread_timer for every value that needed to publish its data 

- Add a kind of polling queue that will launch the method to get and publish 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-2016 Sébastien GALLET aka bibi21000" 

 

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

import logging 

logger = logging.getLogger(__name__) 

 

import datetime 

import threading 

import random 

 

from janitoo.fsm import State, Machine 

import janitoo.value 

from janitoo.value_factory import JNTValueFactoryEntry 

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

from janitoo.utils import TOPIC_NODES, TOPIC_NODES_REPLY, TOPIC_NODES_REQUEST 

from janitoo.utils import TOPIC_BROADCAST_REPLY, TOPIC_BROADCAST_REQUEST 

from janitoo.utils import TOPIC_VALUES_USER, TOPIC_VALUES_CONFIG, TOPIC_VALUES_BASIC, TOPIC_VALUES_SYSTEM, TOPIC_HEARTBEAT 

from janitoo.mqtt import MQTTClient 

from janitoo.options import string_to_bool 

 

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

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

#Must be implemented for non-regression 

from janitoo.classes import COMMAND_DESC 

 

COMMAND_DISCOVERY = 0x5000 

COMMAND_CONFIGURATION = 0x0070 

 

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

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

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

 

class JNTNodeMan(object): 

    """The node manager 

    """ 

 

    fsm_states = [ 

        State(name='NEW'), 

        State(name='BOOT', on_enter=['start_controller_uuid', 'start_heartbeat_sender'], on_exit=['stop_controller_uuid']), 

        State(name='SYSTEM', on_enter=['start_controller_reply', 'start_controller_reply_system']), 

        State(name='CONFIG', on_enter=['start_controller_reply_config']), 

        State(name='INIT', on_enter=['start_nodes_init'], on_exit=['stop_boot_timer', 'stop_controller_reply']), 

        State(name='ONLINE', 

                on_enter=['start_broadcast_request', 'start_nodes_request', 'start_hourly_timer'], 

                on_exit=['stop_broadcast_request', 'stop_nodes_request']), 

        State(name='OFFLINE', 

                on_enter=['stop_boot_timer', 'stop_hourly_timer', 

                    'stop_heartbeat_sender', 'stop_controller_uuid', 

                    'stop_controller_reply','stop_broadcast_request', 

                    'stop_nodes_request']), 

    ] 

 

    states_str = { 

        'BOOT' : "The nodes are booting", 

        'SYSTEM' : "Configuring the nodes (system)", 

        'CONFIG' : "Configuring the nodes", 

        'INIT' : "Initialising the nodes", 

        'ONLINE' : "The nodes are online", 

        'OFFLINE' : "The nodes are offline", 

    } 

 

    def __init__(self, options, section, thread_uuid, **kwargs): 

        """ 

        """ 

        self.options = options 

        self._test = kwargs.get('test', False) 

        """For tests only""" 

        self.loop_sleep = 0.05 

        self.nodes = {} 

        self.polls = {} 

        self.heartbeats = {} 

        self.section = section 

        self.thread_uuid = thread_uuid 

        self.mqtt_nodes = None 

        self.mqtt_nodes_lock = threading.Lock() 

        self.mqtt_controller_reply = None 

        self.mqtt_controller_reply_lock = threading.Lock() 

        self.mqtt_controller_uuid = None 

        self.mqtt_controller_uuid_lock = threading.Lock() 

        self.mqtt_broadcast = None 

        self.mqtt_broadcast_lock = threading.Lock() 

        self.mqtt_heartbeat = None 

        self.mqtt_heartbeat_lock = threading.Lock() 

        self.request_controller_system_response = False 

        self.request_controller_config_response = False 

        self.request_controller_uuid_response = False 

        self.request_nodes_system_response = False 

        self.nodes_system_response = None 

        self.request_nodes_hadds_response = False 

        self.nodes_hadds_response = None 

        self.request_nodes_config_response = False 

        self.nodes_config_response = None 

        self.config_timeout = 3 

        self.slow_start = 0.05 

        self._controller = None 

        self._controller_hadd = None 

        self._requests = {'request_info_nodes' : self.request_info_nodes, 

                          'request_info_users' : self.request_info_users, 

                          'request_info_configs' : self.request_info_configs, 

                          'request_info_systems' : self.request_info_systems, 

                          'request_info_basics' : self.request_info_basics, 

                          'request_info_commands' : self.request_info_commands } 

        self.fsm_state = None 

        self.state = "OFFLINE" 

        self.trigger_reload = None 

        self.hourly_timer = None 

        self._hourly_jobs = None 

        self._daily_jobs = None 

 

        self.request_boot_timer = None 

        self.request_boot_timer_lock = threading.Lock() 

 

    def __del__(self): 

        """ 

        """ 

        try: 

            self.stop() 

            self._hourly_jobs = None 

            self._daily_jobs = None 

        except: 

            pass 

 

    def start(self, trigger_reload=None, loop_sleep=0.1, slow_start=0.05): 

        """ 

        """ 

        if trigger_reload is not None: 

            self.trigger_reload = trigger_reload 

        self.loop_sleep = loop_sleep 

        self.fsm_state = self.create_fsm() 

        self.fsm_state_start() 

        self._hourly_jobs = [] 

        self._daily_jobs = [] 

        self.slow_start = slow_start 

 

    def create_fsm(self): 

        """ 

        """ 

        fsm_state = Machine(model=self, states=self.fsm_states, initial='OFFLINE') 

        fsm_state.add_ordered_transitions() 

        fsm_state.add_transition('fsm_state_start', 'OFFLINE', 'BOOT') 

        fsm_state.add_transition('fsm_state_next', 'BOOT', 'SYSTEM') 

        fsm_state.add_transition('fsm_state_next', 'SYSTEM', 'CONFIG') 

        fsm_state.add_transition('fsm_state_next', 'CONFIG', 'INIT') 

        fsm_state.add_transition('fsm_state_next', 'INIT','ONLINE') 

        fsm_state.add_transition('fsm_state_stop', '*', 'OFFLINE', after=['after_fsm_stop'] 

        ) 

        return fsm_state 

 

    def stop(self): 

        """ 

        """ 

        self.fsm_state_stop() 

        self.stop_hourly_timer() 

        self.stop_heartbeat_sender() 

        self.stop_controller_uuid() 

        self.stop_controller_reply() 

        self.stop_broadcast_request() 

        self.stop_nodes_request() 

        self.stop_boot_timer() 

 

    def after_fsm_stop(self): 

        """ 

        """ 

        self.nodes = {} 

        self.polls = {} 

        self.heartbeats = {} 

        self.fsm_state = None 

        self._hourly_jobs = [] 

        self.state = "OFFLINE" 

 

    @property 

    def is_stopped(self): 

        """Return True if the network is stopped 

        """ 

        return self.fsm_state is None or self.state == "OFFLINE" 

 

    @property 

    def is_started(self): 

        """Return True if the network is started 

        """ 

        return self.state == "ONLINE" 

 

    def start_heartbeat_sender(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'start_heartbeat_sender') 

        if self._test: 

            print "start_heartbeat_sender" 

        else: 

            if self.mqtt_heartbeat is None: 

                self.mqtt_heartbeat_lock.acquire() 

                if self.is_stopped: 

                    return 

                try: 

                    self.mqtt_heartbeat = MQTTClient(options=self.options.data) 

                    self.mqtt_heartbeat.connect() 

                    self.mqtt_heartbeat.start() 

                except: 

                    logger.exception("[%s] - start_heartbeat_sender", self.__class__.__name__) 

                finally: 

                    self.mqtt_heartbeat_lock.release() 

 

    def stop_heartbeat_sender(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'stop_heartbeat_seander') 

        if self._test: 

            print "stop_heartbeat_seander" 

        else: 

            if self.mqtt_heartbeat is not None: 

                self.mqtt_heartbeat_lock.acquire() 

                for node in self.nodes: 

                    if self.nodes[node].hadd is not None: 

                        add_ctrl, add_node = self.nodes[node].split_hadd() 

                        self.mqtt_heartbeat.publish_heartbeat(int(add_ctrl), int(add_node), 'OFFLINE') 

                try: 

                    self.mqtt_heartbeat.stop() 

                    if self.mqtt_heartbeat.is_alive(): 

                        self.mqtt_heartbeat.join() 

                except: 

                    logger.exception("[%s] - stop_heartbeat_sender", self.__class__.__name__) 

                finally: 

                    self.mqtt_heartbeat = None 

                    self.mqtt_heartbeat_lock.release() 

 

    def start_broadcast_request(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'start_broadcast_request') 

        if self._test: 

            print "start_broadcast_request" 

        else: 

            if self.mqtt_broadcast is None: 

                self.mqtt_broadcast_lock.acquire() 

                if self.is_stopped: 

                    return 

                try: 

                    self.mqtt_broadcast = MQTTClient(options=self.options.data) 

                    self.mqtt_broadcast.connect() 

                    self.mqtt_broadcast.subscribe(topic=TOPIC_BROADCAST_REQUEST, callback=self.on_request) 

                    self.mqtt_broadcast.start() 

                except: 

                    logger.exception("[%s] - start_broadcast_request", self.__class__.__name__) 

                finally: 

                    self.mqtt_broadcast_lock.release() 

 

    def stop_broadcast_request(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'stop_broadcast_request') 

        if self._test: 

            print "stop_broadcast_request" 

        else: 

            if self.mqtt_broadcast is not None: 

                self.mqtt_broadcast_lock.acquire() 

                try: 

                    self.mqtt_broadcast.unsubscribe(topic=TOPIC_BROADCAST_REQUEST) 

                    self.mqtt_broadcast.stop() 

                    if self.mqtt_broadcast.is_alive(): 

                        self.mqtt_broadcast.join() 

                except: 

                    logger.exception("[%s] - stop_broadcast_request", self.__class__.__name__) 

                finally: 

                    self.mqtt_broadcast = None 

                    self.mqtt_broadcast_lock.release() 

 

    def start_nodes_request(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'start_nodes_request') 

        if self._test: 

            print "start_nodes_request" 

        else: 

            if self.mqtt_nodes is None: 

                if self.is_stopped: 

                    return 

                self.mqtt_nodes_lock.acquire() 

                try: 

                    self.mqtt_nodes = MQTTClient(options=self.options.data) 

                    self.mqtt_nodes.connect() 

                    add_ctrl, add_node = self._controller.split_hadd() 

                    logger.debug("[%s] - Subscribe to topic %s", self.__class__.__name__, TOPIC_NODES%("%s/#"%add_ctrl)) 

                    self.mqtt_nodes.subscribe(topic=TOPIC_NODES%("%s/#"%add_ctrl), callback=self.on_generic_request) 

                    self.mqtt_nodes.start() 

                    logger.debug("[%s] - Add topic %s", self.__class__.__name__, TOPIC_NODES_REQUEST%(self._controller_hadd)) 

                    self.mqtt_nodes.add_topic(topic=TOPIC_NODES_REQUEST%(self._controller_hadd), callback=self.on_request) 

                    for node in self.nodes: 

                        if self.nodes[node] != self._controller: 

                            logger.debug("[%s] - Add topic %s", self.__class__.__name__, TOPIC_NODES_REQUEST%(self.nodes[node].hadd)) 

                            self.mqtt_nodes.add_topic(topic=TOPIC_NODES_REQUEST%(self.nodes[node].hadd), callback=self.on_request) 

                except: 

                    logger.exception("[%s] - start_nodes_request", self.__class__.__name__) 

                finally: 

                    self.mqtt_nodes_lock.release() 

 

    def stop_nodes_request(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'stop_nodes_request') 

        if self._test: 

            print "stop_nodes_request" 

        else: 

            if self.mqtt_nodes is not None: 

                self.mqtt_nodes_lock.acquire() 

                try: 

                    for node in self.nodes: 

                        self.mqtt_nodes.remove_topic(topic=TOPIC_NODES_REQUEST%(self.nodes[node].hadd)) 

                    self.mqtt_nodes.remove_topic(topic=TOPIC_NODES_REQUEST%(self._controller_hadd)) 

                    add_ctrl, add_node = self._controller.split_hadd() 

                    self.mqtt_nodes.unsubscribe(topic=TOPIC_NODES%("%s/#"%add_ctrl)) 

                    self.mqtt_nodes.stop() 

                    if self.mqtt_nodes.is_alive(): 

                        self.mqtt_nodes.join() 

                except: 

                    logger.exception("[%s] - stop_nodes_request", self.__class__.__name__) 

                finally: 

                    self.mqtt_nodes = None 

                    self.mqtt_nodes_lock.release() 

 

    def start_controller_reply(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'start_controller_reply') 

        if self._test: 

            print "start_controller_reply" 

        else: 

            if self.is_stopped: 

                return 

            if self.mqtt_controller_reply is None: 

                self.mqtt_controller_reply_lock.acquire() 

                try: 

                    pass 

                    #~ self.mqtt_controller_reply = MQTTClient(options=self.options.data) 

                    #~ self.mqtt_controller_reply.connect() 

                    #~ self.mqtt_controller_reply.subscribe(topic=TOPIC_NODES_REPLY%(self._controller.hadd), callback=self.on_reply) 

                    #~ self.mqtt_controller_reply.start() 

                except: 

                    logger.exception("[%s] - start_controller_reply", self.__class__.__name__) 

                finally: 

                    self.mqtt_controller_reply_lock.release() 

 

    def stop_controller_reply(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'stop_controller_reply') 

        if self._test: 

            print "stop_controller_reply" 

        else: 

            if self.mqtt_controller_reply is not None: 

                self.mqtt_controller_reply_lock.acquire() 

                try: 

                    self.mqtt_controller_reply.unsubscribe(topic=TOPIC_NODES_REPLY%(self._controller_hadd)) 

                    self.mqtt_controller_reply.stop() 

                    if self.mqtt_controller_reply.is_alive(): 

                        self.mqtt_controller_reply.join() 

                except: 

                    logger.exception("[%s] - stop_controller_reply", self.__class__.__name__) 

                finally: 

                    self.mqtt_controller_reply = None 

                    self.mqtt_controller_reply_lock.release() 

 

    def start_controller_uuid(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'start_controller_uuid') 

        if self._test: 

            print "start_controller_uuid" 

        else: 

            if self.is_stopped: 

                return 

            if self.mqtt_controller_uuid is None: 

                self.mqtt_controller_uuid_lock.acquire() 

                try: 

                    pass 

                    #~ self.mqtt_controller_uuid = MQTTClient(options=self.options.data) 

                    #~ self.mqtt_controller_uuid.connect() 

                    #~ self.mqtt_controller_uuid.subscribe(topic=TOPIC_NODES_REPLY%(self._controller_hadd), callback=self.on_reply_uuid) 

                    #~ self.mqtt_controller_uuid.start() 

                except: 

                    logger.exception("[%s] - start_controller_uuid", self.__class__.__name__) 

                finally: 

                    self.mqtt_controller_uuid_lock.release() 

            self.request_boot_timer_lock.acquire() 

            try: 

                self._stop_boot_timer() 

                if self.is_stopped: 

                    return 

                self.request_boot_timer = threading.Timer(self.config_timeout+self.slow_start, self.finish_controller_uuid) 

                self.request_boot_timer.start() 

            finally: 

                self.request_boot_timer_lock.release() 

 

    def finish_controller_uuid(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s in state %s", 'finish_controller_uuid', self.state) 

        if self.is_stopped: 

            return 

        self.request_boot_timer_lock.acquire() 

        try: 

            self._stop_boot_timer() 

            if self.is_stopped: 

                return 

            if not self.request_controller_uuid_response: 

                #retrieve hadd from local configuration 

                controller = self.create_controller_node() 

                self.add_controller_node(controller.uuid, controller) 

                self._controller.hadd_get(controller.uuid, None) 

                self._controller_hadd = self._controller.hadd 

                self.add_heartbeat(self._controller) 

                logger.debug("[%s] - Added controller node with uuid %s and hadd %s", self.__class__.__name__, self._controller.uuid, self._controller_hadd) 

                #~ print self._controller.__dict__ 

                #~ print self.config_timeout 

        except: 

            logger.exception("[%s] - finish_controller_uuid", self.__class__.__name__) 

        finally: 

            self.request_boot_timer_lock.release() 

        if not self.is_stopped: 

            self.fsm_state_next() 

 

    def stop_controller_uuid(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s in state %s", 'stop_controller_uuid', self.state) 

        if self._test: 

            print "stop_controller_uuid" 

        else: 

            if self.mqtt_controller_uuid is not None: 

                self.mqtt_controller_uuid_lock.acquire() 

                try: 

                    self.mqtt_controller_uuid.unsubscribe(topic=TOPIC_NODES_REPLY%(self._controller_hadd)) 

                    self.mqtt_controller_uuid.stop() 

                    if self.mqtt_controller_uuid.is_alive(): 

                        self.mqtt_controller_uuid.join() 

                except: 

                    logger.exception("[%s] - stop_controller_uuid", self.__class__.__name__) 

                finally: 

                    self.mqtt_controller_uuid = None 

                    self.mqtt_controller_uuid_lock.release() 

 

    def start_controller_reply_system(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'start_controller_reply_system') 

        if self._test: 

            print "start_controller_reply_system" 

        else: 

            if self.is_stopped: 

                return 

            self.request_boot_timer_lock.acquire() 

            try: 

                self._stop_boot_timer() 

                if self.is_stopped: 

                    return 

                self.request_boot_timer = threading.Timer(self.config_timeout+self.slow_start, self.finish_controller_reply_system) 

                self.request_boot_timer.start() 

            finally: 

                self.request_boot_timer_lock.release() 

 

    def stop_boot_timer(self): 

        """ 

        """ 

        self.request_boot_timer_lock.acquire() 

        try: 

            self._stop_boot_timer() 

        finally: 

            self.request_boot_timer_lock.release() 

 

    def _stop_boot_timer(self): 

        """ 

        """ 

        if self.request_boot_timer is not None: 

            self.request_boot_timer.cancel() 

            self.request_boot_timer = None 

 

    def after_controller_reply_system(self): 

        """ 

        """ 

        pass 

 

    def finish_controller_reply_system(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s in state %s", 'finish_controller_reply_system', self.state) 

        if self.is_stopped: 

            return 

        self.request_boot_timer_lock.acquire() 

        try: 

            self._stop_boot_timer() 

            if self.is_stopped: 

                return 

            if not self.request_controller_system_response: 

                self._controller.load_system_from_local() 

            self.config_timeout = self._controller.config_timeout 

            self.request_controller_controller_system_response = False 

            self.after_controller_reply_system() 

        finally: 

            self.request_boot_timer_lock.release() 

        if not self.is_stopped: 

            self.fsm_state_next() 

 

    def after_controller_reply_config(self): 

        """ 

        """ 

        pass 

 

    def start_controller_reply_config(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'start_controller_reply_config') 

        if self._test: 

            print "start_controller_reply_config" 

        else: 

            if self.is_stopped: 

                return 

            self.request_boot_timer_lock.acquire() 

            try: 

                self._stop_boot_timer() 

                if self.is_stopped: 

                    return 

                self.request_boot_timer = threading.Timer(self.config_timeout+self.slow_start, self.finish_controller_reply_config) 

                self.request_boot_timer.start() 

            finally: 

                self.request_boot_timer_lock.release() 

 

    def before_controller_reply_config(self): 

        """ 

        """ 

        pass 

 

    def after_create_node(self, uuid): 

        """After the node is created 

        """ 

        pass 

 

    def after_system_node(self, uuid): 

        """After the node system 

        """ 

        pass 

 

    def after_config_node(self, uuid): 

        """After the node config 

        """ 

        pass 

 

    def finish_controller_reply_config(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s in state %s", 'finish_reply_config', self.state) 

        if self.is_stopped: 

            return 

        self.request_boot_timer_lock.acquire() 

        try: 

            self._stop_boot_timer() 

            if self.is_stopped: 

                return 

            self.before_controller_reply_config() 

            if not self.request_controller_config_response: 

                self._controller.load_config_from_local() 

            self.request_controller_config_response = False 

            self.after_controller_reply_config() 

        finally: 

            self.request_boot_timer_lock.release() 

        if not self.is_stopped: 

            self.fsm_state_next() 

 

    def start_nodes_init(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s", 'start_nodes_init') 

        if self._test: 

            print "start_nodes_init" 

        else: 

            if self.is_stopped: 

                return 

            self.request_boot_timer_lock.acquire() 

            if self.is_stopped: 

                return 

            try: 

                self.request_boot_timer = threading.Timer(self.config_timeout+self.slow_start, self.finish_nodes_hadds) 

                self.request_boot_timer.start() 

            finally: 

                self.request_boot_timer_lock.release() 

 

    def finish_nodes_hadds(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s in state %s", 'finish_nodes_hadds', self.state) 

        logger.debug("finish_nodes_hadds : nodes = %s", self.nodes) 

        #~ print "self.request_nodes_hadds_response ", self.request_nodes_hadds_response 

        if self.is_stopped: 

            return 

        self.request_boot_timer_lock.acquire() 

        try: 

            self._stop_boot_timer() 

            if self.is_stopped: 

                return 

            if not self.request_nodes_hadds_response: 

                self.nodes_hadds_response = self.get_nodes_hadds_from_local_config() 

                logger.debug("finish_nodes_hadds : nodes_hadds_response = %s", self.nodes_hadds_response) 

                for node in self.nodes_hadds_response: 

                    self.create_node(node, hadd=self.nodes_hadds_response[node]) 

                    self.after_create_node(node) 

                    #~ print onode.__dict__ 

            self.request_boot_timer = threading.Timer(self.config_timeout+self.slow_start, self.finish_nodes_system) 

            self.request_boot_timer.start() 

        finally: 

            self.request_boot_timer_lock.release() 

 

    def finish_nodes_system(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s in state %s", 'finish_nodes_system', self.state) 

        logger.debug("finish_nodes_system : nodes = %s", self.nodes) 

        if self.is_stopped: 

            return 

        self.request_boot_timer_lock.acquire() 

        try: 

            self._stop_boot_timer() 

            if self.is_stopped: 

                return 

            if not self.request_nodes_system_response: 

                for node in self.nodes: 

                    if node != self._controller.uuid and not self.is_stopped: 

                        onode = self.nodes[node] 

                        onode.load_system_from_local() 

                        self.after_system_node(node) 

                        #~ print onode.__dict__ 

            self.request_boot_timer = threading.Timer(self.config_timeout+self.slow_start, self.finish_nodes_config) 

            self.request_boot_timer.start() 

        finally: 

            self.request_boot_timer_lock.release() 

 

    def finish_nodes_config(self): 

        """ 

        """ 

        logger.debug("fsm_state : %s in state %s", 'finish_nodes_config', self.state) 

        logger.debug("finish_nodes_config : nodes = %s", self.nodes) 

        #~ print "self.request_nodes_hadds_response ", self.request_nodes_config_response 

        if self.is_stopped: 

            return 

        self.request_boot_timer_lock.acquire() 

        try: 

            self._stop_boot_timer() 

            if self.is_stopped: 

                return 

            if not self.request_nodes_config_response: 

                for node in self.nodes: 

                    if node != self._controller.uuid and not self.is_stopped: 

                        onode = self.nodes[node] 

                        onode.load_config_from_local() 

                        self.after_config_node(node) 

        finally: 

            self.request_boot_timer_lock.release() 

        if not self.is_stopped: 

            self.add_heartbeat(self._controller) 

            self.fsm_state_next() 

 

    def on_reply(self, client, userdata, message): 

        """On request 

 

        :param client: the Client instance that is calling the callback. 

        :type client: paho.mqtt.client.Client 

        :param userdata: user data of any type and can be set when creating a new client instance or with user_data_set(userdata). 

        :type userdata: all 

        :param message: The message variable is a MQTTMessage that describes all of the message parameters. 

        :type message: paho.mqtt.client.MQTTMessage 

        """ 

        pass 

 

    def on_generic_request(self, client, userdata, message): 

        """On request 

 

        :param client: the Client instance that is calling the callback. 

        :type client: paho.mqtt.client.Client 

        :param userdata: user data of any type and can be set when creating a new client instance or with user_data_set(userdata). 

        :type userdata: all 

        :param message: The message variable is a MQTTMessage that describes all of the message parameters. 

        :type message: paho.mqtt.client.MQTTMessage 

        """ 

        pass 

 

    def on_request(self, client, userdata, message): 

        """On request 

 

        :param client: the Client instance that is calling the callback. 

        :type client: paho.mqtt.client.Client 

        :param userdata: user data of any type and can be set when creating a new client instance or with user_data_set(userdata). 

        :type userdata: all 

        :param message: The message variable is a MQTTMessage that describes all of the message parameters. 

        :type message: paho.mqtt.client.MQTTMessage 

        """ 

        logger.debug("on_request receive message %s", message.payload) 

        try: 

            data = json_loads(message.payload) 

            #~ print data['uuid'] 

            #We should check what value is requested 

            #{'hadd', 'cmd_class', 'type'='list', 'genre'='0x04', 'data'='node|value|config', 'uuid'='request_info'} 

            if data['cmd_class'] == COMMAND_DISCOVERY: 

                if data['genre'] == 0x04: 

                    if data['uuid'] in self._requests: 

                        resp = {} 

                        resp.update(data) 

                        try: 

                            if message.topic.find('broadcast') != -1: 

                                topic = "/broadcast/reply/%s" % data['reply_hadd'] 

                                self._requests[data['uuid']](topic, resp) 

                            else: 

                                topic = "/nodes/%s/reply" % data['reply_hadd'] 

                                self._requests[data['uuid']](topic, resp) 

                            return 

                        except: 

                            logger.exception("Exception when running on_request method") 

                            return 

            elif data['cmd_class'] == COMMAND_CONFIGURATION: 

                #print "message %s" % message 

                if 'reply_hadd' not in data: 

                    logger.warning("No reply_hadd in message %s", message) 

                logger.debug("on_request COMMAND_CONFIGURATION message %s,%s", message.topic, message.payload) 

                node = self.get_node_from_hadd(data['hadd']) 

                #~ print node.values 

                if data['genre'] == 0x04: 

                    #print "message %s" % message 

                    if data['uuid'] in node.values: 

                        read_only = True 

                        write_only = False 

                        try: 

                            read_only = string_to_bool(data['is_readonly']) 

                        except KeyError: 

                            pass 

                        except ValueError: 

                            pass 

                        try: 

                            write_only = string_to_bool(data['is_writeonly']) 

                        except KeyError: 

                            pass 

                        except ValueError: 

                            pass 

                        if write_only: 

                            node.values[data['uuid']].data = data['data'] 

                            if data['uuid'] == "heartbeat": 

                                self.add_heartbeat(node) 

                            data['is_writeonly'] = False 

                            data['is_readonly'] = True 

                        elif read_only: 

                            data['data'] = node.values[data['uuid']].data 

                        data['label'] = node.values[data['uuid']].label 

                        data['help'] = node.values[data['uuid']].help 

                        msg = json_dumps(data) 

                        topic = TOPIC_VALUES_SYSTEM % ("%s/%s" % (data['hadd'], data['uuid'])) 

                        self.publish_request(topic, msg) 

                        return 

                elif data['genre'] == 0x03: 

                    #print "message %s" % message 

                    #~ print node.values 

                    if data['uuid'] in node.values: 

                        read_only = True 

                        write_only = False 

                        try: 

                            read_only = string_to_bool(data['is_readonly']) 

                        except KeyError: 

                            pass 

                        except ValueError: 

                            pass 

                        try: 

                            write_only = string_to_bool(data['is_writeonly']) 

                        except KeyError: 

                            pass 

                        except ValueError: 

                            pass 

                        if write_only: 

                            node.values[data['uuid']].data = data['data'] 

                            data['is_writeonly'] = False 

                            data['is_readonly'] = True 

                        elif read_only: 

                            data['data'] = node.values[data['uuid']].data 

                            data['is_writeonly'] = False 

                            data['is_readonly'] = True 

                        data['label'] = node.values[data['uuid']].label 

                        data['help'] = node.values[data['uuid']].help 

                        msg = json_dumps(data) 

                        topic = TOPIC_VALUES_CONFIG % ("%s/%s" % (data['hadd'], data['uuid'])) 

                        self.publish_request(topic, msg) 

                        return 

            else: 

                logger.debug("on_request else message %s,%s", message.topic, message.payload) 

                node = self.get_node_from_hadd(data['hadd']) 

                #~ print node.values 

                if data['genre'] == 0x02: 

                    #~ print data['cmd_class'], node.values[data['uuid']].cmd_class 

                    #~ print node.hadd 

                    if data['uuid'] in node.values and data['cmd_class'] == node.values[data['uuid']].cmd_class: 

                        res = node.values[data['uuid']].to_dict() 

                        res.update(data) 

                        data = res 

                        read_only = True 

                        write_only = False 

                        try: 

                            read_only = string_to_bool(data['is_readonly']) 

                        except KeyError: 

                            pass 

                        except ValueError: 

                            pass 

                        try: 

                            write_only = string_to_bool(data['is_writeonly']) 

                        except KeyError: 

                            pass 

                        except ValueError: 

                            pass 

                        if write_only: 

                            #~ print "write_only" 

                            node.values[data['uuid']].data = data['data'] 

                            data['is_writeonly'] = False 

                            data['is_readonly'] = True 

                        elif read_only: 

                            #~ print "read_only" 

                            data['data'] = node.values[data['uuid']].data 

                            data['is_writeonly'] = False 

                            data['is_readonly'] = True 

                        data['label'] = node.values[data['uuid']].label 

                        data['help'] = node.values[data['uuid']].help 

                        msg = json_dumps(data) 

                        topic = TOPIC_NODES_REPLY % (data['reply_hadd']) 

                        self.publish_request(topic, msg) 

                        topic = TOPIC_VALUES_USER % ("%s/%s" % (data['hadd'], data['uuid'])) 

                        self.publish_request(topic, msg) 

                        return 

            logger.warning("Unknown request value %s", data) 

        except: 

            logger.exception("Exception in on_request") 

 

    def request_info_nodes(self, reply_topic, resp): 

        """ 

        """ 

        resp['data'] = {} 

        for knode in self.nodes.keys(): 

            resp['data'][knode] = self.nodes[knode].to_dict() 

            ret = self.nodes[knode].check_heartbeat() 

            if ret is None : 

                state = self.state 

            elif ret: 

                state = 'ONLINE' 

            else: 

                state = 'OFFLINE' 

            resp['data'][knode]['state'] = state 

        logger.debug("request_info_nodes : response data %s", resp['data']) 

        msg = json_dumps(resp) 

        self.publish_request(reply_topic, msg) 

 

    def request_info_users(self, reply_topic, resp): 

        """ 

        """ 

        resp['data'] = {} 

        i = 0 

        for knode in self.nodes.keys(): 

            for kvalue in self.nodes[knode].values.keys(): 

                value = self.nodes[knode].values[kvalue] 

                if value.genre == 0x02: 

                    if value.hadd not in resp['data']: 

                        resp['data'][value.hadd] = {} 

                    if value.uuid not in resp['data'][value.hadd]: 

                        resp['data'][value.hadd][value.uuid] = {} 

                    if isinstance(value, JNTValueFactoryEntry) and value.get_max_index() > 0: 

                        for i in range(0, value.get_max_index() + 1 ): 

                            resp['data'][value.hadd][value.uuid][i] = value.to_dict_with_index(i) 

                    else: 

                        resp['data'][value.hadd][value.uuid][0] = value.to_dict() 

        logger.debug("request_info_users : response data %s", resp['data']) 

        msg = json_dumps(resp) 

        self.publish_request(reply_topic, msg) 

 

    def request_info_configs(self, reply_topic, resp): 

        """ 

        """ 

        resp['data'] = {} 

        for knode in self.nodes.keys(): 

            for kvalue in self.nodes[knode].values.keys(): 

                value = self.nodes[knode].values[kvalue] 

                if value.genre == 0x03: 

                    if value.hadd not in resp['data']: 

                        resp['data'][value.hadd] = {} 

                    if value.uuid not in resp['data'][value.hadd]: 

                        resp['data'][value.hadd][value.uuid] = {} 

                    if value.master_config_value is not None and value.master_config_value.get_max_index() > 0 : 

                        for i in range(0, value.master_config_value.get_max_index() + 1 ) : 

                            resp['data'][value.hadd][value.uuid][i] = value.to_dict() 

                            resp['data'][value.hadd][value.uuid][i]['index'] = i 

                            resp['data'][value.hadd][value.uuid][i]['data'] = value.master_config_value.get_config(value.node_uuid, i) 

                    else: 

                        resp['data'][value.hadd][value.uuid][0] = value.to_dict() 

        logger.debug("request_info_configs : response data %s", resp['data']) 

        msg = json_dumps(resp) 

        self.publish_request(reply_topic, msg) 

 

    def request_info_basics(self, reply_topic, resp): 

        """ 

        """ 

        resp['data'] = {} 

        for knode in self.nodes.keys(): 

            for kvalue in self.nodes[knode].values.keys(): 

                value = self.nodes[knode].values[kvalue] 

                if value.genre == 0x01: 

                    if value.hadd not in resp['data']: 

                        resp['data'][value.hadd] = {} 

                    if value.uuid not in resp['data'][value.hadd]: 

                        resp['data'][value.hadd][value.uuid] = {} 

                    #~ if value.master_config_value is not None and value.master_config_value.get_max_index() > 0 : 

                        #~ for i in range(0, value.master_config_value.get_max_index() + 1 ) : 

                            #~ resp['data'][value.hadd][value.uuid][i] = value.to_dict() 

                            #~ resp['data'][value.hadd][value.uuid][i]['index'] = i 

                            #~ resp['data'][value.hadd][value.uuid][i]['data'] = value.master_config_value.get_config(value.node_uuid, i) 

                    #~ else: 

                        #~ resp['data'][value.hadd][value.uuid][0] = value.to_dict() 

                    if isinstance(value, JNTValueFactoryEntry) and value.get_max_index() > 0: 

                        for i in range(0, value.get_max_index() + 1 ): 

                            resp['data'][value.hadd][value.uuid][i] = value.to_dict_with_index(i) 

                    else: 

                        resp['data'][value.hadd][value.uuid][0] = value.to_dict() 

        logger.debug("request_info_basics : response data %s", resp['data']) 

        msg = json_dumps(resp) 

        self.publish_request(reply_topic, msg) 

 

    def request_info_systems(self, reply_topic, resp): 

        """ 

        """ 

        resp['data'] = {} 

        for knode in self.nodes.keys(): 

            for kvalue in self.nodes[knode].values.keys(): 

                value = self.nodes[knode].values[kvalue] 

                if value.genre == 0x04: 

                    if value.hadd not in resp['data']: 

                        resp['data'][value.hadd] = {} 

                    resp['data'][value.hadd][value.uuid] = value.to_dict() 

        msg = json_dumps(resp) 

        self.publish_request(reply_topic, msg) 

 

    def request_info_commands(self, reply_topic, resp): 

        """ 

        """ 

        resp['data'] = {} 

        for knode in self.nodes.keys(): 

            for kvalue in self.nodes[knode].values.keys(): 

                value = self.nodes[knode].values[kvalue] 

                if value.genre == 0x05: 

                    if value.hadd not in resp['data']: 

                        resp['data'][value.hadd] = {} 

                    resp['data'][value.hadd][value.uuid] = value.to_dict() 

        msg = json_dumps(resp) 

        self.publish_request(reply_topic, msg) 

 

    def publish_request(self, reply_topic, msg): 

        """ 

        """ 

        self.mqtt_broadcast.publish(topic=reply_topic, payload=msg) 

 

    def loop(self, stopevent): 

        """ 

        """ 

        if not self.is_started: 

            return 

        to_polls = [] 

        nodes = self.polls.keys() 

        for node in nodes: 

            for key in self.polls[node]: 

                if self.polls[node][key]['next_run'] < datetime.datetime.now(): 

                    to_polls.append(self.polls[node][key]['value']) 

        if len(to_polls)>0: 

            logger.debug('[%s] - Found polls in timeout : [ %s ]', self.__class__.__name__, ', '.join(str(e.uuid) for e in to_polls)) 

        for value in to_polls: 

            self.publish_poll(self.mqtt_nodes, value, stopevent) 

            stopevent.wait(0.1) 

        to_heartbeats = [] 

        keys = self.heartbeats.keys() 

        for node in keys: 

            if self.heartbeats[node]['next_run'] < datetime.datetime.now(): 

                to_heartbeats.append(node) 

        if len(to_heartbeats)>0: 

            logger.debug('[%s] - Found heartbeats in timeout : %s', self.__class__.__name__, to_heartbeats) 

            self.heartbeat(to_heartbeats, self.mqtt_heartbeat, stopevent) 

        try: 

            sleep = float(self.loop_sleep) - 0.02*len(to_polls) - 0.02*len(to_heartbeats) 

        except ValueError: 

            sleep = 0.05 

        if sleep<0: 

            sleep=0.05 

        stopevent.wait(sleep) 

 

    def heartbeat(self, nodes, mqttc=None, stopevent=None): 

        """Send a add_ctrl:-1 heartbeat message. It will ping all devices managed by this controller. 

        """ 

        for node in nodes: 

            self.add_heartbeat(self.nodes[node]) 

            logger.debug('[%s] - Try to send heartbeat for node %s with hadd %s', self.__class__.__name__, node, self.nodes[node].hadd) 

            if self.nodes[node].hadd is not None: 

                #~ print self.nodes[node].hadd 

                add_ctrl, add_node = self.nodes[node].split_hadd() 

                ret = self.nodes[node].check_heartbeat() 

                if ret is None : 

                    state = self.state 

                elif ret == True: 

                    state = 'ONLINE' 

                else: 

                    state = 'OFFLINE' 

                #~ print "node : %s/%s, state : %s"%(add_ctrl, add_node, state) 

                #~ msg = {'add_ctrl':add_ctrl, 'add_node':add_node, 'state':state} 

                mqt = mqttc if mqttc is not None else self.mqtt_heartbeat 

                if mqt is not None: 

                    mqt.publish_heartbeat(int(add_ctrl), int(add_node), state) 

                if stopevent is not None: 

                    stopevent.wait(0.02) 

 

    def publish_poll(self, mqttc, value, stopevent=None): 

        """ 

        """ 

        node = self.nodes[value.node_uuid] 

        mqt = mqttc if mqttc is not None else self.mqtt_nodes 

        genres = {1:'basic', 2:'user', 3:'config', } 

        if value.genre in genres: 

            genre = genres[value.genre] 

        else: 

            genre = "user" 

        if mqt is not None: 

            mqt.publish_value(node.hadd, value, genre) 

            self.add_poll(value) 

        else: 

            self.add_poll(value, timeout=self.config_timeout+self.slow_start) 

 

    def add_controller_node(self, uuid, node): 

        """ 

        """ 

        if uuid not in self.nodes: 

            logger.debug("[%s] - Add controller node with uuid %s and hadd %s", self.__class__.__name__, uuid, node.hadd) 

            self.nodes[uuid] = node 

            self._controller = node 

            self._controller_hadd = node.hadd 

            node.options = self.options 

            self.add_internal_system_values_to_node(node) 

            self.add_internal_config_values_to_node(node) 

            return True 

        else: 

            return False 

 

    def create_controller_node(self, **kwargs): 

        """ 

        """ 

        return JNTNode(options=self.options, **kwargs) 

 

    def create_node(self, uuid, **kwargs): 

        """ 

        """ 

        node = JNTNode(uuid=uuid, options=self.options, **kwargs) 

        self.add_node(node.uuid, node) 

        return node 

 

    def get_controller_node(self): 

        """ 

        """ 

        return self._controller 

 

    def get_node_from_hadd(self, hadd): 

        """ 

        """ 

        for nid in self.nodes.keys(): 

            if self.nodes[nid].hadd == hadd: 

                return self.nodes[nid] 

        return None 

 

    def get_node_from_uuid(self, uuid): 

        """ 

        """ 

        if uuid in self.nodes: 

            return self.nodes[uuid] 

        return None 

 

    def get_add_ctrl(self): 

        """ 

        """ 

        add_ctrl, add_node = self._controller_hadd.split(HADD_SEP) 

        return int(add_ctrl) 

 

    def get_nodes_hadds_from_local_config(self): 

        """ {'uuid':'hadd'} 

        """ 

        return {} 

 

    def get_components(self): 

        """Retrieve components from a section 

        """ 

        return self.options.get_options_key(self.section, "components.") 

 

    def add_node(self, uuid, node): 

        """ 

        """ 

        if uuid not in self.nodes: 

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

            self.nodes[uuid] = node 

            if node.hadd is not None: 

                self.add_heartbeat(node) 

            node.options = self.options 

            self.add_internal_system_values_to_node(node) 

            self.add_internal_config_values_to_node(node) 

            return True 

        else: 

            return False 

 

    def add_internal_system_values_to_node(self, node): 

        """ 

        """ 

        node.add_internal_system_values() 

 

    def add_internal_config_values_to_node(self, node): 

        """ 

        """ 

        node.add_internal_config_values() 

 

    def add_value_to_node(self, uuid, node, value): 

        """ 

        """ 

        if node.uuid not in self.nodes or uuid in self.nodes[node.uuid].values: 

            return False 

        node.add_value(uuid, value) 

        if value.is_polled and not value.is_writeonly: 

            self.add_poll(value) 

        if value.cmd_class not in self.nodes[node.uuid].cmd_classes: 

            self.nodes[node.uuid].cmd_classes.append(value.cmd_class) 

        return True 

 

    def is_polled(self, value): 

        """Is the value poll active (in the polling system) 

        """ 

        if value.hadd not in self.polls: 

            return False 

        if value.uuid not in self.polls[value.hadd]: 

            return False 

 

    def add_poll(self, value, timeout=None, overwrite=True): 

        """Add a value to the polling system 

        """ 

        if value is None: 

            return 

        if value.poll_delay == 0: 

            self.remove_poll(value) 

            return 

        if value.hadd not in self.polls: 

            self.polls[value.hadd] = {} 

        if value.uuid not in self.polls[value.hadd] or timeout: 

            if timeout is None: 

                timeout = self.config_timeout 

            self.polls[value.hadd][value.uuid] = {'next_run':datetime.datetime.now()+datetime.timedelta(seconds=timeout+random.random()*(1+self.slow_start)), 'value':value} 

            value.is_polled = True 

        elif overwrite: 

            self.polls[value.hadd][value.uuid]['next_run'] = datetime.datetime.now()+datetime.timedelta(seconds=value.poll_delay) 

            value.is_polled = True 

 

    def add_polls(self, values, timeout=None, slow_start=False, overwrite=True): 

        """Add values to the polling system 

        """ 

        if timeout is None: 

            timeout = 0 

        if slow_start: 

            slow_start = 1 + self.slow_start 

        else: 

            slow_start = 0.1 

        i = 0 

        for value in values: 

            self.add_poll(value, timeout=slow_start * i + timeout, overwrite=overwrite) 

            i += 1 

 

    def remove_polls(self, values): 

        """Remove polls from polling system 

        """ 

        for value in values: 

            self.remove_poll(value) 

 

    def remove_poll(self, value): 

        """Remove poll from polling system 

        """ 

        if value and value.hadd in self.polls and value.uuid in self.polls[value.hadd]: 

            #~ value.is_polled= False 

            del self.polls[value.hadd][value.uuid] 

        if value and value.hadd in self.polls and len(self.polls[value.hadd]) == 0: 

            del self.polls[value.hadd] 

 

    def add_heartbeat(self, node): 

        """Add a node to the heartbeat system 

        """ 

        #~ print "heartbeats = %s" % self.heartbeats 

        if node.uuid not in self.heartbeats: 

            self.heartbeats[node.uuid] = {'next_run':datetime.datetime.now()+datetime.timedelta(seconds=self.config_timeout+random.random()*(1+self.slow_start))} 

        else: 

            self.heartbeats[node.uuid]['next_run'] = datetime.datetime.now()+datetime.timedelta(seconds=node.heartbeat) 

 

    def remove_heartbeat(self, node): 

        """Remove a node from the heartbeat system 

        """ 

        if node.uuid in self.heartbeats: 

            del self.heartbeats[node.uuid] 

 

    def find_nodes(self, node_oid): 

        """Find a node usinf its uuid 

        """ 

        nodes = [ self.nodes[node] for node in self.nodes if self.nodes[node].oid == node_oid ] 

        return nodes 

 

    def find_node(self, node_uuid): 

        """Find a node using its uuid 

        """ 

        nuuid='%s__%s'%(self.section, node_uuid) 

        #~ nuuid=node_uuid 

        nodes = [ self.nodes[node] for node in self.nodes if self.nodes[node].uuid == nuuid ] 

        if len(nodes)>1: 

            logger.warning("[%s] - Found 2 nodes %s with uuid %s. Returning the fisrt one.", self.__class__.__name__, nodes, node_uuid) 

        if len(nodes)==0: 

            return None 

        return nodes[0] 

 

    def find_value(self, node_uuid, value_uuid): 

        """Find a value using its uuid and the node one 

        """ 

        nuuid='%s__%s'%(self.section, node_uuid) 

        #~ nuuid=node_uuid 

        nodes = [ self.nodes[node] for node in self.nodes if self.nodes[node].uuid == nuuid ] 

        if len(nodes)>1: 

            logger.warning("[%s] - Found 2 nodes %s with uuid %s.", self.__class__.__name__, nodes, node_uuid) 

        if len(nodes)==0: 

            return None 

        vuuid='%s'%(value_uuid) 

        values = [ nodes[0].values[value] for value in nodes[0].values if nodes[0].values[value].uuid == vuuid] 

        if len(values)>1: 

            logger.warning("[%s] - Found 2 valus %s with uuid %s. Returning the fisrt one.", self.__class__.__name__, nodes, value_uuid) 

        if len(values)==0: 

            return None 

        return values[0] 

 

    def start_hourly_timer(self): 

        """Start the hourly timer 

        """ 

        if self.hourly_timer is not None: 

            self.hourly_timer.cancel() 

            self.hourly_timer = None 

        hourly = False 

        try: 

            hourly = string_to_bool(self.options.get_option(self.section, 'hourly_timer', default = False)) 

        except: 

            logger.warning("[%s] - C'ant get hourly_timer from configuration file. Disable it", self.__class__.__name__) 

            hourly = False 

        if hourly: 

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

            self.hourly_timer = threading.Timer(60*60, self.do_hourly_timer) 

            self.hourly_timer.start() 

 

    def stop_hourly_timer(self): 

        """Stop the hourly timer 

        """ 

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

        if self.hourly_timer is not None: 

            self.hourly_timer.cancel() 

            self.hourly_timer = None 

 

    def add_hourly_job(self, callback): 

        """Add an hourly job. 

        """ 

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

        self._hourly_jobs.append(callback) 

 

    def remove_hourly_job(self, callback): 

        """Remove an hourly job. 

        """ 

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

        if self._hourly_jobs is not None and callback in self._hourly_jobs: 

            self._hourly_jobs.remove(callback) 

 

    def add_daily_job(self, callback): 

        """Add an daily job. 

        """ 

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

        self._daily_jobs.append(callback) 

 

    def remove_daily_job(self, callback): 

        """Remove an daily job. 

        """ 

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

        if self._daily_jobs is not None and callback in self._daily_jobs: 

            self._daily_jobs.remove(callback) 

 

    def do_hourly_timer(self): 

        """Do the hourly timer thread 

        """ 

        self.stop_hourly_timer() 

        self.start_hourly_timer() 

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

        try: 

            self.options.set_option(self.section, 'hourly_timer_lastrun', datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S')) 

        except: 

            logger.exception("[%s] - Can't save hourly_timer_lastrun in configuration file.", self.__class__.__name__) 

        for job in self._hourly_jobs: 

            try: 

                job() 

            except: 

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

        if datetime.datetime.now().hour == 0: 

            try: 

                self.options.set_option(self.section, 'daily_timer_lastrun', datetime.datetime.now().strftime('%m/%d/%Y %H:%M:%S')) 

            except: 

                logger.exception("[%s] - Can't save daily_timer_lastrun in configuration file.", self.__class__.__name__) 

            for job in self._daily_jobs: 

                try: 

                    job() 

                except: 

                    logger.exception("[%s] - exception in do daily timers", self.__class__.__name__) 

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

 

class JNTBusNodeMan(JNTNodeMan): 

    """The node manager 

    """ 

    def __init__(self, options, bus, section, thread_uuid, **kwargs): 

        JNTNodeMan.__init__(self, options, section, thread_uuid, **kwargs) 

        self.bus = bus 

        self.uuid = thread_uuid 

 

    def stop(self): 

        """ 

        """ 

        logger.info("[%s] - Stop the node manager", self.__class__.__name__) 

        try: 

            self.bus.stop() 

        except: 

            logger.exception("[%s] - Exception when stopping", self.__class__.__name__) 

        JNTNodeMan.stop(self) 

 

    def after_controller_reply_config(self): 

        """Start the bus 

        """ 

        self.bus.start(self.mqtt_nodes, self.trigger_reload) 

        self.build_bus_components() 

 

    def create_controller_node(self, **kwargs): 

        """ 

        """ 

        return self.bus.create_node(self, None, options=self.options, **kwargs) 

 

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

        """ 

        """ 

        node = None 

        #~ print uuid 

        logger.info("[%s] - Create node for component %s in factory", self.__class__.__name__, uuid) 

        if uuid in self.bus.components: 

            compo = self.bus.components[uuid] 

            node = compo.create_node(hadd) 

            if node is not None: 

                self.add_node(node.uuid, node) 

                for keyv in compo.values.keys(): 

                    value = compo.values[keyv] 

                    self.add_value_to_node(value.uuid, node, value) 

            else: 

                logger.error("[%s] - Can't create node for component %s in factory", self.__class__.__name__, self.bus.components[uuid]) 

        else: 

            logger.error("[%s] - Can't create node because can't find component %s in components %s", self.__class__.__name__, uuid, self.bus.components) 

        return node 

 

    def after_config_node(self, uuid): 

        """After configuring the node 

        """ 

        #~ print self.bus.components 

        if uuid in self.bus.components: 

            compo = self.bus.components[uuid] 

            try: 

                compo.start(self.mqtt_nodes) 

            except: 

                logger.exception("[%s] - Can't start component %s", self.__class__.__name__, uuid) 

        elif not self.is_stopped: 

            if uuid != self._controller.uuid: 

                logger.error("[%s] - Can't start component because can't find %s in components", self.__class__.__name__, uuid) 

 

    def after_fsm_stop(self): 

        """ 

        """ 

        JNTNodeMan.after_fsm_stop(self) 

 

    def get_nodes_hadds_from_local_config(self): 

        """ {'uuid':'hadd'} 

        """ 

        components = self.get_components() 

        res = {} 

        for component in components: 

            uuid = '%s__%s' % (self.bus.uuid, component) 

            options = self.options.get_options(uuid) 

            #~ print "option ", uuid, options 

            if 'hadd' not in options: 

                logger.error("[%s] - Found component %s  without hadd in local config", self.__class__.__name__, uuid) 

            else: 

                res[uuid] = options['hadd'] 

        #~ print res 

        logger.debug("[%s] - Founds hadds in local config %s", self.__class__.__name__, res) 

        return res 

 

    #~ def start_bus_components(self, **kwargs): 

        #~ """Start the components 

        #~ """ 

        #~ logger.debug("[%s] - Start the components", self.__class__.__name__) 

        #~ for key in self.bus.components.keys(): 

            #~ try: 

                #~ compo.start(self.mqtt_nodes) 

            #~ except: 

                #~ logger.exception("[%s] - Can't start component %s on address %s", self.__class__.__name__, self.bus.components[key], compo._addr) 

 

    def build_bus_components(self): 

        """Build the bus components from factory 

        """ 

        components = self.get_components() 

        logger.debug("[%s] - Build components from factory : %s", self.__class__.__name__, components) 

        for key in components.keys(): 

            try: 

                logger.debug('[%s] - Add component %s', self.__class__.__name__, key) 

                if components[key] not in self.bus.factory: 

                    logger.error("[%s] - Can't find component %s in factory", self.__class__.__name__, components[key]) 

                add_comp = '%s__%s' % (self.bus.uuid, key) 

                #add_comp = key 

                self.bus.add_component(components[key], add_comp, options=self.options) 

            except: 

                logger.exception("[%s] - Can't add component %s", self.__class__.__name__, key) 

 

    def before_controller_reply_config(self): 

        """ 

        """ 

        for keyv in self.bus.values.keys(): 

            value = self.bus.values[keyv] 

            self.add_value_to_node(value.uuid, self._controller, value) 

 

    def loop(self, stopevent): 

        """ 

        """ 

        JNTNodeMan.loop(self, stopevent) 

        try: 

            self.bus.loop(stopevent) 

        except: 

            logger.exception("Exception in nodeman loop") 

 

class JNTNode(object): 

    def __init__(self, uuid="a_unik_identifier_for_the_node_on_the_controller", **kwargs): 

        """ 

        :param int uuid: the unique uuid of the node on the controller 

        """ 

        self.uuid = uuid 

        """The UUID of the node""" 

        self.oid = kwargs.get('oid', 'generic') 

        """The oid of the component associated to the node""" 

        self.cmd_classes = kwargs.get('cmd_classes', []) 

        """The command classes implemented by the node""" 

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

        """The name of the node""" 

        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.product_manufacturer = kwargs.get('product_manufacturer', 'Default product manufacturer') 

        """The product manufacturer of the node""" 

        self.location = kwargs.get('location', 'Default location') 

        """The location of the node""" 

        self.values = {} 

        """The values assumed by the node""" 

        self.heartbeat = 30 

        """The heartbeat delay""" 

        self.config_timeout = 3 

        """The delay before reloading the thread""" 

        self._hadd = kwargs.get('hadd', None) 

        """The HAAD of the node""" 

        self._check_hearbeat_cb = kwargs.get('check_hearbeat_cb', None) 

        """The callback to thr check_hearbeat method of the component""" 

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

        """The option inherited from startup""" 

 

    def split_hadd(self): 

        """Return the node part of the address node 

        """ 

        return self.hadd.split(HADD_SEP) 

 

    def check_heartbeat(self): 

        """Check 

        """ 

        if self._check_hearbeat_cb is not None: 

            return self._check_hearbeat_cb() 

        else: 

            return None 

 

    def from_dict(self, adict): 

        """Update internal dict from adict 

        """ 

        self.__dict__.update(adict) 

        return self 

 

    def to_dict(self): 

        """Retrieve a dict version of the node 

        """ 

        res = {} 

        res.update(self.__dict__) 

        for key in res.keys(): 

            if key.startswith('_') or key in ["values", "options"]: 

                del res[key] 

        res['hadd'] = self.hadd 

        return res 

 

    def to_json(self): 

        """Retrieve a json version of the node 

        """ 

        res = self.to_dict() 

        return json_dumps(res) 

 

    def add_internal_system_values(self): 

        """ 

        """ 

        myval = janitoo.value.value_system_heartbeat(get_data_cb=self.heartbeat_get, set_data_cb=self.heartbeat_set) 

        self.add_value(myval.uuid, myval) 

        myval = janitoo.value.value_system_hadd(get_data_cb=self.hadd_get, set_data_cb=self.hadd_set) 

        self.add_value(myval.uuid, myval) 

        myval = janitoo.value.value_system_config_timeout(get_data_cb=self.config_timeout_get, set_data_cb=self.config_timeout_set) 

        self.add_value(myval.uuid, myval) 

 

    def add_internal_config_values(self): 

        """ 

        """ 

        myval = janitoo.value.value_config_name(get_data_cb=self.name_get, set_data_cb=self.name_set) 

        self.add_value(myval.uuid, myval) 

        myval = janitoo.value.value_config_location(get_data_cb=self.location_get, set_data_cb=self.location_set) 

        self.add_value(myval.uuid, myval) 

 

    def add_value(self, uuid, value): 

        """ 

        """ 

        self.values[uuid] = value 

        self.values[uuid].node_uuid = self.uuid 

        self.values[uuid].hadd = self.hadd 

        return True 

 

    def load_system_from_local(self): 

        """Retrieve a json version of the node 

        """ 

        self.config_timeout_get(None,None) 

        self.heartbeat_get(None,None) 

 

    def load_config_from_local(self): 

        """Retrieve a json version of the node 

        """ 

        for value in self.values: 

            #~ print value 

            if self.values[value].genre == 0x03: 

                #~ print self.values[value]._get_data_cb 

                temp = self.values[value].data 

                #~ print "********************load config from local" 

                #~ print "%s = %s"%(value,temp) 

                #~ print self.location 

                #self.__dict__[value] = self.values[value].data 

 

    def heartbeat_get(self, node_uuid, index): 

        """ 

        """ 

        hb = self.options.get_option(node_uuid, 'heartbeat') 

        if hb is not None: 

            try: 

                self.heartbeat = int(hb) 

            except ValueError: 

                logger.exception('Exception when retrieving heartbeat') 

        return self.heartbeat 

 

    def heartbeat_set(self, node_uuid, index, value): 

        """ 

        """ 

        try: 

            self.heartbeat = int(value) 

            self.options.set_option(node_uuid, 'heartbeat', self.heartbeat) 

        except ValueError: 

            logger.exception('Exception when setting heartbeat') 

 

            return self.options.get_option(self.node.uuid, 'heartbeat') 

 

    def config_timeout_get(self, node_uuid, index): 

        """ 

        """ 

        config_timeout = self.options.get_option(node_uuid, 'config_timeout') 

        if config_timeout is not None: 

            try: 

                self.config_timeout = float(config_timeout) 

            except ValueError: 

                logger.exception('Exception when retrieving timeout') 

        return self.config_timeout 

 

    def config_timeout_set(self, node_uuid, index, value): 

        """ 

        """ 

        try: 

            self.config_timeout = float(value) 

            self.options.set_option(node_uuid, 'config_timeout', self.config_timeout) 

        except ValueError: 

            logger.exception('Exception when setting timeout') 

 

    @property 

    def hadd(self): 

        """ 

        """ 

        return self._hadd 

 

    @hadd.setter 

    def hadd(self, value): 

        """ 

        """ 

        self._hadd = value 

        for val in self.values: 

            self.values[val].hadd = value 

 

    def hadd_get(self, node_uuid, index): 

        """ 

        """ 

        hadd = self.options.get_option(node_uuid, 'hadd') 

        if hadd is not None: 

            try: 

                self.hadd = hadd 

            except ValueError: 

                logger.exception('Exception when retrieving hadd') 

        return self.hadd 

 

    def hadd_set(self, node_uuid, index, value): 

        """ 

        """ 

        try: 

            self.hadd = value 

            self.options.set_option(node_uuid, 'hadd', self.hadd) 

        except ValueError: 

            logger.exception('Exception when setting hadd') 

 

    def name_get(self, node_uuid, index): 

        """ 

        """ 

        name = self.options.get_option(node_uuid, 'name') 

        #~ print name 

        if name is not None: 

            self.name = name 

        return self.name 

 

    def name_set(self, node_uuid, index, value): 

        """ 

        """ 

        try: 

            self.name = value 

            self.options.set_option(node_uuid, 'name', self.name) 

            #~ print self.uuid 

        except ValueError: 

            logger.exception('Exception when setting name') 

 

    def location_get(self, node_uuid, index): 

        """ 

        """ 

        location = self.options.get_option(node_uuid, 'location') 

        #~ print location 

        if location is not None: 

            self.location = location 

        return self.location 

 

    def location_set(self, node_uuid, index, value): 

        """ 

        """ 

        try: 

            self.location = value 

            self.options.set_option(node_uuid, 'location', self.location) 

        except ValueError: 

            logger.exception('Exception when setting location')