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

171 lines
4.7 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(1, 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));
});
});
})();
}
}
//路由设置
var routes = [];
var home = '/components/views/home/index.html';
var login = '/components/views/account/login.html';
const router = new VueRouter();
router.beforeEach((to, from, next) => {
var url = to.path === '/' ? home : to.path;
var name = url.substring(1, url.length - 5).replace(/\//g, "-");
var route = routes[name];
if (!route) {
axios.get(url, { headers: { 'Cache-Control': 'no-cache' } }).then(function (response) {
var model = parseModel(response);
route = {
name: name,
path: to.path,
component: model,
meta: model.meta
};
router.addRoutes([route]);
routes[name] = route;
router.push({ path: to.path, query: to.query });
});
}
else {
next();
}
});
//添加设备组件
addVueComponents({
prefix: "/components/devices/",
list: [
'light',
'infrared',
'smoke',
'socket',
'curtain'
]
});
//获取设备数据
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;
}
}
}