|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|