mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-15 12:44:58 +00:00
Merge pull request #1247 from PepperDash/messenger-fixes
ISelectableItemsMessenger & logging updates
This commit is contained in:
@@ -0,0 +1,101 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using PepperDash.Core;
|
||||||
|
using PepperDash.Core.Logging;
|
||||||
|
using PepperDash.Essentials.Core.DeviceTypeInterfaces;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace PepperDash.Essentials.AppServer.Messengers
|
||||||
|
{
|
||||||
|
public class IHasInputsMessenger<TKey> : MessengerBase
|
||||||
|
{
|
||||||
|
private readonly IHasInputs<TKey> itemDevice;
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs a messenger for a device that implements IHasInputs<typeparamref name="TKey"/>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <param name="messagePath"></param>
|
||||||
|
/// <param name="device"></param>
|
||||||
|
public IHasInputsMessenger(string key, string messagePath, IHasInputs<TKey> device) : base(key, messagePath, device)
|
||||||
|
{
|
||||||
|
itemDevice = device;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void RegisterActions()
|
||||||
|
{
|
||||||
|
base.RegisterActions();
|
||||||
|
|
||||||
|
AddAction("/fullStatus", (id, context) =>
|
||||||
|
{
|
||||||
|
SendFullStatus();
|
||||||
|
});
|
||||||
|
|
||||||
|
itemDevice.Inputs.ItemsUpdated += (sender, args) =>
|
||||||
|
{
|
||||||
|
SendFullStatus();
|
||||||
|
};
|
||||||
|
|
||||||
|
itemDevice.Inputs.CurrentItemChanged += (sender, args) =>
|
||||||
|
{
|
||||||
|
SendFullStatus();
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var input in itemDevice.Inputs.Items)
|
||||||
|
{
|
||||||
|
var key = input.Key;
|
||||||
|
var localItem = input.Value;
|
||||||
|
|
||||||
|
AddAction($"/{key}", (id, content) =>
|
||||||
|
{
|
||||||
|
localItem.Select();
|
||||||
|
});
|
||||||
|
|
||||||
|
localItem.ItemUpdated += (sender, args) =>
|
||||||
|
{
|
||||||
|
SendFullStatus();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SendFullStatus()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
this.LogInformation("Sending full status");
|
||||||
|
|
||||||
|
var stateObject = new IHasInputsStateMessage<TKey>
|
||||||
|
{
|
||||||
|
Inputs = new Inputs<TKey>
|
||||||
|
{
|
||||||
|
Items = itemDevice.Inputs.Items,
|
||||||
|
CurrentItem = itemDevice.Inputs.CurrentItem
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
PostStatusMessage(stateObject);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
this.LogError("Error sending full status: {0}", e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class IHasInputsStateMessage<TKey> : DeviceStateMessageBase
|
||||||
|
{
|
||||||
|
[JsonProperty("inputs")]
|
||||||
|
public Inputs<TKey> Inputs { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Inputs<TKey>
|
||||||
|
{
|
||||||
|
[JsonProperty("items")]
|
||||||
|
public Dictionary<TKey, ISelectableItem> Items { get; set; }
|
||||||
|
|
||||||
|
[JsonProperty("currentItem")]
|
||||||
|
public TKey CurrentItem { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,6 +1,4 @@
|
|||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Converters;
|
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
using PepperDash.Core;
|
using PepperDash.Core;
|
||||||
using PepperDash.Core.Logging;
|
using PepperDash.Core.Logging;
|
||||||
using PepperDash.Essentials.Core.DeviceTypeInterfaces;
|
using PepperDash.Essentials.Core.DeviceTypeInterfaces;
|
||||||
@@ -10,11 +8,18 @@ using System.Collections.Generic;
|
|||||||
namespace PepperDash.Essentials.AppServer.Messengers
|
namespace PepperDash.Essentials.AppServer.Messengers
|
||||||
{
|
{
|
||||||
public class ISelectableItemsMessenger<TKey> : MessengerBase
|
public class ISelectableItemsMessenger<TKey> : MessengerBase
|
||||||
{
|
{
|
||||||
private static readonly JsonSerializer serializer = new JsonSerializer { Converters = { new StringEnumConverter() } };
|
|
||||||
private readonly ISelectableItems<TKey> itemDevice;
|
private readonly ISelectableItems<TKey> itemDevice;
|
||||||
|
|
||||||
private readonly string _propName;
|
private readonly string _propName;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Constructs a messenger for a device that implements ISelectableItems<typeparamref name="TKey"/>
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <param name="messagePath"></param>
|
||||||
|
/// <param name="device"></param>
|
||||||
|
/// <param name="propName"></param>
|
||||||
public ISelectableItemsMessenger(string key, string messagePath, ISelectableItems<TKey> device, string propName) : base(key, messagePath, device as IKeyName)
|
public ISelectableItemsMessenger(string key, string messagePath, ISelectableItems<TKey> device, string propName) : base(key, messagePath, device as IKeyName)
|
||||||
{
|
{
|
||||||
itemDevice = device;
|
itemDevice = device;
|
||||||
|
|||||||
@@ -763,11 +763,10 @@ namespace PepperDash.Essentials
|
|||||||
{
|
{
|
||||||
this.LogVerbose("Adding InputsMessenger<string> for {deviceKey}", device.Key);
|
this.LogVerbose("Adding InputsMessenger<string> for {deviceKey}", device.Key);
|
||||||
|
|
||||||
var messenger = new ISelectableItemsMessenger<string>(
|
var messenger = new IHasInputsMessenger<string>(
|
||||||
$"{device.Key}-inputs-{Key}",
|
$"{device.Key}-inputs-{Key}",
|
||||||
$"/device/{device.Key}",
|
$"/device/{device.Key}",
|
||||||
stringInputs.Inputs,
|
stringInputs
|
||||||
"inputs"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
AddDefaultDeviceMessenger(messenger);
|
AddDefaultDeviceMessenger(messenger);
|
||||||
@@ -779,11 +778,10 @@ namespace PepperDash.Essentials
|
|||||||
{
|
{
|
||||||
this.LogVerbose("Adding InputsMessenger for {deviceKey}", device.Key);
|
this.LogVerbose("Adding InputsMessenger for {deviceKey}", device.Key);
|
||||||
|
|
||||||
var messenger = new ISelectableItemsMessenger<byte>(
|
var messenger = new IHasInputsMessenger<byte>(
|
||||||
$"{device.Key}-inputs-{Key}",
|
$"{device.Key}-inputs-{Key}",
|
||||||
$"/device/{device.Key}",
|
$"/device/{device.Key}",
|
||||||
byteInputs.Inputs,
|
byteInputs
|
||||||
"inputs"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
AddDefaultDeviceMessenger(messenger);
|
AddDefaultDeviceMessenger(messenger);
|
||||||
@@ -795,11 +793,10 @@ namespace PepperDash.Essentials
|
|||||||
{
|
{
|
||||||
this.LogVerbose("Adding InputsMessenger for {deviceKey}", device.Key);
|
this.LogVerbose("Adding InputsMessenger for {deviceKey}", device.Key);
|
||||||
|
|
||||||
var messenger = new ISelectableItemsMessenger<int>(
|
var messenger = new IHasInputsMessenger<int>(
|
||||||
$"{device.Key}-inputs-{Key}",
|
$"{device.Key}-inputs-{Key}",
|
||||||
$"/device/{device.Key}",
|
$"/device/{device.Key}",
|
||||||
intInputs.Inputs,
|
intInputs
|
||||||
"inputs"
|
|
||||||
);
|
);
|
||||||
|
|
||||||
AddDefaultDeviceMessenger(messenger);
|
AddDefaultDeviceMessenger(messenger);
|
||||||
|
|||||||
@@ -113,10 +113,7 @@ namespace PepperDash.Essentials
|
|||||||
|
|
||||||
_server.SendMessageToAllClients(message);
|
_server.SendMessageToAllClients(message);
|
||||||
|
|
||||||
_server.LogVerbose("Message TX To all clients: {message}", null, message);
|
_server.LogVerbose("Message TX To all clients: {message}", message);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (ThreadAbortException)
|
catch (ThreadAbortException)
|
||||||
{
|
{
|
||||||
@@ -126,8 +123,6 @@ namespace PepperDash.Essentials
|
|||||||
{
|
{
|
||||||
Debug.LogMessage(ex, "Caught an exception in the Transmit Processor");
|
Debug.LogMessage(ex, "Caught an exception in the Transmit Processor");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user