-
Notifications
You must be signed in to change notification settings - Fork 3
/
ConfigurationReader.cs
107 lines (97 loc) · 4.1 KB
/
ConfigurationReader.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
using System;
using System.Diagnostics;
using System.IO;
namespace MediaSensor
{
class ConfigurationReader
{
internal const string ConfigurationFileName = "mediasensor.yaml";
internal string Url { get; private set; } = string.Empty;
internal string Token { get; private set; } = string.Empty;
internal int Poll { get; private set; }
internal int Latch { get; private set; }
internal bool Initialized { get; private set; }
internal bool SoundSensor { get; private set; }
internal ConfigurationReader()
{
}
internal void Initialize()
{
if (Initialized)
return;
if (File.Exists(ConfigurationFileName))
{
ReadConfiguration();
Initialized = true;
}
else
{
// Produce a sample configuration
File.WriteAllText(ConfigurationFileName,
@"url: http://host:8123/api/states/sensor.media # URL of the API endpoint. See https://developers.home-assistant.io/docs/en/external_api_rest.html
token: InsertLongTermTokenHere # Home Assistant long term token
poll: 250 # Polling delay in milliseconds. This represents delay between calls to the OS.
latch: 1000 # Latching delay in milliseconds. This represents duration of how long media state must be steady before making API call
soundsensor: true # true to use sound sensor. false to use the app as on-off switch
");
Initialized = false;
}
}
private bool ReadConfiguration()
{
bool gotUrl, gotToken, gotPoll, gotLatch, gotSoundSensor;
gotUrl = gotToken = gotPoll = gotLatch = gotSoundSensor = false;
var lines = File.ReadAllLines(ConfigurationFileName);
foreach (var line in lines)
{
var (key, rest) = GetBeforeAndAfter(line, ':');
var (value, comment) = GetBeforeAndAfter(rest, '#');
switch (key)
{
case "url":
Url = value;
gotUrl = true;
break;
case "token":
Token = value;
gotToken = true;
break;
case "soundsensor":
SoundSensor = Boolean.Parse(value);
gotSoundSensor = true;
break;
case "poll":
Poll = Int32.Parse(value);
gotPoll = true;
break;
case "latch":
Latch = Int32.Parse(value);
gotLatch = true;
break;
}
if (gotUrl && gotToken && gotPoll && gotLatch && gotSoundSensor)
return true;
}
if (!gotUrl)
throw new ApplicationException("Configuration did not contain key: url");
if (!gotToken)
throw new ApplicationException("Configuration did not contain key: token");
if (!gotPoll)
throw new ApplicationException("Configuration did not contain key with integer value: poll");
if (!gotLatch)
throw new ApplicationException("Configuration did not contain key with integer value: latch");
if (!gotSoundSensor)
throw new ApplicationException("Configuration did not contain key with Boolean value: soundsensor");
return false;
}
private (string, string) GetBeforeAndAfter(string text, char separator)
{
var separatorIndex = text.IndexOf(separator);
if (separatorIndex == -1)
return (text, string.Empty);
var before = separatorIndex > 0 ? text.Substring(0, separatorIndex) : string.Empty;
var after = separatorIndex + 1 < text.Length ? text.Substring(separatorIndex + 1) : string.Empty;
return (before.Trim(), after.Trim());
}
}
}