You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
134 lines
5.0 KiB
134 lines
5.0 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Events;
|
|
using Infrastructure.Extensions;
|
|
using IoT.Shared.Services;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IoTNode.DeviceServices
|
|
{
|
|
public abstract class BaseDeviceService : IHostedService
|
|
{
|
|
internal readonly IServiceProvider _applicationServices;
|
|
internal readonly IConfiguration _cfg;
|
|
|
|
public BaseDeviceService(IServiceProvider applicationServices, IConfiguration configuration)
|
|
{
|
|
this._applicationServices = applicationServices;
|
|
this._cfg = configuration;
|
|
}
|
|
|
|
public virtual Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
Execute();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
await Task.Delay(_cfg.GetValue("timer.seconds", 60) * 1000).ConfigureAwait(true);
|
|
}
|
|
});
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public abstract void Execute();
|
|
|
|
public virtual Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void SendToServer(string method, object data)
|
|
{
|
|
Console.WriteLine("send device to server");
|
|
using var scope = _applicationServices.CreateScope();
|
|
var iotNodeClient = scope.ServiceProvider.GetService<IoTNodeClient>();
|
|
iotNodeClient.ClientToServer(method, data, null);
|
|
}
|
|
|
|
public void SendDataToServer(Data data)
|
|
{
|
|
using var scope = _applicationServices.CreateScope();
|
|
var iotNodeClient = scope.ServiceProvider.GetService<IoTNodeClient>();
|
|
iotNodeClient.ClientToServer($"Edit{typeof(Data).Name}", data.To<EditDataModel>(), null);
|
|
var eventPubliser = scope.ServiceProvider.GetService<IEventPublisher>();
|
|
eventPubliser.Publish(new EntityUpdatedEvent<Data>(data));
|
|
}
|
|
|
|
public void UpdateDevice(IRepository<Device> repo, Device device)
|
|
{
|
|
if (repo.SaveChanges() > 0)
|
|
{
|
|
this.SendToServer(Methods.EditDevice, device.To<EditDeviceModel>());
|
|
}
|
|
}
|
|
|
|
public void UpdateData(IRepository<Device> repo, Device device, Data data)
|
|
{
|
|
var data2 = device.AddorUpdateData(data);
|
|
if (repo.SaveChanges() > 0 && !data2.Hidden)
|
|
{
|
|
this.SendDataToServer(data2);
|
|
}
|
|
}
|
|
|
|
//public void SendDataToServer(List<Data> list)
|
|
//{
|
|
// using var scope = _applicationServices.CreateScope();
|
|
// var iotNodeClient = scope.ServiceProvider.GetService<IoTNodeClient>();
|
|
// iotNodeClient.ClientToServer($"Edit{typeof(Data).Name}", list, null);
|
|
// var eventPubliser = scope.ServiceProvider.GetService<IEventPublisher>();
|
|
// foreach (var data in list)
|
|
// {
|
|
// eventPubliser.Publish(new EntityUpdatedEvent<Data>(data));
|
|
// }
|
|
//}
|
|
|
|
public Product UpdateProduct(string productName, string productNumber, string path, string categoryNumber, string productIcon)
|
|
{
|
|
var scope = _applicationServices.CreateScope();
|
|
|
|
var categoryRepo = scope.ServiceProvider.GetService<IRepository<Category>>();
|
|
var productRepo = scope.ServiceProvider.GetService<IRepository<Product>>();
|
|
var product = productRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == productNumber);
|
|
if (product == null)
|
|
{
|
|
product = new Product
|
|
{
|
|
Id = productNumber.ToGuid(),
|
|
Number = productNumber,
|
|
Name = productName,
|
|
Path = path,
|
|
Image = $"/images/{productIcon}.png",
|
|
CategoryId = categoryRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == categoryNumber).Id
|
|
};
|
|
if (!string.IsNullOrEmpty(path))
|
|
{
|
|
var iotNodeClient = scope.ServiceProvider.GetService<IoTNodeClient>();
|
|
product.ApiJson = iotNodeClient.GetApiJson(path);
|
|
}
|
|
OpenApiService.UpdateApi(product);
|
|
productRepo.Add(product);
|
|
productRepo.SaveChanges();
|
|
this.SendToServer(Methods.UpdateProductResponse, product);
|
|
}
|
|
return product;
|
|
}
|
|
}
|
|
} |