Former-commit-id: e73e56696f2679cc67921a34251c8618f5f6b20d
Former-commit-id: 859f235940c0e023d983ed6077d73b316a6d2e78
1.0
wanggang 5 years ago
parent 15aad72282
commit d8448c3dcc

@ -9,7 +9,7 @@ namespace Application.Domain
{
public class Statistic:BaseEntity
{
public string Group { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}
}

@ -66,8 +66,57 @@ namespace IoTNode.DeviceServices
{
return Task.CompletedTask;
}
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();
}
//
if (this.GetSetting("notify:enabled") == "true")
{
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)
{
ex.PrintStack();
scope.ServiceProvider.GetRequiredService<ILogger<BaseDeviceService>>().LogError(ex.ToString());
}
}
return product;
}
//start
public void DeleteDevice(Guid id)
{
using var scope = _applicationServices.CreateScope();
@ -95,13 +144,6 @@ namespace IoTNode.DeviceServices
return repo.ReadOnlyTable().FirstOrDefault(o => o.Number == number);
}
public IoTData GetIoTData(string deviceNumber, DataKeys key)
{
using var scope = _applicationServices.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IRepository<IoTData>>();
var name = key.GetName();
return repo.ReadOnlyTable().FirstOrDefault(o => o.IoTDevice.Number == deviceNumber && o.Key == name);
}
public string GetIoTDataValue(Guid deviceId, DataKeys key)
{
using var scope = _applicationServices.CreateScope();
@ -112,18 +154,6 @@ namespace IoTNode.DeviceServices
.Select(o=>o.Value)
.FirstOrDefault();
}
public string GetIoTDataValue(string deviceNumber, DataKeys key)
{
using var scope = _applicationServices.CreateScope();
var repo = scope.ServiceProvider.GetRequiredService<IRepository<IoTData>>();
var name = key.GetName();
return repo.ReadOnlyTable()
.Where(o => o.IoTDevice.Number == deviceNumber && o.Key == name)
.Select(o=>o.Value)
.FirstOrDefault();
}
public void AddIoTData(Guid deviceId, DataKeys key, object value, string description = "")
{
using var scope = _applicationServices.CreateScope();
@ -158,7 +188,6 @@ namespace IoTNode.DeviceServices
this.UpdateIoTDataValue(data, value);
repo.SaveChanges();
}
//end
private void UpdateIoTDataValue(IoTData data,object value)
{
@ -210,71 +239,5 @@ namespace IoTNode.DeviceServices
};
return data;
}
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();
}
//
if (this.GetSetting("notify:enabled") == "true")
{
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)
{
ex.PrintStack();
scope.ServiceProvider.GetRequiredService<ILogger<BaseDeviceService>>().LogError(ex.ToString());
}
}
return product;
}
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);
}
}
}

@ -21,11 +21,13 @@ namespace IoTNode.Services
IEventHander<EntityInsertedEvent<IoTData>>,
IEventHander<EntityUpdatedEvent<IoTData>>,
IEventHander<EntityDeletedEvent<IoTData>>
{
private readonly ILogger<IoTNodeEventHandler> _logger;
private readonly IHubContext<BasePageHub> _hub;
private readonly OnvifService _onvifService;
private readonly IoTNodeClient _ioTNodeClient;
public IoTNodeEventHandler(ILogger<IoTNodeEventHandler> logger,
@ -59,14 +61,17 @@ namespace IoTNode.Services
this._ioTNodeClient.Connect();
}
}
public void Handle(EntityInsertedEvent<IoTDevice> message)
{
this.Notify(message);
}
public void Handle(EntityUpdatedEvent<IoTDevice> message)
{
this.Notify(message);
}
public void Handle(EntityDeletedEvent<IoTDevice> message)
{
this.Notify(message);
@ -93,7 +98,7 @@ namespace IoTNode.Services
{
try
{
_hub.Clients.Group("page").SendAsync(Methods.ServerToClient, $"{typeof(T).Name}{message.Arg}", message.Data.ToJson(true), null,null);
_hub.Clients.Group("page").SendAsync(Methods.ServerToClient, $"{typeof(T).Name}{message.Arg}", message.Data.ToJson(true), null, null);
}
catch (Exception ex)
{
@ -108,7 +113,7 @@ namespace IoTNode.Services
{
try
{
this._ioTNodeClient.ClientToServer($"{typeof(T).Name}{message.Arg}",message.Data,null);
this._ioTNodeClient.ClientToServer($"{typeof(T).Name}{message.Arg}", message.Data, null);
}
catch (Exception ex)
{

Loading…
Cancel
Save