internal static async Task <JObject> SyncPortletAsync(this RequestInfo requestInfo, CancellationToken cancellationToken = default) { var data = requestInfo.GetBodyExpando(); var portlet = await Portlet.GetAsync <Portlet>(data.Get <string>("ID"), cancellationToken).ConfigureAwait(false); if (portlet == null) { portlet = Portlet.CreateInstance(data); await Portlet.CreateAsync(portlet, cancellationToken).ConfigureAwait(false); } else { portlet.Fill(data); await Portlet.UpdateAsync(portlet, true, cancellationToken).ConfigureAwait(false); } // clear related cache if (requestInfo.GetHeaderParameter("x-converter") != null) { portlet.ClearRelatedCacheAsync(ServiceBase.ServiceComponent.CancellationToken, requestInfo.CorrelationID).Run(); } else { await portlet.ClearCacheAsync(cancellationToken, requestInfo.CorrelationID).ConfigureAwait(false); } // send update messages var json = portlet.ToJson(); var objectName = portlet.GetTypeName(true); await Task.WhenAll( Utility.RTUService.SendUpdateMessageAsync(new UpdateMessage { Type = $"{requestInfo.ServiceName}#{objectName}#Update", Data = json, DeviceID = "*" }, cancellationToken), Utility.RTUService.SendInterCommunicateMessageAsync(new CommunicateMessage(requestInfo.ServiceName) { Type = $"{objectName}#Update", Data = json, ExcludedNodeID = Utility.NodeID }, cancellationToken) ).ConfigureAwait(false); // return the response return(new JObject { { "Sync", "Success" }, { "ID", portlet.ID }, { "Type", objectName } }); }
public static Portlet CreatePortletInstance(this ExpandoObject data, string excluded = null, Action <Portlet> onCompleted = null) => Portlet.CreateInstance(data, excluded?.ToHashSet(), portlet => { portlet.Normalize(); onCompleted?.Invoke(portlet); });