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.
139 lines
5.0 KiB
139 lines
5.0 KiB
using Infrastructure.Application.Services.Settings;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using IoT.Shared.Services;
|
|
using Application.Domain.Entities;
|
|
using IoT.Shared.Application.Models;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using System.Net.Http;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace IoTNode.DeviceServices
|
|
{
|
|
public class BaseDeviceService : IHostedService
|
|
{
|
|
internal readonly IServiceProvider _applicationServices;
|
|
protected readonly IWebHostEnvironment _env;
|
|
|
|
public BaseDeviceService(IServiceProvider applicationServices, IWebHostEnvironment env)
|
|
{
|
|
this._applicationServices = applicationServices;
|
|
this._env = env;
|
|
}
|
|
|
|
protected string GetSetting(string name)
|
|
{
|
|
using var scope = _applicationServices.CreateScope();
|
|
return scope.ServiceProvider.GetRequiredService<ISettingService>().GetSetting(name).Value;
|
|
}
|
|
|
|
public virtual Task StartAsync(CancellationToken cancellationToken)
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
while (!cancellationToken.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
Execute();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
await Task.Delay(Convert.ToInt32(GetSetting("timer.seconds")) * 1000).ConfigureAwait(true);
|
|
}
|
|
});
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public virtual void Execute()
|
|
{
|
|
}
|
|
|
|
public virtual Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public void SendToServer(string method, object data)
|
|
{
|
|
Debug.WriteLine("send device to server");
|
|
using var scope = _applicationServices.CreateScope();
|
|
var iotNodeClient = scope.ServiceProvider.GetService<IoTNodeClient>();
|
|
iotNodeClient.ClientToServer(method, data, null);
|
|
}
|
|
|
|
public void SendDataToServer(IoTData data)
|
|
{
|
|
using var scope = _applicationServices.CreateScope();
|
|
var iotNodeClient = scope.ServiceProvider.GetService<IoTNodeClient>();
|
|
iotNodeClient.ClientToServer($"Edit{typeof(IoTData).Name}", data.To<EditIoTDataModel>(), null);
|
|
}
|
|
|
|
public void UpdateDevice(IRepository<IoTDevice> repo, IoTDevice device)
|
|
{
|
|
repo.SaveChanges();
|
|
}
|
|
|
|
public void UpdateData(IRepository<IoTDevice> repo, IoTDevice device, IoTData data)
|
|
{
|
|
device.AddorUpdateData(data);
|
|
repo.SaveChanges();
|
|
}
|
|
|
|
public IoTProduct UpdateProduct(string productName, string productNumber, string path, string productIcon)
|
|
{
|
|
var scope = _applicationServices.CreateScope();
|
|
//
|
|
var productRepo = scope.ServiceProvider.GetService<IRepository<IoTProduct>>();
|
|
var product = productRepo.ReadOnlyTable().FirstOrDefault(o => o.Number == productNumber);
|
|
if (product == null)
|
|
{
|
|
product = new IoTProduct
|
|
{
|
|
Id = $"productid-{productNumber}".ToGuid(),
|
|
Number = productNumber,
|
|
Name = productName,
|
|
Path = path,
|
|
Image = $"/images/{productIcon}.svg"
|
|
};
|
|
if (!string.IsNullOrEmpty(path))
|
|
{
|
|
var iotNodeClient = scope.ServiceProvider.GetService<IoTNodeClient>();
|
|
product.ApiJson = iotNodeClient.GetApiJson(path);
|
|
//OpenApiService.UpdateApi(product);
|
|
}
|
|
productRepo.Add(product);
|
|
productRepo.SaveChanges();
|
|
}
|
|
//
|
|
try
|
|
{
|
|
var settingRepo = scope.ServiceProvider.GetRequiredService<ISettingService>();
|
|
var factory = scope.ServiceProvider.GetRequiredService<IHttpClientFactory>();
|
|
var notifyHost = settingRepo.GetValue("notify:host");
|
|
var url = $"{notifyHost}/Server/HasProduct/{productNumber}";
|
|
var httpClient = factory.CreateClient();
|
|
var response = httpClient.GetStringAsync(url).Result.FromJson<bool>();
|
|
if (!response)
|
|
{
|
|
scope.ServiceProvider.GetRequiredService<IoTNodeClient>().ClientToServer($"{nameof(IoTProduct)}EntityUpdated", product, null);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
scope.ServiceProvider.GetRequiredService<ILogger<BaseDeviceService>>().LogError(ex.ToString());
|
|
}
|
|
return product;
|
|
}
|
|
}
|
|
}
|