Compare commits
No commits in common. "9d55c39e33481bb211a5579a0d1c26c3e28ac048" and "3cdaa2dc3560f8ce1a256197a06d2df4ab4ebb74" have entirely different histories.
9d55c39e33
...
3cdaa2dc35
@ -1,5 +1,5 @@
|
||||
export default {
|
||||
probability_method_name: 'NotifyMessagesAsync',
|
||||
probability_method_name: 'NotifyDeviceAsync',
|
||||
update_method_name: 'NotifyDeviceUpdatedAsync',
|
||||
update_all_method_name: 'NotifyAllUpdatedAsync',
|
||||
};
|
@ -57,9 +57,6 @@ class Dashboard extends Component {
|
||||
componentWillUnmount() {
|
||||
this.context.removeHandler(C.update_all_method_name, this.updateSeries);
|
||||
this.context.removeHandler(C.update_method_name, this.updateSeries);
|
||||
if (this.updateTimer) {
|
||||
clearTimeout(this.updateTimer);
|
||||
}
|
||||
}
|
||||
|
||||
getItemsWithStatus(iterate, status) {
|
||||
@ -128,7 +125,7 @@ class Dashboard extends Component {
|
||||
barDevicePoints[d.id] = Array(3).fill(0);
|
||||
}
|
||||
|
||||
const processHeatmapItem = (items, index) => {
|
||||
const processMethod = (items, index) => {
|
||||
const p = items[index];
|
||||
if (p.date > minuteAgo) {
|
||||
var seconds = Math.floor((p.date.getTime() - minuteAgo.getTime()) / 1000);
|
||||
@ -167,7 +164,7 @@ class Dashboard extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
const onFinished = () => {
|
||||
const finishMethod = () => {
|
||||
const minuteHeatmapSeries = [];
|
||||
|
||||
var i = 0;
|
||||
@ -239,23 +236,20 @@ class Dashboard extends Component {
|
||||
return categories;
|
||||
}
|
||||
|
||||
const toUpdate = [
|
||||
{ heatmapSecondsSeries: minuteHeatmapSeries },
|
||||
{ heatmapMinutesSeries: hourHeatmapSeries },
|
||||
{ barSeries: barSeries },
|
||||
{ barCategories: getBarCategories() },
|
||||
{ lineSeries: lineSeries }
|
||||
];
|
||||
|
||||
//Set states must be done separately otherwise ApexChart's UI update freezes the page.
|
||||
this.performTask(toUpdate, 2, 300, (list, index) => {
|
||||
this.setState(list[index]);
|
||||
},
|
||||
() => {
|
||||
this.updateTimer = setTimeout(this.updateDynamic, 1000);
|
||||
this.setState({
|
||||
heatmapSecondsSeries: minuteHeatmapSeries,
|
||||
heatmapMinutesSeries: hourHeatmapSeries,
|
||||
barSeries: barSeries,
|
||||
barCategories: getBarCategories(),
|
||||
lineSeries: lineSeries,
|
||||
});
|
||||
|
||||
setTimeout(this.updateDynamic, 1000);
|
||||
}
|
||||
|
||||
const processHeatmapItem = processMethod.bind(this);
|
||||
const onFinished = finishMethod.bind(this)
|
||||
|
||||
this.performTask(this.context.heatmapPoints, Math.ceil(this.context.heatmapPoints.length / 50), 20,
|
||||
processHeatmapItem, onFinished);
|
||||
}
|
||||
|
@ -8,21 +8,6 @@ export class HeatmapChart extends Component {
|
||||
|
||||
this.state = {
|
||||
options: {
|
||||
chart: {
|
||||
animations: {
|
||||
enabled: true,
|
||||
easing: 'linear',
|
||||
speed: 250,
|
||||
animateGradually: {
|
||||
enabled: false,
|
||||
speed: 250,
|
||||
},
|
||||
dynamicAnimation: {
|
||||
enabled: true,
|
||||
speed: 250
|
||||
}
|
||||
}
|
||||
},
|
||||
dataLabels: {
|
||||
enabled: false
|
||||
},
|
||||
|
@ -20,18 +20,11 @@ export default class MapContainer extends Component {
|
||||
};
|
||||
|
||||
this.probabilityHandler = this.probabilityHandler.bind(this);
|
||||
this.handlePoint = this.handlePoint.bind(this);
|
||||
}
|
||||
|
||||
static contextType = DevicesContext;
|
||||
|
||||
probabilityHandler(points) {
|
||||
for (var point of points) {
|
||||
this.handlePoint(point);
|
||||
}
|
||||
}
|
||||
|
||||
handlePoint(point) {
|
||||
probabilityHandler(point) {
|
||||
if (point.prob > 0.5) {
|
||||
|
||||
this.setState({
|
||||
|
@ -99,19 +99,15 @@ export default class DevicesContextProvider extends Component {
|
||||
.then(_ => {
|
||||
console.log('Devices hub Connected!');
|
||||
|
||||
newConnection.on(C.probability_method_name, (messages) => {
|
||||
newConnection.on(C.probability_method_name, (id, date, prob) => {
|
||||
//console.log(method_name + " recieved: [id: " + id + ", date: " + date + ", prob: " + prob + "]");
|
||||
const newPoints = [];
|
||||
for (var message of messages) {
|
||||
var device = this.state.devices.filter(function (x) { return x.id === message.deviceId })[0]
|
||||
var newPoint = { deviceId: device.id, lat: device.coordinates.latitude, lng: device.coordinates.longitude, prob: message.probability, date: new Date(message.date) };
|
||||
newPoints.push(newPoint);
|
||||
}
|
||||
var device = this.state.devices.filter(function (x) { return x.id === id })[0]
|
||||
var newPoint = { deviceId: device.id, lat: device.coordinates.latitude, lng: device.coordinates.longitude, prob: prob, date: new Date(date) };
|
||||
this.setState({
|
||||
heatmapPoints: this.state.heatmapPoints.concat(newPoints)
|
||||
heatmapPoints: [...this.state.heatmapPoints, newPoint]
|
||||
});
|
||||
|
||||
this.invokeHandlers(C.probability_method_name, newPoints);
|
||||
this.invokeHandlers(C.probability_method_name, newPoint);
|
||||
});
|
||||
|
||||
newConnection.on(C.update_all_method_name, () => {
|
||||
|
@ -1,14 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Birdmap.API.Services
|
||||
{
|
||||
public record Message(Guid DeviceId, DateTime Date, double Probability);
|
||||
|
||||
public interface IDevicesHubClient
|
||||
{
|
||||
Task NotifyMessagesAsync(IEnumerable<Message> messages);
|
||||
Task NotifyDeviceAsync(Guid deviceId, DateTime date, double probability);
|
||||
Task NotifyDeviceUpdatedAsync(Guid deviceId);
|
||||
Task NotifyAllUpdatedAsync();
|
||||
}
|
||||
|
@ -9,11 +9,9 @@ using MQTTnet.Client.Disconnecting;
|
||||
using MQTTnet.Client.Options;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace Birdmap.API.Services.Mqtt
|
||||
{
|
||||
@ -24,9 +22,6 @@ namespace Birdmap.API.Services.Mqtt
|
||||
private readonly ILogger<MqttClientService> _logger;
|
||||
private readonly IInputService _inputService;
|
||||
private readonly IHubContext<DevicesHub, IDevicesHubClient> _hubContext;
|
||||
private readonly Timer _hubTimer;
|
||||
private readonly List<Message> _messages = new();
|
||||
private readonly object _messageLock = new();
|
||||
|
||||
public bool IsConnected => _mqttClient.IsConnected;
|
||||
|
||||
@ -36,30 +31,10 @@ namespace Birdmap.API.Services.Mqtt
|
||||
_logger = logger;
|
||||
_inputService = inputService;
|
||||
_hubContext = hubContext;
|
||||
_hubTimer = new Timer()
|
||||
{
|
||||
AutoReset = true,
|
||||
Interval = 1000,
|
||||
};
|
||||
_hubTimer.Elapsed += SendMqttMessagesWithSignalR;
|
||||
|
||||
_mqttClient = new MqttFactory().CreateMqttClient();
|
||||
ConfigureMqttClient();
|
||||
}
|
||||
|
||||
private void SendMqttMessagesWithSignalR(object sender, System.Timers.ElapsedEventArgs e)
|
||||
{
|
||||
lock (_messageLock)
|
||||
{
|
||||
if (_messages.Any())
|
||||
{
|
||||
_logger.LogInformation($"Sending ({_messages.Count}) messages: {string.Join(" | ", _messages)}");
|
||||
_hubContext.Clients.All.NotifyMessagesAsync(_messages);
|
||||
_messages.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void ConfigureMqttClient()
|
||||
{
|
||||
_mqttClient.ConnectedHandler = this;
|
||||
@ -80,7 +55,7 @@ namespace Birdmap.API.Services.Mqtt
|
||||
{
|
||||
var message = eventArgs.ApplicationMessage.ConvertPayloadToString();
|
||||
|
||||
_logger.LogDebug($"Recieved [{eventArgs.ClientId}] " +
|
||||
_logger.LogInformation($"Recieved [{eventArgs.ClientId}] " +
|
||||
$"Topic: {eventArgs.ApplicationMessage.Topic} | Payload: {message} | QoS: {eventArgs.ApplicationMessage.QualityOfServiceLevel} | Retain: {eventArgs.ApplicationMessage.Retain}");
|
||||
|
||||
try
|
||||
@ -88,10 +63,7 @@ namespace Birdmap.API.Services.Mqtt
|
||||
var payload = JsonConvert.DeserializeObject<Payload>(message);
|
||||
var inputResponse = await _inputService.GetInputAsync(payload.TagID);
|
||||
|
||||
lock (_messageLock)
|
||||
{
|
||||
_messages.Add(new Message(inputResponse.Message.Device_id, inputResponse.Message.Date.UtcDateTime, payload.Probability));
|
||||
}
|
||||
await _hubContext.Clients.All.NotifyDeviceAsync(inputResponse.Message.Device_id, inputResponse.Message.Date.UtcDateTime, payload.Probability);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -107,7 +79,6 @@ namespace Birdmap.API.Services.Mqtt
|
||||
_logger.LogInformation($"Connected. Auth result: {eventArgs.AuthenticateResult}. Subscribing to topic: {topic}");
|
||||
|
||||
await _mqttClient.SubscribeAsync(topic);
|
||||
_hubTimer.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -123,7 +94,6 @@ namespace Birdmap.API.Services.Mqtt
|
||||
|
||||
try
|
||||
{
|
||||
_hubTimer.Stop();
|
||||
await _mqttClient.ConnectAsync(_options, CancellationToken.None);
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
Loading…
Reference in New Issue
Block a user