|
|
@ -1,13 +1,19 @@
|
|
|
|
using Application.Domain.Entities;
|
|
|
|
using Application.Domain.Entities;
|
|
|
|
using CSScriptLib;
|
|
|
|
using CSScriptLib;
|
|
|
|
using Hangfire;
|
|
|
|
using Hangfire;
|
|
|
|
|
|
|
|
using Infrastructure.Data;
|
|
|
|
using Infrastructure.Events;
|
|
|
|
using Infrastructure.Events;
|
|
|
|
using Infrastructure.Extensions;
|
|
|
|
using Infrastructure.Extensions;
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using System;
|
|
|
|
using System;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
using Vibrant.InfluxDB.Client;
|
|
|
|
|
|
|
|
using Vibrant.InfluxDB.Client.Rows;
|
|
|
|
|
|
|
|
|
|
|
|
namespace IoTCenter.Services
|
|
|
|
namespace IoTCenter.Services
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -172,12 +178,14 @@ namespace IoTCenter.Services
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this.Notify(message);
|
|
|
|
this.Notify(message);
|
|
|
|
this.TiggerHandle(message);
|
|
|
|
this.TiggerHandle(message);
|
|
|
|
|
|
|
|
this.LogData(message.Data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Handle(EntityUpdatedEvent<Data> message)
|
|
|
|
public void Handle(EntityUpdatedEvent<Data> message)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
this.Notify(message);
|
|
|
|
this.Notify(message);
|
|
|
|
this.TiggerHandle(message);
|
|
|
|
this.TiggerHandle(message);
|
|
|
|
|
|
|
|
this.LogData(message.Data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Handle(EntityDeletedEvent<Data> message)
|
|
|
|
public void Handle(EntityDeletedEvent<Data> message)
|
|
|
@ -277,5 +285,65 @@ namespace IoTCenter.Services
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void LogData(Data data)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
using var scope = this._sp.CreateScope();
|
|
|
|
|
|
|
|
var _cfg = scope.ServiceProvider.GetRequiredService<IConfiguration>();
|
|
|
|
|
|
|
|
var url = _cfg["influxdb:url"];
|
|
|
|
|
|
|
|
var usr = _cfg["influxdb:usr"];
|
|
|
|
|
|
|
|
var pwd = _cfg["influxdb:pwd"];
|
|
|
|
|
|
|
|
var deviceRepo = scope.ServiceProvider.GetRequiredService<IRepository<Device>>();
|
|
|
|
|
|
|
|
var device = deviceRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == data.DeviceId);
|
|
|
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(data.Value))
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.Type != DeviceDataType.Int && data.Type != DeviceDataType.Float)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
var dbName = "iot";
|
|
|
|
|
|
|
|
var measurementName = "data";
|
|
|
|
|
|
|
|
using var client = new InfluxClient(new Uri(url), usr, pwd);
|
|
|
|
|
|
|
|
await client.CreateDatabaseAsync(dbName);
|
|
|
|
|
|
|
|
var row = new DynamicInfluxRow
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Timestamp = DateTime.UtcNow
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
row.Fields.Add("DeviceNumber", device.Number);
|
|
|
|
|
|
|
|
row.Fields.Add("DeviceName", device.Name);
|
|
|
|
|
|
|
|
row.Fields.Add(data.Key, this.GetDataValue(data));
|
|
|
|
|
|
|
|
await client.WriteAsync(dbName, measurementName, new List<DynamicInfluxRow> { row });
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private object GetDataValue(Data model)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return model.Type switch
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
DeviceDataType.Int => Convert.ToInt32(model.Value),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DeviceDataType.Float => Convert.ToSingle(model.Value),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_ => model.Value,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|