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

1695

1696

1697

1698

1699

1700

1701

1702

1703

1704

1705

1706

1707

1708

1709

1710

1711

1712

1713

1714

1715

1716

1717

1718

1719

1720

1721

1722

1723

1724

1725

1726

1727

1728

1729

1730

1731

1732

1733

1734

1735

1736

1737

1738

1739

1740

1741

1742

1743

1744

1745

1746

1747

1748

1749

1750

1751

1752

1753

1754

1755

1756

1757

1758

1759

1760

1761

1762

1763

1764

1765

1766

1767

1768

1769

1770

1771

1772

1773

1774

1775

1776

1777

1778

1779

1780

1781

1782

1783

1784

1785

1786

1787

1788

1789

1790

1791

1792

1793

1794

1795

1796

1797

1798

1799

1800

1801

1802

1803

1804

1805

1806

1807

1808

1809

1810

1811

1812

1813

1814

1815

1816

1817

1818

1819

1820

1821

1822

1823

1824

1825

1826

1827

1828

1829

1830

1831

1832

1833

1834

1835

1836

1837

1838

1839

1840

1841

1842

1843

1844

1845

1846

1847

1848

1849

1850

1851

1852

1853

1854

1855

1856

1857

1858

1859

1860

1861

1862

1863

1864

1865

1866

1867

1868

1869

1870

1871

1872

1873

1874

1875

1876

1877

1878

1879

1880

1881

1882

1883

1884

1885

1886

1887

1888

1889

1890

1891

1892

1893

1894

1895

1896

1897

1898

1899

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

1912

1913

1914

1915

1916

1917

1918

1919

1920

1921

1922

1923

1924

1925

1926

1927

1928

1929

1930

1931

1932

1933

1934

1935

1936

1937

1938

1939

1940

1941

1942

1943

1944

1945

1946

1947

1948

1949

1950

1951

1952

1953

1954

1955

1956

1957

1958

1959

1960

1961

1962

1963

1964

1965

1966

1967

1968

1969

1970

1971

1972

1973

1974

1975

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

1986

1987

1988

1989

1990

1991

1992

1993

1994

1995

1996

1997

1998

1999

2000

2001

2002

2003

2004

2005

2006

2007

2008

2009

2010

2011

2012

2013

2014

2015

2016

2017

2018

2019

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

2032

2033

2034

2035

2036

2037

2038

2039

2040

2041

2042

2043

2044

2045

2046

2047

2048

2049

2050

2051

2052

2053

2054

2055

2056

2057

2058

2059

2060

2061

2062

2063

2064

2065

2066

2067

2068

2069

2070

2071

2072

2073

2074

2075

2076

2077

2078

2079

2080

2081

2082

2083

2084

2085

2086

2087

2088

2089

2090

2091

2092

2093

2094

2095

2096

2097

2098

2099

2100

2101

2102

2103

2104

2105

2106

2107

2108

2109

2110

2111

2112

2113

2114

2115

2116

2117

2118

2119

2120

2121

2122

2123

2124

2125

2126

2127

2128

2129

2130

2131

2132

2133

2134

2135

2136

2137

2138

2139

2140

2141

2142

2143

2144

2145

2146

2147

2148

2149

2150

2151

2152

2153

2154

2155

2156

2157

2158

2159

2160

2161

2162

2163

2164

2165

2166

2167

2168

2169

2170

2171

2172

2173

2174

2175

2176

2177

2178

2179

2180

2181

2182

2183

2184

2185

2186

2187

2188

2189

2190

2191

2192

2193

2194

2195

2196

2197

2198

2199

2200

2201

2202

2203

2204

2205

2206

2207

2208

2209

2210

2211

2212

2213

2214

2215

2216

2217

2218

2219

2220

2221

2222

2223

2224

2225

2226

2227

2228

2229

2230

2231

2232

2233

2234

2235

2236

2237

2238

2239

2240

2241

2242

2243

2244

2245

2246

2247

2248

2249

2250

2251

2252

2253

2254

2255

2256

2257

2258

2259

2260

2261

2262

2263

2264

2265

2266

2267

2268

2269

2270

2271

2272

2273

2274

2275

2276

2277

2278

2279

2280

2281

2282

2283

2284

2285

2286

2287

2288

2289

2290

2291

2292

2293

2294

2295

2296

2297

2298

2299

2300

2301

2302

2303

2304

2305

2306

2307

2308

2309

2310

2311

2312

2313

2314

2315

2316

2317

2318

2319

2320

2321

2322

2323

2324

2325

2326

2327

2328

2329

2330

2331

2332

2333

2334

2335

2336

2337

2338

2339

2340

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

"""The dhcp library 

 

TODO : 

- actuellement l'etat HEARTBEAT n'existe que pour les primary car il sert pour le discover. 

- il faut l'implémenter pour les secondary : par /resolv ? /dhcp/? 

 

""" 

 

__license__ = """ 

    This file is part of Janitoo. 

 

    Janitoo is free software: you can redistribute it and/or modify 

    it under the terms of the GNU General Public License as published by 

    the Free Software Foundation, either version 3 of the License, or 

    (at your option) any later version. 

 

    Janitoo is distributed in the hope that it will be useful, 

    but WITHOUT ANY WARRANTY; without even the implied warranty of 

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

    GNU General Public License for more details. 

 

    You should have received a copy of the GNU General Public License 

    along with Janitoo. If not, see <http://www.gnu.org/licenses/>. 

 

""" 

__author__ = 'Sébastien GALLET aka bibi21000' 

__email__ = 'bibi21000@gmail.com' 

__copyright__ = "Copyright © 2013-2014-2015-2016 Sébastien GALLET aka bibi21000" 

 

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

import logging 

logger = logging.getLogger(__name__) 

 

import os, sys 

import datetime 

import threading 

 

from janitoo.fsm import Machine, State 

from janitoo.utils import HADD, HADD_SEP 

from janitoo.utils import json_dumps, json_loads, hadd_split 

from janitoo.utils import TOPIC_NODES, TOPIC_NODES_REPLY, TOPIC_NODES_REQUEST 

from janitoo.utils import TOPIC_BROADCAST_REPLY, TOPIC_BROADCAST_REQUEST, TOPIC_RESOLV_REQUEST, TOPIC_RESOLV, TOPIC_RESOLV_REPLY, TOPIC_RESOLV_BROADCAST 

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 JNTOptions, string_to_bool 

from janitoo.value import JNTValue 

from janitoo.node import JNTNode 

 

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

#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 

 

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

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

 

#https://github.com/tyarkoni/transitions 

 

leases_states = { 

    'ONLINE' : 5, #The machine is online 

    'BOOT' : 4, #The machine is booting 

    'CONFIG' : 4, #The machine is configuring 

    'PENDING' : 3, #The machine did not send a ping 

    'FAILED' : 2, 

    'DEAD' : 1, 

    'OFFLINE' : 0, #The machine is offline. 

    'UNKNOWN' : -1, #Hummm ... we don' know. 

    } 

 

def normalize_request_info_values(data): 

    """ """ 

    if 'uuid' in data: 

        ndata = {'0': {'0':data}} 

    elif len(data)==0 or 'uuid' in data[data.keys()[0]]: 

        ndata = {'0':data} 

    else: 

        ndata = data 

    return ndata 

 

def normalize_request_info_nodes(data): 

    """ """ 

    if 'hadd' in data: 

        ndata = {'0':data} 

    else: 

        ndata = data 

    return ndata 

 

class CacheManager(object): 

 

    def __init__(self): 

        """ 

        """ 

        self.entries = {} 

 

    def start(self, query): 

        """Load the cache from db 

 

        """ 

        #Initialise the cache 

        if query is not None: 

            self.entries = {} 

            data = query.all() 

            for line in data: 

                self.update(line.add_ctrl, line.add_node, state=line.state, hearbeat=line.heartbeat, last_seen=line.last_seen) 

 

    def flush(self, query=None): 

        """Flush the cache to db. Remmove failed name from cache 

 

        :param session: the session to use to communicate with db. May be a scoped_session if used in a separate tread. If None, use the common session. 

        :type session: sqlalchemy session 

        """ 

        if query is not None: 

            data = query.all() 

            for line in data: 

                if line.add_ctrl in self.entries and line.add_node in self.entries[line.add_ctrl]: 

                    line.state = self.entries[line.add_ctrl][line.add_node]['state'] 

                    line.last_seen = self.entries[line.add_ctrl][line.add_node]['last_seen'] 

        #Remove failed nodes from cache 

        for ctrl in self.entries.keys(): 

            for node in self.entries[ctrl].keys(): 

                if self.entries[ctrl][node]['state'] == 'dead': 

                    self.remove(ctrl, node) 

 

    def update(self, add_ctrl, add_node, state="ONLINE", heartbeat=30, last_seen=None): 

        """Update an entry in cache 

 

        :param add_ctrl: the controller part of the address 

        :type add_ctrl: Integer 

        :param add_node: the node part of the address. 0 for controller, -1 for all nodes managed by controller. 

        :type add_node: Integer 

        :param state: the state of the node. 

        :type state: String 

        :param last_seen: the last time the node have been seen. 

        :type last_seen: datetime 

        """ 

        #~ print "update heartbeat" 

        #~ print add_ctrl, add_node, state 

        if last_seen is None: 

            last_seen=datetime.datetime.now() 

        #Create/Update an entry in cache 

        #~ print "heartbeat update %s, %s, %s : %s" % (add_ctrl, add_node, heartbeat, last_seen) 

        if add_ctrl not in self.entries: 

            self.entries[add_ctrl] = {} 

        nodes = [] 

        if add_node == -1: 

            for nds in self.entries[add_ctrl]: 

                nodes.append(nds) 

        else: 

            nodes.append(add_node) 

        #print nodes 

        for nd in nodes: 

            if nd not in self.entries[add_ctrl]: 

                self.entries[add_ctrl][nd] = {} 

            self.entries[add_ctrl][nd]["state"] = state.upper() 

            self.entries[add_ctrl][nd]["heartbeat"] = heartbeat * 1.1 

            #print "Update state here" 

            self.entries[add_ctrl][nd]["last_seen"] = last_seen 

            if state == 'ONLINE' or state == 'BOOT': 

                #Reset the counter 

                self.entries[add_ctrl][nd]["count"] = 0 

 

    def get_state(self, add_ctrl, add_node): 

        """Return the state of an entry in cache 

 

        :param add_ctrl: the controller part of the address 

        :type add_ctrl: Integer 

        :param add_node: the node part of the address. 0 for controller, -1 for all nodes managed by controller. 

        :type add_node: Integer 

        """ 

        #Create/Update an entry in cache 

        if add_ctrl not in self.entries or add_node not in self.entries[add_ctrl]: 

            return 'OFFLINE' 

        return self.entries[add_ctrl][add_node]['state'] 

 

    def has_entry(self, add_ctrl, add_node): 

        """Chech if an entry is in cache 

 

        :param add_ctrl: the controller part of the address 

        :type add_ctrl: Integer 

        :param add_node: the node part of the address. 0 for controller, -1 for all nodes managed by controller. 

        :type add_node: Integer 

        """ 

        #Create/Update an entry in cache 

        if add_ctrl not in self.entries: 

            return False 

        if add_node == -1: 

            add_node = 0 

        if add_node in self.entries[add_ctrl]: 

            return True 

        return False 

 

    def remove(self, add_ctrl, add_node): 

        """Remove an entry fom cache 

 

        :param add_ctrl: the controller part of the address 

        :type add_ctrl: Integer 

        :param add_node: the node part of the address. 0 for controller, -1 for all nodes managed by controller. 

        :type add_node: Integer 

        """ 

        #Remove an antry from cache and clean tree in needed 

        if add_ctrl not in self.entries: 

            return 

        if add_node in self.entries[add_ctrl]: 

            del self.entries[add_ctrl][add_node] 

        if len(self.entries[add_ctrl]) == 0 or add_node == -1: 

            del self.entries[add_ctrl] 

 

    def len(self): 

        """Number of entries in the cache 

 

        """ 

        return len(self.entries) 

 

    def check_heartbeats(self, heartbeat_timeout=60, heartbeat_count=3, heartbeat_dead=604800): 

        """Check the states of the machine. Must be called in a timer 

        Called in a separate thread. Must use a scoped_session. 

 

        :param session: the session to use to communicate with db. May be a scoped_session if used in a separate tread. If None, use the common session. 

        :type session: sqlalchemy session 

        """ 

        #~ print self.entries 

        #~ print "Check heartbeat" 

        now = datetime.datetime.now() 

        lleases = list() 

        for ctrl in self.entries.keys(): 

            for node in self.entries[ctrl].keys(): 

                if (now - self.entries[ctrl][node]['last_seen']).total_seconds() > self.entries[ctrl][node]['heartbeat'] \ 

                  and self.entries[ctrl][node]['state'] in ['ONLINE', 'BOOT', 'CONFIG', 'PENDING', 'FAILED', 'DEAD', 'OFFLINE']: 

                    #~ print "add heartbeat %s,%s : %s"   % (ctrl, node, self.entries[ctrl][node]['last_seen']) 

                    lleases.append((ctrl, node)) 

        for ctrl, node in lleases: 

            if self.entries[ctrl][node]['state'] == 'FAILED' \ 

              and (now - self.entries[ctrl][node]['last_seen']).total_seconds() > heartbeat_dead: 

                self.entries[ctrl][node]['state'] = 'DEAD' 

                self.entries[ctrl][node]['count'] = 0 

            else : 

                #~ print self.entries[ctrl][node] 

                if "count" not in self.entries[ctrl][node]: 

                    self.entries[ctrl][node]['count'] = 1 

                else: 

                    self.entries[ctrl][node]['count'] += 1 

                if self.entries[ctrl][node]['count'] >= heartbeat_count: 

                    #The count is reached 

                    #We need to change the state 

                    if self.entries[ctrl][node]['state'] == 'ONLINE': 

                        self.entries[ctrl][node]['state'] = 'PENDING' 

                        self.entries[ctrl][node]['count'] = 0 

                    elif self.entries[ctrl][node]['state'] == 'BOOT': 

                        self.entries[ctrl][node]['state'] = 'PENDING' 

                        self.entries[ctrl][node]['count'] = 0 

                    elif self.entries[ctrl][node]['state'] == 'PENDING': 

                        self.entries[ctrl][node]['state'] = 'FAILED' 

                        self.entries[ctrl][node]['count'] = 0 

                else: 

                    lleases.remove((ctrl, node)) 

            #~ print heartbeat_count 

            #~ print self.entries[ctrl][node] 

        #~ print self.entries 

        #~ print lleases 

        return lleases 

 

class HeartbeatMessage(object): 

    """ 

    """ 

    def __init__(self, message): 

        """ 

        """ 

        self.message = message 

 

    def get_heartbeat(self): 

        add_ctrl = -1 

        add_node = -1 

        state = 'ONLINE' 

        try: 

            #Try to decode payload as json 

            #And retrieve add_ctrl and add_node from it 

            data = json_loads(self.message.payload) 

            for ffield in ['add_ctrl', 'add_node']: 

                if ffield not in data: 

                    logger.warning("mqtt_on_heartbeat receive a request with no %s", ffield) 

                    return None, None, None 

                try: 

                    add_ctrl = int(data['add_ctrl']) 

                except ValueError: 

                    logger.exception("mqtt_on_heartbeat can't convert add_ctrl %s to integer", data['add_ctrl']) 

                    return None, None, None 

                except TypeError: 

                    logger.exception("mqtt_on_heartbeat can't convert add_ctrl %s to integer", data['add_ctrl']) 

                    return None, None, None 

                try: 

                    add_node = int(data['add_node']) 

                except ValueError: 

                    logger.exception("mqtt_on_heartbeat can't convert add_node %s to integer", data['add_node']) 

                    return None, None, None 

                except TypeError: 

                    logger.exception("mqtt_on_heartbeat can't convert add_node %s to integer", data['add_node']) 

                    return None, None, None 

            if 'state' in data: 

                state = data['state'] 

        except ValueError: 

            #Try to retrieve adds from topic 

            #hadd = message.topic.split('/')[-1] 

            hadd = self.message.topic.replace("/dhcp/heartbeat/","") 

            #print "hadd %s" % hadd 

            try: 

                add_ctrl,add_node = hadd_split(hadd) 

                if add_ctrl is None: 

                    return None, None, None 

                state = self.message.payload 

            except ValueError: 

                logger.exception("get_heartbeat exception") 

                return None, None, None 

            except TypeError: 

                logger.exception("get_heartbeat exception") 

                return None, None, None 

        return add_ctrl, add_node, state 

 

def threaded_send_resolv(thread_event, options, hadd, resp, data): 

    """ 

    """ 

    if thread_event.is_set(): 

        return 

    mqttc = MQTTClient(options=options.data) 

    mqttc.connect() 

    mqttc.start() 

    max_in_a_loop = 10 

    time_to_sleep = 0.06 

    if hadd is None or hadd=='': 

        topic = TOPIC_RESOLV_BROADCAST 

    else: 

        topic = TOPIC_RESOLV_REPLY % hadd 

    if data is None or len(data) == 0: 

        #We send the response as is 

        msg = json_dumps(resp) 

        mqttc.publish(topic=topic, payload=msg) 

    else : 

        #~ print "data : %s"%data 

        while len(data)>0: 

            in_loop = 0 

            for key in data.keys(): 

                if key in ["nodes", "systems", "configs", "commands", "basics", "users"]: 

                    resp['data'] = {} 

                    for kkey in data[key].keys(): 

                        #~ print "data 2: %s"%data 

                        #print "in_loop : %s"%in_loop 

                        if in_loop < max_in_a_loop: 

                            resp['data'][kkey] = data[key][kkey] 

                            del data[key][kkey] 

                            in_loop +=1 

                        else: 

                            break 

                    if len(data[key]) == 0: 

                        del data[key] 

                    if len(resp['data'])==0: 

                        break 

                    #~ print "resp = %s"%resp 

                    msg = json_dumps(resp) 

                    mqttc.publish(topic=topic, payload=msg) 

                    thread_event.wait(time_to_sleep) 

    mqttc.stop() 

    if mqttc.is_alive(): 

        try: 

            mqttc.join() 

        except: 

            logger.exception("Catched exception") 

    mqttc = None 

 

 

class JNTNetwork(object): 

    """The network manager : handle all nodes, values, ... 

 

    Will be used in dhcp server and web server. 

 

    Should work in 2 modes : 

     - resolv : use the dhcp to retrieve information : nodes, values, configs, ... 

      - listen to nodes updates sent by the dhcp (node_update, node_added, node_remove, ... 

     - broadcast : to retrieve informations from nodes : in case of a dhcp fail or for the dhcp itself 

      - heartbeat listening : when we receive a heartbeat from an unknown node, we send it standard system queries 

      - send notification for node_added, node_update_info, node_update_config, ... 

 

    Actually, we depend on socket/flask to emit update for the listener. This should be removed for the merge with the dhcp manager 

    We can achieve it by callbacks or class heritage. 

 

    Same problem for the lease manager 

 

    heartbeat : heartbeat will be managed inside this class. No more dependcy to /dhcp = > new topic /heartbeat 

 

    Starting as master is simple 

 

    We should implement startup as a state machine : 

    master : broadcast -> heartbeat 

    secondary : resolv 

    and if needed -> failover 

 

    State machines : 

 

        - fsm_network : a global one to manage primary, secondary and fail over states 

 

    """ 

 

    fsm_network_states = [ 

        State(name='STOPPED', on_enter=['stop_resolv_heartbeat','stop_resolv_discover', 'stop_resolv_request', 'stop_nodes_discover', 'stop_heartbeat', 'stop_heartbeat_discover', 'stop_dispatch_heartbeat', 

                'unset_failed', 'stop_broadcast_nodes_discover', 'stop_broadcast_configs_discover', 'stop_broadcast_systems_discover', 'stop_broadcast_basics_discover', 

                'stop_broadcast_users_discover']), 

        State(name='STARTED', on_enter=['start_values_listener', 'fsm_on_started'], on_exit=['stop_values_listener', 'stop_resolv_discover']), 

        #~ State(name='BROADCAST_START', on_exit=['start_broadcast_discover']), 

        #~ State(name='BROADCAST_STOP', on_exit=['stop_broadcast_discover']), 

        State(name='BROADCAST_NODES', on_enter=['start_nodes_discover','start_broadcast_nodes_discover'], on_exit=['stop_broadcast_nodes_discover']), 

        State(name='BROADCAST_SYSTEMS', on_enter=['start_broadcast_systems_discover'], on_exit=['stop_broadcast_systems_discover']), 

        State(name='BROADCAST_CONFIGS', on_enter=['start_broadcast_configs_discover'], on_exit=['stop_broadcast_configs_discover']), 

        State(name='BROADCAST_COMMANDS', on_enter=['start_broadcast_commands_discover'], on_exit=['stop_broadcast_commands_discover']), 

        State(name='BROADCAST_BASICS', on_enter=['start_broadcast_basics_discover'], on_exit=['stop_broadcast_basics_discover']), 

        State(name='BROADCAST_USERS', on_enter=['start_broadcast_users_discover'], on_exit=['stop_broadcast_users_discover', 'stop_broadcast_discover']), 

        State(name='RESOLV', on_enter=['start_resolv_discover', 'start_resolv_heartbeat']), 

        State(name='HEARTBEAT_DISCOVER', on_enter=['start_heartbeat_discover']), 

        State(name='HEARTBEAT', on_enter=['start_heartbeat']), 

        State(name='DISPATCH', on_enter=['start_dispatch_heartbeat', 'start_resolv_request'], on_exit=['stop_resolv_request']), 

    ] 

 

    states_str = { 

        'STOPPED' : "Network is stopped", 

        'STARTED' : "Network is started", 

        'BROADCAST_NODES' : "Broadcasting nodes", 

        'BROADCAST_USERS' : "Broadcasting user values", 

        'BROADCAST_CONFIGS' : "Broadcasting config values", 

        'BROADCAST_SYSTEMS' : "Broadcasting system values", 

        'BROADCAST_BASICS' : "Broadcasting basic values", 

        'BROADCAST_COMMANDS' : "Broadcasting command values", 

        'RESOLV' : "Resolving nodes", 

        'HEARTBEAT_DISCOVER' : "Start headbeart discovering", 

        'HEARTBEAT' : "Start headbeart listening", 

        'DISPATCH' : "Start headbeart dispatch", 

    } 

 

    """The states of the network 

    """ 

 

    def __init__(self, stopevent, options, **kwargs): 

        """ 

        """ 

        self.options = options 

        self._stopevent = stopevent 

        self.home_id = kwargs.get('home_id', "Unknown") 

        self.is_primary = kwargs.get('is_primary', True) 

        self.do_heartbeat_dispatch = kwargs.get('do_heartbeat_dispatch', True) 

        self.is_secondary = kwargs.get('is_secondary', False) 

        self._is_failed = kwargs.get('is_failed', False) 

        self.broadcast_timeout = kwargs.get('broadcast_timeout', 3) 

        self.resolv_timeout = kwargs.get('resolv_timeout', 5) 

        self.request_timeout = kwargs.get('request_timeout', 3) 

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

        """For tests only""" 

 

        self.state = 'STOPPED' 

        self.fsm_network = None 

        self.nodes = {} 

        self.users = {} 

        self.configs = {} 

        self.basics = {} 

        self.systems = {} 

        self.commands = {} 

        self._lock = threading.Lock() 

        self.broadcast_mqttc = None 

        self.broadcast_nodes_timer = None 

        self.broadcast_configs_timer = None 

        self.broadcast_systems_timer = None 

        self.broadcast_users_timer = None 

        self.broadcast_basics_timer = None 

        self.broadcast_commands_timer = None 

        self.heartbeat_discover_mqttc = None 

        self.nodes_mqttc = None 

        self.resolv_mqttc = None 

        self.resolv_timeout_timer = None 

        self.resolv_heartbeat_mqttc = None 

        self.resolv_heartbeat_timer = None 

        self.dispatch_heartbeat_mqttc = None 

        self.dispatch_heartbeat_timer = None 

        self.values_mqttc = None 

        self.resolv_request_mqttc = None 

        self.heartbeat_mqttc = None 

 

        self.dbcon = None 

        self.hadds = {} 

        self.heartbeat_cache = None 

        self.threads_timers = [] 

 

        #~ self._replies = {'request_info_nodes' : self.add_nodes, 'request_info_users' : self.add_users, 'request_info_configs' : self.add_configs, 

            #~ 'request_info_systems' : self.add_systems, 'request_info_basics' : self.add_basics, 'request_info_commands' : self.add_commands } 

 

    def __del__(self): 

        """ 

        """ 

        try: 

            self.stop() 

        except: 

            pass 

 

    @property 

    def is_failed(self): 

        """ 

        """ 

        return self._is_failed 

 

    @property 

    def is_stopped(self): 

        """Return True if the network is stopped 

        """ 

        return self.fsm_network == None 

 

    @property 

    def is_started(self): 

        """Return True if the network is started 

        """ 

        return self.state == "STARTED" 

 

    def set_failed(self): 

        """ 

        """ 

        self._is_failed = True 

        self.emit_network() 

 

    def unset_failed(self): 

        """ 

        """ 

        self._is_failed = False 

        self.emit_network() 

 

    def start(self, loop_sleep=0.1): 

        """Start the network 

        """ 

        self.heartbeat_cache = CacheManager() 

        self.loop_sleep = loop_sleep 

        options = self.options.get_options('network') 

        self.from_dict(options) 

        #print self.__dict__ 

        if self.is_primary and self.is_secondary: 

            raise RuntimeError("Can't start in both modes : primary or secondary") 

        logger.debug("Start network with options %s" % options) 

        self.heartbeat_cache.start(None) 

        self.emit_nodes() 

        self.emit_network() 

        self._stopevent.clear() 

        if self.fsm_network is None: 

            self.fsm_network = self.create_fsm() 

            self.state = 'STOPPED' 

        self.fsm_network_start() 

 

    def create_fsm(self): 

        """ 

        """ 

        fsm = Machine(model=self, states=self.fsm_network_states, initial='STOPPED') 

        fsm.add_ordered_transitions() 

        #~ fsm.add_transition('fsm_network_start', 'STOPPED', 'BROADCAST_START', conditions=['is_primary']) 

        fsm.add_transition('fsm_network_start', 'STOPPED', 'BROADCAST_NODES', conditions=['fsm_is_primary']) 

        fsm.add_transition('fsm_network_start', 'STOPPED', 'RESOLV', conditions=['fsm_is_secondary']) 

        #~ fsm.add_transition('fsm_network_next', 'BROADCAST_START', 'BROADCAST_NODES') 

        fsm.add_transition('fsm_network_next', 'BROADCAST_NODES', 'BROADCAST_SYSTEMS') 

        fsm.add_transition('fsm_network_next', 'BROADCAST_SYSTEMS', 'BROADCAST_COMMANDS') 

        fsm.add_transition('fsm_network_next', 'BROADCAST_COMMANDS', 'BROADCAST_CONFIGS') 

        fsm.add_transition('fsm_network_next', 'BROADCAST_CONFIGS', 'BROADCAST_BASICS') 

        fsm.add_transition('fsm_network_next', 'BROADCAST_BASICS', 'BROADCAST_USERS') 

        #~ fsm.add_transition('fsm_network_next', 'BROADCAST_CONFIGS', 'BROADCAST_STOP') 

        fsm.add_transition('fsm_network_next', 'BROADCAST_USERS', 'HEARTBEAT_DISCOVER') 

        #~ fsm.add_transition('fsm_network_next', 'BROADCAST_STOP', 'HEARTBEAT_DISCOVER') 

        fsm.add_transition('fsm_network_next', 'HEARTBEAT_DISCOVER', 'DISPATCH', conditions=['fsm_do_heartbeat_dispatch']) 

        fsm.add_transition('fsm_network_next', 'HEARTBEAT_DISCOVER', 'STARTED') 

        fsm.add_transition('fsm_network_next', 'DISPATCH', 'STARTED') 

        fsm.add_transition('fsm_network_next', 'RESOLV', 'HEARTBEAT') 

        fsm.add_transition('fsm_network_next', 'HEARTBEAT', 'STARTED') 

        fsm.add_transition('fsm_network_fail', 'STARTED', 'BROADCAST_NODES', before = ['set_failed']) 

        fsm.add_transition('fsm_network_fail', 'RESOLV', 'BROADCAST_NODES', before = ['set_failed']) 

        fsm.add_transition('fsm_network_recover', 'STARTED', 'RESOLV', before = ['stop_heartbeat_discover', 'stop_dispatch_heartbeat', 'unset_failed']) 

        fsm.add_transition('fsm_network_stop', '*', 'STOPPED', before = ['before_stop_fsm'], after = ['delete_fsm']) 

        return fsm 

 

    def delete_fsm(self): 

        """Delete the fsm 

        """ 

        self.fsm_network = None 

        self.clean_all() 

 

    def clean_all(self): 

        """Delete the fsm 

        """ 

        if self.heartbeat_cache is not None: 

            self.heartbeat_cache.flush() 

            self.heartbeat_cache = None 

        self._lock.acquire() 

        try: 

            self.threads_timers = [] 

            self.nodes = {} 

            self.hadds = {} 

            self.configs = {} 

            self.users = {} 

            self.bascis = {} 

            self.systems = {} 

        except: 

            logger.exception("Exception in network stop") 

        finally: 

            self._lock.release() 

 

    def before_stop_fsm(self): 

        """Stop the network 

        """ 

        self._stopevent.set() 

        self.stop_resolv_heartbeat_timer() 

        self.stop_dispatch_heartbeat_timer() 

 

    def stop(self): 

        """Stop the network 

        """ 

        self.before_stop_fsm() 

        if self.fsm_network is not None: 

            self.fsm_network_stop() 

        self.clean_all() 

        #~ self.emit_nodes() 

        #~ self.emit_network() 

 

    def fsm_is_primary(self): 

        """ 

        """ 

        return self.is_primary 

 

    def fsm_is_secondary(self): 

        """ 

        """ 

        return self.is_secondary and not self.is_primary 

 

    def fsm_do_heartbeat_dispatch(self): 

        """ 

        """ 

        return self.do_heartbeat_dispatch 

        # Temporary fix https://travis-ci.org/bibi21000/janitoo/builds/95463154 

        # return True 

 

    def start_broadcast_nodes_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_broadcast_nodes_discover') 

        if self._test: 

            print "start_broadcast_nodes_discover" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            if self.broadcast_mqttc is None: 

                self.broadcast_mqttc = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) 

                self.broadcast_mqttc.connect() 

                self.broadcast_mqttc.subscribe(topic=TOPIC_BROADCAST_REPLY%self.hadds[0], callback=self.on_reply) 

                self.broadcast_mqttc.start() 

            msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_nodes', 'reply_hadd':self.hadds[0]} 

            self.broadcast_mqttc.publish(TOPIC_BROADCAST_REQUEST, json_dumps(msg)) 

            self.broadcast_nodes_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_nodes_discover) 

            self.broadcast_nodes_timer.start() 

        self.emit_network() 

        self.emit_nodes() 

 

    def stop_broadcast_nodes_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_broadcast_nodes_discover') 

        if self._test: 

            print "stop_nodes_broadcast_discover" 

        else: 

            if self.broadcast_nodes_timer is not None: 

                self.broadcast_nodes_timer.cancel() 

            self.broadcast_nodes_timer = None 

        self.emit_network() 

        self.emit_nodes() 

 

    def stop_broadcast_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_broadcast_discover') 

        if self._test: 

            print "stop_broadcast_discover" 

        else: 

            if self.broadcast_mqttc is not None: 

                self.broadcast_mqttc.unsubscribe(topic=TOPIC_BROADCAST_REPLY%self.hadds[0]) 

                self.broadcast_mqttc.stop() 

                if self.broadcast_mqttc.is_alive(): 

                    try: 

                        self.broadcast_mqttc.join() 

                    except: 

                        logger.exception("Catched exception") 

                self.broadcast_mqttc = None 

        self.emit_network() 

        self.emit_nodes() 

 

    def finish_broadcast_nodes_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'finish_broadcast_nodes_discover') 

        self.emit_network() 

        self.emit_nodes() 

        if not self.is_started and not self._stopevent.is_set(): 

            self.fsm_network_next() 

 

    def start_broadcast_users_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_broadcast_users_discover') 

        if self._test: 

            print "start_broadcast_users_discover" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_users', 'reply_hadd':self.hadds[0]} 

            self.broadcast_mqttc.publish(TOPIC_BROADCAST_REQUEST, json_dumps(msg)) 

            self.broadcast_users_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_users_discover) 

            self.broadcast_users_timer.start() 

        self.emit_network() 

        self.emit_nodes() 

 

    def stop_broadcast_users_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_broadcast_users_discover') 

        if self._test: 

            print "stop_broadcast_users_discover" 

        else: 

            if self.broadcast_users_timer is not None: 

                self.broadcast_users_timer.cancel() 

            self.broadcast_users_timer = None 

        self.emit_network() 

        self.emit_nodes() 

 

    def finish_broadcast_users_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'finish_broadcast_users_discover') 

        self.emit_network() 

        self.emit_users() 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        self.fsm_network_next() 

 

    def start_broadcast_configs_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_broadcast_configs_discover') 

        if self._test: 

            print "start_broadcast_configs_discover" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_configs', 'reply_hadd':self.hadds[0]} 

            self.broadcast_mqttc.publish(TOPIC_BROADCAST_REQUEST, json_dumps(msg)) 

            self.broadcast_configs_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_configs_discover) 

            self.broadcast_configs_timer.start() 

        self.emit_network() 

        self.emit_nodes() 

 

    def stop_broadcast_configs_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_broadcast_configs_discover') 

        if self._test: 

            print "stop_broadcast_configs_discover" 

        else: 

            if self.broadcast_configs_timer is not None: 

                self.broadcast_configs_timer.cancel() 

            self.broadcast_configs_timer = None 

 

    def finish_broadcast_configs_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'finish_broadcast_configs_discover') 

        self.emit_network() 

        self.emit_configs() 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        self.fsm_network_next() 

 

    def start_broadcast_basics_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_broadcast_basics_discover') 

        if self._test: 

            print "start_broadcast_basics_discover" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_basics', 'reply_hadd':self.hadds[0]} 

            self.broadcast_mqttc.publish(TOPIC_BROADCAST_REQUEST, json_dumps(msg)) 

            self.broadcast_basics_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_basics_discover) 

            self.broadcast_basics_timer.start() 

        self.emit_network() 

        self.emit_nodes() 

 

    def stop_broadcast_basics_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_broadcast_basics_discover') 

        if self._test: 

            print "stop_broadcast_basics_discover" 

        else: 

            if self.broadcast_basics_timer is not None: 

                self.broadcast_basics_timer.cancel() 

            self.broadcast_basics_timer = None 

 

    def finish_broadcast_basics_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'finish_broadcast_basics_discover') 

        self.emit_network() 

        self.emit_basics() 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        self.fsm_network_next() 

 

    def start_broadcast_systems_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_broadcast_systems_discover') 

        if self._test: 

            print "start_broadcast_systems_discover" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_systems', 'reply_hadd':self.hadds[0]} 

            self.broadcast_mqttc.publish(TOPIC_BROADCAST_REQUEST, json_dumps(msg)) 

            self.broadcast_systems_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_systems_discover) 

            self.broadcast_systems_timer.start() 

        self.emit_network() 

        self.emit_nodes() 

 

    def stop_broadcast_systems_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_broadcast_systems_discover') 

        if self._test: 

            print "stop_broadcast_systems_discover" 

        else: 

            if self.broadcast_systems_timer is not None: 

                self.broadcast_systems_timer.cancel() 

            self.broadcast_systems_timer = None 

 

    def finish_broadcast_systems_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'finish_broadcast_systems_discover') 

        self.emit_network() 

        self.emit_systems() 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        self.fsm_network_next() 

 

    def start_broadcast_commands_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_broadcast_commands_discover') 

        if self._test: 

            print "start_broadcast_commands_discover" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_commands', 'reply_hadd':self.hadds[0]} 

            self.broadcast_mqttc.publish(TOPIC_BROADCAST_REQUEST, json_dumps(msg)) 

            self.broadcast_commands_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_commands_discover) 

            self.broadcast_commands_timer.start() 

        self.emit_network() 

        self.emit_nodes() 

 

    def stop_broadcast_commands_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_broadcast_commands_discover') 

        if self._test: 

            print "stop_broadcast_commands_discover" 

        else: 

            if self.broadcast_commands_timer is not None: 

                self.broadcast_commands_timer.cancel() 

            self.broadcast_commands_timer = None 

 

    def finish_broadcast_commands_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'finish_broadcast_commands_discover') 

        self.emit_network() 

        self.emit_commands() 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        self.fsm_network_next() 

 

    def fsm_on_started(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'fsm_on_started') 

        self.emit_all() 

 

    def start_resolv_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_resolv_discover') 

        if self._test: 

            print "start_resolv_discover" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            if self.resolv_mqttc is None: 

                self.resolv_mqttc = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) 

                self.resolv_mqttc.connect() 

                self.resolv_mqttc.subscribe(topic="%s#"%TOPIC_RESOLV) 

                self.resolv_mqttc.add_topic(topic=TOPIC_RESOLV_REPLY%self.hadds[0], callback=self.on_resolv_reply) 

                self.resolv_mqttc.add_topic(topic=TOPIC_RESOLV_BROADCAST+'#', callback=self.on_resolv_reply) 

                self.resolv_mqttc.start() 

                try: 

                    self._stopevent.wait(0.5) 

                except: 

                    logger.exception(u'Catched exception') 

                #~ self.resolv_mqttc.on_connect = self.resolv_mqttc_on_connect 

                th = threading.Timer(self.request_timeout/4, self.request_resolv_nodes) 

                th.start() 

                th = threading.Timer(2*self.request_timeout/4, self.request_resolv_systems) 

                th.start() 

                th = threading.Timer(3*self.request_timeout/4, self.request_resolv_configs) 

                th.start() 

                th = threading.Timer(4*self.request_timeout/4, self.request_resolv_basics) 

                th.start() 

                th = threading.Timer(1+self.request_timeout/4, self.request_resolv_users) 

                th.start() 

                th = threading.Timer(1+2*self.request_timeout/4, self.request_resolv_commands) 

                th.start() 

            if self.resolv_timeout_timer is not None: 

                self.resolv_timeout_timer.cancel() 

                self.resolv_timeout_timer = None 

            self.resolv_timeout_timer = threading.Timer(self.resolv_timeout, self.finish_resolv_discover) 

            self.resolv_timeout_timer.start() 

        self.emit_network() 

        self.emit_nodes() 

 

    def finish_resolv_discover(self): 

        """This function is called when we did nod receive informations on /dhcp/resolv defore timeout. The dhcp server must have send its 'online' status ... so he his dead 

        fallback to fail mode 

        """ 

        logger.debug("fsm_network : %s", 'finish_resolv_discover') 

        self.resolv_timeout_timer = None 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        if not self.is_failed: 

            logger.warning("The network switch to failed mode") 

            self.fsm_network_fail() 

        self.emit_network() 

 

    def stop_resolv_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_resolv_discover') 

        if self._test: 

            print "stop_resolv_discover" 

        else: 

            if self.resolv_timeout_timer is not None: 

                self.resolv_timeout_timer.cancel() 

                self.resolv_timeout_timer = None 

            if self.resolv_mqttc is not None: 

                self.resolv_mqttc.remove_topic(topic=TOPIC_RESOLV_REPLY%self.hadds[0]) 

                self.resolv_mqttc.remove_topic(topic=TOPIC_RESOLV_BROADCAST+'#') 

                self.resolv_mqttc.unsubscribe(topic="%s#"%TOPIC_RESOLV) 

                try: 

                    self.resolv_mqttc.stop() 

                except: 

                    logger.exception("Catched exception") 

                if self.resolv_mqttc.is_alive(): 

                    try: 

                        self.resolv_mqttc.join() 

                    except: 

                        logger.exception("Catched exception") 

                self.resolv_mqttc = None 

 

    def start_resolv_request(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_resolv_request') 

        if self._test: 

            print "start_resolv_request" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            try: 

                if self.resolv_request_mqttc is None: 

                    self.resolv_request_mqttc = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) 

                    self.resolv_request_mqttc.connect() 

                    self.resolv_request_mqttc.subscribe(topic="%s#"%TOPIC_RESOLV_REQUEST, callback=self.on_resolv_request) 

                    self.resolv_request_mqttc.start() 

            except AttributeError: 

                if self._stopevent.is_set() or self.is_stopped: 

                    return 

                else: 

                    raise 

 

    def stop_resolv_request(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_resolv_request') 

        if self._test: 

            print "stop_resolv_request" 

        else: 

            if self.resolv_request_mqttc is not None: 

                self.resolv_request_mqttc.unsubscribe(topic="%s#"%TOPIC_RESOLV_REQUEST) 

                try: 

                    self.resolv_request_mqttc.stop() 

                except: 

                    logger.exception("Catched exception") 

                try: 

                    self.resolv_request_mqttc.join() 

                except: 

                    logger.exception("Catched exception") 

                self.resolv_request_mqttc = None 

 

    def start_resolv_heartbeat(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_resolv_heartbeat') 

        if self._test: 

            print "start_resolv_heartbeat" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            try: 

                if self.resolv_heartbeat_mqttc is None: 

                    self.resolv_heartbeat_mqttc = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) 

                    self.resolv_heartbeat_mqttc.connect() 

                    self.resolv_heartbeat_mqttc.subscribe(topic="%sheartbeat"%TOPIC_RESOLV, callback=self.on_resolv_heartbeat) 

                    self.resolv_heartbeat_mqttc.start() 

                    self.stop_resolv_heartbeat_timer() 

            except AttributeError: 

                if self._stopevent.is_set() or self.is_stopped: 

                    return 

                else: 

                    raise 

 

    def start_resolv_heartbeat_timer(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_resolv_heartbeat_timer') 

        if self._test: 

            print "start_resolv_heartbeat_timer" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            self.stop_resolv_heartbeat_timer() 

            self.resolv_heartbeat_timer = threading.Timer(self.resolv_timeout, self.finish_resolv_heartbeat_timer) 

            self.resolv_heartbeat_timer.start() 

 

    def finish_resolv_heartbeat_timer(self): 

        """This function is called when we did nod receive informations on /dhcp/resolv defore timeout. The dhcp server must have send its 'online' status ... so he his dead 

        fallback to fail mode 

        """ 

        logger.debug("fsm_network : %s", 'finish_resolv_heartbeat_timer') 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        if not self.is_failed and self.is_started: 

            logger.warning("The network switch to failed mode") 

            self.fsm_network_fail() 

        self.emit_network() 

 

    def stop_resolv_heartbeat(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_resolv_heartbeat') 

        if self._test: 

            print "stop_resolv_heartbeat" 

        else: 

            self.stop_resolv_heartbeat_timer() 

            if self.resolv_heartbeat_mqttc is not None: 

                self.resolv_heartbeat_mqttc.unsubscribe(topic="%sheartbeat"%TOPIC_RESOLV) 

                try: 

                    self.resolv_heartbeat_mqttc.stop() 

                except: 

                    logger.exception("Catched exception") 

                try: 

                    self.resolv_heartbeat_mqttc.join() 

                except: 

                    logger.exception("Catched exception") 

                self.resolv_heartbeat_mqttc = None 

 

    def stop_resolv_heartbeat_timer(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_resolv_heartbeat_timer') 

        if self._test: 

            print "stop_resolv_heartbeat_timer" 

        else: 

            if self.resolv_heartbeat_timer is not None: 

                self.resolv_heartbeat_timer.cancel() 

                self.resolv_heartbeat_timer = None 

 

    def stop_nodes_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_nodes_discover') 

        if self._test: 

            print "stop_nodes_discover" 

        else: 

            if self.nodes_mqttc is not None: 

                self.nodes_mqttc.unsubscribe(topic='/nodes/%s/reply/#'%self.hadds[0]) 

                try: 

                    self.nodes_mqttc.stop() 

                except: 

                    logger.exception("Catched exception") 

                try: 

                    self.nodes_mqttc.join() 

                except: 

                    logger.exception("Catched exception") 

                self.nodes_mqttc = None 

 

    def start_nodes_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_nodes_discover') 

        if self._test: 

            print "start_nodes_discover" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            try: 

                if self.nodes_mqttc is None: 

                    self.nodes_mqttc = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) 

                    self.nodes_mqttc.connect() 

                    self.nodes_mqttc.subscribe(topic='/nodes/%s/reply/#'%self.hadds[0], callback=self.on_reply) 

                    logger.debug("start_nodes_discover : listen to %s", self.hadds[0]) 

                    self.nodes_mqttc.start() 

            except AttributeError: 

                if self._stopevent.is_set() or self.is_stopped: 

                    return 

                else: 

                    raise 

        self.emit_nodes() 

        self.emit_network() 

 

    def start_heartbeat_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_heartbeat_discover') 

        if self._test: 

            print "start_heartbeat_discover" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            try: 

                if self.heartbeat_discover_mqttc is None: 

                    self.heartbeat_discover_mqttc = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) 

                    self.heartbeat_discover_mqttc.connect() 

                    self.heartbeat_discover_mqttc.subscribe(topic='/dhcp/heartbeat/#', callback=self.on_heartbeat_discover) 

                    self.heartbeat_discover_mqttc.start() 

            except AttributeError: 

                if self._stopevent.is_set() or self.is_stopped: 

                    return 

                else: 

                    raise 

        self.emit_network() 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        self.fsm_network_next() 

 

    def stop_heartbeat_discover(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_heartbeat_discover') 

        if self._test: 

            print "stop_heartbeat_discover" 

        else: 

            if self.heartbeat_discover_mqttc is not None: 

                self.heartbeat_discover_mqttc.unsubscribe(topic='/dhcp/heartbeat/#') 

                try: 

                    self.heartbeat_discover_mqttc.stop() 

                except: 

                    logger.exception("Catched exception") 

                try: 

                    self.heartbeat_discover_mqttc.join() 

                except: 

                    logger.exception("Catched exception") 

                self.heartbeat_discover_mqttc = None 

        self.emit_network() 

 

    def start_heartbeat(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_heartbeat') 

        if self._test: 

            print "start_heartbeat" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            try: 

                if self.heartbeat_mqttc is None: 

                    self.heartbeat_mqttc = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) 

                    self.heartbeat_mqttc.connect() 

                    self.heartbeat_mqttc.subscribe(topic='/dhcp/heartbeat/#', callback=self.on_heartbeat) 

                    self.heartbeat_mqttc.start() 

            except AttributeError: 

                if self._stopevent.is_set() or self.is_stopped: 

                    return 

                else: 

                    raise 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        self.emit_network() 

        self.fsm_network_next() 

 

    def stop_heartbeat(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_heartbeat') 

        if self._test: 

            print "stop_heartbeat" 

        else: 

            if self.heartbeat_mqttc is not None: 

                self.heartbeat_mqttc.unsubscribe(topic='/dhcp/heartbeat/#') 

                try: 

                    self.heartbeat_mqttc.stop() 

                except: 

                    logger.exception("Catched exception") 

                try: 

                    self.heartbeat_mqttc.join() 

                except: 

                    logger.exception("Catched exception") 

                self.heartbeat_mqttc = None 

        self.emit_network() 

 

    def start_dispatch_heartbeat(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_dispatch_heartbeat') 

        if self._test: 

            print "start_dispatch_heartbeat" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            if self.dispatch_heartbeat_mqttc is None: 

                self.dispatch_heartbeat_mqttc = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) 

                self.dispatch_heartbeat_mqttc.connect() 

                self.dispatch_heartbeat_mqttc.subscribe(topic='/dhcp/heartbeat/#', callback=self.on_heartbeat) 

                self.dispatch_heartbeat_mqttc.start() 

                self.start_dispatch_heartbeat_timer() 

                self.emit_network() 

                self.fsm_network_next() 

 

    def start_dispatch_heartbeat_timer(self): 

        """ 

        """ 

        #~ logger.debug("fsm_network : %s", 'start_dispatch_heartbeat_timer') 

        if self._test: 

            print "start_dispatch_heartbeat_timer" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            self.stop_dispatch_heartbeat_timer() 

            self.dispatch_heartbeat_timer = threading.Timer(1, self.finish_dispatch_heartbeat_timer) 

            self.dispatch_heartbeat_timer.start() 

 

    def finish_dispatch_heartbeat_timer(self): 

        """This function is called when we did nod receive informations on /dhcp/resolv defore timeout. The dhcp server must have send its 'online' status ... so he his dead 

        fallback to fail mode 

        """ 

        #~ logger.debug("fsm_network : %s", 'finish_dispatch_heartbeat_timer') 

        if self._stopevent.is_set() or self.is_stopped: 

            return 

        self.stop_dispatch_heartbeat_timer() 

        self.start_dispatch_heartbeat_timer() 

        timeouts = self.heartbeat_cache.check_heartbeats() 

        for add_ctrl, add_node in timeouts: 

            #~ print add_ctrl, add_node 

            msg = {'add_ctrl':add_ctrl, 'add_node':add_node, 'state':self.heartbeat_cache.entries[add_ctrl][add_node]['state']} 

            #~ print msg 

            self.dispatch_heartbeat_mqttc.publish_heartbeat_msg(msg) 

            #~ self.dispatch_heartbeat_mqttc.publish_heartbeat_resolv_msg(msg) 

            if self._stopevent is not None: 

                self._stopevent.wait(0.02) 

 

    def stop_dispatch_heartbeat(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_dispatch_heartbeat') 

        if self._test: 

            print "stop_dispatch_heartbeat" 

        else: 

            self.stop_dispatch_heartbeat_timer() 

            if self.dispatch_heartbeat_mqttc is not None: 

                try: 

                    self.dispatch_heartbeat_mqttc.stop() 

                except: 

                    logger.exception("Catched exception") 

                try: 

                    self.dispatch_heartbeat_mqttc.join() 

                except: 

                    logger.exception("Catched exception") 

                self.dispatch_heartbeat_mqttc = None 

 

    def stop_dispatch_heartbeat_timer(self): 

        """ 

        """ 

        #~ logger.debug("fsm_network : %s", 'stop_dispatch_heartbeat_timer') 

        if self._test: 

            print "stop_dispatch_heartbeat_timer" 

        else: 

            if self.dispatch_heartbeat_timer is not None: 

                self.dispatch_heartbeat_timer.cancel() 

                self.dispatch_heartbeat_timer = None 

 

    def start_values_listener(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'start_values_listener') 

        if self._test: 

            print "start_values_listener" 

        else: 

            if self._stopevent.is_set() or self.is_stopped: 

                return 

            try: 

                if self.values_mqttc is None: 

                    self.values_mqttc = MQTTClient(options=self.options.data, loop_sleep=self.loop_sleep) 

                    self.values_mqttc.connect() 

                    self.values_mqttc.subscribe(topic='/values/#', callback=self.on_value) 

                    self.values_mqttc.start() 

            except AttributeError: 

                if self._stopevent.is_set() or self.is_stopped: 

                    return 

                else: 

                    raise 

        self.emit_network() 

 

    def stop_values_listener(self): 

        """ 

        """ 

        logger.debug("fsm_network : %s", 'stop_values_listener') 

        if self._test: 

            print "stop_values_listener" 

        else: 

            if self.values_mqttc is not None: 

                self.values_mqttc.unsubscribe(topic='/values/#') 

                try: 

                    self.values_mqttc.stop() 

                except: 

                    logger.exception("Catched exception") 

                try: 

                    self.values_mqttc.join() 

                except: 

                    logger.exception("Catched exception") 

                self.values_mqttc = None 

        self.emit_network() 

 

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

        """On value 

 

        :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("[%s] - on_value %s", self.__class__.__name__, message.payload) 

        try: 

            mdata = json_loads(message.payload) 

            #~ print mdata 

            if 'genre' in mdata: 

                data = {0:mdata} 

            else: 

                data = mdata 

            logger.debug("[%s] - on_value 2 %s", self.__class__.__name__, data) 

            for key in data.keys(): 

                #~ print data 

                if data[key]['genre'] == 0x01: 

                    self.add_basics(data) 

                    self.emit_basic(data) 

                elif data[key]['genre'] == 0x02: 

                    self.add_users(data) 

                    self.emit_user(data) 

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

                    self.add_configs(data) 

                    self.emit_config(data) 

                elif data[key]['genre'] == 0x04: 

                    self.add_systems(data) 

                    self.emit_system(data) 

                elif data[key]['genre'] == 0x05: 

                    self.add_commands(data) 

                    self.emit_command(data) 

                else : 

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

        except: 

            logger.exception("Exception in on_value") 

 

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

        """On diqpatch 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("[%s] - on_resolv_request %s", self.__class__.__name__, message.payload) 

        #~ logger.debug("[%s] - on_resolv_request %s", self.__class__.__name__, 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'} 

            #print self.systems 

            if data['cmd_class'] == COMMAND_DISCOVERY: 

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

                    resp = {} 

                    resp.update(data) 

                    #~ print "data uuid %s" %data['uuid'] 

                    data_to_send = {'nodes':{}, 'systems':{}, 'configs':{}, 'commands':{}, 'basics':{}, 'users':{}} 

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

                        #~ print " self.nodes %s"%self.nodes 

                        for knode in self.nodes.keys(): 

                            #~ print " self.nodes.keys %s"%self.nodes.keys() 

                            data_to_send['nodes'][knode] = self.nodes[knode] 

                            #~ print "data_to_send %s"%data_to_send 

                    elif data['uuid'] == "request_info_configs": 

                        for knode in self.configs.keys(): 

                            #~ print knode 

                            #~ print knode 

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

                                #~ print kvalue 

                                value = self.configs[knode][kvalue] 

                                #~ print value 

                                if value['genre'] == 0x03: 

                                    if not value['hadd'] in data_to_send['configs']: 

                                        data_to_send['configs'][value['hadd']] = {} 

                                    data_to_send['configs'][value['hadd']][value['uuid']] = value 

                    elif data['uuid'] == "request_info_systems": 

                        for knode in self.systems.keys(): 

                            #~ print knode 

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

                                #~ print kvalue 

                                value = self.systems[knode][kvalue] 

                                #~ print value 

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

                                    if not value['hadd'] in data_to_send['systems']: 

                                        data_to_send['systems'][value['hadd']] = {} 

                                    data_to_send['systems'][value['hadd']][value['uuid']] = value 

                    elif data['uuid'] == "request_info_commands": 

                        for knode in self.commands.keys(): 

                            #~ print knode 

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

                                #~ print kvalue 

                                value = self.commands[knode][kvalue] 

                                #~ print value 

                                if value['genre'] == 0x05: 

                                    if not value['hadd'] in data_to_send['commands']: 

                                        data_to_send['commands'][value['hadd']] = {} 

                                    data_to_send['commands'][value['hadd']][value['uuid']] = value 

                    elif data['uuid'] == "request_info_users": 

                        for knode in self.users.keys(): 

                            #~ print knode 

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

                                #~ print kvalue 

                                value = self.users[knode][kvalue] 

                                #~ print value 

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

                                    if not value['hadd'] in data_to_send['users']: 

                                        data_to_send['users'][value['hadd']] = {} 

                                    data_to_send['users'][value['hadd']][value['uuid']] = value 

                    elif data['uuid'] == "request_info_basics": 

                        for knode in self.basics.keys(): 

                            #~ print knode 

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

                                #~ print kvalue 

                                value = self.basics[knode][kvalue] 

                                #~ print value 

                                if value['genre'] == 0x01: 

                                    if not value['hadd'] in data_to_send['basics']: 

                                        data_to_send['basics'][value['hadd']] = {} 

                                    data_to_send['basics'][value['hadd']][value['uuid']] = value 

                    else: 

                        logger.warning("Can't find % in %s", data['uuid'],'on_resolv_request') 

                        return 

                    #~ print "final data_to_send %s"%data_to_send 

                    #~ print 

                    try: 

                        th = threading.Timer(0.05, threaded_send_resolv, args = (self._stopevent, self.options, data['reply_hadd'], resp, data_to_send)) 

                        th.start() 

                        self.threads_timers.append(th) 

                    except: 

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

                        return 

        except: 

            logger.exception("Exception in on_resolv_request") 

 

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

        """On reply 

 

        :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("[%s] - on_reply %s", self.__class__.__name__, message.payload) 

        try: 

            data = json_loads(message.payload) 

            #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: 

                    logger.warning("Data in %s : %s",data['uuid'], data) 

                    if len(data['data']) == 0: 

                        return 

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

                        self.add_nodes(data['data']) 

                    elif data['uuid'] == "request_info_configs": 

                        if 'genre' in data['data']: 

                            data = {0:data['data']} 

                        else: 

                            data = data['data'] 

                        #~ print "data", data 

                        for key in data.keys(): 

                            self.add_configs(data[key]) 

                    elif data['uuid'] == "request_info_systems": 

                        self.add_systems(data['data']) 

                    elif data['uuid'] == "request_info_commands": 

                        self.add_commands(data['data']) 

                    elif data['uuid'] == "request_info_users": 

                        if 'genre' in data['data']: 

                            data = {0:data['data']} 

                        else: 

                            data = data['data'] 

                        #~ print "data", data 

                        for key in data.keys(): 

                            self.add_users(data[key]) 

                    elif data['uuid'] == "request_info_basics": 

                        if 'genre' in data['data']: 

                            data = {0:data['data']} 

                        else: 

                            data = data['data'] 

                        #~ print "data", data 

                        for key in data.keys(): 

                            self.add_basics(data[key]) 

                    else: 

                        logger.warning("Unknown value% in %s", data['uuid'],'on_reply') 

                        return 

                    if self.is_primary and self.is_started: 

                        th = threading.Timer(0.05, threaded_send_resolv, args = (self._stopevent, self.options, data['reply_hadd'], data, None)) 

                        th.start() 

                        self.threads_timers.append(th) 

        except: 

            logger.exception("Exception in on_reply") 

 

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

        """On resolv 

 

        :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("[%s] - on_resolv %s", self.__class__.__name__, message.payload) 

        self.start_resolv_heartbeat_timer() 

        if self.is_failed and self.is_started: 

            logger.warning("The network switch to normal mode") 

            self.fsm_network_recover() 

        if self.resolv_timeout_timer is not None: 

            self.resolv_timeout_timer.cancel() 

            self.resolv_timeout_timer = None 

            self.fsm_network_next() 

        if message.topic == "/dhcp/resolv/heartbeat": 

            logger.debug("on_resolv : %s", 'receive heartbeat') 

            return 

 

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

        """On resolv 

 

        :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("[%s] - on_resolv_reply %s", self.__class__.__name__, message.payload) 

        self.start_resolv_heartbeat_timer() 

        self.on_reply(client, userdata, message) 

 

    def emit_network(self): 

        """Emit a network state event 

        """ 

        pass 

 

    def emit_nodes(self): 

        """Emit a nodes state event 

        """ 

        pass 

 

    def emit_node(self, nodes): 

        """Emit a node state event 

        nodes : a single node or a dict of nodes 

        """ 

        pass 

 

    def emit_users(self): 

        """Emit a nodes state event 

        """ 

        pass 

 

    def emit_user(self, users): 

        """Emit a node state event 

        nodes : a single node or a dict of nodes 

        """ 

        pass 

 

    def emit_configs(self): 

        """Emit a nodes state event 

        """ 

        pass 

 

    def emit_config(self, configs): 

        """Emit a node state event 

        nodes : a single node or a dict of nodes 

        """ 

        pass 

 

    def emit_basics(self): 

        """Emit a nodes state event 

        """ 

        pass 

 

    def emit_basic(self, basics): 

        """Emit a node state event 

        nodes : a single node or a dict of nodes 

        """ 

        pass 

 

    def emit_commands(self): 

        """Emit a nodes state event 

        """ 

        pass 

 

    def emit_command(self, systems): 

        """Emit a node state event 

        nodes : a single node or a dict of nodes 

        """ 

        pass 

 

    def emit_systems(self): 

        """Emit a nodes state event 

        """ 

        pass 

 

    def emit_system(self, systems): 

        """Emit a node state event 

        nodes : a single node or a dict of nodes 

        """ 

        pass 

 

    def emit_all(self): 

        """Emit all events 

        nodes : a single node or a dict of nodes 

        """ 

        self.emit_users() 

        self.emit_configs() 

        self.emit_systems() 

        self.emit_commands() 

        self.emit_configs() 

        self.emit_basics() 

 

    def request_node_nodes(self, hadd): 

        """ 

        """ 

        if self.heartbeat_discover_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_nodes', 'reply_hadd':self.hadds[0]} 

        self.heartbeat_discover_mqttc.publish(TOPIC_NODES_REQUEST%hadd, json_dumps(msg)) 

 

    def request_node_users(self, hadd): 

        """ 

        """ 

        if self.heartbeat_discover_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_users', 'reply_hadd':self.hadds[0]} 

        self.heartbeat_discover_mqttc.publish(TOPIC_NODES_REQUEST%hadd, json_dumps(msg)) 

 

    def request_node_systems(self, hadd): 

        """ 

        """ 

        if self.heartbeat_discover_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_systems', 'reply_hadd':self.hadds[0]} 

        self.heartbeat_discover_mqttc.publish(TOPIC_NODES_REQUEST%hadd, json_dumps(msg)) 

 

    def request_node_basics(self, hadd): 

        """ 

        """ 

        if self.heartbeat_discover_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_basics', 'reply_hadd':self.hadds[0]} 

        self.heartbeat_discover_mqttc.publish(TOPIC_NODES_REQUEST%hadd, json_dumps(msg)) 

 

    def request_node_configs(self, hadd): 

        """ 

        """ 

        if self.heartbeat_discover_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_configs', 'reply_hadd':self.hadds[0]} 

        self.heartbeat_discover_mqttc.publish(TOPIC_NODES_REQUEST%hadd, json_dumps(msg)) 

 

    def request_node_commands(self, hadd): 

        """ 

        """ 

        if self.heartbeat_discover_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_commands', 'reply_hadd':self.hadds[0]} 

        self.heartbeat_discover_mqttc.publish(TOPIC_NODES_REQUEST%hadd, json_dumps(msg)) 

 

    def request_resolv_nodes(self): 

        """ 

        """ 

        if self.resolv_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_nodes', 'reply_hadd':self.hadds[0]} 

        self.resolv_mqttc.publish(TOPIC_RESOLV_REQUEST, json_dumps(msg)) 

 

    def request_resolv_users(self): 

        """ 

        """ 

        if self.resolv_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_users', 'reply_hadd':self.hadds[0]} 

        self.resolv_mqttc.publish(TOPIC_RESOLV_REQUEST, json_dumps(msg)) 

 

    def request_resolv_systems(self): 

        """ 

        """ 

        if self.resolv_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_systems', 'reply_hadd':self.hadds[0]} 

        self.resolv_mqttc.publish(TOPIC_RESOLV_REQUEST, json_dumps(msg)) 

 

    def request_resolv_basics(self): 

        """ 

        """ 

        if self.resolv_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_basics', 'reply_hadd':self.hadds[0]} 

        self.resolv_mqttc.publish(TOPIC_RESOLV_REQUEST, json_dumps(msg)) 

 

    def request_resolv_configs(self): 

        """ 

        """ 

        if self.resolv_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_configs', 'reply_hadd':self.hadds[0]} 

        self.resolv_mqttc.publish(TOPIC_RESOLV_REQUEST, json_dumps(msg)) 

 

    def request_resolv_commands(self): 

        """ 

        """ 

        if self.resolv_mqttc is None: 

            return 

        msg = { 'cmd_class': COMMAND_DISCOVERY, 'genre':0x04, 'uuid':'request_info_commands', 'reply_hadd':self.hadds[0]} 

        self.resolv_mqttc.publish(TOPIC_RESOLV_REQUEST, json_dumps(msg)) 

 

    def boot(self, hadds, loop_sleep=0.1): 

        """Boot the node manager 

        """ 

        self.hadds = hadds 

        self.start(loop_sleep=loop_sleep) 

 

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

        """on_heartbeat 

 

        :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("[%s] - on_heartbeat %s", self.__class__.__name__, message.payload) 

        hb = HeartbeatMessage(message) 

        add_ctrl, add_node, state = hb.get_heartbeat() 

        #~ print "!"*30, "On heartbeat", add_ctrl, add_node 

        if add_ctrl is None or add_node is None: 

            return 

        hadd = HADD % (add_ctrl, add_node) 

        #~ print self.nodes 

        if hadd not in self.nodes: 

            return 

        if hadd in self.nodes: 

        #~ if hadd in self.nodes and state != self.heartbeat_cache.get_state(add_ctrl, add_node): 

            node = {} 

            node.update(self.nodes[hadd]) 

            node['state'] = state if state is not None else 'PENDING' 

            self.emit_node(node) 

        #~ print " node : %s" % self.nodes[hadd] 

        self.heartbeat_cache.update(add_ctrl, add_node, state=state, heartbeat=self.nodes[hadd]['heartbeat']) 

 

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

        """on_heartbeat_discover 

 

        :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("[%s] - on_heartbeat_discover %s", self.__class__.__name__, message.payload) 

        hb = HeartbeatMessage(message) 

        add_ctrl, add_node, state = hb.get_heartbeat() 

        self.incoming_hearbeat(add_ctrl, add_node, state) 

 

    def incoming_hearbeat(self, add_ctrl, add_node, state): 

        """ 

        """ 

        #print add_ctrl, add_node, state 

        if add_ctrl == None: 

            return 

        if add_node == -1: 

            hadd = HADD % (add_ctrl, 0) 

        else: 

            hadd = HADD % (add_ctrl, add_node) 

        #Check if we already know this entry 

        if self.heartbeat_cache.has_entry(add_ctrl, add_node) == False: 

            #NO. So we ask from some info 

            logger.debug("heartbeat from an unknown device %s,%s,%s", add_ctrl, add_node, state) 

            th = threading.Timer(self.request_timeout/4, self.request_node_nodes, [hadd]) 

            th.start() 

            self.threads_timers.append(th) 

            th = threading.Timer(2*self.request_timeout/4, self.request_node_systems, [hadd]) 

            th.start() 

            self.threads_timers.append(th) 

            th = threading.Timer(3*self.request_timeout/4, self.request_node_configs, [hadd]) 

            th.start() 

            self.threads_timers.append(th) 

            th = threading.Timer(self.request_timeout, self.request_node_basics, [hadd]) 

            th.start() 

            self.threads_timers.append(th) 

            th = threading.Timer(self.request_timeout+self.request_timeout/4, self.request_node_users, [hadd]) 

            th.start() 

            self.threads_timers.append(th) 

            th = threading.Timer(self.request_timeout+2*self.request_timeout/4, self.request_node_commands, [hadd]) 

            th.start() 

            self.threads_timers.append(th) 

        else : 

            #~ print " node : %s" % self.nodes[hadd] 

            if hadd in self.nodes and state != self.heartbeat_cache.get_state(add_ctrl, add_node): 

                node = {} 

                node.update(self.nodes[hadd]) 

                node['state'] = state if state != None else 'PENDING' 

                #~ print "   node : %s" % node 

                self.emit_node(node) 

            if hadd in self.nodes: 

                self.heartbeat_cache.update(add_ctrl, add_node, state=state, heartbeat=self.nodes[hadd]['heartbeat']) 

 

    def add_nodes(self, data): 

        """ 

        """ 

        initial_startup = False 

        do_emit = False 

        self._lock.acquire() 

        #~ print 'nodes data', data 

        try: 

            if self.broadcast_nodes_timer != None: 

                #This is the initial startup. 

                initial_startup = True 

                self.broadcast_nodes_timer.cancel() 

                self.broadcast_nodes_timer = None 

                self.broadcast_nodes_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_nodes_discover) 

                self.broadcast_nodes_timer.start() 

            ndata = normalize_request_info_nodes(data) 

            #~ print "nodes ddddaaaaaaaaaaaaaaaaaaaata : %s" % ndata 

            for knode in ndata.keys(): 

                self.nodes[ndata[knode]['hadd']] = {} 

                self.nodes[ndata[knode]['hadd']].update(JNTNode().to_dict()) 

                self.nodes[ndata[knode]['hadd']].update(ndata[knode]) 

                self.nodes[ndata[knode]['hadd']].update(ndata[knode]) 

                add_ctrl, add_node = hadd_split(ndata[knode]['hadd']) 

                if self.heartbeat_cache.has_entry(add_ctrl, add_node) == False: 

                    do_emit = True 

                #~ print self.nodes[ndata[knode]['hadd']] 

                #~ print self.nodes[ndata[knode]['hadd']]['heartbeat'] 

                self.heartbeat_cache.update(add_ctrl, add_node, heartbeat=self.nodes[ndata[knode]['hadd']]['heartbeat']) 

                ndata[knode]['state'] = 'PENDING' 

            #~ if do_emit == True and initial_startup == False: 

            self.emit_node(ndata) 

        except: 

            logger.exception("Exception in add_nodes") 

        finally: 

            self._lock.release() 

        self.emit_network() 

 

    def add_users(self, data): 

        """ 

        """ 

        self._lock.acquire() 

        #~ print 'users data', data 

        try: 

            if self.broadcast_users_timer != None: 

                self.broadcast_users_timer.cancel() 

                self.broadcast_users_timer = None 

                self.broadcast_users_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_users_discover) 

                self.broadcast_users_timer.start() 

            ndata = normalize_request_info_values(data) 

            #~ print "ddddaaaaaaaaaaaaaaaaaaaata : %s" % ndata 

            for nval in ndata: 

                for kval in ndata[nval]: 

                    hadd = ndata[nval][kval]['hadd'] 

                    uuid = ndata[nval][kval]['uuid'] 

                    #~ print "haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadddddddddd", hadd, uuid 

                    index = 0 

                    if 'index' in ndata[nval][kval]: 

                        index = ndata[nval][kval]['index'] 

                    if hadd not in self.users: 

                        self.users[hadd] = {} 

                    if uuid not in self.users[hadd]: 

                        self.users[hadd][uuid] = {} 

                    if index not in self.users[hadd][uuid]: 

                        self.users[hadd][uuid][index] = ndata[nval][kval] 

                    else: 

                        self.users[hadd][uuid][index].update(ndata[nval][kval]) 

                    ndata[nval][kval].update(self.users[hadd][uuid][index]) 

                    #~ print 'add_users', self.users[hadd][uuid][index] 

            #~ print "seeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelf.users" 

            #~ print self.users 

        except: 

            logger.exception("Exception in add_users") 

        finally: 

            self._lock.release() 

 

    def add_configs(self, data): 

        """ 

        """ 

        self._lock.acquire() 

        #~ print 'configs data', data 

        try: 

            if self.broadcast_configs_timer != None: 

                self.broadcast_configs_timer.cancel() 

                self.broadcast_configs_timer = None 

                self.broadcast_configs_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_configs_discover) 

                self.broadcast_configs_timer.start() 

            ndata = normalize_request_info_values(data) 

            for nval in ndata: 

                for kval in ndata[nval]: 

                    #~ print "haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadddddddddd", ndata[nval][kval] 

                    hadd = ndata[nval][kval]['hadd'] 

                    uuid = ndata[nval][kval]['uuid'] 

                    #~ print "haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadddddddddd", hadd, uuid 

                    index = 0 

                    if 'index' in ndata[nval][kval]: 

                        index = ndata[nval][kval]['index'] 

                    if hadd not in self.configs: 

                        self.configs[hadd] = {} 

                    if uuid not in self.configs[hadd]: 

                        self.configs[hadd][uuid] = {} 

                    if index not in self.configs[hadd][uuid]: 

                        self.configs[hadd][uuid][index] = ndata[nval][kval] 

                    else: 

                        self.configs[hadd][uuid][index].update(ndata[nval][kval]) 

                    ndata[nval][kval].update(self.configs[hadd][uuid][index]) 

            #~ for nval in ndata: 

                #~ for kval in ndata[nval]: 

                    #~ if ndata[nval][kval]['hadd'] not in self.configs: 

                        #~ self.configs[ndata[nval][kval]['hadd']] = {} 

                    #~ self.configs[ndata[nval][kval]['hadd']][ndata[nval][kval]['uuid']] = ndata[nval][kval] 

            #~ print "add_configs self.configs ", self.configs 

        except: 

            logger.exception("Exception in add_configs") 

        finally: 

            self._lock.release() 

 

    def add_basics(self, data): 

        """ 

        """ 

        self._lock.acquire() 

        #~ print 'basics data', data 

        try: 

            if self.broadcast_basics_timer != None: 

                self.broadcast_basics_timer.cancel() 

                self.broadcast_basics_timer = None 

                self.broadcast_basics_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_basics_discover) 

                self.broadcast_basics_timer.start() 

            ndata = normalize_request_info_values(data) 

            #~ print "ddddaaaaaaaaaaaaaaaaaaaata : %s" % ndata 

            for nval in ndata: 

                for kval in ndata[nval]: 

                    hadd = ndata[nval][kval]['hadd'] 

                    uuid = ndata[nval][kval]['uuid'] 

                    #~ print "haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadddddddddd", hadd, uuid 

                    index = 0 

                    if 'index' in ndata[nval][kval]: 

                        index = ndata[nval][kval]['index'] 

                    if hadd not in self.basics: 

                        self.basics[hadd] = {} 

                    if uuid not in self.basics[hadd]: 

                        self.basics[hadd][uuid] = {} 

                    if index not in self.basics[hadd][uuid]: 

                        self.basics[hadd][uuid][index] = ndata[nval][kval] 

                    else: 

                        self.basics[hadd][uuid][index].update(ndata[nval][kval]) 

                    ndata[nval][kval].update(self.basics[hadd][uuid][index]) 

                    #~ print 'add_basics', self.basics[hadd][uuid][index] 

            #~ print "seeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelf.basics" 

            #~ print self.basics 

        except: 

            logger.exception("Exception in add_basics") 

        finally: 

            self._lock.release() 

 

    def add_systems(self, data): 

        """ 

        """ 

        self._lock.acquire() 

        #~ print 'systems data', data 

        try: 

            if self.broadcast_systems_timer != None: 

                self.broadcast_systems_timer.cancel() 

                self.broadcast_systems_timer = None 

                self.broadcast_systems_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_systems_discover) 

                self.broadcast_systems_timer.start() 

            ndata = normalize_request_info_nodes(data) 

            #print "ddddaaaaaaaaaaaaaaaaaaaata : %s" % ndata 

            for nval in ndata: 

                for kval in ndata[nval]: 

                    if ndata[nval][kval]['hadd'] not in self.systems: 

                        self.systems[ndata[nval][kval]['hadd']] = {} 

                    self.systems[ndata[nval][kval]['hadd']][ndata[nval][kval]['uuid']] = ndata[nval][kval] 

                    if 'node_uuid' not in ndata[nval][kval]: 

                        ndata[nval][kval]['node_uuid'] = self.nodes[ndata[nval][kval]['hadd']] 

        except: 

            logger.exception("Exception in add_systems") 

        finally: 

            self._lock.release() 

 

    def add_commands(self, data): 

        """ 

        """ 

        self._lock.acquire() 

        #~ print 'commands data', data 

        try: 

            if self.broadcast_commands_timer != None: 

                self.broadcast_commands_timer.cancel() 

                self.broadcast_commands_timer = None 

                self.broadcast_commands_timer = threading.Timer(self.broadcast_timeout, self.finish_broadcast_commands_discover) 

                self.broadcast_commands_timer.start() 

            ndata = normalize_request_info_values(data) 

            #~ print "ddddaaaaaaaaaaaaaaaaaaaata : %s" % ndata 

            for nval in ndata: 

                for kval in ndata[nval]: 

                    hadd = ndata[nval][kval]['hadd'] 

                    uuid = ndata[nval][kval]['uuid'] 

                    #~ print "haaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaadddddddddd", hadd, uuid 

                    index = 0 

                    if 'index' in ndata[nval][kval]: 

                        index = ndata[nval][kval]['index'] 

                    if hadd not in self.commands: 

                        self.commands[hadd] = {} 

                    if uuid not in self.commands[hadd]: 

                        self.commands[hadd][uuid] = {} 

                    if index not in self.commands[hadd][uuid]: 

                        self.commands[hadd][uuid][index] = ndata[nval][kval] 

                    else: 

                        self.commands[hadd][uuid][index].update(ndata[nval][kval]) 

                    ndata[nval][kval].update(self.commands[hadd][uuid][index]) 

                    #~ print 'add_commands', self.commands[hadd][uuid][index] 

            #~ print "seeeeeeeeeeeeeeeeeeeeeeeeeeeeeeelf.commands" 

            #~ print self.commands 

        except: 

            logger.exception("Exception in add_commands") 

        finally: 

            self._lock.release() 

 

    #~ def loop(self, mqttc, stopevent): 

        #~ """ 

        #~ """ 

        #~ if mqttc is None: 

            #~ mqttc = self.nodes_mqttc 

        #~ try: 

            #~ for th in self.threads_timers: 

                #~ if not th.is_alive(): 

                    #~ self.threads_timers.remove(th) 

        #~ except: 

            #~ logger.exception("Catched exception in loop") 

        #~ to_polls = [] 

        #~ keys = self.polls.keys() 

        #~ for key in keys: 

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

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

        #~ if len(to_polls)>0: 

            #~ logger.debug(u'Found polls in timeout : %s', to_polls) 

        #~ for value in to_polls: 

            #~ self.publish_poll(mqttc, value, self._stopevent) 

            #~ self._stopevent.wait(0.05) 

        #~ try: 

            #~ sleep = float(self.loop_sleep) - 0.05*len(to_polls) 

        #~ except ValueError: 

            #~ sleep = 0 

        #~ if sleep<0: 

            #~ sleep=0.1 

        #~ self._stopevent.wait(sleep) 

 

    @property 

    def nodes_count(self): 

        """ 

 

        :rtype: int 

 

        """ 

        return len(self.nodes) 

 

    @property 

    def users_count(self): 

        """ 

 

        :rtype: int 

 

        """ 

        lvl1 = len(self.users) 

        lvl2 = 0 

        lvl3 = 0 

        for k in self.users: 

            lvl2 += len(self.users[k]) 

            for kk in self.users[k]: 

                lvl3 += len(self.users[k][kk]) 

        return lvl1, lvl2, lvl3 

 

    @property 

    def configs_count(self): 

        """ 

 

        :rtype: int 

 

        """ 

        lvl1 = len(self.configs) 

        lvl2 = 0 

        lvl3 = 0 

        for k in self.configs: 

            lvl2 += len(self.configs[k]) 

            for kk in self.configs[k]: 

                lvl3 += len(self.configs[k][kk]) 

        return lvl1, lvl2, lvl3 

 

    @property 

    def systems_count(self): 

        """ 

 

        :rtype: int 

 

        """ 

        return len(self.systems) 

 

    @property 

    def commands_count(self): 

        """ 

 

        :rtype: int 

 

        """ 

        lvl1 = len(self.commands) 

        lvl2 = 0 

        lvl3 = 0 

        for k in self.commands: 

            lvl2 += len(self.commands[k]) 

            for kk in self.commands[k]: 

                lvl3 += len(self.commands[k][kk]) 

        return lvl1, lvl2, lvl3 

 

    @property 

    def basics_count(self): 

        """ 

 

        :rtype: int 

 

        """ 

        lvl1 = len(self.basics) 

        lvl2 = 0 

        lvl3 = 0 

        for k in self.basics: 

            lvl2 += len(self.basics[k]) 

            for kk in self.basics[k]: 

                lvl3 += len(self.basics[k][kk]) 

        return lvl1, lvl2, lvl3 

 

    @property 

    def state_str(self): 

        """ 

 

        :rtype: str 

 

        """ 

        return self.states_str[self.state] 

 

    @property 

    def kvals(self): 

        """ 

        The keyvals store in db for this object. 

 

        :rtype: {} 

 

        """ 

        if self.dbcon is None: 

            return None 

        res = {} 

        cur = self.network.dbcon.cursor() 

        cur.execute("SELECT key,value FROM %s WHERE object_id=%s"%(self.__class__.__name__, self.object_id)) 

        while True: 

            row = cur.fetchone() 

            if row == None: 

                break 

            res[row[0]] = row[1] 

        return res 

 

    @kvals.setter 

    def kvals(self, kvs): 

        """ 

        The keyvals store in db for this object. 

 

        :param kvs: The key/valuse to store in db. Setting a value to None will remove it. 

        :type kvs: {} 

        :rtype: boolean 

 

        """ 

        if self.dbcon is None: 

            return False 

        if len(kvs) == 0: 

            return True 

        cur = self.network.dbcon.cursor() 

        for key in kvs.keys(): 

            logger.debug("DELETE FROM %s WHERE object_id=%s and key='%s'", self.__class__.__name__, self.object_id, key) 

            cur.execute("DELETE FROM %s WHERE object_id=%s and key='%s'"%(self.__class__.__name__, self.object_id, key)) 

            if kvs[key] is not None: 

                logger.debug("INSERT INTO %s(object_id, 'key', 'value') VALUES (%s,'%s','%s');", self.__class__.__name__, self.object_id, key, kvs[key]) 

                cur.execute("INSERT INTO %s(object_id, 'key', 'value') VALUES (%s,'%s','%s');"%(self.__class__.__name__, self.object_id, key, kvs[key])) 

        self.network.dbcon.commit() 

        return True 

 

    def from_dict(self, adict): 

        """Update internal dict from adict 

        """ 

        for field in ['is_primary','do_heartbeat_dispatch','is_secondary','is_failed','is_stopped','is_started']: 

            if field in adict: 

                try: 

                    if type(adict[field]) == type(''): 

                        adict[field] = string_to_bool(adict[field]) 

                except ValueError: 

                    logger.exception("Error in from_dict") 

        for field in ['broadcast_timeout','resolv_timeout','request_timeout']: 

            if field in adict: 

                try: 

                    adict[field] = float(adict[field]) 

                except ValueError: 

                    logger.exception("Error in fron_dict") 

        if 'resolv_timeout' in adict and 'request_timeout' in adict: 

            assert (adict['resolv_timeout'] > adict['request_timeout']),"request_timeout must be smaller than resolv_timeout" 

        self.__dict__.update(adict) 

 

    def get_scenes(self): 

        """Retrieves scenes on the network 

        """ 

        pass 

 

    def emit_scenes(self): 

        """Emit a scene state event 

        """ 

        pass 

 

    def emit_scene(self, scene): 

        """Emit a scene state event 

        nodes : a single scene or a dict of scene 

        """ 

        pass 

 

    def get_scenarios(self): 

        """Retrieves scenarios on the network 

        """ 

        pass 

 

    def emit_scenarios(self): 

        """Emit a scenario state event 

        """ 

        pass 

 

    def emit_scenario(self, scenario): 

        """Emit a scenario state event 

        nodes : a single scenario or a dict of scenario 

        """ 

        pass 

 

    def get_crons(self): 

        """Retrieves crons on the network 

        """ 

        pass 

 

    def emit_crons(self): 

        """Emit a cron state event 

        """ 

        pass 

 

    def emit_cron(self, cron): 

        """Emit a cron state event 

        nodes : a single cron or a dict of cron 

        """ 

        pass 

 

def check_heartbeats(entries, heartbeat_timeout=60, heartbeat_count=3, heartbeat_dead=604800): 

    """Check the states of the machine. Must be called in a timer 

    Called in a separate thread. Must use a scoped_session. 

 

    :param session: the session to use to communicate with db. May be a scoped_session if used in a separate tread. If None, use the common session. 

    :type session: sqlalchemy session 

    """ 

    now = datetime.datetime.now() 

    lleases = list() 

    for ctrl in entries.keys(): 

        for node in entries[ctrl].keys(): 

            if (now - entries[ctrl][node]['last_seen']).total_seconds() > heartbeat_timeout \ 

              and entries[ctrl][node]['state'] in ['online', 'boot', 'pending', 'failed']: 

                lleases.append((ctrl, node)) 

    for ctrl, node in lleases: 

        if entries[ctrl][node]['state'] == 'failed' \ 

          and (now - entries[ctrl][node]['last_seen']).total_seconds() > heartbeat_dead: 

            entries[ctrl][node]['state'] = 'dead' 

            entries[ctrl][node]['count'] = 0 

        else : 

            if "count" not in entries[ctrl][node]: 

                entries[ctrl][node]['count'] = 1 

            else: 

                entries[ctrl][node]['count'] += 1 

            if entries[ctrl][node]['count'] >= heartbeat_count: 

                #The count is reached 

                #We need to change the state 

                if entries[ctrl][node]['state'] == 'online': 

                    entries[ctrl][node]['state'] = 'pending' 

                    entries[ctrl][node]['count'] = 0 

                elif entries[ctrl][node]['state'] == 'boot': 

                    entries[ctrl][node]['state'] = 'pending' 

                    entries[ctrl][node]['count'] = 0 

                elif entries[ctrl][node]['state'] == 'pending': 

                    entries[ctrl][node]['state'] = 'failed' 

                    entries[ctrl][node]['count'] = 0