private void sinkDeviceExpired(TheUPnPDeviceInfo tHistory) { foreach (string tUID in mUPnpLost.Keys) { if (mUPnpLost[tUID] == null) { continue; } if (tUID.Equals("*")) { mUPnpLost[tUID](tHistory); } else { string[] stParts = TheCommonUtils.cdeSplit(tUID, ";:;", true, true); if (stParts.Length == 1) { if (tUID == "*" || tHistory?.ST?.ToUpper().Contains(tUID.ToUpper()) == true) { mUPnpLost[tUID](tHistory); } } else { if (CompareUPnPField(tHistory, stParts[0], stParts[1])) { mUPnpLost[tUID](tHistory); } } } } }
internal static bool CheckAddPermission(cdeConcurrentDictionary <eWebPlatform, ThePropertyBag> pPlatBag, TheClientInfo pInfo) { cdeConcurrentDictionary <string, string> platPlat = null; if (pPlatBag.ContainsKey(pInfo.WebPlatform)) { platPlat = ThePropertyBag.GetDictionary(pPlatBag[pInfo.WebPlatform], "="); } if (pPlatBag.ContainsKey(eWebPlatform.Any)) { cdeConcurrentDictionary <string, string> tPlat = ThePropertyBag.GetDictionary(pPlatBag[eWebPlatform.Any], "="); if (tPlat.ContainsKey("HideAddButton") && TheCommonUtils.CBool(tPlat["HideAddButton"])) { if (platPlat == null || (platPlat.ContainsKey("ShowAddButton") && !TheCommonUtils.CBool(platPlat["ShowAddButton"]))) { return(false); } } if (tPlat.ContainsKey("RequireFirstNodeForAdd") && TheCommonUtils.CBool(tPlat["RequireFirstNodeForAdd"]) && !pInfo.IsFirstNode) { if (platPlat == null || (platPlat.ContainsKey("AllowAddOnAllNodes") && !TheCommonUtils.CBool(platPlat["AllowAddOnAllNodes"]))) { return(false); } } } else { if (platPlat != null) { if (platPlat.ContainsKey("HideAddButton") && TheCommonUtils.CBool(platPlat["HideAddButton"])) { return(false); } if (platPlat.ContainsKey("RequireFirstNodeForAdd") && TheCommonUtils.CBool(platPlat["RequireFirstNodeForAdd"]) && !pInfo.IsFirstNode) { return(false); } } } return(true); }
private static void CreateKPIIndexes() { // We could wait here for engines, but then other KPIs will not be written in meantime // await TheBaseEngine.WaitForEnginesStartedAsync(); KPIIndexes = new cdeConcurrentDictionary <string, int>(); lock (KPIIndexes) { Array indexes = Enum.GetValues(typeof(eKPINames)); foreach (var index in indexes) { KPIIndexes.TryAdd(Enum.GetName(typeof(eKPINames), index), (int)index); } List <string> engineNames = TheThingRegistry.GetEngineNames(false); foreach (string engineName in engineNames) { KPIIndexes.TryAdd($"TSMbyENG-{engineName}", KPIIndexes.Count); } } }
/// <summary> /// Issues a REST GET call to a given URI /// </summary> /// <param name="pUri">Target URI for the REST call</param> /// <param name="tTimeOut">Timeout of the call.If no return was received by this time (in seconds) the call will fail</param> /// <param name="UID">Username for calls with authentication</param> /// <param name="PWD">Password for calls with authentication</param> /// <param name="Domain">Domain if required for cookies</param> /// <param name="tCallback">Callback when the REST call is finished</param> /// <param name="pCookie">Cookie to be handed back with the callback</param> /// <param name="pCookies">Cookies to send to the REST server</param> public static void GetRESTAsync(Uri pUri, int tTimeOut, string UID, string PWD, string Domain, Action <TheRequestData> tCallback, object pCookie, cdeConcurrentDictionary <string, string> pCookies) { TheRequestData pData = new TheRequestData(); if (TheBaseAssets.MyServiceHostInfo != null) { pData.RemoteAddress = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false); } pData.RequestUri = pUri; pData.TimeOut = tTimeOut; pData.UID = UID; pData.PWD = PWD; pData.DOM = Domain; pData.CookieObject = pCookie; pData.RequestCookies = pCookies; GetRESTAsync(pData, tCallback, null); }
/// <summary> /// REST POST Call to a server address with the URI /// </summary> /// <param name="pUri">Target URI of the server</param> /// <param name="pCallback">Callback with Response when POST returns</param> /// <param name="stmPostBuffer">Stream of a buffer to be posted to the server</param> /// <param name="pContentType">Content type of the POST Call</param> /// <param name="pRequestor">Requestor ID</param> /// <param name="pCookie">Cookie to be handed back with the response</param> /// <param name="pErrorCallback">Callback in case POST fails</param> /// <param name="pCookies">Cookies to send with the POST</param> public void PostRESTAsync(Uri pUri, Action <TheRequestData> pCallback, Stream stmPostBuffer, string pContentType, Guid pRequestor, object pCookie, Action <TheRequestData> pErrorCallback, cdeConcurrentDictionary <string, string> pCookies) { TheRequestData pData = new TheRequestData { RemoteAddress = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false), RequestUri = pUri, CookieObject = pCookie, RequestCookies = pCookies, PostDataStream = stmPostBuffer, ResponseMimeType = pContentType, DeviceID = pRequestor, HttpMethod = "POST" }; PostRESTAsync(pData, pCallback, pErrorCallback); }
/// <summary> /// REST POST Call to a server address with the URI /// </summary> /// <param name="pUri">Target URI of the server</param> /// <param name="pCallback">Callback with Response when POST returns</param> /// <param name="pPostBuffer">Post-buffer string to be posted to the server</param> /// <param name="pContentType">Content type of the POST Call</param> /// <param name="pRequestor">Requestor ID</param> /// <param name="pCookie">Cookie to be handed back with the response</param> /// <param name="pErrorCallback">Callback in case POST fails</param> /// <param name="pCookies">Cookies to send with the POST</param> public void PostRESTAsync(Uri pUri, Action <TheRequestData> pCallback, string pPostBuffer, string pContentType, Guid pRequestor, object pCookie, Action <TheRequestData> pErrorCallback, cdeConcurrentDictionary <string, string> pCookies) { PostRESTAsync(pUri, pCallback, Encoding.UTF8.GetBytes(pPostBuffer), pContentType, pRequestor, pCookie, pErrorCallback, pCookies); }
public void CheckForDeviceMatch(TheUPnPDeviceInfo tHistory, Uri pLocationUrl) { if (mUPnpUIDs.Count <= 0) { return; } if (pLocationUrl == null || TheCommonUtils.IsGuid(pLocationUrl.Host)) { pLocationUrl = null; } bool sendOnce = false; bool hasAllFlag = false; foreach (string tUID in mUPnpUIDs.Keys) { if (tUID.Equals("*") && mUPnpUIDs[tUID] == null) { if (pLocationUrl == null && mUPnpUIDs[tUID] != null) { mUPnpUIDs[tUID](tHistory); } hasAllFlag = true; } else { string[] stParts = TheCommonUtils.cdeSplit(tUID, ";:;", true, true); if (stParts.Length == 1) { if (!string.IsNullOrEmpty(tHistory?.ST) && (tUID == "*" || tHistory?.ST?.ToUpper().Contains(tUID.ToUpper()) == true)) { if ((pLocationUrl == null || !string.IsNullOrEmpty(tHistory.USN)) && mUPnpUIDs[tUID] != null) { mUPnpUIDs[tUID](tHistory); } else { var t = TheThingRegistry.GetThingByMID(tHistory.ScannerID); (t.GetObject() as ICDEDiscoService)?.GetDeviceDetails(tHistory, mUPnpUIDs[tUID]); //if (pLocationUrl!=null) // TheREST.GetRESTAsync(pLocationUrl, 0, sinkUPnPCustomDeviceFound, mUPnpUIDs[tUID]); } sendOnce = true; } } else { if (CompareUPnPField(tHistory, stParts[0], stParts[1])) { if ((pLocationUrl == null || !string.IsNullOrEmpty(tHistory.USN)) && mUPnpUIDs[tUID] != null) { mUPnpUIDs[tUID](tHistory); } else { var t = TheThingRegistry.GetThingByMID(tHistory.ScannerID); (t.GetObject() as ICDEDiscoService)?.GetDeviceDetails(tHistory, mUPnpUIDs[tUID]); //if (pLocationUrl != null) // TheREST.GetRESTAsync(pLocationUrl, 0, sinkUPnPCustomDeviceFound, mUPnpUIDs[tUID]); } sendOnce = true; } } } } if (!sendOnce && hasAllFlag && pLocationUrl != null) { //TheSystemMessageLog.ToCo(string.Format("UPnP Get Meta From: {0}", pLocationUrl)); var t = TheThingRegistry.GetThingByMID(tHistory.ScannerID); (t?.GetObject() as ICDEDiscoService)?.GetDeviceDetails(tHistory, null); //TheREST.GetRESTAsync(pLocationUrl, 0, sinkUPnPCustomDeviceFound, null); } }
public override bool CreateUX() { if (!mIsUXInitStarted) { mIsUXInitStarted = true; // Creates a "portal" for each rule. This is how we create // a tile for each rule on the rules engine's main page. mThisFormFields = TheNMIEngine.AddStandardForm(MyBaseThing, MyBaseThing.FriendlyName, 18); // Update our status. TheFormInfo tFormGuid = mThisFormFields["Form"] as TheFormInfo; tFormGuid.RegisterEvent2(eUXEvents.OnShow, (pMSG, para) => { UpdateStatus(); }); // Create "Rule Status" settings group // Field Order = 10 // Parent = 1 (main form) // Get standard Status Block. var tstFlds = TheNMIEngine.AddStatusBlock(MyBaseThing, tFormGuid, idGroupStatus); tstFlds["Group"].SetParent(idForm); tstFlds["Group"].Header = "Rule Status"; // tstFlds["Group"].PropertyBag = new ThePropertyBag { "DoClose=true" }; tstFlds["FriendlyName"].Header = "Rule Name"; // When the Friendly Name changes, propogate to the other UI elements that use it. tstFlds["FriendlyName"].RegisterUXEvent(MyBaseThing, eUXEvents.OniValueChanged, null, (psender, pPara) => { (mThisFormFields["Header"] as TheFieldInfo).SetUXProperty(Guid.Empty, $"Title={MyBaseThing.FriendlyName}"); (mThisFormFields["DashIcon"] as TheDashPanelInfo).SetUXProperty(Guid.Empty, $"Caption={MyBaseThing.FriendlyName}"); }); // Add fields to Status Block that are specific to this plugin. TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.SingleCheck, 60, 2, 0, "Activate Rule", "IsRuleActive", new nmiCtrlSingleCheck { ParentFld = idGroupStatus, TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.SingleCheck, 70, 2, 0, "Log Action", "IsRuleLogged", new nmiCtrlSingleCheck { ParentFld = idGroupStatus, TileWidth = 3 }); TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.SingleCheck, 75, 2, 0, "Log Action to Eventlog", "IsEVTLogged", new nmiCtrlSingleCheck { ParentFld = idGroupStatus, TileWidth = 3 }); TheFieldInfo tTriggerBut = TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.TileButton, 65, 2, 0xC0, "Trigger Now", null, new nmiCtrlTileButton { ParentFld = idGroupStatus, ClassName = "cdeGoodActionButton", NoTE = true, TileWidth = 3 }); tTriggerBut.RegisterUXEvent(MyBaseThing, eUXEvents.OnClick, "TriggerNow", (psender, pPara) => { TheProcessMessage pMSG = pPara as TheProcessMessage; if (pMSG == null || pMSG.Message == null) { return; } string[] cmd = pMSG.Message.PLS.Split(':'); if (cmd.Length > 2) { TheThing tThing = TheThingRegistry.GetThingByMID("*", TheCommonUtils.CGuid(cmd[2])); if (tThing == null) { return; } TheRule tRule = tThing.GetObject() as TheRule; if (tRule != null) { //FireAction(tRule, true); tRule.FireAction(true); MyBaseEngine.ProcessMessage(new TheProcessMessage(new TSM(MyBaseEngine.GetEngineName(), "FIRE_RULE", MyBaseThing.cdeMID.ToString()))); TheCommCore.PublishToOriginator(pMSG.Message, new TSM(eEngineName.NMIService, "NMI_TOAST", string.Format("Rule {0} triggered", tRule.GetBaseThing().FriendlyName))); } } }); // Create "Trigger Object" settings group (Field Order = idGroupTriggerObject (100), Parent = 1) TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.CollapsibleGroup, idGroupTriggerObject, 2, 0, "Trigger Object", null, new nmiCtrlCollapsibleGroup { ParentFld = idForm, TileWidth = 6, IsSmall = true }); // Create "Action Settings" settings group (Field Order = idGroupActionSettings (500), Parent = 1) TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.CollapsibleGroup, idGroupActionSettings, 2, 0, "Action Settings", null, new nmiCtrlCollapsibleGroup { ParentFld = idForm, TileWidth = 6, IsSmall = true }); // Create "Thing Property Action" settings group (Field Order = idGroupThingPropAction (550), Parent = 500) TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.CollapsibleGroup, idGroupThingPropAction, 2, 0, "Thing/Property Action", null, new nmiCtrlCollapsibleGroup { ParentFld = idGroupActionSettings, TileWidth = 6, IsSmall = true }); // Create "TSM Action" settings group (Field Order = idGroupTSMAction (600), Parent = 500) TheNMIEngine.AddSmartControl(MyBaseThing, tFormGuid, eFieldType.CollapsibleGroup, idGroupTSMAction, 2, 0, "TSM Action", null, new nmiCtrlCollapsibleGroup { ParentFld = idGroupActionSettings, TileWidth = 6, IsSmall = true, DoClose = true }); // Create all other (non-group header) fields. TheNMIEngine.AddFields(tFormGuid, new List <TheFieldInfo> { /* Trigger Object Group */ { new TheFieldInfo() { FldOrder = 140, DataItem = "MyPropertyBag.TriggerObject.Value", Flags = 2, Type = eFieldType.ThingPicker, Header = "Trigger Object", PropertyBag = new nmiCtrlThingPicker() { ParentFld = idGroupTriggerObject, HelpText = "If this objects...", IncludeEngines = true } } }, { new TheFieldInfo() { FldOrder = 150, DataItem = "MyPropertyBag.TriggerProperty.Value", Flags = 2, Type = eFieldType.PropertyPicker, Header = "Trigger Property", DefaultValue = "Value", PropertyBag = new nmiCtrlPropertyPicker() { ParentFld = idGroupTriggerObject, HelpText = "...property is...", ThingFld = 140 } } }, { new TheFieldInfo() { FldOrder = 160, DataItem = "MyPropertyBag.TriggerCondition.Value", Flags = 2, Type = eFieldType.ComboBox, Header = "Trigger Condition", DefaultValue = "2", PropertyBag = new nmiCtrlComboBox() { ParentFld = idGroupTriggerObject, HelpText = "... then this value, this rule will fire...", DefaultValue = "2", Options = "Fire:0;State:1;Equals:2;Larger:3;Smaller:4;Not:5;Contains:6;Set:7;StartsWith:8;EndsWith:9;Flank:10" } } }, { new TheFieldInfo() { FldOrder = 170, DataItem = "MyPropertyBag.TriggerValue.Value", Flags = 2, Type = eFieldType.SingleEnded, Header = "Trigger Value", PropertyBag = new ThePropertyBag() { "ParentFld=100", "HelpText=...this objects..." } } }, /* Action Settings Group */ { new TheFieldInfo() { FldOrder = 505, DataItem = "MyPropertyBag.ActionObjectType.Value", Flags = 2, Type = eFieldType.ComboBox, Header = "Action Object Type", PropertyBag = new nmiCtrlComboBox() { ParentFld = idGroupActionSettings, Options = "Set Property on a Thing:CDE_THING;Publish Central:CDE_PUBLISHCENTRAL;Publish to Service:CDE_PUBLISH2SERVICE", DefaultValue = "CDE_THING" } } }, { new TheFieldInfo() { FldOrder = 506, DataItem = "MyPropertyBag.ActionDelay.Value", Flags = 2, Type = eFieldType.Number, Header = "Delay", DefaultValue = "0", PropertyBag = new ThePropertyBag() { "ParentFld=500", "HelpText=...after a delay of these seconds..." } } }, /* Thing / Property Action Sub-Group */ { new TheFieldInfo() { FldOrder = 560, DataItem = "MyPropertyBag.ActionObject.Value", Flags = 2, Type = eFieldType.ThingPicker, Header = "Action Object", PropertyBag = new nmiCtrlThingPicker() { ParentFld = 550, HelpText = "...this objects...", IncludeEngines = true } } }, { new TheFieldInfo() { FldOrder = 562, DataItem = "MyPropertyBag.ActionProperty.Value", Flags = 2, Type = eFieldType.PropertyPicker, Header = "Action Property", DefaultValue = "Value", PropertyBag = new nmiCtrlPropertyPicker() { ParentFld = idGroupThingPropAction, HelpText = "...property will change to...", ThingFld = 560 } } }, { new TheFieldInfo() { FldOrder = 563, DataItem = "MyPropertyBag.ActionValue.Value", Flags = 2, Type = eFieldType.SingleEnded, Header = "Action Value", PropertyBag = new nmiCtrlSingleEnded { ParentFld = idGroupThingPropAction, HelpText = "...this value", Style = "text-overflow:ellipsis;overflow:hidden; max-width:400px" } } }, /* TSM Action Sub-Group */ { new TheFieldInfo() { FldOrder = 630, DataItem = "MyPropertyBag.TSMEngine.Value", Flags = 2, Type = eFieldType.ThingPicker, Header = "TSM Engine", PropertyBag = new nmiCtrlThingPicker() { ParentFld = 600, ValueProperty = "EngineName", IncludeEngines = true, Filter = "DeviceType=IBaseEngine" } } }, { new TheFieldInfo() { FldOrder = 631, DataItem = "MyPropertyBag.TSMText.Value", Flags = 2, Type = eFieldType.SingleEnded, Header = "TSM Text", PropertyBag = new ThePropertyBag() { "ParentFld=600", "HelpText=Command of the TSM" } } }, { new TheFieldInfo() { FldOrder = 632, DataItem = "MyPropertyBag.TSMPayload.Value", Flags = 2, Type = eFieldType.TextArea, Header = "TSM Payload", PropertyBag = new nmiCtrlTextArea() { ParentFld = idGroupTSMAction, TileHeight = 2, HelpText = "Body of the TSM" } } }, }); mIsUXInitCompleted = true; } return(true); }
public bool PostToWeb(Uri pUrl, string tPost, Action <TheRequestData> pCallback, string MimeTypeOverride = null, cdeConcurrentDictionary <string, string> paddHeader = null, string pMethodOveride = "POST") { if (!IsConnected || !TheBaseAssets.MasterSwitch || MyRequestData == null || pUrl == null) { return(false); } TheRequestData tD = MyRequestData; if (!string.IsNullOrEmpty(tPost)) { tD.PostData = TheCommonUtils.CUTF8String2Array(tPost); } else { tD.PostData = null; } tD.RequestUri = pUrl; // new Uri($"https://ptdevices.com{Url}"); tD.HttpMethod = pMethodOveride; tD.Header = null; if (paddHeader != null) { tD.Header = new cdeConcurrentDictionary <string, string>(); foreach (string key in paddHeader.Keys) { tD.Header.TryAdd(key, paddHeader[key]); } } tD.DisableRedirect = true; tD.RequestCookies = MyRequestData?.SessionState?.StateCookies; if (!string.IsNullOrEmpty(MimeTypeOverride)) { tD.ResponseMimeType = MimeTypeOverride; // "application/x-www-form-urlencoded; charset=UTF-8"; } else { tD.ResponseMimeType = mPostMimeType; // "application/x-www-form-urlencoded; charset=UTF-8"; } while (MyRest.IsPosting > 1 && IsConnected) { TheCommonUtils.SleepOneEye(1000, 100); } if (IsConnected) { MyRest.PostRESTAsync(tD, pCallback, sinkError); } return(true); }
public void GetDevices(Uri pDeviceURL = null, string tPost = null, cdeConcurrentDictionary <string, string> addHeader = null) { if (!IsConnected || !TheBaseAssets.MasterSwitch) { return; } if (pDeviceURL != null) { mDeviceUrl = pDeviceURL; } if (tPost != null) { mDevicePost = tPost; } if (addHeader != null) { mAddHeader = addHeader; } if (mDevicePost == null) { TheRequestData tRequest = new TheRequestData { RequestUri = mDeviceUrl, RequestCookies = MyRequestData?.SessionState?.StateCookies }; if (mAddHeader != null) { tRequest.Header = new cdeConcurrentDictionary <string, string>(); foreach (string key in mAddHeader.Keys) { tRequest.Header.TryAdd(key, mAddHeader[key]); } } TheREST.GetRESTAsync(tRequest, ParseDevices, sinkError); } else { MyRequestData.RequestUri = mDeviceUrl; // new Uri("https://ptdevices.com/device/all"); //tPost = $"_token={WebSocketSharp.Ext.UrlEncode(mToken)}"; MyRequestData.PostData = TheCommonUtils.CUTF8String2Array(mDevicePost); MyRequestData.HttpMethod = "POST"; MyRequestData.Header = null; if (mAddHeader != null) { MyRequestData.Header = new cdeConcurrentDictionary <string, string>(); foreach (string key in mAddHeader.Keys) { MyRequestData.Header.TryAdd(key, mAddHeader[key]); } } MyRequestData.RequestCookies = MyRequestData?.SessionState?.StateCookies; MyRequestData.ResponseMimeType = mPostMimeType; // "application/x-www-form-urlencoded; charset=UTF-8"; while (MyRest.IsPosting > 1 && IsConnected) { TheCommonUtils.SleepOneEye(1000, 100); } if (IsConnected) { MyRest.PostRESTAsync(MyRequestData, ParseDevices, sinkError); } } }
public void DoLogin(Uri pLoginUrl, string tPost, string MimeTypeOverride = null, cdeConcurrentDictionary <string, string> paddHeader = null) { if (MyRequestData == null) { MyRequestData = new TheRequestData(); } MyRequestData.PostData = TheCommonUtils.CUTF8String2Array(tPost); MyRequestData.RequestUri = pLoginUrl; MyRequestData.HttpMethod = "POST"; MyRequestData.Header = null; if (paddHeader != null) { MyRequestData.Header = new cdeConcurrentDictionary <string, string>(); foreach (string key in paddHeader.Keys) { MyRequestData.Header.TryAdd(key, paddHeader[key]); } } if (!string.IsNullOrEmpty(MimeTypeOverride)) { MyRequestData.ResponseMimeType = MimeTypeOverride; // "application/x-www-form-urlencoded; charset=UTF-8"; } else { MyRequestData.ResponseMimeType = mPostMimeType; // "application/x-www-form-urlencoded; charset=UTF-8"; } MyRequestData.ResponseBuffer = null; MyRequestData.TimeOut = 15000; MyRequestData.DisableRedirect = true; MyRequestData.RequestCookies = MyRequestData?.SessionState?.StateCookies; MyRest.PostRESTAsync(MyRequestData, LoginSuccess, sinkError); }