Former-commit-id: a37be4468c75101b42fc6883efd2b83a950f2511
TangShanKaiPing
wanggang 6 years ago
parent 2566932874
commit 8c0186279a

@ -13,7 +13,7 @@ $.validator.unobtrusive.adapters.addBool("radio", "required");
function updateSelectList(url, id) {
$.getJSON(url, function (data) {
$.each(data, function (i, v) {
$(id).append('<option value="' + v.Value + '">' + v.Text + '</option>');
$(id).append('<option value="' + v.value + '">' + v.text + '</option>');
});
});
}
@ -303,7 +303,7 @@ function update(url, parent, targetId) {
url += '?parentId=' + encodeURI(parent);
$.getJSON(url, function (data) {
$.each(data, function (i, v) {
$(targetId).append('<option value="' + v.Value + '">' + v.Text + '</option>');
$(targetId).append('<option value="' + v.value + '">' + v.text + '</option>');
});
});
}

@ -0,0 +1,18 @@
using System.ComponentModel.DataAnnotations;
namespace Application.Models
{
public class ApiRequestModel
{
[Required]
public string ConnectionId { get; set; }
[Required]
public string Number { get; set; }
[Required]
public string Method { get; set; }
public string Query { get; set; }
}
}

@ -0,0 +1,91 @@
using Application.Domain.Entities;
using Application.Models;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Infrastructure.Jwt;
using IoTCenter.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace UserCenter.Controllers
{
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[ApiController]
public class ApiController : Controller
{
private readonly IHostEnvironment _env;
private readonly IConfiguration _configuration;
private readonly IJwtHelper _jwtHelper;
private readonly IRepository<Category> _categoryRepo;
private readonly IRepository<Product> _productRepo;
private readonly IRepository<Node> _nodeRepo;
private readonly IRepository<Scene> _sceneRepo;
private readonly IRepository<SceneCommand> _sceneCommandRepo;
private readonly IRepository<Command> _commandRepo;
private readonly IRepository<Device> _deviceRepo;
private readonly IRepository<LiveRecord> _liveRecordRepo;
private readonly IHubContext<IoTCenterHub> _hub;
public ApiController(
IHostEnvironment env,
IConfiguration configuration,
IJwtHelper jwtHelper,
IRepository<Category> categoryRepo,
IRepository<Product> productRepo,
IRepository<Node> nodeRepo,
IRepository<Scene> sceneRepo,
IRepository<SceneCommand> sceneCommandRepo,
IRepository<Command> commandRepo,
IRepository<Device> deviceRepo,
IRepository<LiveRecord> liveRecordRepo,
IHubContext<IoTCenterHub> hub)
{
this._env = env;
this._configuration = configuration;
this._jwtHelper = jwtHelper;
this._categoryRepo = categoryRepo;
this._productRepo = productRepo;
this._nodeRepo = nodeRepo;
this._sceneRepo = sceneRepo;
this._sceneCommandRepo = sceneCommandRepo;
this._commandRepo = commandRepo;
this._deviceRepo = deviceRepo;
this._liveRecordRepo = liveRecordRepo;
this._hub = hub;
}
[HttpPost]
public ActionResult Exec([FromBody]ApiRequestModel model)
{
try
{
this.CallApi(model.ConnectionId, model.Number, model.Method, model.Query);
return Ok();
}
catch (Exception ex)
{
ex.PrintStack();
return Problem(ex.Message);
}
}
private void CallApi(string connectionId, string number, string method, string query)
{
var device = this._deviceRepo.ReadOnlyTable().Include(o => o.Node).FirstOrDefault(o => o.Number == number);
if (device != null)
{
var message = $"{method}?number={number}{(string.IsNullOrEmpty(query) ? "" : "&")}{query}";
var group = device.Node.Number;
this._hub.ServerToClient(Methods.ExecApiRequest, message, group, connectionId);
}
}
}
}

@ -37,10 +37,10 @@ namespace IoTNode
services.AddHostedService(o => o.GetService<IoTNodeClient>());
services.AddSingleton<OnvifService>();
services.AddHostedService(o => o.GetService<OnvifService>());
//services.AddSingleton<FBeeService>();
//services.AddHostedService(o => o.GetService<FBeeService>());
//services.AddSingleton<SerialPortService>();
//services.AddHostedService(o => o.GetService<SerialPortService>());
services.AddSingleton<FBeeService>();
services.AddHostedService(o => o.GetService<FBeeService>());
services.AddSingleton<SerialPortService>();
services.AddHostedService(o => o.GetService<SerialPortService>());
base.ConfigureServices(services);
}

@ -1,5 +1,6 @@
var hubUrl = "/IoTCenter/hub?group=page";
var connection = new signalR.HubConnectionBuilder().withUrl(hubUrl).build();
var connectionId;
function connect() {
if (connection.state === signalR.HubConnectionState.Disconnected) {
connection.start().then(function () {

@ -52,10 +52,32 @@ const store = new Vuex.Store({
updateByNumber(state.nodes, model);
}
if (state.node) {
copy(data.model, state.node);
if (state.node.number === model.number) {
copy(model, state.node);
}
}
},
dataEntityUpdated(state, data) {
var model = data.model;
if (state.node) {
var device = Enumerable.from(state.node.devices).where(function (o) { return o.id === model.deviceId; }).firstOrDefault();
if (device) {
updateByKey(device.data, model);
}
}
if (state.product) {
var device = Enumerable.from(state.product.devices).where(function (o) { return o.id === model.deviceId; }).firstOrDefault();
if (device) {
updateByKey(device.data, model);
}
}
if (state.device) {
var device =state.device.number === model.number;
if (device) {
updateByKey(device.data, model);
}
}
}
//update(state,method,message,to,from) {
// alert(1);
//},

@ -1,32 +1,63 @@
function copy(from, to) {
for (var attr in to) {
if (from.hasOwnProperty(attr)) {
if (!(from[attr] instanceof Array)) {
to[attr] = from[attr];
}
}
}
//update
function updateById(list, item) {
return update(list, item, 'id');
}
function updateByNumber(list, item) {
return update(list, item, 'number');
}
function updateByKey(list, item) {
return update(list, item, 'key');
}
function updateByNumber(list, from) {
function update(list, item, key) {
key = key || 'number';
var result = false;
var to = Enumerable.from(list).where(function (o) { return o.number === from.number; }).firstOrDefault();
var to = Enumerable.from(list).where(function (o) { return o[key] === item[key]; }).firstOrDefault();
if (to) {
copy(from, to);
copy(item, to);
}
else {
list.push(from);
list.push(item);
result = true;
}
console.log(result?'insert':'update'+' data by '+key);
return result;
}
function deleteByNumber(list, item) {
//delete
function remove(list, item,key) {
var result = false;
for (var i = 0; i < list.length; i++) {
if (list[i].number == item.number) {
if (list[i][key] == item[key]) {
list.splice(i, 1);
result = true;
break;
}
}
return result;
}
//util
function copy(from, to) {
for (var attr in to) {
if (from.hasOwnProperty(attr)) {
if (!(from[attr] instanceof Array)) {
to[attr] = from[attr];
}
}
}
}
function callApi(number, method, query) {
var loading = weui.loading('提交中...');
axios.post('/IoTCenter/api/v1/api/exec', { connectionId,number,method, query })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
loading.hide();
});
}

@ -222,8 +222,8 @@
</div>
<div class="col col-md-8 col-sm-8 align-self-center">
<div class="row" style="line-height:28px;">
<img v-if="getDataValue(device.number,'状态')==='开'" v-on:click="CallApi(device.number,'/Socket/Off')" src="/IoTCenter/images/on.png" :data-status="getDataValue(device.number,'状态')" />
<img v-if="getDataValue(device.number,'状态')==='关'" v-on:click="CallApi(device.number,'/Socket/On')" src="/IoTCenter/images/off.png" :data-status="getDataValue(device.number,'状态')" />
<img v-if="getDataValue(device.number,'状态')==='开'" v-on:click="callApi(device.number,'/Socket/Off')" src="/IoTCenter/images/on.png" :data-status="getDataValue(device.number,'状态')" />
<img v-if="getDataValue(device.number,'状态')==='关'" v-on:click="callApi(device.number,'/Socket/On')" src="/IoTCenter/images/off.png" :data-status="getDataValue(device.number,'状态')" />
</div>
</div>
</div>

Loading…
Cancel
Save