-
Notifications
You must be signed in to change notification settings - Fork 6
/
HSHelper.cs
158 lines (139 loc) · 5.01 KB
/
HSHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using HomeSeerAPI;
using Scheduler.Classes;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Hspi
{
internal class HSHelper
{
public HSHelper(IHSApplication hS)
{
HS = hS;
LoadSettings();
}
public IDictionary<int, string> GetDevices()
{
Dictionary<int, string> devices = new Dictionary<int, string>();
var deviceEnumerator = HS.GetDeviceEnumerator() as clsDeviceEnumeration;
do
{
DeviceClass device = deviceEnumerator.GetNext();
if (device != null)
{
devices.Add(device.get_Ref(HS), GetName(device));
}
}
while (!deviceEnumerator.Finished);
return devices;
}
public string GetName(int deviceRefId)
{
DeviceClass device = HS.GetDeviceByRef(deviceRefId) as DeviceClass;
return device != null ? GetName(device) : null;
}
public string GetName(DeviceClass device)
{
List<string> parts = new List<string>();
string location1 = device.get_Location(HS);
if (location2Enabled)
{
string location2 = device.get_Location2(HS);
if (location1First)
{
AddIfNotEmpty(parts, location1);
AddIfNotEmpty(parts, location2);
}
else
{
AddIfNotEmpty(parts, location2);
AddIfNotEmpty(parts, location1);
}
}
else
{
AddIfNotEmpty(parts, location1);
}
AddIfNotEmpty(parts, device.get_Name(HS));
return string.Join(" ", parts);
}
private static void AddIfNotEmpty(List<string> parts, string name)
{
if (!string.IsNullOrWhiteSpace(name))
{
parts.Add(name);
}
}
private static string FindTypeString(IEnumerable<string> descriptionStrings)
{
foreach (var descriptionString in descriptionStrings)
{
var descriptionStringUpper = descriptionString.ToUpperInvariant();
foreach (var typeString in measurementTypes)
{
if (descriptionStringUpper.Contains(typeString))
{
#pragma warning disable CA1308 // Normalize strings to uppercase
return typeString.ToLowerInvariant();
#pragma warning restore CA1308 // Normalize strings to uppercase
}
}
}
return null;
}
public void Fill(int deviceRefId, out string measurement, out double? maxValidValue, out double? minValidValue)
{
measurement = null;
maxValidValue = null;
minValidValue = null;
DeviceClass device = HS.GetDeviceByRef(deviceRefId) as DeviceClass;
var deviceType = device.get_DeviceType_Get(HS);
measurement = FindTypeString(new string[] {
deviceType?.Device_SubType_Description,
deviceType?.Device_API_Description,
device.get_Name(HS) });
switch (measurement)
{
case "temperature":
maxValidValue = 255;
minValidValue = -255;
break;
case "battery":
case "humidity":
maxValidValue = 100;
minValidValue = 0;
break;
case "watts":
case "kwh":
case "pressure":
case "amperes":
case "co2":
case "luminance":
case "pm25":
minValidValue = 0;
break;
}
}
private void LoadSettings()
{
location2Enabled = Convert.ToBoolean(
HS.GetINISetting("Settings", "bUseLocation2", Convert.ToString(false, CultureInfo.InvariantCulture), ""),
CultureInfo.InvariantCulture);
if (location2Enabled)
{
location1First = Convert.ToBoolean(
HS.GetINISetting("Settings", "bLocationFirst", Convert.ToString(false, CultureInfo.InvariantCulture), ""),
CultureInfo.InvariantCulture);
}
else
{
location1First = true;
}
}
private readonly IHSApplication HS;
private bool location2Enabled;
private bool location1First;
private static string[] measurementTypes = { "TEMPERATURE", "HUMIDITY", "WATTS", "KWH", "BATTERY",
"PRESSURE", "AMPERES", "CO2", "PM25", "VOLTS", "LUMINANCE" };
}
}