Skip to content

Commit 7c503ee

Browse files
authored
Add possibility to listen to logging events (#30)
1 parent 8f6e0bf commit 7c503ee

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Visual Studio Projects/ZWaveJS.NET/ZWaveJS.NET/Driver.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Driver
2121

2222
private Dictionary<string, Action<JObject>> NodeEventMap;
2323
private Dictionary<string, Action<JObject>> ControllerEventMap;
24+
private Dictionary<string, Action<JObject>> DriverEventMap;
2425
private static int SchemaVersionID = 17;
2526
private string SerialPort;
2627

@@ -52,6 +53,13 @@ public string ZWaveJSServerVersion
5253
public delegate void StartupError(string Message);
5354
public event StartupError StartupErrorEvent;
5455

56+
public delegate void LoggingEventDelegate(LoggingEventArgs args);
57+
public event LoggingEventDelegate LoggingEvent;
58+
internal void Trigger_LoggingEvent(LoggingEventArgs args)
59+
{
60+
LoggingEvent?.Invoke(args);
61+
}
62+
5563
private void MapNodeEvents()
5664
{
5765
NodeEventMap.Add("check lifeline health progress", (JO) =>
@@ -303,13 +311,36 @@ private void MapControllerEvents()
303311
});
304312
}
305313

314+
private void MapDriverEvents()
315+
{
316+
DriverEventMap.Add("logging", (JO) =>
317+
{
318+
LoggingEventArgs args = new LoggingEventArgs();
319+
args.formattedMessage = JO.SelectToken("event.formattedMessage")?.Value<string>();
320+
args.direction = JO.SelectToken("event.direction")?.Value<string>();
321+
args.primaryTags = JO.SelectToken("event.primaryTags")?.Value<string>();
322+
args.secondaryTags = JO.SelectToken("event.secondaryTags")?.Value<string>();
323+
args.secondaryTagPadding = JO.SelectToken("event.secondaryTagPadding")?.Value<int>();
324+
args.multiline = JO.SelectToken("event.multiline")?.Value<bool>();
325+
args.timestamp = JO.SelectToken("event.timestamp")?.Value<string>();
326+
args.label = JO.SelectToken("event.label")?.Value<string>();
327+
args.message = JO.SelectToken("event.message")?.Value<string>();
328+
args.level = JO.SelectToken("event.level")?.Value<string>();
329+
330+
Trigger_LoggingEvent(args);
331+
});
332+
}
333+
306334
private void MapEvents()
307335
{
308336
NodeEventMap = new Dictionary<string, Action<JObject>>();
309337
MapNodeEvents();
310338

311339
ControllerEventMap = new Dictionary<string, Action<JObject>>();
312340
MapControllerEvents();
341+
342+
DriverEventMap = new Dictionary<string, Action<JObject>>();
343+
MapDriverEvents();
313344
}
314345

315346

@@ -429,6 +460,13 @@ private void WebsocketClient_MessageReceived(object sender, WatsonWebsocket.Mess
429460
}
430461
break;
431462

463+
case "driver":
464+
if (DriverEventMap.ContainsKey(_Event))
465+
{
466+
DriverEventMap[_Event].Invoke(JO);
467+
}
468+
break;
469+
432470
}
433471
return;
434472
}
@@ -572,5 +610,51 @@ private void StartListetningCB(JObject JO)
572610
}
573611
}
574612
}
613+
614+
public Task<CMDResult> StartListeningLogs()
615+
{
616+
Guid ID = Guid.NewGuid();
617+
618+
TaskCompletionSource<CMDResult> Result = new TaskCompletionSource<CMDResult>();
619+
620+
Callbacks.Add(ID, (JO) =>
621+
{
622+
CMDResult Res = new CMDResult(JO);
623+
Result.SetResult(Res);
624+
});
625+
626+
Dictionary<string, object> Request = new Dictionary<string, object>();
627+
628+
Request.Add("messageId", ID);
629+
Request.Add("command", Enums.Commands.StartListeningLogs);
630+
631+
string RequestPL = Newtonsoft.Json.JsonConvert.SerializeObject(Request);
632+
Client.SendAsync(RequestPL);
633+
634+
return Result.Task;
635+
}
636+
637+
public Task<CMDResult> StopListeningLogs()
638+
{
639+
Guid ID = Guid.NewGuid();
640+
641+
TaskCompletionSource<CMDResult> Result = new TaskCompletionSource<CMDResult>();
642+
643+
Callbacks.Add(ID, (JO) =>
644+
{
645+
CMDResult Res = new CMDResult(JO);
646+
Result.SetResult(Res);
647+
});
648+
649+
Dictionary<string, object> Request = new Dictionary<string, object>();
650+
651+
Request.Add("messageId", ID);
652+
Request.Add("command", Enums.Commands.StopListeningLogs);
653+
654+
string RequestPL = Newtonsoft.Json.JsonConvert.SerializeObject(Request);
655+
Client.SendAsync(RequestPL);
656+
657+
return Result.Task;
658+
}
575659
}
576660
}

Visual Studio Projects/ZWaveJS.NET/ZWaveJS.NET/Enums.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ internal class Commands
7070
public const string MCGetDefinedValueIDs = "multicast_group.get_defined_value_ids";
7171
public const string MCSupportsCCAPI = "multicast_group.supports_cc_api";
7272
public const string MCInvokeCCAPI = "multicast_group.invoke_cc_api";
73+
public const string StartListeningLogs = "driver.start_listening_logs";
74+
public const string StopListeningLogs = "driver.stop_listening_logs";
7375
}
7476

7577
public enum SecurityClass

Visual Studio Projects/ZWaveJS.NET/ZWaveJS.NET/Structures.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,18 @@ public class InclusionUserCallbacks
272272
public GrantSecurityClasses grantSecurityClasses { get; set; }
273273
public Abort abort { get; set; }
274274
}
275+
276+
public class LoggingEventArgs
277+
{
278+
public string formattedMessage { get; set; }
279+
public string direction { get; set; }
280+
public string primaryTags { get; set; }
281+
public string secondaryTags { get; set; }
282+
public int? secondaryTagPadding { get; set; }
283+
public bool? multiline { get; set; }
284+
public string timestamp { get; set; }
285+
public string label { get; set; }
286+
public string message { get; set; }
287+
public string level { get; set; }
288+
}
275289
}

0 commit comments

Comments
 (0)