Former-commit-id: 4903b725c5f21ebbc760a43d4e7b8619dc2352cf
TangShanKaiPing
wanggang 6 years ago
parent 228d266993
commit 9d95112d06

@ -9,6 +9,7 @@
public const string HealthCheckResponse = nameof(HealthCheckResponse);
public const string CallApi = nameof(CallApi);
public const string CallScene = nameof(CallScene);
public const string ApiCallback = nameof(ApiCallback);
public const string GetProductRequest = nameof(GetProductRequest);

@ -98,14 +98,37 @@ namespace IoT.Shared.Infrastructure
}
else if (method == Methods.CallApi)
{
var serviceProvider = scope.ServiceProvider;
var cfg = serviceProvider.GetService<IConfiguration>();
var cfg = scope.ServiceProvider.GetService<IConfiguration>();
var port = cfg["server.urls"].Split(':')[2];
var url = $"http://localhost:{port}{message}";
var httpClient = serviceProvider.GetService<IHttpClientFactory>().CreateClient();
var httpClient = scope.ServiceProvider.GetService<IHttpClientFactory>().CreateClient();
var result = httpClient.GetStringAsync(url).Result;
this.Connection.SendAsync(Methods.ClientToServer, Methods.ApiCallback, result, cfg["connectId"]);
}
else if (method == Methods.CallScene)
{
var commandRepo = scope.ServiceProvider.GetService<IRepository<Command>>();
var commands = commandRepo.ReadOnlyTable()
.Include(o => o.Api)
.Include(o => o.Device)
.Where(o => o.Scene.Name == message)
.ToList();
foreach (var command in commands)
{
try
{
var cfg = scope.ServiceProvider.GetService<IConfiguration>();
var port = cfg["server.urls"].Split(':')[2];
var url = $"http://localhost:{port}{command.Api.Path}{command.Api.Command}?number={command.Device.Number}{(string.IsNullOrEmpty(command.QueryString)?"":"&")}{command.QueryString}";
var httpClient = scope.ServiceProvider.GetService<IHttpClientFactory>().CreateClient();
var result = httpClient.GetStringAsync(url).Result;
}
catch (Exception ex)
{
ex.PrintStack();
}
}
}
else if (method == Methods.EditNodeRequest)
{
var model = message.FromJson<EditNodeModel>();

@ -117,6 +117,8 @@ namespace IoTCenter.Areas.Admin.Controllers
{
model.NodeId = entity.Scene.NodeId;
model.DeviceId = entity.DeviceId;
model.NodeNumber = entity.Scene.Node.Number;
model.DeviceNumber = entity.Device.Number;
}
ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.NodeId));
ViewData.SelectList(o => model.SceneId, () => this._ajax.GetSceneSelectList(model.NodeId.Value, model.SceneId), model.NodeId.HasValue);

@ -25,6 +25,7 @@ namespace IoTCenter.Controllers
private readonly IConfiguration _configuration;
private readonly IJwtHelper _jwtHelper;
private readonly IRepository<Node> _nodeRepo;
private readonly IRepository<Scene> _sceneRepo;
private readonly IRepository<Device> _deviceRepo;
private readonly IRepository<LiveRecord> _liveRecordRepo;
private readonly IHubContext<PageHub> _pageHubContext;
@ -32,6 +33,7 @@ namespace IoTCenter.Controllers
public AppController(IConfiguration configuration,
IJwtHelper jwtHelper,
IRepository<Node> nodeRepo,
IRepository<Scene> sceneRepo,
IRepository<Device> deviceRepo,
IRepository<LiveRecord> liveRecordRepo,
IHubContext<PageHub> pageHubContext)
@ -39,6 +41,7 @@ namespace IoTCenter.Controllers
this._configuration = configuration;
this._jwtHelper = jwtHelper;
this._nodeRepo = nodeRepo;
this._sceneRepo = sceneRepo;
this._deviceRepo = deviceRepo;
this._liveRecordRepo = liveRecordRepo;
this._pageHubContext = pageHubContext;
@ -133,11 +136,12 @@ namespace IoTCenter.Controllers
}
}
public IActionResult Sence(string connectionId, string node, Guid id)
public IActionResult Scene(string connectionId, string node, string name)
{
try
{
this._pageHubContext.Clients.Group(node).SendAsync(nameof(INodeService.Scene), connectionId, id);
var scene = this._sceneRepo.ReadOnlyTable().FirstOrDefault(o => o.Node.Number == node && o.Name == name);
this._pageHubContext.Clients.Group(node).SendAsync(Methods.ServerToClient, Methods.CallScene, name, null);
return Json(ApiResponse.AsyncSuccess());
}
catch (Exception ex)
@ -240,6 +244,18 @@ namespace IoTCenter.Controllers
return Json(ApiResponse.AsyncSuccess());
}
public IActionResult NodePowerOn(string connectionId, string node)
{
this.Power(connectionId, new string[] { node }, "On", o => o.Name.Contains("开关") || o.Name.Contains("插座"));
return Json(ApiResponse.AsyncSuccess());
}
public IActionResult NodePowerOff(string connectionId, string node)
{
this.Power(connectionId, new string[] { node }, "Off", o => o.Name.Contains("开关") || o.Name.Contains("插座"));
return Json(ApiResponse.AsyncSuccess());
}
private void Power(string connectionId, string[] nodes, string command, Func<Device, bool> func)
{
var devices = this._deviceRepo.ReadOnlyTable()
@ -250,9 +266,16 @@ namespace IoTCenter.Controllers
.ToList();
foreach (var device in devices)
{
var api = device.Product.Apis.FirstOrDefault(o => o.Command == command);
var message = $"{api.Path}{api.Command}?number={device.Number}";
this._pageHubContext.Clients.Group(device.Node.Number).SendAsync(Methods.ServerToClient, Methods.CallApi, message, connectionId);
try
{
var api = device.Product.Apis.FirstOrDefault(o => o.Command == command);
var message = $"{api.Path}{api.Command}?number={device.Number}";
this._pageHubContext.Clients.Group(device.Node.Number).SendAsync(Methods.ServerToClient, Methods.CallApi, message, connectionId);
}
catch (Exception ex)
{
ex.PrintStack();
}
}
}
}

@ -75,17 +75,17 @@
<div class="box-footer">
<div class="row">
<div class="col-md-6">
<button class="btn btn-block btn-primary ajax nodepoweron">一键开</button>
<button class="btn btn-block btn-primary ajax NodePowerOn" :data-node-number="node.Number">一键开</button>
</div>
<div class="col-md-6">
<button class="btn btn-block btn-primary ajax nodepoweroff">一键关</button>
<button class="btn btn-block btn-primary ajax NodePowerOff" :data-node-number="node.Number">一键关</button>
</div>
</div>
<hr />
<div class="row" v-if="node.Scenes.length">
<template v-for="scene in node.Scenes">
<div class="col-md-3" v-if="scene.Tag=='home'">
<button class="btn btn-block btn-info ajax" :data-node-number="node.Number" :data-scene-name="scene.Name">{{scene.Name}}</button>
<button class="btn btn-block btn-info ajax Scene" :data-node-number="node.Number" :data-scene-name="scene.Name">{{scene.Name}}</button>
</div>
</template>
</div>
@ -214,8 +214,13 @@
ajax('/App/AllSocketOn', { nodes: data }, 'post');
} else if ($(this).hasClass('AllSocketOff')) {
ajax('/App/AllSocketOff', { nodes: data }, 'post');
} else if ($(this).hasClass('NodePowerOn')) {
ajax('/App/NodePowerOn', { node: $(this).attr('data-node-number') }, 'post');
} else if ($(this).hasClass('NodePowerOff')) {
ajax('/App/NodePowerOff', { node: $(this).attr('data-node-number') }, 'post');
} else if ($(this).hasClass('Scene')) {
ajax('/App/Scene', { node: $(this).attr('data-node-number'), name: $(this).attr('data-scene-name') }, 'post');
}
return false;
});
</script>

Loading…
Cancel
Save