Fix in websocket newData message sending
This commit is contained in:
		
							parent
							
								
									0f7389744d
								
							
						
					
					
						commit
						94df778a5a
					
				| 
						 | 
					@ -75,14 +75,13 @@ private:
 | 
				
			||||||
    void saveToFileSystem()
 | 
					    void saveToFileSystem()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        static const uint32_t arrayHeaderOffset = ChunkT::arrayHeaderOffset();
 | 
					        static const uint32_t arrayHeaderOffset = ChunkT::arrayHeaderOffset();
 | 
				
			||||||
        Serial.printf(" --------  Array header offset ---- %u\n", arrayHeaderOffset);
 | 
					 | 
				
			||||||
        const uint32_t numMeasurements = chunk->numMeasurements();
 | 
					        const uint32_t numMeasurements = chunk->numMeasurements();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // todo: check this! free doesn't mean that the file writing actually works ok
 | 
					        // todo: check this! free doesn't mean that the file writing actually works ok
 | 
				
			||||||
        // use error codes of write instead? anyway: test it!
 | 
					        // use error codes of write instead? anyway: test it!
 | 
				
			||||||
        Serial.printf("%ld saveToFileSystem start()\n", millis());
 | 
					        Serial.printf("%ld saveToFileSystem start\n", millis());
 | 
				
			||||||
        deleteUntilBytesFree(CONFIG_SESSION_MAX_SIZE);
 | 
					        deleteUntilBytesFree(CONFIG_SESSION_MAX_SIZE);
 | 
				
			||||||
        Serial.printf("%ld after deleteUntilBytesFree()\n", millis());
 | 
					        Serial.printf("   %ld after deleteUntilBytesFree()\n", millis());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        String filename = String(CONFIG_DATA_PATH) + "/" + String(chunk->getStartTime());
 | 
					        String filename = String(CONFIG_DATA_PATH) + "/" + String(chunk->getStartTime());
 | 
				
			||||||
        if (portablefs::exists(filename.c_str()))
 | 
					        if (portablefs::exists(filename.c_str()))
 | 
				
			||||||
| 
						 | 
					@ -94,12 +93,12 @@ private:
 | 
				
			||||||
            Measurement_T *startPtr = chunk->getDataPointer() + existingMeasurements;
 | 
					            Measurement_T *startPtr = chunk->getDataPointer() + existingMeasurements;
 | 
				
			||||||
            file.write((uint8_t *)(startPtr), measurementsToWrite * sizeof(Measurement_T));
 | 
					            file.write((uint8_t *)(startPtr), measurementsToWrite * sizeof(Measurement_T));
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            Serial.printf("%ld Incr Save: before header patch\n", millis());
 | 
					            Serial.printf("   %ld Incr Save: before header patch\n", millis());
 | 
				
			||||||
            file.seek(arrayHeaderOffset);
 | 
					            file.seek(arrayHeaderOffset);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            StreamingMsgPackEncoder<portablefs::File> encoder(&file);
 | 
					            StreamingMsgPackEncoder<portablefs::File> encoder(&file);
 | 
				
			||||||
            encoder.template sendArrayHeader<Measurement_T>(numMeasurements);
 | 
					            encoder.template sendArrayHeader<Measurement_T>(numMeasurements);
 | 
				
			||||||
            Serial.printf("%ld total measurements up to now %u\n", millis(), numMeasurements);
 | 
					            Serial.printf("   %ld total measurements up to now %u\n", millis(), numMeasurements);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
| 
						 | 
					@ -108,7 +107,7 @@ private:
 | 
				
			||||||
            StreamingMsgPackEncoder<portablefs::File> encoder(&file);
 | 
					            StreamingMsgPackEncoder<portablefs::File> encoder(&file);
 | 
				
			||||||
            chunk->serialize(encoder);
 | 
					            chunk->serialize(encoder);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        Serial.printf("%ld saveToFileSystem done-------------\n", millis());
 | 
					        Serial.printf("   %ld saveToFileSystem done\n", millis());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    void deleteUntilBytesFree(size_t requiredSpace)
 | 
					    void deleteUntilBytesFree(size_t requiredSpace)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -15,7 +15,7 @@ class WebsocketServer
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
  WebsocketServer(SessionManager<SessionT> &sessionManager, int port)
 | 
					  WebsocketServer(SessionManager<SessionT> &sessionManager, int port)
 | 
				
			||||||
      : sessionManager_(sessionManager), nextFreeClient_(0), port_(port),
 | 
					      : sessionManager_(sessionManager), nextFreeClient_(0), port_(port),
 | 
				
			||||||
          numSentMeasurements_(0), running_(false)
 | 
					        running_(false)
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -43,7 +43,7 @@ private:
 | 
				
			||||||
  websockets::WebsocketsClient clients_[MAX_CONNECTIONS];
 | 
					  websockets::WebsocketsClient clients_[MAX_CONNECTIONS];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // previous session state
 | 
					  // previous session state
 | 
				
			||||||
    size_t numSentMeasurements_;
 | 
					  size_t numSentMeasurements_[MAX_CONNECTIONS];
 | 
				
			||||||
  bool running_;
 | 
					  bool running_;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -53,10 +53,16 @@ using websockets::WebsocketsClient;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enum MessageType
 | 
					enum MessageType
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  // from swim tracker device to frontend
 | 
				
			||||||
  INITIAL_INFO = 1,
 | 
					  INITIAL_INFO = 1,
 | 
				
			||||||
  SESSION_STARTED = 2,
 | 
					  SESSION_STARTED = 2,
 | 
				
			||||||
  SESSION_STOPPED = 3,
 | 
					  SESSION_STOPPED = 3,
 | 
				
			||||||
    SESSION_NEW_DATA = 4
 | 
					  SESSION_NEW_DATA = 4,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // from frontend to device
 | 
				
			||||||
 | 
					  START_SESSION = 5,
 | 
				
			||||||
 | 
					  STOP_SESSION = 6,
 | 
				
			||||||
 | 
					  TARE = 7
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#pragma pack(push, 1)
 | 
					#pragma pack(push, 1)
 | 
				
			||||||
| 
						 | 
					@ -130,12 +136,46 @@ private:
 | 
				
			||||||
template <typename SessionT>
 | 
					template <typename SessionT>
 | 
				
			||||||
void WebsocketServer<SessionT>::iteration()
 | 
					void WebsocketServer<SessionT>::iteration()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  using namespace websockets;
 | 
				
			||||||
 | 
					  auto onMessage = [this](WebsocketsClient &client, WebsocketsMessage message) {
 | 
				
			||||||
 | 
					    if (message.isPing())
 | 
				
			||||||
 | 
					      client.pong();
 | 
				
			||||||
 | 
					    else if (message.isBinary())
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      const char *data = message.c_str();
 | 
				
			||||||
 | 
					      const size_t length = message.length();
 | 
				
			||||||
 | 
					      if (length < 1)
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        client.close(CloseReason_UnsupportedData);
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      uint8_t opCode = uint8_t(data[0]);
 | 
				
			||||||
 | 
					      switch (opCode)
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					      case START_SESSION:
 | 
				
			||||||
 | 
					        this->sessionManager_.startMeasurements();
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      case STOP_SESSION:
 | 
				
			||||||
 | 
					        this->sessionManager_.stopMeasurements();
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      case TARE:
 | 
				
			||||||
 | 
					        this->sessionManager_.tare();
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					      default:
 | 
				
			||||||
 | 
					        client.close(CloseReason_UnsupportedData);
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (server_.poll())
 | 
					  if (server_.poll())
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    clients_[nextFreeClient_] = server_.accept();
 | 
					    clients_[nextFreeClient_] = server_.accept();
 | 
				
			||||||
        //clients_[nextFreeClient_].onMessage(onMessage); // TODO
 | 
					    clients_[nextFreeClient_].onMessage(onMessage);
 | 
				
			||||||
    Serial.println("new websocket connection");
 | 
					    Serial.println("new websocket connection");
 | 
				
			||||||
    sendMessageOnConnection(clients_[nextFreeClient_]);
 | 
					    sendMessageOnConnection(clients_[nextFreeClient_]);
 | 
				
			||||||
 | 
					    numSentMeasurements_[nextFreeClient_] = sessionManager_.session().numMeasurements();
 | 
				
			||||||
    nextFreeClient_ = (nextFreeClient_ + 1) % MAX_CONNECTIONS;
 | 
					    nextFreeClient_ = (nextFreeClient_ + 1) % MAX_CONNECTIONS;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -148,19 +188,20 @@ void WebsocketServer<SessionT>::iteration()
 | 
				
			||||||
template <typename SessionT>
 | 
					template <typename SessionT>
 | 
				
			||||||
void WebsocketServer<SessionT>::reportSessionUpdate()
 | 
					void WebsocketServer<SessionT>::reportSessionUpdate()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    auto &session = sessionManager_.session();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // start/stop messages
 | 
					 | 
				
			||||||
  if (!running_ && sessionManager_.isMeasuring())
 | 
					  if (!running_ && sessionManager_.isMeasuring())
 | 
				
			||||||
        sendSessionStartMessages();
 | 
					 | 
				
			||||||
    else if (running_ && !sessionManager_.isMeasuring())
 | 
					 | 
				
			||||||
        sendSessionStopMessages();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // new data
 | 
					 | 
				
			||||||
    if (session.numMeasurements() - (NUM_DATA_CHUNK_SIZE - 1) > numSentMeasurements_)
 | 
					 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
        sendNewDataMessages();
 | 
					    sendSessionStartMessages();
 | 
				
			||||||
 | 
					    for (int i = 0; i < MAX_CONNECTIONS; ++i)
 | 
				
			||||||
 | 
					      numSentMeasurements_[i] = 0;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					  else if (running_ && !sessionManager_.isMeasuring())
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    sendSessionStopMessages();
 | 
				
			||||||
 | 
					    for (int i = 0; i < MAX_CONNECTIONS; ++i)
 | 
				
			||||||
 | 
					      numSentMeasurements_[i] = 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  sendNewDataMessages();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template <typename SessionT>
 | 
					template <typename SessionT>
 | 
				
			||||||
| 
						 | 
					@ -188,15 +229,22 @@ void WebsocketServer<SessionT>::sendNewDataMessages()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  using MeasurementT = typename SessionT::MeasurementType;
 | 
					  using MeasurementT = typename SessionT::MeasurementType;
 | 
				
			||||||
  auto &session = sessionManager_.session();
 | 
					  auto &session = sessionManager_.session();
 | 
				
			||||||
    MeasurementT *dataToSend = session.getDataPointer() + numSentMeasurements_;
 | 
					 | 
				
			||||||
    size_t numMeasurementsToSend = session.numMeasurements() - numSentMeasurements_;
 | 
					 | 
				
			||||||
    SessionNewDataMessage<MeasurementT> msg(dataToSend, numMeasurementsToSend);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (auto &c : clients_)
 | 
					  for (int i = 0; i < MAX_CONNECTIONS; ++i)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    auto &c = clients_[i];
 | 
				
			||||||
    if (c.available())
 | 
					    if (c.available())
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      MeasurementT *dataToSend = session.getDataPointer() + numSentMeasurements_[i];
 | 
				
			||||||
 | 
					      int32_t numMeasurementsToSend = int32_t(session.numMeasurements()) - int32_t(numSentMeasurements_[i]);
 | 
				
			||||||
 | 
					      if (numMeasurementsToSend > 0)
 | 
				
			||||||
 | 
					      {
 | 
				
			||||||
 | 
					        SessionNewDataMessage<MeasurementT> msg(dataToSend, numMeasurementsToSend);
 | 
				
			||||||
        msg.send(c);
 | 
					        msg.send(c);
 | 
				
			||||||
 | 
					        numSentMeasurements_[i] += msg.numMeasurements();
 | 
				
			||||||
    numSentMeasurements_ += msg.numMeasurements();
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
template <typename SessionT>
 | 
					template <typename SessionT>
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue