/// <summary> /// Timer callback that fires for every second elapsed without a message in or out /// </summary> /// <param name="usrObj">unused</param> private void OnGapTimerExpired(object usrObj) { _eventsLock.Enter(); try { _messageFromMp = string.Empty; _messageFromDrv = string.Empty; if (_events.Count > 1) { CrpcEvent lastEvent = _events[_events.Count - 1]; if (lastEvent.EventType == CrpcEvent.Type.Gap) { lastEvent.GapSecs++; } else { _events.Add(new CrpcEvent() { EventType = CrpcEvent.Type.Gap, GapSecs = 1 }); } } } finally { _eventsLock.Leave(); } }
/// <summary> /// Process a single JSON object and populate the event list appropriately /// </summary> /// <param name="json">The incoming message (a single object)</param> /// <returns>true if all goes well, otherwise false</returns> private bool ProcessCrpcMessage(string json) { bool bRet = true; CrpcEvent newEvt = null; try { newEvt = JsonConvert.DeserializeObject <CrpcEvent>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); newEvt.OriginalJson = json; // Handle result being EITHER bool or Dict<str,obj> try { newEvt.resultDict = JsonConvert.DeserializeObject <Dictionary <string, object> >(newEvt.result.ToString()); } catch { newEvt.resultDict = null; } if (!string.IsNullOrEmpty(newEvt.method)) { string[] methodParts = newEvt.method.Split('.'); if (methodParts.Length >= 2) { newEvt.MethodObject = methodParts[0]; newEvt.MethodSig = methodParts[1]; } else { newEvt.MethodSig = newEvt.method; } if (newEvt.MethodSig.Contains("RegisterEvent")) { newEvt.EventType = CrpcEvent.Type.RegisterEvent; newEvt.Description = newEvt.parameters["ev"].ToString(); } else if (newEvt.MethodSig.Contains("Event")) { newEvt.EventType = CrpcEvent.Type.Event; newEvt.Description = newEvt.parameters["ev"].ToString(); if (newEvt.parameters.ContainsKey("parameters")) { newEvt.Detail = newEvt.parameters["parameters"].ToString(); } else { newEvt.Detail = "No payload"; } } else if (newEvt.MethodSig.Contains("GetProperty")) { newEvt.EventType = CrpcEvent.Type.GetProperty; newEvt.Description = newEvt.parameters["propName"].ToString(); } else { newEvt.EventType = CrpcEvent.Type.CallMethod; newEvt.Description = "Method call - see MethodSig"; } } else if (newEvt.error != null) { newEvt.EventType = CrpcEvent.Type.CrpcError; newEvt.Description = newEvt.error.message; } else { newEvt.EventType = CrpcEvent.Type.Result; string prop, val; newEvt.GetResultPropertyValue(out prop, out val); newEvt.Description = prop; newEvt.Detail = val; } } catch (Exception e) { bRet = false; #if DEBUG CrestronConsole.PrintLine("CrpcVisualiser Process: Exception: " + e.Message); CrestronConsole.PrintLine(": " + json); #endif newEvt = new CrpcEvent() { EventType = CrpcEvent.Type.Exception, Description = e.GetType().ToString() + " > " + e.Message + ": " + json, Detail = e.ToString(), OriginalJson = json }; } if (newEvt != null) { _eventsLock.Enter(); try { #if DEBUG CrestronConsole.PrintLine("Adding Event: [{0}] {1}", newEvt.id, newEvt.EventType); #endif _gapTimer.Reset(VIS_GAP_TIME, VIS_GAP_TIME); _events.Add(newEvt); } finally { _eventsLock.Leave(); } } else { #if DEBUG CrestronConsole.PrintLine("FAILED Adding Event!"); #endif } return(bRet); }
/// <summary> /// Write out the event XML file to the given filepath /// </summary> /// <param name="fname">the file path e.g. \\HTML\\crpc_vis.xml</param> public void Write(SimplSharpString fname) { _eventsLock.Enter(); try { // Pre-process the event list to match up results with requests foreach (CrpcEvent evt in _events) { evt.Ignore = false; if (((evt.EventType == CrpcEvent.Type.Result) || (evt.EventType == CrpcEvent.Type.CrpcError)) && (evt.id > 1)) { CrpcEvent reqEvt = _events.Find(e => (e.id == evt.id) && (e.EventType != CrpcEvent.Type.Result) && (e.EventType != CrpcEvent.Type.CrpcError)); if (reqEvt != null) { evt.Ignore = true; reqEvt.ResultEvent = evt; } } } using (FileStream fs = new FileStream(fname.ToString(), FileMode.Create)) { fs.Write("<?xml version=\"1.0\"?>\n", Encoding.UTF8); fs.Write("<?xml-stylesheet type=\"text/xsl\" href=\"crpc.xsl\"?>\n", Encoding.UTF8); fs.Write("<crpcCapture>\n", Encoding.UTF8); fs.Write(" <crpcSummary>\n", Encoding.UTF8); fs.Write(string.Format(" <from>{0}</from>\n", From), Encoding.UTF8); fs.Write(string.Format(" <to>{0}</to>\n", To), Encoding.UTF8); fs.Write(" </crpcSummary>\n", Encoding.UTF8); fs.Write(" <crpcEvents>\n", Encoding.UTF8); foreach (CrpcEvent evt in _events) { if (!evt.Ignore) { fs.Write(string.Format(" <event type=\"{0}\" id=\"{1}\">\n", evt.EventType.ToString(), evt.id), Encoding.UTF8); if (!string.IsNullOrEmpty(evt.Description)) { fs.Write(string.Format(" <description><![CDATA[{0}]]></description>\n", evt.Description), Encoding.UTF8); } if (!string.IsNullOrEmpty(evt.MethodObject)) { fs.Write(string.Format(" <object><![CDATA[{0}]]></object>\n", evt.MethodObject), Encoding.UTF8); } if (!string.IsNullOrEmpty(evt.MethodSig)) { fs.Write(string.Format(" <method><![CDATA[{0}]]></method>\n", evt.MethodSig), Encoding.UTF8); } if (!string.IsNullOrEmpty(evt.Detail)) { fs.Write(string.Format(" <detail><![CDATA[{0}]]></detail>\n", evt.Detail), Encoding.UTF8); } if (evt.GapSecs > 0) { fs.Write(string.Format(" <gap>{0}</gap>\n", evt.GapSecs), Encoding.UTF8); } fs.Write(string.Format(" <original><![CDATA[{0}]]></original>\n", evt.OriginalJson), Encoding.UTF8); // Do we have a result event too? if (evt.ResultEvent != null) { fs.Write(string.Format(" <response type=\"{0}\">\n", evt.ResultEvent.EventType.ToString()), Encoding.UTF8); if (!string.IsNullOrEmpty(evt.ResultEvent.Description)) { fs.Write(string.Format(" <description><![CDATA[{0}]]></description>\n", evt.ResultEvent.Description), Encoding.UTF8); } if (!string.IsNullOrEmpty(evt.ResultEvent.Detail)) { fs.Write(string.Format(" <detail><![CDATA[{0}]]></detail>\n", evt.ResultEvent.Detail), Encoding.UTF8); } fs.Write(string.Format(" <original><![CDATA[{0}]]></original>\n", evt.ResultEvent.OriginalJson), Encoding.UTF8); fs.Write(" </response>\n", Encoding.UTF8); } fs.Write(string.Format(" </event>\n"), Encoding.UTF8); } } fs.Write(" </crpcEvents>\n", Encoding.UTF8); fs.Write("</crpcCapture>\n", Encoding.UTF8); fs.Close(); } } finally { _eventsLock.Leave(); } }