/
Program.cs
354 lines (343 loc) · 13.2 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// --------------------------------------------------------------------------
// Gurux Ltd
//
//
//
// Filename: $HeadURL$
//
// Version: $Revision$,
// $Date$
// $Author$
//
// Copyright (c) Gurux Ltd
//
//---------------------------------------------------------------------------
//
// DESCRIPTION
//
// This file is a part of Gurux Device Framework.
//
// Gurux Device Framework is Open Source software; you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2 of the License.
// Gurux Device Framework is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// This code is licensed under the GNU General Public License v2.
// Full text may be retrieved at http://www.gnu.org/licenses/gpl-2.0.txt
//---------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GuruxAMI.Client;
using GuruxAMI.Common;
using Gurux.Common;
namespace GuruxAMI.DataCollector
{
class Program
{
static void ShowHelp()
{
}
/// <summary>
/// Hide password.
/// </summary>
/// <returns></returns>
static string GetPassword()
{
ConsoleKeyInfo key;
string str = "";
do
{
key = System.Console.ReadKey(true);
if (key.Key != ConsoleKey.Backspace && key.Key != ConsoleKey.Enter)
{
str += key.KeyChar;
System.Console.Write("*");
}
else
{
if (key.Key == ConsoleKey.Backspace && str.Length > 0)
{
str = str.Substring(0, (str.Length - 1));
System.Console.Write("\b \b");
}
}
}
// Stops Receving Keys Once Enter is Pressed
while (key.Key != ConsoleKey.Enter);
System.Console.WriteLine();
return str;
}
/// <summary>
/// Start GuruxAMI Data collector as console.
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
//Update previous installed settings.
if (Properties.Settings.Default.UpdateSettings)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpdateSettings = false;
Properties.Settings.Default.Save();
}
bool trace = false;
GXAmiDataCollectorServer collector = null;
try
{
for (int pos = 0; pos != args.Length; ++pos)
{
string tag = args[pos];
if (tag[0] == '/' || tag[0] == '-')
{
tag = tag.Substring(1).ToLower();
if (tag == "h")
{
GuruxAMI.DataCollector.Properties.Settings.Default.AmiHostName = args[++pos];
}
if (tag == "p")
{
GuruxAMI.DataCollector.Properties.Settings.Default.AmiHostPort = args[++pos];
}
//Register Data Collector again.
if (tag == "r")
{
GuruxAMI.DataCollector.Properties.Settings.Default.AmiDCGuid = Guid.Empty;
}
//Trace messages
if (tag == "t")
{
trace = true;
}
}
}
string host = GuruxAMI.DataCollector.Properties.Settings.Default.AmiHostName;
if (string.IsNullOrEmpty(host))
{
ShowHelp();
return;
}
Guid guid = GuruxAMI.DataCollector.Properties.Settings.Default.AmiDCGuid;
Console.WriteLine("Starting Data Collector...");
if (!host.StartsWith("http://"))
{
host = "http://" + host + ":" + GuruxAMI.DataCollector.Properties.Settings.Default.AmiHostPort + "/";
}
Console.WriteLine("Connecting " + host);
GXAmiUser user = null;
string r, dcName = null;
GuruxAMI.Client.GXAmiClient cl = null;
if (guid == Guid.Empty)
{
Console.WriteLine("Registering Data Collector to GuruxAMI Service: ");
int pos = 0;
do
{
Console.WriteLine("Enter user name:");
string username = Console.ReadLine();
if (username == "")
{
return;
}
Console.WriteLine("Enter password:");
string password = GetPassword();
if (password == "")
{
return;
}
cl = new GXAmiClient(host, username, password);
//Get info from registered user.
try
{
user = cl.GetUserInfo();
break;
}
catch(UnauthorizedAccessException)
{
continue;
}
}while(++pos != 3);
//If authorisation failed.
if (user == null)
{
return;
}
Console.WriteLine("Finding data collectors.");
GXAmiDataCollector[] dcs = cl.GetDataCollectors();
//If there are existing DCs...
if (dcs.Length != 0)
{
Console.WriteLine("Do you want to register new data collector or bind old? (n/b)");
do
{
r = Console.ReadLine().Trim().ToLower();
if (r == "n" || r == "b")
{
break;
}
}
while (r == "");
}
else
{
r = "n";
}
//Old DC replaced.
if (r == "b")
{
Console.WriteLine("Select data collector number that you want to bind:");
pos = 0;
foreach (GXAmiDataCollector it in dcs)
{
++pos;
Console.WriteLine(pos.ToString() + ". " + it.Name);
}
do
{
r = Console.ReadLine().Trim();
int sel = 0;
if (int.TryParse(r, out sel) && sel > 0 && sel <= pos)
{
guid = dcs[sel - 1].Guid;
break;
}
}
while (true);
}
else
{
do
{
Console.WriteLine("Enter name of the data collector:");
dcName = Console.ReadLine().Trim();
if (dcName == "")
{
return;
}
if (cl.Search(new string[] { dcName }, ActionTargets.DataCollector, SearchType.Name).Length == 0)
{
GXAmiDataCollector tmp = new GXAmiDataCollector(dcName, "", "");
cl.AddDataCollector(tmp, cl.GetUserGroups(false));
guid = tmp.Guid;
break;
}
Console.WriteLine("Name exists. Give new one.");
}
while (true);
}
}
collector = new GXAmiDataCollectorServer(host, guid);
if (trace)
{
collector.OnTasksAdded += new TasksAddedEventHandler(OnTasksAdded);
collector.OnTasksClaimed += new TasksClaimedEventHandler(OnTasksClaimed);
collector.OnTasksRemoved += new TasksRemovedEventHandler(OnTasksRemoved);
collector.OnError += new ErrorEventHandler(OnError);
}
collector.OnAvailableSerialPorts += new AvailableSerialPortsEventHandler(OnAvailableSerialPorts);
GXAmiDataCollector dc = collector.Init(dcName);
//If new Data collector is added bind it to the user groups.
if (guid == Guid.Empty && cl != null)
{
cl.AddDataCollector(dc, cl.GetUserGroups(false));
}
if (dc != null)
{
GuruxAMI.DataCollector.Properties.Settings.Default.AmiDCGuid = dc.Guid;
}
Console.WriteLine(string.Format("Data Collector '{0}' started.", dc.Name));
GuruxAMI.DataCollector.Properties.Settings.Default.Save();
}
catch (Exception ex)
{
if (ex is UnauthorizedAccessException)
{
Console.WriteLine("Unknown data collector.");
GuruxAMI.DataCollector.Properties.Settings.Default.AmiDCGuid = Guid.Empty;
GuruxAMI.DataCollector.Properties.Settings.Default.Save();
}
else
{
Console.WriteLine(ex.Message);
}
}
//Wait until user press enter.
ConsoleKeyInfo key;
while ((key = System.Console.ReadKey()).Key != ConsoleKey.Enter)
{
System.Console.Write("\b \b");
}
if (collector != null)
{
collector.Dispose();
}
}
/// <summary>
/// Get info from available serial ports.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void OnAvailableSerialPorts(object sender, GXSerialPortInfo e)
{
e.SerialPorts = Gurux.Serial.GXSerial.GetPortNames();
}
/// <summary>
/// Error has occurred.
/// </summary>
/// <param name="sender"></param>
/// <param name="ex"></param>
static void OnError(object sender, Exception ex)
{
Console.WriteLine(ex.Message);
}
/// <summary>
/// Task is removed.
/// </summary>
/// <param name="sender"></param>
/// <param name="tasks"></param>
static void OnTasksRemoved(object sender, GXAmiTask[] tasks)
{
foreach(GXAmiTask it in tasks)
{
Console.WriteLine(string.Format("Task {0} removed.", it.Id));
}
}
/// <summary>
/// Task is claimed.
/// </summary>
/// <param name="sender"></param>
/// <param name="tasks"></param>
static void OnTasksClaimed(object sender, GXAmiTask[] tasks)
{
foreach (GXAmiTask it in tasks)
{
Console.WriteLine(string.Format("{0} Task {1} claimed.", it.TaskType, it.Id));
}
}
/// <summary>
/// New task is added.
/// </summary>
/// <param name="sender"></param>
/// <param name="tasks"></param>
static void OnTasksAdded(object sender, GXAmiTask[] tasks)
{
foreach (GXAmiTask it in tasks)
{
if (it.TaskType == TaskType.MediaWrite)
{
string[] tmp = it.Data.Split(Environment.NewLine.ToCharArray());
Console.WriteLine(string.Format("{0} Task {1} Added. {2}", it.TaskType, it.Id, tmp[tmp.Length - 1]));
}
else
{
Console.WriteLine(string.Format("{0} Task {1} Added.", it.TaskType, it.Id));
}
}
}
}
}