mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-08 01:04:56 +00:00
In order to solve some dependency issues that keep cropping up, MC should be moved back into the Essentials repo and loaded automatically on startup. This will allow for all plugins that use the MC Messengers library to use the same version without fear of overwriting a dll due to loading of plugin libraries.
71 lines
2.0 KiB
C#
71 lines
2.0 KiB
C#
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using PepperDash.Core;
|
|
using PepperDash.Essentials.Core.DeviceTypeInterfaces;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Newtonsoft.Json.Converters;
|
|
|
|
namespace PepperDash.Essentials.AppServer.Messengers
|
|
{
|
|
public class ISelectableItemsMessenger<TKey> : MessengerBase
|
|
{
|
|
private static readonly JsonSerializer serializer = new JsonSerializer { Converters = { new StringEnumConverter() } };
|
|
private ISelectableItems<TKey> itemDevice;
|
|
|
|
private readonly string _propName;
|
|
public ISelectableItemsMessenger(string key, string messagePath, ISelectableItems<TKey> device, string propName) : base(key, messagePath, device as Device)
|
|
{
|
|
itemDevice = device;
|
|
_propName = propName;
|
|
}
|
|
|
|
protected override void RegisterActions()
|
|
{
|
|
base.RegisterActions();
|
|
|
|
AddAction("/fullStatus", (id, context) =>
|
|
{
|
|
SendFullStatus();
|
|
});
|
|
|
|
itemDevice.ItemsUpdated += (sender, args) =>
|
|
{
|
|
SendFullStatus();
|
|
};
|
|
|
|
itemDevice.CurrentItemChanged += (sender, args) =>
|
|
{
|
|
SendFullStatus();
|
|
};
|
|
|
|
foreach (var input in itemDevice.Items)
|
|
{
|
|
var key = input.Key;
|
|
var localItem = input.Value;
|
|
|
|
AddAction($"/{key}", (id, content) =>
|
|
{
|
|
localItem.Select();
|
|
});
|
|
|
|
localItem.ItemUpdated += (sender, args) =>
|
|
{
|
|
SendFullStatus();
|
|
};
|
|
}
|
|
}
|
|
|
|
private void SendFullStatus()
|
|
{
|
|
var stateObject = new JObject();
|
|
stateObject[_propName] = JToken.FromObject(itemDevice, serializer);
|
|
PostStatusMessage(stateObject);
|
|
}
|
|
}
|
|
|
|
}
|