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.
iot/projects/Platform/wwwroot/js/app.js

151 lines
4.2 KiB

//util
function copy(from, to) {
for (var attr in to) {
if (from.hasOwnProperty(attr)) {
if (!(from[attr] instanceof Array)) {
to[attr] = from[attr];
}
}
}
}
//loading
var loading = {
show: function (msg) {
$('#loadingToast').show();
},
hide: function () {
$('#loadingToast').hide();
}
};
//pubsub批量订阅取消订阅
PubSub.subscribes = function (events, action) {
if (!events || !action) {
return;
}
for (var i = 0; i < events.length; i++) {
PubSub.subscribe(events[i], function (m, d) { action(m, d); });
}
}
PubSub.unsubscribes = function (events) {
if (!events) {
return;
}
for (var i = 0; i < events.length; i++) {
PubSub.unsubscribe(events[i]);
}
}
//eval解析vue组件
function parseModel(response) {
var html = new DOMParser().parseFromString(response.data, 'text/html');
var template = html.getElementsByTagName('template')[0].innerHTML;
var script = html.getElementsByTagName('script')[0].innerHTML;
script = '(' + script.replace(/^\s*export\s*default\s*/, '').replace(/;?\s*$/, '') + ')\n//# sourceURL=' + response.config.url;
var model = eval(script);
model.template = template;
return model;
}
//循环添加vue组件
function addVueComponents(config) {
for (var i in config.list) {
(function () {
var item = config.list[i];
var url = config.prefix + item + '.html';
var name = url.substring(12, url.length - 5).replace(/\//g, "-");
Vue.component(name, function (resolve, reject) {
axios.get(url, { headers: { 'Cache-Control': 'no-cache' } }).then(function (response) {
resolve(parseModel(response));
});
});
})();
}
}
//获取设备数据
function getIoTData(device,name) {
var data = Enumerable.from(device.data).where(o => o.name === name).firstOrDefault();
if (data) {
return data['value'] + ' ' + data['unit'] + ' ' + data['description'];
}
return null;
}
function getIoTDataValue(device, name) {
var data = Enumerable.from(device.data).where(o => o.name === name).firstOrDefault();
if (data) {
return data['value'];
}
return null;
};
function getIoTDataUnit(device, name) {
var data = Enumerable.from(device.data).where(o => o.name === name).firstOrDefault();
if (data) {
return data['unit'];
}
return null;
}
function getIoTDataDescription(device, name) {
var data = Enumerable.from(device.data).where(o => o.name === name).firstOrDefault();
if (data) {
return data['description'];
}
return null;
}
//调用API
function execApi(number, method, query) {
loading.show();
axios.post(config.baseUrl + '/platform/api/v1/api/execApi', { connectionId, number, method, query })
.then(function (response) {
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
loading.hide();
});
}
//调用场景
function execScene(id) {
loading.show();
axios.post('/platform/api/v1/api/execScene', { id, connectionId })
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.finally(function () {
loading.hide();
});
}
//更新列表项
function updateById(list, item) {
var key = 'id';
var to = Enumerable.from(list).where(function (o) { return o[key] === item[key]; }).firstOrDefault();
if (to) {
copy(item, to);
}
else {
list.push(item);
result = true;
}
}
//移除列表项
function removeById(list, item) {
var key = 'id';
for (var i = 0; i < list.length; i++) {
if (list[i][key] === item[key]) {
list.splice(i, 1);
result = true;
break;
}
}
}
//Signal
function connect() {
if (connection.state === signalR.HubConnectionState.Disconnected) {
connection.start().then(function () {
console.log('signalr connected');
}).catch(function (err) {
console.log(err);
setTimeout(connect, 5000);
});
}
}