internal static async Task ProcessInterCommunicateMessageOfModuleAsync(this CommunicateMessage message, CancellationToken cancellationToken = default) { if (message.Type.IsEndsWith("#Create")) { var module = message.Data.ToExpandoObject().CreateModuleInstance(); module._contentTypeIDs = null; await module.FindContentTypesAsync(cancellationToken, false).ConfigureAwait(false); module.Set(); } else if (message.Type.IsEndsWith("#Update")) { var module = message.Data.Get("ID", "").GetModuleByID(false, false); module = module == null ? message.Data.ToExpandoObject().CreateModuleInstance() : module.UpdateModuleInstance(message.Data.ToExpandoObject()); module._contentTypeIDs = null; await module.FindContentTypesAsync(cancellationToken, false).ConfigureAwait(false); module.Set(); } else if (message.Type.IsEndsWith("#Delete")) { message.Data.ToExpandoObject().CreateModuleInstance().Remove(); } }
protected override async Task ProcessInterCommunicateMessageAsync(CommunicateMessage message, CancellationToken cancellationToken = default(CancellationToken)) { // prepare var data = message.Data?.ToExpandoObject(); if (data == null) { return; } // update counters if (message.Type.IsEquals("File#Download") && !string.IsNullOrWhiteSpace(data.Get <string>("x-object-id"))) { try { var content = await Content.GetAsync <Content>(data.Get <string>("x-object-id"), cancellationToken).ConfigureAwait(false); if (content != null) { var result = await this.UpdateCounterAsync(content, Components.Security.Action.Download.ToString(), cancellationToken).ConfigureAwait(false); await this.SendUpdateMessageAsync(new UpdateMessage { DeviceID = "*", Type = $"{this.ServiceName}#Content#Counters", Data = result }, cancellationToken).ConfigureAwait(false); } } catch (Exception ex) { await this.WriteLogsAsync(UtilityService.NewUUID, "Error occurred while updating counters", ex).ConfigureAwait(false); } } }
internal static async Task ProcessInterCommunicateMessageOfOrganizationAsync(this CommunicateMessage message, CancellationToken cancellationToken = default) { if (message.Type.IsEndsWith("#Create")) { var organization = message.Data.ToExpandoObject().CreateOrganizationInstance(); organization._siteIDs = organization._moduleIDs = null; await Task.WhenAll( organization.FindSitesAsync(cancellationToken, false), organization.FindModulesAsync(cancellationToken, false) ).ConfigureAwait(false); organization.Set(); } else if (message.Type.IsEndsWith("#Update")) { var organization = message.Data.Get("ID", "").GetOrganizationByID(false, false); organization = organization == null ? message.Data.ToExpandoObject().CreateOrganizationInstance() : organization.UpdateOrganizationInstance(message.Data.ToExpandoObject()); organization._siteIDs = organization._moduleIDs = null; await Task.WhenAll( organization.FindSitesAsync(cancellationToken, false), organization.FindModulesAsync(cancellationToken, false) ).ConfigureAwait(false); organization.Set(); } else if (message.Type.IsEndsWith("#Delete")) { message.Data.ToExpandoObject().CreateOrganizationInstance().Remove(); } }
internal static async Task ProcessInterCommunicateMessageOfPortletAsync(this CommunicateMessage message, CancellationToken cancellationToken = default) { if (message.Type.IsEndsWith("#Create") || message.Type.IsEndsWith("#Update")) { Portlet portlet = null; if (message.Type.IsEndsWith("#Create")) { portlet = message.Data.ToExpandoObject().CreatePortletInstance(); } else { portlet = await Portlet.GetAsync <Portlet>(message.Data.Get <string>("ID"), cancellationToken).ConfigureAwait(false); portlet = portlet == null ? message.Data.ToExpandoObject().CreatePortletInstance() : portlet.UpdatePortletInstance(message.Data.ToExpandoObject()); } var desktop = await(portlet.DesktopID ?? "").GetDesktopByIDAsync(cancellationToken).ConfigureAwait(false); if (desktop != null && desktop._portlets != null) { if (!string.IsNullOrWhiteSpace(portlet.OriginalPortletID)) { portlet._originalPortlet = await Portlet.GetAsync <Portlet>(portlet.OriginalPortletID, cancellationToken).ConfigureAwait(false); } var index = desktop._portlets.FindIndex(p => p.ID.IsEquals(portlet.ID)); if (index < 0) { desktop._portlets.Add(portlet); } else { desktop._portlets[index] = portlet; } desktop.Set(); } } else if (message.Type.IsEndsWith("#Delete")) { var portlet = message.Data.ToExpandoObject().CreatePortletInstance(); var desktop = await(portlet.DesktopID ?? "").GetDesktopByIDAsync(cancellationToken).ConfigureAwait(false); if (desktop != null && desktop._portlets != null) { var index = desktop._portlets.FindIndex(p => p.ID.IsEquals(portlet.ID)); if (index > -1) { desktop._portlets.RemoveAt(index); desktop.Set(); } } } }
public Task SendInterCommunicateMessageAsync(CommunicateMessage message, CancellationToken cancellationToken = default) => UtilityService.ExecuteTask(() => { if (message != null && !string.IsNullOrWhiteSpace(message.ServiceName)) { try { this.GetInterCommunicateMessagePublisher(message.ServiceName).OnNext(message); Global.OnSendRTUMessageSuccess?.Invoke( $"Publish an inter-communicate message successful" + "\r\n" + $"- Service: {message.ServiceName}" + "\r\n" + $"- Type: {message.Type}" + "\r\n" + $"- Data: {message.Data.ToString(Formatting.None)}" ); } catch (Exception exception) { Global.OnSendRTUMessageFailure?.Invoke($"Error occurred while publishing an inter-communicate message: {exception.Message}", exception); } } }, cancellationToken);
public async Task SendInterCommunicateMessageAsync(string type, JToken data = null) { if (this.RTUService == null) { return; } var message = new CommunicateMessage("APIGateway") { Type = type, Data = data ?? new JObject() }; try { await this.RTUService.SendInterCommunicateMessageAsync(message).ConfigureAwait(false); } catch (Exception ex) { if (ex is WampException && (ex as WampException).ErrorUri.Equals("wamp.error.no_such_procedure")) { try { await Task.Delay(UtilityService.GetRandomNumber(456, 789)).ConfigureAwait(false); this.RTUService = Router.OutgoingChannel.RealmProxy.Services.GetCalleeProxy <IRTUService>(ProxyInterceptor.Create()); await this.RTUService.SendInterCommunicateMessageAsync(message).ConfigureAwait(false); } catch (Exception exception) { Global.OnError?.Invoke($"Error occurred while sending an inter-communicate message => {exception.Message}", exception); } } else { Global.OnError?.Invoke($"Error occurred while sending an inter-communicate message => {ex.Message}", ex); } } }
internal static Task ProcessInterCommunicateMessageOfContentTypeAsync(this CommunicateMessage message, CancellationToken cancellationToken = default) { if (message.Type.IsEndsWith("#Create")) { message.Data.ToExpandoObject().CreateContentTypeInstance().Set(); } else if (message.Type.IsEndsWith("#Update")) { var contentType = message.Data.Get("ID", "").GetContentTypeByID(false, false); contentType = contentType == null ? message.Data.ToExpandoObject().CreateContentTypeInstance() : contentType.UpdateContentTypeInstance(message.Data.ToExpandoObject()); contentType.Set(); } else if (message.Type.IsEndsWith("#Delete")) { message.Data.ToExpandoObject().CreateContentTypeInstance().Remove(); } return(Task.CompletedTask); }
internal static Task ProcessInterCommunicateMessageOfExpressionAsync(this CommunicateMessage message, CancellationToken cancellationToken = default) { if (message.Type.IsEndsWith("#Create")) { message.Data.ToExpandoObject().CreateExpressionInstance().Set(); } else if (message.Type.IsEndsWith("#Update")) { var expression = message.Data.Get("ID", "").GetExpressionByID(false, false); expression = expression == null ? message.Data.ToExpandoObject().CreateExpressionInstance() : expression.UpdateExpressionInstance(message.Data.ToExpandoObject()); expression.Set(); } else if (message.Type.IsEndsWith("#Delete")) { message.Data.ToExpandoObject().CreateExpressionInstance().Remove(); } return(Task.CompletedTask); }
void ProcessInterCommunicateMessage(CommunicateMessage message) { // controller info if (message.Type.IsStartsWith("Controller#")) { ControllerInfo controller = null; switch (message.Type.ToArray('#').Last().ToLower()) { case "info": case "connect": if (this.Controllers.TryGetValue(message.Data.Get <string>("ID"), out controller)) { controller.CopyFrom(message.Data, "ID".ToHashSet()); } else { controller = message.Data.FromJson <ControllerInfo>(); this.Controllers.TryAdd(controller.ID, controller); } #if DEBUG Global.OnProcess?.Invoke($"{(message.Type.ToArray('#').Last().IsEquals("info") ? "Got information of a controller" : "A controller was connected")} => {message.ToJson()}"); #endif break; case "disconnect": controller = message.Data.FromJson <ControllerInfo>(); if (this.Controllers.TryGetValue(controller.ID, out controller)) { controller.Available = false; controller.Timestamp = DateTime.Now; this.Services.ForEach(kvp => { var svcInfo = kvp.Value.FirstOrDefault(svc => svc.Name.IsEquals(kvp.Key) && svc.ControllerID.IsEquals(controller.ID)); if (svcInfo != null) { svcInfo.Available = svcInfo.Running = false; } }); } #if DEBUG Global.OnProcess?.Invoke($"A controller was disconnected => {message.ToJson()}"); #endif break; case "requestinfo": this.RequestTime = DateTime.Now; #if DEBUG Global.OnProcess?.Invoke($"Got a request to update information of a controller => {message.ToJson()}"); #endif break; } } // service info else if (message.Type.IsEquals("Service#Info")) { var serviceInfo = message.Data.FromJson <ServiceInfo>(); if (!this.Services.TryGetValue(serviceInfo.Name.ToLower(), out var services)) { services = new List <ServiceInfo>(); this.Services.TryAdd(serviceInfo.Name.ToLower(), services); } var svcInfo = services.FirstOrDefault(svc => svc.Name.Equals(serviceInfo.Name) && svc.UniqueName.Equals(serviceInfo.UniqueName) && svc.ControllerID.Equals(serviceInfo.ControllerID)); if (svcInfo == null) { services.Add(serviceInfo); } else { if (svcInfo.Available != serviceInfo.Available || svcInfo.Running != serviceInfo.Running) { svcInfo.Timestamp = DateTime.Now; } svcInfo.Available = serviceInfo.Available; svcInfo.Running = serviceInfo.Running; if (svcInfo.Running) { svcInfo.InvokeInfo = serviceInfo.InvokeInfo; } } if (serviceInfo.Running) { this.OnServiceStarted?.Invoke(serviceInfo.ControllerID, serviceInfo.Name); } else { this.OnServiceStopped?.Invoke(serviceInfo.ControllerID, serviceInfo.Name); } } // registered handler this.OnInterCommunicateMessageReceived?.Invoke(message); }