Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EnergyClient and DeviceStatistics client #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Quamotion.WebDriver.Client/DeviceStatisticClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.VisualStudio.Threading;
using Newtonsoft.Json;
using Quamotion.WebDriver.Client.Models;
using System;
using System.IO;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace Quamotion.WebDriver.Client
{
public class DeviceStatisticClient: TracingClient
{
public DeviceStatistic DeviceStatistic { get; private set; }

private string DeviceId { get; }

public DeviceStatisticClient(string deviceId)
{
this.DeviceId = deviceId;
}

protected override string Url
{
get
{
return $"http://localhost:17894/wd/hub/quamotion/device/{this.DeviceId}/stats";
}
}

protected override void HandleEvent(string data)
{
var deviceStatistic = JsonConvert.DeserializeObject<Data<DeviceStatistic>>($"{{{data}}}");
this.DeviceStatistic = deviceStatistic.Value;
}
}
}
91 changes: 91 additions & 0 deletions Quamotion.WebDriver.Client/EnergyClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using Newtonsoft.Json;
using Quamotion.WebDriver.Client.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quamotion.WebDriver.Client
{
public class EnergyClient: TracingClient
{

public double PowerState { get; private set; }

public double Level { get; private set; }

public EnergyDeviceState Bluetooth { get; private set; }

public EnergyDeviceState Gps { get; private set; }

public EnergyDeviceState Wifi { get; private set; }

public string PowerSourceEvents { get; private set; }

public double Brightness { get; private set; }

public NetworkActivity NetworkActivity { get; private set; }

public Activity Activity { get; private set; }

private string DeviceId { get; }

public EnergyClient(string deviceId)
{
this.DeviceId = deviceId;
}

protected override string Url
{
get
{
return $"http://localhost:17894/wd/hub/quamotion/device/{this.DeviceId}/energy";
}
}

protected override void HandleEvent(string data)
{
if(string.IsNullOrEmpty(data))
{
return;
}

var powerMeasurementType = JsonConvert.DeserializeObject<Data<PowerMeasurement<object>>>($"{{{data}}}").Value.Type;

switch (powerMeasurementType)
{
case PowerMeasurementType.Activity:
this.Activity = JsonConvert.DeserializeObject<Data<PowerMeasurement<Activity>>>($"{{{data}}}").Value.Payload;
break;
case PowerMeasurementType.Bluetooth:
this.Bluetooth = JsonConvert.DeserializeObject<Data<PowerMeasurement<EnergyDeviceState>>>($"{{{data}}}").Value.Payload;
break;
case PowerMeasurementType.Brightness:
this.Brightness = JsonConvert.DeserializeObject<Data<PowerMeasurement<double>>>($"{{{data}}}").Value.Payload;
break;
case PowerMeasurementType.Gps:
this.Gps = JsonConvert.DeserializeObject<Data<PowerMeasurement<EnergyDeviceState>>>($"{{{data}}}").Value.Payload;
break;
case PowerMeasurementType.Level:
this.Level = JsonConvert.DeserializeObject<Data<PowerMeasurement<double>>>($"{{{data}}}").Value.Payload;
break;
case PowerMeasurementType.NetworkActivity:
this.NetworkActivity = JsonConvert.DeserializeObject<Data<PowerMeasurement<NetworkActivity>>>($"{{{data}}}").Value.Payload;
break;
case PowerMeasurementType.PowerSourceEvents:
this.PowerSourceEvents = JsonConvert.DeserializeObject<Data<PowerMeasurement<string>>>($"{{{data}}}").Value.Payload;
break;
case PowerMeasurementType.PowerState:
this.PowerState = JsonConvert.DeserializeObject<Data<PowerMeasurement<double>>>($"{{{data}}}").Value.Payload;
break;
case PowerMeasurementType.Wifi:
this.Wifi = JsonConvert.DeserializeObject<Data<PowerMeasurement<EnergyDeviceState>>>($"{{{data}}}").Value.Payload;
break;
default:
Console.WriteLine("Default case");
break;
}
}
}
}
45 changes: 45 additions & 0 deletions Quamotion.WebDriver.Client/Models/Activity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quamotion.WebDriver.Client.Models
{
/// <summary>
/// Represents activity data measured by the Energy Log.
/// </summary>
public struct Activity
{
/// <summary>
/// Gets the size of an activity, in binary format.
/// </summary>
public const int Size = 0x28;

/// <summary>
/// Gets or sets the duration of the sampling period.
/// </summary>
public TimeSpan Duration { get; set; }

/// <summary>
/// Gets or sets an unknown value.
/// </summary>
public double Unknown { get; set; }

/// <summary>
/// Gets or sets the amount of CPU consumed by the foreground app.
/// </summary>
public double ForegroundApp { get; set; }

/// <summary>
/// Gets or sets the amount of CPU consumed by graphics components.
/// </summary>
public double Graphics { get; set; }

/// <summary>
/// Gets or sets the amount of CPU consumed by media components.
/// </summary>
public double Media { get; set; }

}
}
26 changes: 26 additions & 0 deletions Quamotion.WebDriver.Client/Models/Data.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// <copyright file="Data.cs" company="Quamotion">
// Copyright (c) Quamotion. All rights reserved.
// </copyright>

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quamotion.WebDriver.Client.Models
{
/// <summary>
/// Class used to store the value of a data event.
/// </summary>
/// <typeparam name="T"></typeparam>
public class Data<T>
{
/// <summary>
/// Gets or sets the value of the data event.
/// </summary>
[JsonProperty("data")]
public T Value { get; set; }
}
}
33 changes: 33 additions & 0 deletions Quamotion.WebDriver.Client/Models/DeviceStatistic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <copyright file="DeviceStatistic.cs" company="Quamotion">
// Copyright (c) Quamotion. All rights reserved.
// </copyright>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quamotion.WebDriver.Client.Models
{
/// <summary>
/// Represents performance data of a device.
/// </summary>
public class DeviceStatistic
{
/// <summary>
/// Gets or sets the time at which the performance data was captured.
/// </summary>
public DateTimeOffset Date { get; set; }

/// <summary>
/// Gets or sets a value which represents the total CPU usage.
/// </summary>
public double? CpuUsage { get; set; }

/// <summary>
/// Gets or sets a value which represents the total memory usage (in bytes).
/// </summary>
public long? MemoryUsage { get; set; }
}
}
27 changes: 27 additions & 0 deletions Quamotion.WebDriver.Client/Models/EnergyDeviceState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quamotion.WebDriver.Client.Models
{
public enum EnergyDeviceState
{

/// <summary>
/// The state of the device is unknown.
/// </summary>
Unknown,

/// <summary>
/// The device is active.
/// </summary>
On,

/// <summary>
/// The device is off.
/// </summary>
Off
}
}
84 changes: 84 additions & 0 deletions Quamotion.WebDriver.Client/Models/NetworkActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quamotion.WebDriver.Client.Models
{
/// <summary>
/// Summarizes the network activity of a device.
/// </summary>
public class NetworkActivity
{
/// <summary>
/// Gets the size, in bytes, of a network activity struct on the wire.
/// </summary>
public const int Size = 0xC8;

/// <summary>
/// Gets or sets the amount of time during which the trace was captured.
/// </summary>
public TimeSpan Duration { get; set; }

/// <summary>
/// Gets or sets the amount of packets received over the WiFi connection.
/// </summary>
public double WifiPacketsIn { get; set; }

/// <summary>
/// Gets or sets the amount of packets sent over the WiFi connection.
/// </summary>
public double WifiPacketsOut { get; set; }

/// <summary>
/// Gets or sets the amount of bytes received over the WiFi connection.
/// </summary>
public double WifiBytesIn { get; set; }

/// <summary>
/// Gets or sets the amount of bytes sent over the WiFi connection.
/// </summary>
public double WifiBytesOut { get; set; }

/// <summary>
/// Gets or sets the number of receive errors over the WiFi connection.
/// </summary>
public double WifiReceiveErrors { get; set; }

/// <summary>
/// Gets or sets the number of transmit errors over the WiFi connection.
/// </summary>
public double WifiSendErrors { get; set; }

/// <summary>
/// Gets or sets the amount of packets received over the cellular connection.
/// </summary>
public double CellularPacketsIn { get; set; }

/// <summary>
/// Gets or sets the amount of packets sent over the cellular connection.
/// </summary>
public double CellularPacketsOut { get; set; }

/// <summary>
/// Gets or sets the amount of bytes received over the cellular connection.
/// </summary>
public double CellularBytesIn { get; set; }

/// <summary>
/// Gets or sets the amount of bytes sent over the cellular connection.
/// </summary>
public double CellularBytesOut { get; set; }

/// <summary>
/// Gets or sets the number of receive errors over the cellular connection.
/// </summary>
public double CellularReceiveErrors { get; set; }

/// <summary>
/// Gets or sets the number of transmit errors over the cellular connection.
/// </summary>
public double CellularSendErrors { get; set; }
}
}
38 changes: 38 additions & 0 deletions Quamotion.WebDriver.Client/Models/PowerMeasurement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Quamotion.WebDriver.Client.Models
{
/// <summary>
/// Represents a power measurement.
/// </summary>
public class PowerMeasurement<T>
{
/// <summary>
/// Gets or sets the measurement data.
/// </summary>
public T Payload { get; set; }

/// <summary>
/// Gets or sets the type of data being measured.
/// </summary>
[JsonConverter(typeof(StringEnumConverter))]
public PowerMeasurementType Type { get; set; }

/// <summary>
/// Gets or sets the date and time at which the event occurred.
/// </summary>
public DateTimeOffset AbsoluteTime { get; set; }

/// <summary>
/// Gets or sets the time at which the measurement was captured. This value is relative
/// to the start of the trace.
/// </summary>
public TimeSpan Timestamp { get; set; }
}
}
Loading