bool ProcessCDEngine(TheUPnPDeviceInfo tHistory, Uri tLocationUrl) { if ((tLocationUrl.Host.Equals(TheBaseAssets.MyServiceHostInfo.MyStationIP) && tLocationUrl.Port == TheBaseAssets.MyServiceHostInfo.MyStationPort) || !tHistory.PacketString.Contains(TheBaseAssets.MyServiceHostInfo.ApplicationName) || tLocationUrl.Host.Contains("127.0.0.1")) { return(true); } TheBaseAssets.MySYSLOG.WriteToLog(111, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("UPnP", string.Format("C-DEngine Root Device Found: {0}", tHistory), eMsgLevel.l7_HostDebugMessage)); if (!TheBaseAssets.MyServiceHostInfo.DisableUPnPAutoConnect && TheBaseAssets.MyServiceHostInfo.ApplicationName.Equals(tHistory.ModelName)) { if (!string.IsNullOrEmpty(tHistory.CDEContextID)) { if (!string.IsNullOrEmpty(tHistory.CDEProvisionUrl)) { TheBaseAssets.MySettings.SetSetting("AutoProvisioningUrl", tHistory.CDEProvisionUrl); } if (!TheBaseAssets.MyScopeManager.IsScopingEnabled || (TheBaseAssets.MyScopeManager.IsScopingEnabled && !TheBaseAssets.MyScopeManager.ScopeID.Equals(TheBaseAssets.MyScopeManager.GetRealScopeID(tHistory.CDEContextID)))) //GRSI: rare { return(false); } } else { if (TheBaseAssets.MyScopeManager.IsScopingEnabled || !TheBaseAssets.MyServiceHostInfo.AllowUnscopedMesh) { return(false); } } if (!TheCommonUtils.DoesContainLocalhost(tHistory.CDEConnectUrl) && !TheCommonUtils.DoesContainLocalhost(tHistory.CDEConnectWsUrl)) { string strStationRoles = tHistory.FriendlyName; int pos = strStationRoles.LastIndexOf(':') + 1; if (!string.IsNullOrEmpty(strStationRoles) && pos >= 0) { strStationRoles = strStationRoles.Substring(pos, strStationRoles.Length - pos); string[] sRoles = strStationRoles.Split(';'); List <string> discoveredStationRoles = new List <string>(); foreach (string gt in sRoles) { discoveredStationRoles.Add(TheBaseAssets.MyScopeManager.AddScopeID(gt)); } string[] apps = TheCommonUtils.cdeSplit(tHistory.FriendlyName, " at:", false, false); string appName = "Unknown App"; if (apps != null && apps.Length > 0) { appName = apps[0].Trim(); } string tConnectUrl = tHistory.CDEConnectUrl; if (!string.IsNullOrEmpty(tHistory.CDEConnectWsUrl)) { tConnectUrl = tHistory.CDEConnectWsUrl; } TheBaseAssets.MySYSLOG.WriteToLog(112, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("UPnP", $"Compatible Service Found: {tHistory.FriendlyName} {tHistory.Manufacturer} {tHistory.ModelName} {tHistory.ModelNumber} {tHistory.SerialNumber} {tConnectUrl}", eMsgLevel.l7_HostDebugMessage)); eventWSDMatchFound?.Invoke(tHistory.CDENodeID, tConnectUrl, discoveredStationRoles); TheBaseAssets.MySYSLOG.WriteToLog(113, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("UPnP", "New " + appName + " Station found at: " + tConnectUrl, eMsgLevel.l3_ImportantMessage)); } } } return(true); }
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); } } } } }
public TheUPnPDeviceInfo GetUPnPDeviceInfo(Guid USN) { TheUPnPDeviceInfo tInfo = null; if (MyUPnPDiscoveryPast?.MyMirrorCache?.Count > 0) { tInfo = MyUPnPDiscoveryPast.MyMirrorCache.GetEntryByID(USN); } return(tInfo); }
void sinkByeBye(string pUSN) { TheUPnPDeviceInfo tInfo = MyUPnPDiscoveryPast?.MyMirrorCache?.GetEntryByFunc(s => s.USN == pUSN); if (tInfo != null) { sinkDeviceExpired(tInfo); MyUPnPDiscoveryPast.RemoveAnItem(tInfo, null); } }
private static bool CompareUPnPField(TheUPnPDeviceInfo tHistory, string pUPnpField, string pMatch) { bool tSendNow = false; switch (pUPnpField) { case "VendorData": if (!string.IsNullOrEmpty(tHistory.VendorData) && tHistory.VendorData.ToUpper().Contains(pMatch.ToUpper())) { tSendNow = true; } break; case "ModelName": if (!string.IsNullOrEmpty(tHistory.ModelName) && tHistory.ModelName.ToUpper().Contains(pMatch.ToUpper())) { tSendNow = true; } break; case "DeviceType": if (!string.IsNullOrEmpty(tHistory.DeviceName) && tHistory.DeviceName.ToUpper().Contains(pMatch.ToUpper())) { tSendNow = true; } break; case "FriendlyName": if (!string.IsNullOrEmpty(tHistory.FriendlyName) && tHistory.FriendlyName.ToUpper().Contains(pMatch.ToUpper())) { tSendNow = true; } break; case "Manufacturer": if (!string.IsNullOrEmpty(tHistory.Manufacturer) && tHistory.Manufacturer.ToUpper().Contains(pMatch.ToUpper())) { tSendNow = true; } break; } return(tSendNow); }
void sinkRegResult(TheRequestData pData) { if (pData.CookieObject is Action <string, TheUPnPDeviceInfo> tResultCallback) { switch (pData.StatusCode) { case (int)eHttpStatusCode.OK: TheUPnPDeviceInfo tInfo = null; try { if (pData.PostData != null && pData.PostData.Length > 0) { tInfo = TheCommonUtils.DeserializeJSONStringToObject <TheUPnPDeviceInfo>(TheCommonUtils.CArray2UTF8String(pData.ResponseBuffer)); } tResultCallback("OK", tInfo); } catch { tResultCallback("FAILED-No valid TheUPnPDeviceInfo received", null); } break; case (int)eHttpStatusCode.ServerError: tResultCallback("FAILED-Server Not found or has errors", null); break; case (int)eHttpStatusCode.NotAcceptable: tResultCallback("FAILED-Server failed to register device", null); break; default: tResultCallback("FAILED-Unkown error", null); break; } } }
private void StartUPnPDiscovery(string pLiveEngines) { if (MyUPnPDiscoveryPast != null) { TheBaseAssets.MySYSLOG.WriteToLog(109, new TSM("UPnP", "DISCO aready running!")); return; } MyUPnPDiscoveryPast = new TheStorageMirror <TheUPnPDeviceInfo>(Engines.TheCDEngines.MyIStorageService) { IsRAMStore = true, BlockWriteIfIsolated = true }; MyUPnPDiscoveryPast.SetRecordExpiration(DISCOExpireTime, sinkDeviceExpired); //MyUPnPDiscoveryPast.CacheStoreInterval = 60; //MyUPnPDiscoveryPast.IsStoreIntervalInSeconds = true; //MyUPnPDiscoveryPast.IsCachePersistent = true; MyUPnPDiscoveryPast.InitializeStore(true, true); var tCDEConnectUrl = TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false); TheBaseAssets.MySYSLOG.WriteToLog(107, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("UPnP", "Starting Device", eMsgLevel.l3_ImportantMessage)); MyDeviceUPnPInfo = new TheUPnPDeviceInfo() { FriendlyName = TheBaseAssets.MyServiceHostInfo.ApplicationTitle + " at: " + tCDEConnectUrl + " - Services:" + pLiveEngines,// pStationEngines; Manufacturer = TheBaseAssets.MyServiceHostInfo.VendorName, ManufacturerUrl = TheBaseAssets.MyServiceHostInfo.VendorUrl, ModelName = TheBaseAssets.MyServiceHostInfo.ApplicationName, ModelNumber = string.Format("V{0:0.0000}", TheBaseAssets.MyServiceHostInfo.CurrentVersion), SerialNumber = TheBaseAssets.CurrentVersionInfo, PacketString = "", RawMetaXml = "", LocationURL = tCDEConnectUrl, ST = "C-DEngine", CDEConnectUrl = tCDEConnectUrl, CDENodeID = TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID, UUID = TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID.ToString(), USN = TheBaseAssets.MyServiceHostInfo.MyDeviceInfo.DeviceID.ToString(), IsCDEngine = true }; if (!TheBaseAssets.MyServiceHostInfo.RequiresConfiguration) { MyDeviceUPnPInfo.CDEContextID = TheBaseAssets.MyScopeManager.GetScrambledScopeID(); //GRSI: rare } if (TheCommonUtils.IsHostADevice()) { MyDeviceUPnPInfo.HasPresentation = false; } else { if (!TheBaseAssets.MyServiceHostInfo.DisableWebSockets && TheBaseAssets.MyServiceHostInfo.MyStationWSPort > 0) { var builder = new UriBuilder(TheBaseAssets.MyServiceHostInfo.GetPrimaryStationURL(false)); builder.Scheme = builder.Scheme == "https" ? "wss" : "ws"; builder.Port = TheBaseAssets.MyServiceHostInfo.MyStationWSPort; MyDeviceUPnPInfo.CDEConnectWsUrl = TheCommonUtils.TruncTrailingSlash(builder.Uri.ToString()); } MyDeviceUPnPInfo.HasPresentation = true; } eventDiscoReady?.Invoke(true); TheQueuedSenderRegistry.RegisterHealthTimer(DoScan4Devices); TheBaseAssets.MySYSLOG.WriteToLog(108, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("UPnP", "DISCO engine ready...")); }
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 bool RegisterDevice(TheUPnPDeviceInfo pNewDevice, bool ForceUpdate) { TheDiagnostics.SetThreadName("UPnP-Notify", true); if (!TheBaseAssets.MasterSwitch || (TheCommonUtils.cdeIsLocked(LockNotify) && !ForceUpdate) || string.IsNullOrEmpty(pNewDevice?.LocationURL)) { return(false); } lock (LockNotify) { TheUPnPDeviceInfo tHistory = null; //tHistory = MyUPnPDiscoveryPast.MyMirrorCache.GetEntryByFunc((s) => s.LocationURL.Equals(pNewDevice.LocationURL, StringComparison.OrdinalIgnoreCase)); // string[] tUsn = pNewDevice.USN.Split(':'); string tNewUsn = pNewDevice.USN; if (tUsn.Length > 2) { tNewUsn = tUsn[0] + ':' + tUsn[1]; } tHistory = MyUPnPDiscoveryPast.MyMirrorCache.GetEntryByFunc((s) => s.USN.StartsWith(tNewUsn, StringComparison.OrdinalIgnoreCase)); // if (tHistory != null) //gUSN { try { tHistory.SourceIP = IPAddress.TryParse(pNewDevice.LocationURL.Split(':')[0], out IPAddress tIp) ? pNewDevice.LocationURL : new Uri(pNewDevice.LocationURL).Host; tHistory.LocationURL = pNewDevice.LocationURL; if (DateTimeOffset.Now.Subtract(tHistory.LastSeenAt).TotalSeconds < tHistory.MaxAge) { MyUPnPDiscoveryPast.MyMirrorCache.AddOrUpdateItem(tHistory); //gUSN if (tHistory.IsCDEngine) { ProcessCDEngine(tHistory, TheCommonUtils.CUri(tHistory.LocationURL, false)); } else { CheckForDeviceMatch(tHistory, null); } return(true); } tHistory.LastSeenAt = DateTimeOffset.Now; } catch (Exception e) { TheBaseAssets.MySYSLOG.WriteToLog(111, TSM.L(eDEBUG_LEVELS.OFF) ? null : new TSM("UPnP", string.Format("UPnP {0} Invalid Device URL", pNewDevice), eMsgLevel.l1_Error, e.ToString())); return(false); } } if (tHistory == null) { tHistory = pNewDevice; } MyUPnPDiscoveryPast.MyMirrorCache.AddOrUpdateItem(tHistory); //gUSN Uri tLocationUrl; try { tLocationUrl = TheCommonUtils.CUri(tHistory.LocationURL, false); TheBaseAssets.MySYSLOG.WriteToLog(111, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("UPnP", string.Format("UPnP {0} Device Found", tHistory), eMsgLevel.l3_ImportantMessage)); CheckForDeviceMatch(tHistory, tLocationUrl); } catch (Exception eee) { TheBaseAssets.MySYSLOG.WriteToLog(111, TSM.L(eDEBUG_LEVELS.ESSENTIALS) ? null : new TSM("UPnP", string.Format("Non UPnP conform device found: {0}", tHistory), eMsgLevel.l7_HostDebugMessage, eee.ToString())); return(false); } if (tHistory.IsCDEngine) { ProcessCDEngine(tHistory, tLocationUrl); } } return(true); }
public void RegisterDevice(TheUPnPDeviceInfo pNewDevice) { RegisterDevice(pNewDevice, false); }