This repository has been archived by the owner on Oct 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
104 lines (89 loc) · 2.69 KB
/
Program.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
using System;
using System.Runtime.CompilerServices;
namespace HSPI_LiftMasterMyQ
{
public class Program
{
private const string DEFAULT_SERVER_ADDRESS = "127.0.0.1";
private const int DEFAULT_SERVER_PORT = 10400;
public static HomeSeerAPI.IHSApplication HsClient;
public static void Main(string[] args) {
string serverAddress = DEFAULT_SERVER_ADDRESS;
int serverPort = DEFAULT_SERVER_PORT;
foreach (string arg in args) {
string[] parts = arg.Split('=');
switch (parts[0].ToLower()) {
case "server":
serverAddress = parts[1];
break;
default:
Console.WriteLine("Warning: Unknown command line argument " + parts[0]);
break;
}
}
HSPI plugin = new HSPI();
Console.WriteLine("Plugin " + plugin.Name + " is connecting to HS3 at " + serverAddress + ":" + serverPort);
try {
plugin.Connect(serverAddress, serverPort);
Console.WriteLine("Connection established");
}
catch (Exception ex) {
Console.WriteLine("Unable to connect to HS3: " + ex.Message);
return;
}
try {
while (true) {
System.Threading.Thread.Sleep(250);
if (!plugin.Connected) {
Console.WriteLine("Connection to HS3 lost!");
break;
}
if (plugin.Shutdown) {
Console.WriteLine("Plugin has been shut down; exiting");
break;
}
}
}
catch (Exception ex) {
Console.WriteLine("Unhandled exception: " + ex.Message);
}
}
public static void WriteLog(LogType logType, string message, [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string caller = null) {
#if !DEBUG
if (logType <= LogType.Verbose) {
// Don't log Console, Silly, and Verbose messages in production builds
return;
}
#endif
string type = logType.ToString().ToLower();
#if DEBUG
if (logType != LogType.Console) {
// Log to HS3 log
string hs3LogType = HSPI.PLUGIN_NAME;
if (logType == LogType.Silly) {
hs3LogType += " Silly";
}
HsClient.WriteLog(hs3LogType, type + ": [" + caller + ":" + lineNumber + "] " + message);
}
Console.WriteLine("[" + type + "] " + message);
#else
string hs3LogType = HSPI.PLUGIN_NAME;
if (logType == LogType.Debug) {
hs3LogType += " Debug";
}
HsClient.WriteLog(hs3LogType, type + ": " + message);
#endif
}
}
public enum LogType
{
Console = 1, // DEBUG ONLY: Printed to the console
Silly = 2, // DEBUG ONLY: Logged to HS3 log under type "PluginName Silly"
Verbose = 3, // DEBUG ONLY: Logged to HS3 log under normal type
Debug = 4, // In debug builds, logged to HS3 log under normal type. In production builds, logged to HS3 log under type "PluginName Debug"
Info = 5,
Warn = 6,
Error = 7,
Critical = 8,
}
}