Skip to content

Commit

Permalink
v1.2.7
Browse files Browse the repository at this point in the history
- Fixes UPnP discovery for local services
- Fixes UPnP DMR control that was not working on some recent smart TVs models
- Fixes UPnP missing ProtocolInfo
- Fixes UPnP device list not refreshing
  • Loading branch information
genemars committed Aug 29, 2023
1 parent 138439c commit 2d30120
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
2 changes: 1 addition & 1 deletion MIG.Protocols/MIG.Protocols.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<ItemGroup>
<PackageReference Include="MIG" Version="1.2.11" />
<PackageReference Include="MIG.HomeAutomation" Version="1.2.19" />
<PackageReference Include="UPnP" Version="1.1.4" />
<PackageReference Include="UPnP" Version="1.1.5" />
</ItemGroup>

</Project>
61 changes: 45 additions & 16 deletions MIG.Protocols/UPnP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class UPnP : MigInterface
#region Private fields

private UpnpSmartControlPoint controlPoint;
private UPnPControlPoint _uPnPControlPoint;
private bool isConnected;
private object deviceOperationLock = new object();
private List<InterfaceModule> modules = new List<InterfaceModule>();
Expand Down Expand Up @@ -134,9 +135,9 @@ public bool Connect()
if (controlPoint == null)
{
controlPoint = new UpnpSmartControlPoint();
controlPoint.OnAddedDevice += controPoint_OnAddedDevice;
controlPoint.OnRemovedDevice += controPoint_OnRemovedDevice;
controlPoint.OnDeviceExpired += controPoint_OnDeviceExpired;
controlPoint.OnAddedDevice += controlPoint_OnAddedDevice;
controlPoint.OnRemovedDevice += controlPoint_OnRemovedDevice;
controlPoint.OnDeviceExpired += controlPoint_OnDeviceExpired;
isConnected = true;
}
OnInterfaceModulesChanged(this.GetDomain());
Expand All @@ -155,9 +156,9 @@ public void Disconnect()
*/
if (controlPoint != null)
{
controlPoint.OnAddedDevice -= controPoint_OnAddedDevice;
controlPoint.OnRemovedDevice -= controPoint_OnRemovedDevice;
controlPoint.OnDeviceExpired -= controPoint_OnDeviceExpired;
controlPoint.OnAddedDevice -= controlPoint_OnAddedDevice;
controlPoint.OnRemovedDevice -= controlPoint_OnRemovedDevice;
controlPoint.OnDeviceExpired -= controlPoint_OnDeviceExpired;
controlPoint.ShutDown();
controlPoint = null;
}
Expand All @@ -171,7 +172,7 @@ public bool IsDevicePresent()

public object InterfaceControl(MigInterfaceCommand request)
{
object returnValue = null;
object returnValue = new ResponseStatus(Status.Ok);
bool raiseEvent = false;
string eventParameter = "Status.Unhandled";
string eventValue = "";
Expand Down Expand Up @@ -348,9 +349,23 @@ public object InterfaceControl(MigInterfaceCommand request)
foreach (var i in root.Elements())
{
string itemId = i.Attribute("id").Value;
string itemTitle = i.Descendants().Where(n => n.Name.LocalName == "title").First().Value;
string itemClass = i.Descendants().Where(n => n.Name.LocalName == "class").First().Value;
jsonres += "{ \"Id\" : \"" + itemId + "\", \"Title\" : \"" + itemTitle.Replace("\"", "\\\"") + "\", \"Class\" : \"" + itemClass + "\" },\n";
string itemTitle = i.Descendants().First(n => n.Name.LocalName == "title").Value;
string itemClass = i.Descendants().First(n => n.Name.LocalName == "class").Value;
var protocolInfo = i.Descendants().FirstOrDefault(n => n.Name.LocalName == "res");
if ((itemClass.StartsWith("object.item.videoItem") || itemClass.StartsWith("object.item.audioItem") || itemClass.StartsWith("object.item.imageItem")) && protocolInfo.Attribute("protocolInfo") != null)
{

string itemProtocolInfo = "";
if (protocolInfo.Attribute("protocolInfo") != null)
{
itemProtocolInfo = protocolInfo.Attribute("protocolInfo").Value;
}
jsonres += "{ \"Id\": \"" + itemId + "\", \"Title\": \"" + itemTitle.Replace("\"", "\\\"") + "\", \"Class\": \"" + itemClass + "\", \"ProtocolInfo\": \"" + itemProtocolInfo + "\" },\n";
}
else
{
jsonres += "{ \"Id\": \"" + itemId + "\", \"Title\": \"" + itemTitle.Replace("\"", "\\\"") + "\", \"Class\": \"" + itemClass + "\" },\n";
}
}
jsonres = jsonres.TrimEnd(',', '\n') + "]";
//
Expand Down Expand Up @@ -706,7 +721,7 @@ private void InvokeUpnpDeviceService(UPnPDevice device, string serviceId, string
}
}

private void controPoint_OnAddedDevice(UpnpSmartControlPoint sender, UPnPDevice device)
private void controlPoint_OnAddedDevice(UpnpSmartControlPoint sender, UPnPDevice device)
{
if (String.IsNullOrWhiteSpace(device.StandardDeviceType))
return;
Expand Down Expand Up @@ -762,7 +777,7 @@ private void controPoint_OnAddedDevice(UpnpSmartControlPoint sender, UPnPDevice
OnInterfaceModulesChanged(this.GetDomain());
}

private void controPoint_OnRemovedDevice(UpnpSmartControlPoint sender, UPnPDevice device)
private void controlPoint_OnRemovedDevice(UpnpSmartControlPoint sender, UPnPDevice device)
{
lock (deviceOperationLock)
{
Expand All @@ -776,7 +791,7 @@ private void controPoint_OnRemovedDevice(UpnpSmartControlPoint sender, UPnPDevic
}
}

private void controPoint_OnDeviceExpired(UpnpSmartControlPoint sender, UPnPDevice device)
private void controlPoint_OnDeviceExpired(UpnpSmartControlPoint sender, UPnPDevice device)
{
lock (deviceOperationLock)
{
Expand Down Expand Up @@ -942,22 +957,32 @@ public sealed class UpnpSmartControlPoint
private WeakEvent OnUpdatedDeviceEvent = new WeakEvent();
private string searchFilter = "upnp:rootdevice";
//"ssdp:all"; //
private System.Timers.Timer searchInterval;

public UpnpSmartControlPoint()
{
deviceFactory.OnDevice += DeviceFactoryCreationSink;
deviceLifeTimeClock.OnExpired += DeviceLifeTimeClockSink;
deviceUpdateClock.OnExpired += DeviceUpdateClockSink;

hostNetworkInfo = new NetworkInfo(NetworkInfoNewInterfaceSink);
hostNetworkInfo.OnInterfaceDisabled += NetworkInfoOldInterfaceSink;

genericControlPoint = new UPnPControlPoint(hostNetworkInfo);
genericControlPoint.OnSearch += UPnPControlPointSearchSink;
genericControlPoint.OnNotify += SSDPNotifySink;
genericControlPoint.FindDeviceAsync(searchFilter);

searchInterval = new System.Timers.Timer();
searchInterval.Interval = 10000;
searchInterval.Elapsed += (sender, args) => Rescan();
searchInterval.Enabled = true;
}

public void ShutDown()
{
searchInterval.Enabled = false;
searchInterval.Dispose();
deviceFactory.OnDevice -= DeviceFactoryCreationSink;
deviceLifeTimeClock.OnExpired -= DeviceLifeTimeClockSink;
deviceUpdateClock.OnExpired -= DeviceUpdateClockSink;
Expand Down Expand Up @@ -1167,16 +1192,20 @@ internal void RemoveMe(UPnPDevice _d)

public void Rescan()
{
lock (deviceTableLock)
Hashtable ht = (Hashtable)this.deviceTable.Clone();
lock (this.deviceTableLock)
{
IDictionaryEnumerator enumerator = deviceTable.GetEnumerator();
IDictionaryEnumerator enumerator = ht.GetEnumerator();
while (enumerator.MoveNext())
{
string key = (string)enumerator.Key;
deviceLifeTimeClock.Add(key, 20);
}
}
genericControlPoint.FindDeviceAsync(searchFilter);
if (genericControlPoint != null)
{
genericControlPoint.FindDeviceAsync(searchFilter);
}
}

internal void SSDPNotifySink(IPEndPoint source, IPEndPoint local, Uri LocationURL, bool IsAlive, string USN, string SearchTarget, int MaxAge, HTTPMessage Packet)
Expand Down

0 comments on commit 2d30120

Please sign in to comment.