WSMessageRequest TembooMessaging::poll() { startMessaging(); long int now = millis(); WSMessageRequest rc = WS_NO_MESSAGE; while (millis() - now < POLL_TIMEOUT) { if (millis() - m_lastPingTime >= MCU_PING_PERIOD_MS) { m_lastPingTime = millis(); sendPing(); } if (Mailbox.messageAvailable()) { uint8_t msg[MAX_MAILBOX_MESSAGE_SIZE] = {0}; int recvLen = Mailbox.readMessage(msg, MAX_MAILBOX_MESSAGE_SIZE); if (recvLen > 0) { rc = handleResponse(msg, m_pinTable, m_pinTableDepth, m_connectionStatus); if (rc == WS_UPDATE_CONNECTED) { logTembooDebug("Connected to Temboo"); m_connectionStatus = true; } else if (rc == WS_UPDATE_DISCONNECTED) { logTembooDebug("Disconnected from Temboo"); m_connectionStatus = false; } else if (rc == WS_REQUEST_ERROR) { // disconnect sendError("Message request error"); } } } } return rc; }
void TembooMessaging::initiateConnection() { unsigned long now = millis(); if (now - m_connectionAttemptTime < INITIATE_TIMEOUT_MS) { poll(); return; } if (m_accountName == NULL || *m_accountName == '\0') { logTembooDebug("Account name invalid or not set"); return; } if (m_appKeyName == NULL || *m_appKeyName == '\0') { logTembooDebug("Appkey name invalid or not set"); return; } if (m_deviceID == NULL || *m_deviceID == '\0') { logTembooDebug("DeviceID invalid or not set"); return; } if (m_appKey == NULL || *m_appKey == '\0') { logTembooDebug("Appkey invalid or not set"); return; } startMessaging(); int messageSize = strlen(m_accountName) + strlen(m_appKey) + strlen(m_appKeyName) + strlen(m_deviceID) + 11; uint8_t msg[messageSize]; if (messageSize < MAX_MAILBOX_MESSAGE_SIZE) { Console.println("Attempting to connect to Temboo"); messageSize = snprintf((char*)msg, messageSize, "MI|N%s|K%s|B%s|A%s", m_accountName, m_appKeyName, m_deviceID, m_appKey); Mailbox.writeMessage(msg, messageSize); m_connectionAttemptTime = now; } else { logTembooDebug("Initiation request too large"); return; } }
int TembooMessaging::addTembooSensor(TembooSensor* sensor) { int i = 0; for (; i < m_sensorTableSize; i++) { if (m_sensorTable[i] == sensor) { logTembooDebug("Sensor already added"); return -1; } if (m_sensorTable[i] == NULL) { m_sensorTable[i] = sensor; m_sensorTableDepth++; return 0; } } logTembooDebug("Sensor table full, sensor not added"); return -1; }
int TembooMessaging::retrievePinValue(int pinNum) { // search through pin structure and return the pin value int i = 0; for (; i < m_sensorTableDepth; i++) { if (m_sensorTable[i]->getSensorPin(m_sensorTable[i]->sensorConfig) == pinNum) { return m_sensorTable[i]->read(m_sensorTable[i]->sensorConfig); } } logTembooDebug("Unable to obtain pin value"); return 0; }
int TembooMessaging::retrievePinValue(int pinNum) { // search through pin structure and return the pin value int i = 0; for (; i < m_pinTableDepth; i++) { if (m_pinTable[i].pin == pinNum) { return m_pinTable[i].currentPinValue; } } logTembooDebug("Pin not found, unable to obtain value"); return 0; }
void TembooMessaging::updatePinValue(int pinNum, int pinVal) { // save the data to the strcuture and then send to Temboo int i = 0; for (; i < m_sensorTableDepth; i++) { if (m_sensorTable[i]->getSensorPin(m_sensorTable[i]->sensorConfig) == pinNum) { // if pin has pinWrite as NULL, it is an input // pin and needs to be stored. If not NULL, // pin is an actuator and should not be stored if(m_sensorTable[i]->write == NULL){ sendData(pinNum, pinVal, false); } else { sendData(pinNum, pinVal, true); } return; } } logTembooDebug("Unable to update pin"); }
void TembooMessaging::updatePinValue(int pinNum, int pinVal) { // save the data to the strcuture and then send to Temboo int i = 0; for (; i < m_pinTableDepth; i++) { if (m_pinTable[i].pin == pinNum) { m_pinTable[i].currentPinValue = pinVal; // if pin has pinWrite as NULL, it is an input // pin and needs to be stored. If not NULL, // pin is an actuator and should not be stored if(m_pinTable[i].pinWrite == NULL){ sendData(pinNum, pinVal, false); } else { sendData(pinNum, pinVal, true); } return; } } logTembooDebug("Pin not found, unable to update"); }