2020-11-08 18:51:19 +01:00
|
|
|
|
using Birdmap.BLL.Helpers;
|
|
|
|
|
using Birdmap.BLL.Interfaces;
|
2020-11-07 14:19:29 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Birdmap.BLL.Services
|
|
|
|
|
{
|
2020-11-08 18:51:19 +01:00
|
|
|
|
public class DummyDeviceAndInputService : DeviceAndInputServiceBase
|
2020-11-07 14:19:29 +01:00
|
|
|
|
{
|
2020-11-17 21:51:53 +01:00
|
|
|
|
private const int numberOfDevices = 15;
|
2020-11-08 18:51:19 +01:00
|
|
|
|
|
2020-11-07 14:19:29 +01:00
|
|
|
|
private const double centerLong = 21.469640;
|
|
|
|
|
private const double centerLat = 48.275939;
|
2020-11-11 16:55:50 +01:00
|
|
|
|
private const double radius = 0.001;
|
2020-11-07 14:19:29 +01:00
|
|
|
|
|
2020-11-19 20:43:01 +01:00
|
|
|
|
private static readonly Random Rand = new();
|
2020-11-08 18:51:19 +01:00
|
|
|
|
|
2020-11-19 20:43:01 +01:00
|
|
|
|
private static readonly Lazy<ICollection<Device>> Devices = new(GenerateDevices);
|
2020-11-08 18:51:19 +01:00
|
|
|
|
|
2020-11-19 20:43:01 +01:00
|
|
|
|
private static readonly Dictionary<Guid, InputSingeResponse> TagToInput = new();
|
|
|
|
|
private static readonly object InputLock = new();
|
2020-11-08 18:51:19 +01:00
|
|
|
|
|
2020-11-07 14:19:29 +01:00
|
|
|
|
private static ListOfDevices GenerateDevices()
|
|
|
|
|
{
|
|
|
|
|
var devices = new ListOfDevices();
|
|
|
|
|
|
|
|
|
|
T GetRandomEnum<T>()
|
|
|
|
|
{
|
|
|
|
|
var values = Enum.GetValues(typeof(T));
|
2020-11-08 18:51:19 +01:00
|
|
|
|
return (T)values.GetValue(Rand.Next(values.Length));
|
2020-11-07 14:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double GetPlusMinus(double center, double radius)
|
|
|
|
|
{
|
2020-11-08 18:51:19 +01:00
|
|
|
|
return center - radius + Rand.NextDouble() * radius * 2;
|
2020-11-07 14:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 18:51:19 +01:00
|
|
|
|
for (int d = 0; d < numberOfDevices; d++)
|
2020-11-07 14:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
var sensors = new ArrayofSensors();
|
2020-11-08 18:51:19 +01:00
|
|
|
|
for (int s = 0; s < Rand.Next(1, 6); s++)
|
2020-11-07 14:19:29 +01:00
|
|
|
|
{
|
2020-11-19 20:43:01 +01:00
|
|
|
|
sensors.Add(new()
|
2020-11-07 14:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
Status = GetRandomEnum<SensorStatus>(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 20:43:01 +01:00
|
|
|
|
devices.Add(new()
|
2020-11-07 14:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
Sensors = sensors,
|
|
|
|
|
Status = GetRandomEnum<DeviceStatus>(),
|
|
|
|
|
Url = "dummyservice.device.url",
|
2020-11-19 20:43:01 +01:00
|
|
|
|
Coordinates = new()
|
2020-11-07 14:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
Latitude = GetPlusMinus(centerLat, radius),
|
|
|
|
|
Longitude = GetPlusMinus(centerLong, radius),
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return devices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task<ICollection<Device>> GetallAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2020-11-08 18:51:19 +01:00
|
|
|
|
return Task.FromResult(Devices.Value);
|
2020-11-07 14:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task<Device> GetdeviceAsync(Guid deviceID, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2020-11-08 18:51:19 +01:00
|
|
|
|
return Task.FromResult(Devices.Value.SingleOrDefault(d => d.Id == deviceID));
|
2020-11-07 14:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task<Sensor> GetsensorAsync(Guid deviceID, Guid sensorID, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2020-11-08 18:51:19 +01:00
|
|
|
|
return Task.FromResult(Devices.Value.SingleOrDefault(d => d.Id == deviceID)?.Sensors.SingleOrDefault(s => s.Id == sensorID));
|
2020-11-07 14:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task OfflineallAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
SetStatus(DeviceStatus.Offline, SensorStatus.Offline);
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task OfflinedeviceAsync(Guid deviceID, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
SetDeviceStatus(deviceID, DeviceStatus.Offline);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task OfflinesensorAsync(Guid deviceID, Guid sensorID, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
SetSensorStatus(deviceID, sensorID, SensorStatus.Offline);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task OnlineallAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
SetStatus(DeviceStatus.Online, SensorStatus.Online);
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task OnlinedeviceAsync(Guid deviceID, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
SetDeviceStatus(deviceID, DeviceStatus.Online);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override Task OnlinesensorAsync(Guid deviceID, Guid sensorID, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
SetSensorStatus(deviceID, sensorID, SensorStatus.Online);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetStatus(DeviceStatus deviceStatus, SensorStatus sensorStatus)
|
|
|
|
|
{
|
2020-11-08 18:51:19 +01:00
|
|
|
|
foreach (var device in Devices.Value)
|
2020-11-07 14:19:29 +01:00
|
|
|
|
{
|
|
|
|
|
device.Status = deviceStatus;
|
|
|
|
|
foreach (var sensor in device.Sensors)
|
|
|
|
|
{
|
|
|
|
|
sensor.Status = sensorStatus;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetDeviceStatus(Guid deviceID, DeviceStatus status)
|
|
|
|
|
{
|
|
|
|
|
var device = GetdeviceAsync(deviceID).Result;
|
|
|
|
|
device.Status = status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetSensorStatus(Guid deviceId, Guid sensorID, SensorStatus status)
|
|
|
|
|
{
|
|
|
|
|
var sensor = GetsensorAsync(deviceId, sensorID).Result;
|
|
|
|
|
sensor.Status = status;
|
|
|
|
|
}
|
2020-11-08 18:51:19 +01:00
|
|
|
|
|
|
|
|
|
public override Task<InputSingeResponse> GetInputAsync(Guid tagID, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
lock (InputLock)
|
|
|
|
|
{
|
|
|
|
|
if (!TagToInput.TryGetValue(tagID, out var value))
|
|
|
|
|
{
|
2020-11-19 20:43:01 +01:00
|
|
|
|
value = new()
|
2020-11-08 18:51:19 +01:00
|
|
|
|
{
|
|
|
|
|
Status = "Dummy_OK",
|
2020-11-19 20:43:01 +01:00
|
|
|
|
Message = new()
|
2020-11-08 18:51:19 +01:00
|
|
|
|
{
|
|
|
|
|
Tag = tagID,
|
|
|
|
|
Date = DateTime.Now,
|
|
|
|
|
Device_id = Devices.Value.Where(d => d.Status == DeviceStatus.Online).RandomElementAt(Rand).Id,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
TagToInput.TryAdd(tagID, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-07 14:19:29 +01:00
|
|
|
|
}
|
|
|
|
|
}
|