forked from dresden-elektronik/deconz-rest-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermitJoin.cpp
149 lines (127 loc) · 4.28 KB
/
permitJoin.cpp
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
/*
* Copyright (c) 2016 dresden elektronik ingenieurtechnik gmbh.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
* style license a copy of which has been included with this distribution in
* the LICENSE.txt file.
*
*/
#include "de_web_plugin.h"
#include "de_web_plugin_private.h"
/*! Inits permit join manager.
The manager will observe and ensure the global permit join state.
*/
void DeRestPluginPrivate::initPermitJoin()
{
permitJoinFlag = false;
permitJoinTimer = new QTimer(this);
permitJoinTimer->setSingleShot(false);
connect(permitJoinTimer, SIGNAL(timeout()),
this, SLOT(permitJoinTimerFired()));
permitJoinTimer->start(1000);
permitJoinLastSendTime = QTime::currentTime();
}
/*! Sets the permit join interval
\param duration specifies the interval in which joining is enabled
- 0 disabled
- 1..254 duration in seconds until joining will be disabled
- 255 always permit
* \return
*/
bool DeRestPluginPrivate::setPermitJoinDuration(uint8_t duration)
{
if (gwPermitJoinDuration != duration)
{
gwPermitJoinDuration = duration;
}
// force resend
permitJoinLastSendTime = QTime();
return true;
}
/*! Handle broadcasting of permit join interval.
This is done every PERMIT_JOIN_SEND_INTERVAL to ensure
any node in the network has the same settings.
*/
void DeRestPluginPrivate::permitJoinTimerFired()
{
Q_Q(DeRestPlugin);
if (!q->pluginActive())
{
return;
}
if ((gwPermitJoinDuration > 0) && (gwPermitJoinDuration < 255))
{
permitJoinFlag = true;
gwPermitJoinDuration--;
updateEtag(gwConfigEtag); // update Etag so that webApp can count down permitJoin duration
//periodically check if there are deleted lights and undelete them
if (gwPermitJoinDuration % 10 == 0)
{
std::vector<LightNode>::iterator i = nodes.begin();
std::vector<LightNode>::iterator end = nodes.end();
for (; i != end; ++i)
{
if (i->state() == LightNode::StateDeleted)
{
if (i->isAvailable())
{
i->setState(LightNode::StateNormal);
i->setNeedSaveDatabase(true);
queSaveDb(DB_LIGHTS, DB_SHORT_SAVE_DELAY);
}
}
}
}
}
if (gwPermitJoinDuration == 0 && permitJoinFlag)
{
permitJoinFlag = false;
}
if (!isInNetwork())
{
return;
}
if (gwPermitJoinDuration == 0 && otauLastBusyTimeDelta() < (60 * 5))
{
// don't pollute channel while OTA is running
return;
}
QTime now = QTime::currentTime();
int diff = permitJoinLastSendTime.msecsTo(now);
if (!permitJoinLastSendTime.isValid() || (diff > PERMIT_JOIN_SEND_INTERVAL))
{
deCONZ::ApsDataRequest apsReq;
quint8 tcSignificance = 0x01;
apsReq.setDstAddressMode(deCONZ::ApsNwkAddress);
apsReq.dstAddress().setNwk(deCONZ::BroadcastRouters);
apsReq.setProfileId(ZDP_PROFILE_ID);
apsReq.setClusterId(ZDP_MGMT_PERMIT_JOINING_REQ_CLID);
apsReq.setDstEndpoint(ZDO_ENDPOINT);
apsReq.setSrcEndpoint(ZDO_ENDPOINT);
apsReq.setTxOptions(0);
apsReq.setRadius(0);
QDataStream stream(&apsReq.asdu(), QIODevice::WriteOnly);
stream.setByteOrder(QDataStream::LittleEndian);
stream << (uint8_t)now.second(); // seqno
stream << gwPermitJoinDuration;
stream << tcSignificance;
DBG_Assert(apsCtrl != 0);
if (!apsCtrl)
{
return;
}
// set for own node
apsCtrl->setPermitJoin(gwPermitJoinDuration);
// broadcast
if (apsCtrl->apsdeDataRequest(apsReq) == deCONZ::Success)
{
DBG_Printf(DBG_INFO, "send permit join, duration: %d\n", gwPermitJoinDuration);
permitJoinLastSendTime = now;
}
else
{
DBG_Printf(DBG_INFO, "send permit join failed\n");
}
}
}