2021-02-21 13:04:24 +00:00
|
|
|
|
using System;
|
2021-02-24 08:05:11 +00:00
|
|
|
|
using System.Threading;
|
2021-02-21 13:04:24 +00:00
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using MassiveKnob.Plugin.MockDevice.Settings;
|
|
|
|
|
|
|
|
|
|
namespace MassiveKnob.Plugin.MockDevice.Devices
|
|
|
|
|
{
|
|
|
|
|
public class MockDevice : IMassiveKnobDevice
|
|
|
|
|
{
|
|
|
|
|
public Guid DeviceId { get; } = new Guid("e1a4977a-abf4-4c75-a17d-fd8d3a8451ff");
|
|
|
|
|
public string Name { get; } = "Mock device";
|
|
|
|
|
public string Description { get; } = "Emulates the actual device but does not communicate with anything.";
|
2021-02-24 08:05:11 +00:00
|
|
|
|
|
|
|
|
|
public IMassiveKnobDeviceInstance Create()
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
return new Instance();
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class Instance : IMassiveKnobDeviceInstance
|
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
private IMassiveKnobDeviceContext deviceContext;
|
|
|
|
|
private MockDeviceSettings settings;
|
|
|
|
|
private Timer inputChangeTimer;
|
|
|
|
|
|
|
|
|
|
private int reportedAnalogInputCount;
|
|
|
|
|
private int reportedDigitalInputCount;
|
|
|
|
|
private readonly Random random = new Random();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Initialize(IMassiveKnobDeviceContext context)
|
2021-02-21 13:04:24 +00:00
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
deviceContext = context;
|
|
|
|
|
settings = deviceContext.GetSettings<MockDeviceSettings>();
|
|
|
|
|
|
|
|
|
|
ApplySettings();
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
inputChangeTimer?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ApplySettings()
|
|
|
|
|
{
|
|
|
|
|
if (settings.AnalogCount != reportedAnalogInputCount ||
|
|
|
|
|
settings.DigitalCount != reportedDigitalInputCount)
|
|
|
|
|
{
|
|
|
|
|
deviceContext.Connected(new DeviceSpecs(settings.AnalogCount, settings.DigitalCount, 0, 0));
|
|
|
|
|
|
|
|
|
|
reportedAnalogInputCount = settings.AnalogCount;
|
|
|
|
|
reportedDigitalInputCount = settings.DigitalCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var interval = TimeSpan.FromSeconds(Math.Max(settings.Interval, 1));
|
|
|
|
|
|
|
|
|
|
if (inputChangeTimer == null)
|
|
|
|
|
inputChangeTimer = new Timer(Tick, null, interval, interval);
|
|
|
|
|
else
|
|
|
|
|
inputChangeTimer.Change(interval, interval);
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public UserControl CreateSettingsControl()
|
|
|
|
|
{
|
2021-02-24 08:05:11 +00:00
|
|
|
|
var viewModel = new MockDeviceSettingsViewModel(settings);
|
|
|
|
|
viewModel.PropertyChanged += (sender, args) =>
|
|
|
|
|
{
|
|
|
|
|
deviceContext.SetSettings(settings);
|
|
|
|
|
ApplySettings();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new MockDeviceSettingsView(viewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void Tick(object state)
|
|
|
|
|
{
|
|
|
|
|
var totalInputCount = reportedAnalogInputCount + reportedDigitalInputCount;
|
|
|
|
|
if (totalInputCount == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var changeInput = random.Next(0, totalInputCount);
|
|
|
|
|
|
|
|
|
|
if (changeInput < reportedAnalogInputCount)
|
|
|
|
|
deviceContext.AnalogChanged(changeInput, (byte)random.Next(0, 101));
|
|
|
|
|
else
|
|
|
|
|
deviceContext.DigitalChanged(changeInput - reportedAnalogInputCount, random.Next(0, 2) == 1);
|
2021-02-21 13:04:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|