Просмотр исходного кода

Add queue size and dropped packets to GET /about

Christopher Mullins лет назад: 6
Родитель
Сommit
7c262c7724

+ 11 - 2
lib/MiLight/PacketQueue.cpp

@@ -1,5 +1,9 @@
 #include <PacketQueue.h>
 
+PacketQueue::PacketQueue()
+  : droppedPackets(0)
+{ }
+
 void PacketQueue::push(const uint8_t* packet, const MiLightRemoteConfig* remoteConfig, const size_t repeatsOverride) {
   std::shared_ptr<QueuedPacket> qp = checkoutPacket();
   memcpy(qp->packet, packet, remoteConfig->packetFormatter->getPacketLength());
@@ -7,16 +11,21 @@ void PacketQueue::push(const uint8_t* packet, const MiLightRemoteConfig* remoteC
   qp->repeatsOverride = repeatsOverride;
 }
 
-bool PacketQueue::isEmpty() {
+bool PacketQueue::isEmpty() const {
   return queue.size() == 0;
 }
 
+size_t PacketQueue::getDroppedPacketCount() const {
+  return droppedPackets;
+}
+
 std::shared_ptr<QueuedPacket> PacketQueue::pop() {
   return queue.shift();
 }
 
 std::shared_ptr<QueuedPacket> PacketQueue::checkoutPacket() {
   if (queue.size() == MILIGHT_MAX_QUEUED_PACKETS) {
+    ++droppedPackets;
     return queue.getLast();
   } else {
     std::shared_ptr<QueuedPacket> packet = std::make_shared<QueuedPacket>();
@@ -28,6 +37,6 @@ std::shared_ptr<QueuedPacket> PacketQueue::checkoutPacket() {
 void PacketQueue::checkinPacket(std::shared_ptr<QueuedPacket> packet) {
 }
 
-size_t PacketQueue::size() {
+size_t PacketQueue::size() const {
   return queue.size();
 }

+ 7 - 2
lib/MiLight/PacketQueue.h

@@ -18,12 +18,17 @@ struct QueuedPacket {
 
 class PacketQueue {
 public:
+  PacketQueue();
+
   void push(const uint8_t* packet, const MiLightRemoteConfig* remoteConfig, const size_t repeatsOverride);
   std::shared_ptr<QueuedPacket> pop();
-  bool isEmpty();
-  size_t size();
+  bool isEmpty() const;
+  size_t size() const;
+  size_t getDroppedPacketCount() const;
 
 private:
+  size_t droppedPackets;
+
   std::shared_ptr<QueuedPacket> checkoutPacket();
   void checkinPacket(std::shared_ptr<QueuedPacket> packet);
 

+ 8 - 1
lib/MiLight/PacketSender.cpp

@@ -7,7 +7,6 @@ PacketSender::PacketSender(
   PacketSentHandler packetSentHandler
 ) : radioSwitchboard(radioSwitchboard)
   , settings(settings)
-  , stateStore(stateStore)
   , currentPacket(nullptr)
   , packetRepeatsRemaining(0)
   , packetSentHandler(packetSentHandler)
@@ -77,6 +76,14 @@ void PacketSender::handleCurrentPacket() {
   }
 }
 
+size_t PacketSender::queueLength() const {
+  return queue.size();
+}
+
+size_t PacketSender::droppedPackets() const {
+  return queue.getDroppedPacketCount();
+}
+
 void PacketSender::sendRepeats(size_t num) {
   size_t len = currentPacket->remoteConfig->packetFormatter->getPacketLength();
 

+ 4 - 0
lib/MiLight/PacketSender.h

@@ -22,6 +22,10 @@ public:
   // Return true if there are queued packets
   bool isSending();
 
+  // Return the number of queued packets
+  size_t queueLength() const;
+  size_t droppedPackets() const;
+
 private:
   RadioSwitchboard& radioSwitchboard;
   Settings& settings;

+ 4 - 0
lib/WebServer/MiLightHttpServer.cpp

@@ -148,6 +148,10 @@ void MiLightHttpServer::onGroupDeleted(GroupDeletedHandler handler) {
 
 void MiLightHttpServer::handleAbout(RequestContext& request) {
   AboutHelper::generateAboutObject(request.response.json);
+
+  JsonObject queueStats = request.response.json.createNestedObject("queue_stats");
+  queueStats[F("length")] = packetSender->queueLength();
+  queueStats[F("dropped_packets")] = packetSender->droppedPackets();
 }
 
 void MiLightHttpServer::handleGetRadioConfigs(RequestContext& request) {