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.
267 lines
8.2 KiB
267 lines
8.2 KiB
/*var*/
|
|
var isApp = location.href.indexOf('http') !== 0;
|
|
var server = isApp ? localStorage.getItem('server') : null;
|
|
var iotCenter = isApp ? server + '/IoTCenter' : '';
|
|
var dataUrl = iotCenter + '/App/GetProduct';
|
|
var token = isApp ? localStorage.getItem('token') : null;
|
|
var wsUrl = iotCenter + '/hub?group=page';
|
|
var connectionId;
|
|
const connection = new signalR.HubConnectionBuilder()
|
|
.withUrl(wsUrl)
|
|
.build();
|
|
var app;
|
|
var vm;
|
|
//
|
|
var livePlayer;
|
|
var timer;
|
|
var decodedFrames;
|
|
var muted = true;
|
|
var volume = 0.5;
|
|
//
|
|
if (isApp) {
|
|
Framework7.use(Framework7Vue);
|
|
}
|
|
toastr.options.timeOut = 500;
|
|
toastr.options.positionClass = "toast-top-center";
|
|
/*fun*/
|
|
function alert(message, title) {
|
|
if (isApp) {
|
|
title = title || '消息';
|
|
app.dialog.alert(message, title);
|
|
}
|
|
else {
|
|
alert(message);
|
|
}
|
|
}
|
|
function loadData() {
|
|
var formData = new FormData();
|
|
formData.append('number', new URI().search(true).number);
|
|
if (isApp) {
|
|
formData.append('token', token);
|
|
}
|
|
axios.post(dataUrl, formData)
|
|
.then(function (response) {
|
|
console.log(response);
|
|
data = response.data;
|
|
if (data.Template) {
|
|
loadTemplate();
|
|
}
|
|
else {
|
|
init();
|
|
}
|
|
toastr.success('数据加载成功!');
|
|
})
|
|
.catch(function (error) {
|
|
alert(error)
|
|
});
|
|
}
|
|
function loadTemplate() {
|
|
var url = iotCenter + '/App/GetTemplate?template=' + data.Template;
|
|
axios.post(url, { crossDomain: true })
|
|
.then(function (response) {
|
|
var html = response.data;
|
|
$('#template').html(html);
|
|
init();
|
|
})
|
|
.catch(function (error) {
|
|
alert(error)
|
|
});
|
|
}
|
|
|
|
|
|
function ajax(url, data, type) {
|
|
url = iotCenter + url
|
|
console.log(url);
|
|
type = type || 'get';
|
|
$('.overlay').show();
|
|
$.ajax({
|
|
type: type,
|
|
url: url,
|
|
data: data,
|
|
success: AjaxCallBack
|
|
}).fail(function (result) {
|
|
console.log('error');
|
|
console.log(result);
|
|
}).always(function () {
|
|
$('.overlay').hide();
|
|
});
|
|
}
|
|
function AjaxCallBack(response) {
|
|
var result = response;
|
|
if (result.code === 0) {
|
|
if (result.type === 0) {
|
|
if (result.format === 1) {
|
|
console.log('format/1/base64 jpeg image');
|
|
$('#callback .page-content').html('<img class="shot" src="' + result.data + '">');
|
|
}
|
|
else {
|
|
console.log('format/0/json object');
|
|
$('#callback .page-content').html(result.data);
|
|
}
|
|
app.popup.open('#callback');
|
|
}
|
|
}
|
|
}
|
|
function updateItem(list, item) {
|
|
var update = false;
|
|
for (var i = 0; i < list.length; i++) {
|
|
if (list[i].Id == item.Id) {
|
|
update = true;
|
|
break;
|
|
}
|
|
}
|
|
if (update) {
|
|
list.splice(i, 1, item);
|
|
}
|
|
else {
|
|
list.push(item);
|
|
}
|
|
}
|
|
function deleteItem(list, item) {
|
|
for (var i = 0; i < list.length; i++) {
|
|
if (list[i].Id == item.Id) {
|
|
list.splice(i, 1);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
/*ws*/
|
|
function connect() {
|
|
if (debug) {
|
|
console.log('start connect to server:' + Date());
|
|
}
|
|
if (connection.state === signalR.HubConnectionState.Disconnected) {
|
|
connection.start().then(function () {
|
|
|
|
}).catch(function (err) {
|
|
console.error(err.toString());
|
|
setTimeout(connect, 15 * 1000);
|
|
});
|
|
}
|
|
}
|
|
connection.on('Connected', function (id) {
|
|
connectionId = id;
|
|
console.log(connectionId);
|
|
});
|
|
connection.onclose(function (err) {
|
|
setTimeout(connect, 15 * 1000);
|
|
});
|
|
connection.on("ServerToClient", function (method, json, from) {
|
|
console.log(method + ':' + json);
|
|
onMessage(method, json, from);
|
|
});
|
|
function onMessage(method, json, from) {
|
|
var item = JSON.parse(json);
|
|
if (method == 'DeviceEntityInserted' || method == 'DeviceEntityUpdated') {
|
|
toastr.info(item.DisplayName+'更新');
|
|
updateItem(vm.model.Devices, item);
|
|
} else if (method == 'DeviceEntityDeleted') {
|
|
deleteItem(vm.model.Devices, item);
|
|
toastr.info(item.DisplayName + '删除');
|
|
} else if (method == 'DataEntityInserted' || method == 'DataEntityUpdated') {
|
|
var device = Enumerable.from(vm.model.Devices).firstOrDefault(function (o) { return o.Id === item.DeviceId; })
|
|
if (device) {
|
|
updateItem(device.Data, item);
|
|
toastr.info(device.DisplayName + '更新');
|
|
}
|
|
}
|
|
}
|
|
/*vue*/
|
|
function init() {
|
|
$('#template style').each(function (i, n) {
|
|
$('head').append(n);
|
|
});
|
|
vm = new Vue({
|
|
el: '#app',
|
|
data() {
|
|
return {
|
|
f7params: {
|
|
routes: [],
|
|
name: 'My App',
|
|
id: 'com.myapp.test',
|
|
theme: 'ios',
|
|
},
|
|
model: data
|
|
};
|
|
},
|
|
mounted() {
|
|
if (isApp) {
|
|
if (!server) {
|
|
location.href = "config.html";
|
|
}
|
|
if (!token) {
|
|
location.href = "login.html";
|
|
}
|
|
this.$f7ready((f7) => {
|
|
app = this.$f7;
|
|
});
|
|
}
|
|
connect();
|
|
},
|
|
methods: {
|
|
HasBatchCommand() {
|
|
return this.model.Name === '调色灯'
|
|
|| this.model.Name.indexOf('窗帘电机') >= 0
|
|
|| this.model.Name.indexOf('开关') >= 0
|
|
|| this.model.Name.indexOf('插座') >= 0;
|
|
},
|
|
SelectDevice(e) {
|
|
var checkbox = $(e.target);
|
|
if (checkbox.hasClass('checkall')) {
|
|
if (e.target.checked) {
|
|
$('input.item').not(':checked').prop("checked", true);
|
|
}
|
|
else {
|
|
$('input.item').filter(':checked').prop("checked", false);
|
|
}
|
|
}
|
|
else if (checkbox.hasClass('uncheck')) {
|
|
$('input.item').each(function () {
|
|
$(this).prop("checked", !$(this).prop("checked")).change();
|
|
});
|
|
}
|
|
var parent = $('input.checkall');
|
|
if ($('input.item').not(':checked').length === 0) {
|
|
parent.prop("indeterminate", false);
|
|
parent.prop("checked", true);
|
|
}
|
|
else if ($('input.item').filter(':checked').length === 0) {
|
|
parent.prop("indeterminate", false);
|
|
parent.prop("checked", false);
|
|
}
|
|
else {
|
|
parent.prop("indeterminate", true);
|
|
}
|
|
},
|
|
CallApiAll(method) {
|
|
var numbers = [];
|
|
$('.item:checked').each(function () {
|
|
numbers.push($(this).val());
|
|
});
|
|
ajax('/App/ExecApiAll', { token: token, connectionId: connectionId, method: vm.model.Path + method, numbers: numbers }, 'post');
|
|
},
|
|
GetDataValue(number, name) {
|
|
var device = Enumerable.from(this.model.Devices)
|
|
.where(function (o) { return o.Number === number; })
|
|
.firstOrDefault();
|
|
if (device != null) {
|
|
var data = Enumerable.from(device.Data)
|
|
.where(function (o) { return o.Name == name })
|
|
.firstOrDefault();
|
|
if (data != null) {
|
|
return data.Value;
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
GetDeviceDataValue(number, name, attr) {
|
|
var device = Enumerable.from(this.model.Devices).where(function (o) { return o.Number === number; }).firstOrDefault();
|
|
var data = Enumerable.from(device.Data).where(o => o.Name === name).firstOrDefault();
|
|
if (data != null) {
|
|
return data[attr];
|
|
}
|
|
return null;
|
|
},
|
|
}
|
|
});
|
|
} |