Newer
Older
QTcpSocket * SHactuator::m_socket = nullptr;
QSerialPort *SHactuator::m_serial = nullptr;
SHactuator::SHactuator(/* QString name,*/ int nid, int powerDemand, SensorType sid, QString url) :
m_powerDemand(powerDemand)
{
// Hinzufügen, d.h. überschreiben -> wieso geht das auch ohne?
// int pos = hash(m_nodeID, m_sensorID);
// S[pos] = this;
/*
* Konstruktor
*/
SHactuator::SHactuator(int nid, int powerDemand, QString url) :
SHsensor(nid, SensorType::Switch, url),
m_powerDemand(powerDemand)
{
m_ignoreF3error = true;
}
{
// qDebug() << "SHactuator::setValue(nid=" << m_nodeID << "sid=" << m_sensorID << "payl=" << payl << ")";
if (payl == "ON")
intState = 1;
else if (payl == "OFF")
// 1=on, 0=off
switch (intState) {
case 0:
m_switchState = State::OFF;
SHsensor::setValue("OFF");
if (not m_ignoreF3error and m_switchState != m_assignedState)
setError(switchError);
else
resetError(switchError);
break;
case 1:
m_switchState = State::ON;
SHsensor::setValue("ON");
if (not m_ignoreF3error and m_switchState != m_assignedState)
setError(switchError);
else
resetError(switchError);
break;
default:
m_switchState = State::UNKOWN;
SHsensor::setValue(NOVALUE);
break;
}
}
/*
QString cmd; // tasmota
State switchState; // for testing
QString value; // display
if (on) {
m_assignedState = State::ON;
cmd = "cm?cmnd=Power%20On";
switchState = State::ON;
value = "1";
} else {
m_assignedState = State::OFF;
cmd = "cm?cmnd=Power%20Off";
switchState = State::OFF;
value = "0";
}
if (tasmota() and m_switchState != switchState) { // nur bei Änderungen. TODO ggf. auch bei Arduino
// qDebug() << "SHactuator::swtch" << on;
QString urlRequest = m_url + "/" + cmd;
QNetworkRequest request; // -> what type of demand? get, post, ...
request.setUrl(QUrl(urlRequest));
m_tasmota->get(request);
poll(); // get ACK
} else {
m_socket->write(QByteArray(msg(on).toUtf8()));
#ifndef SHREMOTE
if (m_serial)
m_serial->write(QByteArray(msg(on).toUtf8()));
#endif
}
m_switchState = switchState; // for testing
SHsensor::setValue( m_nodeID, m_sensorID, value);
/*
*
*/
void SHactuator::OFF()
{
swtch(false);
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
}
/*
*
*/
void SHactuator::set(SHactuator::State state)
{
switch (state) {
case State::ON:
ON();
break;
case State::OFF:
OFF();
break;
case State::UNKOWN:
break;
}
}
/*
*
*/
SHactuator::State SHactuator::switchState() const
{
return m_switchState;
}
/*
*
*/
int SHactuator::powerDemand() const
/*
*
*/
void SHactuator::setIgnoreF3error() // beim setzen eines neuen Modes im Controller
{
m_ignoreF3error = true;
}
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
/*
*
*/
QString SHactuator::msg(int payload)
{
/* command
Type Value Comment
presentation 0 Sent by a node when they present attached sensors. This is usually done in the presentation() function which runs at startup.
set 1 This message is sent from or to a sensor when a sensor value should be updated
req 2 Requests a variable value (usually from an actuator destined for controller).
internal 3 This is a special internal message. See table below for the details
stream 4 Used for OTA firmware updates
*/
int command = C_SET;
int ack = 1;
int type = V_STATUS;
QString msg =
QString::number(m_nodeID) + ";" +
QString::number(m_sensorID) + ";" +
QString::number(command) + ";" +
QString::number(ack) + ";" +
QString::number(type) + ";" +
QString::number(payload) + "\n";
return msg;
}
/*
*
*/
void SHactuator::setSocket(QTcpSocket *newSocket)
{
m_socket = newSocket;
}
/*
*
*/
void SHactuator::setSerial(QSerialPort *newSerial)
{
m_serial = newSerial;
}