From dfa13818d72eade4fb1f9a645ef27d4c5574f275 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20M=C3=BCller?= Date: Wed, 19 Jun 2019 10:36:11 +0200 Subject: [PATCH] Add DeviceStatistics --- DeviceStatistics.go | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 DeviceStatistics.go diff --git a/DeviceStatistics.go b/DeviceStatistics.go new file mode 100644 index 0000000..8a5b718 --- /dev/null +++ b/DeviceStatistics.go @@ -0,0 +1,65 @@ +package gonetworkmanager + +import ( + "encoding/json" + + "github.com/godbus/dbus" +) + +const ( + DeviceStatisticsInterface = DeviceInterface + ".Statistics" + + DeviceStatisticsPropertyRefreshRateMs = DeviceStatisticsInterface + ".RefreshRateMs" // readwrite u + DeviceStatisticsPropertyTxBytes = DeviceStatisticsInterface + ".TxBytes" // readable t + DeviceStatisticsPropertyRxBytes = DeviceStatisticsInterface + ".RxBytes" // readable t +) + +type DeviceStatistics interface { + GetPath() dbus.ObjectPath + + // Refresh rate of the rest of properties of this interface. The properties are guaranteed to be refreshed each RefreshRateMs milliseconds in case the underlying counter has changed too. If zero, there is no guaranteed refresh rate of the properties. + GetPropertyRefreshRateMs() uint32 + + // Number of transmitted bytes + GetPropertyTxBytes() uint64 + + // Number of received bytes + GetPropertyRxBytes() uint64 +} + +func NewDeviceStatistics(objectPath dbus.ObjectPath) (DeviceStatistics, error) { + var d deviceStatistics + return &d, d.init(NetworkManagerInterface, objectPath) +} + +type deviceStatistics struct { + dbusBase +} + +func (d *deviceStatistics) GetPath() dbus.ObjectPath { + return d.obj.Path() +} + +func (d *deviceStatistics) GetPropertyRefreshRateMs() uint32 { + return d.getUint32Property(DeviceStatisticsPropertyRefreshRateMs) +} + +func (d *deviceStatistics) GetPropertyTxBytes() uint64 { + return d.getUint64Property(DeviceStatisticsPropertyTxBytes) +} + +func (d *deviceStatistics) GetPropertyRxBytes() uint64 { + return d.getUint64Property(DeviceStatisticsPropertyRxBytes) +} + +func (d *deviceStatistics) marshalMap() map[string]interface{} { + return map[string]interface{}{ + "RefreshRateMs": d.GetPropertyRefreshRateMs(), + "TxBytes": d.GetPropertyTxBytes(), + "RxBytes": d.GetPropertyRxBytes(), + } +} + +func (d *deviceStatistics) MarshalJSON() ([]byte, error) { + return json.Marshal(d.marshalMap()) +}