-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
173 lines (148 loc) · 7.36 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
namespace UAEdgeHEMS
{
using Models;
using Newtonsoft.Json;
using Opc.Ua;
using Opc.Ua.Configuration;
using Serilog;
using System;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
class Program
{
static ApplicationInstance App { get; set; }
static async Task Main(string[] args)
{
// setup logging
string pathToLogFile = Directory.GetCurrentDirectory();
if (Environment.GetEnvironmentVariable("LOG_FILE_PATH") != null)
{
pathToLogFile = Environment.GetEnvironmentVariable("LOG_FILE_PATH");
}
InitLogging(pathToLogFile);
// create OPC UA client app
string appName = "UAEdgeHEMS";
if (Environment.GetEnvironmentVariable("APP_NAME") != null)
{
appName = Environment.GetEnvironmentVariable("APP_NAME");
}
ApplicationInstance.MessageDlg = new ApplicationMessageDlg();
App = new ApplicationInstance
{
ApplicationName = appName,
ApplicationType = ApplicationType.Server,
ConfigSectionName = "Ua.Edge.HEMS"
};
await App.LoadApplicationConfiguration(false).ConfigureAwait(false);
await App.CheckApplicationInstanceCertificate(false, 0).ConfigureAwait(false);
Utils.Tracing.TraceEventHandler += new EventHandler<TraceEventArgs>(OpcStackLoggingHandler);
// create OPC UA cert validator
App.ApplicationConfiguration.CertificateValidator = new CertificateValidator();
App.ApplicationConfiguration.CertificateValidator.CertificateValidation += new CertificateValidationEventHandler(OPCUAClientCertificateValidationCallback);
App.ApplicationConfiguration.CertificateValidator.Update(App.ApplicationConfiguration.SecurityConfiguration).GetAwaiter().GetResult();
// start the server
await App.Start(new UAServer()).ConfigureAwait(false);
Log.Logger.Information("UA Edge HEMS is running.");
await Task.Delay(Timeout.Infinite).ConfigureAwait(false);
}
private static void OPCUAClientCertificateValidationCallback(CertificateValidator validator, CertificateValidationEventArgs e)
{
// always trust the OPC UA client certificate
if (e.Error.StatusCode == StatusCodes.BadCertificateUntrusted)
{
e.Accept = true;
}
}
private static void OpcStackLoggingHandler(object sender, TraceEventArgs e)
{
if ((e.TraceMask & App.ApplicationConfiguration.TraceConfiguration.TraceMasks) != 0)
{
if (e.Arguments != null)
{
Log.Logger.Information("OPC UA Stack: " + string.Format(CultureInfo.InvariantCulture, e.Format, e.Arguments).Trim());
}
else
{
Log.Logger.Information("OPC UA Stack: " + e.Format.Trim());
}
}
}
private static void InitLogging(string pathToLogFile)
{
LoggerConfiguration loggerConfiguration = new LoggerConfiguration();
#if DEBUG
loggerConfiguration.MinimumLevel.Debug();
#else
loggerConfiguration.MinimumLevel.Information();
#endif
if (!Directory.Exists(pathToLogFile))
{
Directory.CreateDirectory(pathToLogFile);
}
// set logging sinks
loggerConfiguration.WriteTo.Console();
loggerConfiguration.WriteTo.File(Path.Combine(pathToLogFile, "uaedgehems.logfile.txt"), fileSizeLimitBytes: 1024 * 1024, rollOnFileSizeLimit: true, retainedFileCountLimit: 10);
Log.Logger = loggerConfiguration.CreateLogger();
Log.Logger.Information($"Log file is: {Path.Combine(pathToLogFile, "uaedgehems.logfile.txt")}");
}
private static void ActivateTPKasaSmartDevice(string deviceName, string username, string password, bool activate)
{
try
{
// login
HttpClient webClient = new();
string queryBodyJson = "{\"method\":\"login\",\"params\":{\"appType\":\"Kasa_Android\",\"cloudPassword\":\"" + password + "\",\"cloudUserName\":\"" + username + "\",\"terminalUUID\":\"" + Guid.NewGuid().ToString() + "\"}}";
HttpResponseMessage response = webClient.Send(new HttpRequestMessage(HttpMethod.Post, "https://wap.tplinkcloud.com")
{
Content = new StringContent(queryBodyJson, Encoding.UTF8, "application/json"),
});
string responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
LoginResponse tpLinkLoginResponse = JsonConvert.DeserializeObject<LoginResponse>(responseString);
// get device list
queryBodyJson = "{\"method\":\"getDeviceList\"}";
response = webClient.Send(new HttpRequestMessage(HttpMethod.Post, "https://wap.tplinkcloud.com/?token=" + tpLinkLoginResponse.result.token)
{
Content = new StringContent(queryBodyJson, Encoding.UTF8, "application/json"),
});
responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
GetDeviceListResponse getDeviceListResponse = JsonConvert.DeserializeObject<GetDeviceListResponse>(responseString);
// find our device
string deviceID = string.Empty;
foreach (DeviceListItem item in getDeviceListResponse.result.deviceList)
{
if (item.alias == deviceName)
{
deviceID = item.deviceId;
break;
}
}
// activate/deactivate it if we found it
if (!string.IsNullOrEmpty(deviceID))
{
int active = 0;
if (activate)
{
active = 1;
}
queryBodyJson = "{\"method\":\"passthrough\",\"params\":{\"deviceId\":\"" + deviceID + "\",\"requestData\":'{\"system\":{\"set_relay_state\":{ \"state\":" + active.ToString() + "}}}'}}";
response = webClient.Send(new HttpRequestMessage(HttpMethod.Post, "https://wap.tplinkcloud.com/?token=" + tpLinkLoginResponse.result.token)
{
Content = new StringContent(queryBodyJson, Encoding.UTF8, "application/json"),
});
responseString = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
PassThroughResponse passthroughResponse = JsonConvert.DeserializeObject<PassThroughResponse>(responseString);
}
webClient.Dispose();
}
catch (Exception ex)
{
Log.Error(ex, "TPLink Kasa device activation/deactivation failed!");
}
}
}
}