OpcUaCanOpen
CANopen OPC-UA server
CanStatistics.cpp
Go to the documentation of this file.
1 
26 #include <CanModuleUtils.h>
27 #include "CanStatistics.h"
28 
29 #include <iostream>
30 
31 
32 namespace CanModule
33 {
35  m_totalTransmitted(0),
36  m_totalReceived(0),
37  m_transmitted(0),
38  m_received(0),
39  m_transmittedOctets(0),
40  m_receivedOctets(0)
41  {}
42 
44  {
45  m_internals.m_observationStart = std::chrono::system_clock::now();
46  m_transmitted = 0;
47  m_received = 0;
49  m_receivedOctets = 0;
50  }
51 
52  void CanStatistics::computeDerived(unsigned int baudRate)
53  {
54  system_clock::time_point tnom = system_clock::now();
55 
56  auto nDiff = tnom - m_internals.m_observationStart;
57  auto period = duration_cast<seconds>(nDiff).count();
58 
59  m_internals.m_transmittedPerSec = (period != 0 ? float(m_transmitted / period) : 0);
60  m_internals.m_receivedPerSec = (period != 0 ? float(m_received / period) : 0);
61 
62  if (baudRate > 0)
63  { // baud rate is known
64  unsigned int octets = m_transmittedOctets.load() + m_receivedOctets.load();
65  float maxOctetsInSecond = float(baudRate / 8.0);
66  m_internals.m_busLoad = float(octets / maxOctetsInSecond);
67  } else {
69  }
70  }
71 
72  void CanStatistics::onTransmit(unsigned int dataLength)
73  {
75  m_transmitted++;
76  m_transmittedOctets += 2 + 1 + dataLength + 2; /* ID, DLC, USER DATA, CRC */
77  }
78 
79  void CanStatistics::onReceive(unsigned int dataLength)
80  {
82  m_received++;
83  m_receivedOctets += 2 + 1 + dataLength + 2; /* ID, DLC, USER DATA, CRC */
84  }
85 
87  {
88  this->m_received = other.m_received.load();
89  this->m_totalReceived = other.m_totalReceived.load();
90  this->m_transmitted = other.m_transmitted.load();
91  this->m_totalTransmitted = other.m_totalTransmitted.load();
92  this->m_transmittedOctets = other.m_transmittedOctets.load();
93  this->m_receivedOctets = other.m_receivedOctets.load();
94  this->m_internals = other.m_internals;
95  }
96 }