-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also: * Established a pattern to maintain specialized behavior for specific device types (see GenericDevice, FlumeDevice) * Device attributes can now be accessed and used like a Dictionary by casting to IDictionary * Refactored to consolidate behavior of single-device and all-device queries * Other minor refactorings
- Loading branch information
Showing
5 changed files
with
398 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using AttributeValue = OneOf.OneOf<string, string[], int?, double?, OneOf.OneOf<double, string>?>; | ||
using hubitat2prom.HubitatDevice; | ||
|
||
namespace hubitat2prom.PrometheusExporter.DeviceTypes; | ||
|
||
public abstract class DeviceType | ||
{ | ||
protected const double MISSING_VALUE_DEFAULT = 0d; | ||
|
||
protected DeviceType() { } | ||
public abstract double ExtractMetric(string attributeName, AttributeValue attributeValue); | ||
|
||
public static DeviceType CreateDeviceType(DeviceSummary deviceSummary) | ||
{ | ||
switch(deviceSummary.type) | ||
{ | ||
case "Flume Device": | ||
return new FlumeDevice(); | ||
default: | ||
return new GenericDevice(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using AttributeValue = OneOf.OneOf<string, string[], int?, double?, OneOf.OneOf<double, string>?>; | ||
|
||
namespace hubitat2prom.PrometheusExporter.DeviceTypes; | ||
|
||
|
||
public class FlumeDevice: GenericDevice | ||
{ | ||
public FlumeDevice() { } | ||
|
||
public override double ExtractMetric(string attributeName, AttributeValue attributeValue) | ||
{ | ||
// We can return if the value type is not T0 (string) | ||
// because that is the only type this method extracts | ||
if (!attributeValue.IsT0) return base.ExtractMetric(attributeName, attributeValue); | ||
var attributeStringValue = attributeValue.AsT0; | ||
double value; | ||
switch (attributeName) | ||
{ | ||
case "commstatus": | ||
if (TryGetCommStatus(attributeStringValue, out value)) return value; | ||
break; | ||
case "flowstatus": | ||
if (TryGetFlowStatus(attributeStringValue, out value)) return value; | ||
break; | ||
case "presence": | ||
if (TryGetPresence(attributeStringValue, out value)) return value; | ||
break; | ||
case "water": | ||
if (TryGetWater(attributeStringValue, out value)) return value; | ||
break; | ||
default: | ||
System.Diagnostics.Debug.WriteLine($"Unknown attribute \"{attributeName}\" from Flume device."); | ||
break; | ||
} | ||
|
||
return MISSING_VALUE_DEFAULT; | ||
} | ||
|
||
private static bool TryGetCommStatus(string commStatus, out double value) | ||
{ | ||
switch (commStatus) | ||
{ | ||
case "good": value = 0; return true; | ||
case "unknown": value = 1; return true; | ||
case "error": value = 2; return true; | ||
} | ||
|
||
value = -1; | ||
return false; | ||
} | ||
|
||
private static bool TryGetFlowStatus(string flowStatus, out double value) | ||
{ | ||
switch (flowStatus) | ||
{ | ||
case "stopped": value = 0; return true; | ||
case "running": value = 1; return true; | ||
case "monitoring": value = 2; return true; | ||
} | ||
|
||
value = -1; | ||
return false; | ||
} | ||
|
||
private static bool TryGetPresence(string presence, out double value) | ||
{ | ||
switch (presence) | ||
{ | ||
case "not present": value = 0; return true; | ||
case "present": value = 1; return true; | ||
} | ||
|
||
value = -1; | ||
return false; | ||
} | ||
|
||
private static bool TryGetWater(string water, out double value) | ||
{ | ||
switch (water) | ||
{ | ||
case "dry": value = 0; return true; | ||
case "wet": value = 1; return true; | ||
} | ||
|
||
value = -1; | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using AttributeValue = OneOf.OneOf<string, string[], int?, double?, OneOf.OneOf<double, string>?>; | ||
|
||
namespace hubitat2prom.PrometheusExporter.DeviceTypes; | ||
|
||
public class GenericDevice: DeviceType | ||
{ | ||
public GenericDevice() { } | ||
|
||
public override double ExtractMetric(string attributeName, AttributeValue attributeValue) | ||
{ | ||
double value; | ||
var metricValue = attributeValue.Match( | ||
@string => | ||
{ | ||
var name = attributeName.ToLowerInvariant(); | ||
if (name == "switch") return @string == "on" ? 1 : 0; | ||
if (name == "power" && TryGetPower(@string, out value)) return value; | ||
if (name == "thermostatoperatingstate" && TryGetThermostatOperatingState(@string, out value)) return value; | ||
if (name == "thermostatmode" && TryGetThermostatMode(@string, out value)) return value; | ||
return MISSING_VALUE_DEFAULT; | ||
}, | ||
stringArray => MISSING_VALUE_DEFAULT, | ||
nullableInt => nullableInt ?? MISSING_VALUE_DEFAULT, | ||
nullableDouble => nullableDouble ?? MISSING_VALUE_DEFAULT, | ||
nullableOneOfDoubleString => nullableOneOfDoubleString.HasValue | ||
? nullableOneOfDoubleString.Value.Match( | ||
@double => @double, | ||
@string => MISSING_VALUE_DEFAULT | ||
) | ||
: MISSING_VALUE_DEFAULT | ||
); | ||
|
||
return metricValue; | ||
} | ||
|
||
private static bool TryGetPower(string power, out double value) | ||
{ | ||
if (power == "off") | ||
{ | ||
value = 0; | ||
return true; | ||
} | ||
|
||
if (power == "on") | ||
{ | ||
value = 1; | ||
return true; | ||
} | ||
|
||
value = -1; | ||
return false; | ||
} | ||
|
||
private static bool TryGetThermostatOperatingState(string thermostatOperatingState, out double value) | ||
{ | ||
switch (thermostatOperatingState) | ||
{ | ||
case "heating": value = 0; return true; | ||
case "pending cool": value = 1; return true; | ||
case "pending heat": value = 2; return true; | ||
case "vent economizer": value = 3; return true; | ||
case "idle": value = 4; return true; | ||
case "cooling": value = 5; return true; | ||
case "fan only": value = 6; return true; | ||
} | ||
|
||
value = -1; | ||
return false; | ||
} | ||
|
||
private static bool TryGetThermostatMode(string thermostatMode, out double value) | ||
{ | ||
switch (thermostatMode) | ||
{ | ||
case "auto": value = 0; return true; | ||
case "off": value = 1; return true; | ||
case "heat": value = 2; return true; | ||
case "emergency heat": value = 3; return true; | ||
case "cool": value = 4; return true; | ||
} | ||
|
||
value = -1; | ||
return false; | ||
} | ||
} |
Oops, something went wrong.