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/WebSPA/wwwroot/js/message.js

125 lines
3.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

var hubUrl = config.hubUrl;
var connection = new signalR.HubConnectionBuilder().withUrl(hubUrl).build();
var connectionId;
function connect() {
if (connection.state === signalR.HubConnectionState.Disconnected) {
connection.start().then(function () {
console.log('signalR 连接成功');
}).catch(function (err) {
console.log(err);
setTimeout(connect, 5000);
});
}
}
connection.onclose(function () {
console.log('connect closed');
connect();
});connection.on('Connected', function (id) {
connectionId = id;
console.log('signalR 连接Id:' + connectionId);
});
/**
* 接收从服务器发出的信息
*/
connection.on("ServerToClient", function (method, message, to, from) {
/**
* 格式化事件字符串
*/
var event = method.substr(0, 1).toLowerCase() + method.substr(1);
/**
* 根据当前路由调用组件关联的消息回调方法
*/
var model = JSON.parse(message);
router.currentRoute.meta.onMessage(event, model, to, from);
console.log('receive:');
console.log(method); // 调用方法
console.log(message); // 消息内容
// 本地存储消息
saveMessage(message);
console.log('old messages:');
console.log('===');
console.log(store.state.messages);
console.log('save message');
console.log('===event===');
console.log(event);
console.log('-------------');
//store.commit('setMessages', []);
console.log('-------------');
/**
* 调用定义的update事件处理
* 注:设备移除
* payload: {event,model,to,from}
* event-pre: 'data', 'device', 'node'
* event: 'EntityUpdated','EntityUpdated','EntityInserted','EntityDeleted'
* model: {key: "State", value: "开", name: "状态", type: 50, unit: null, …}
* to: 'page'
* from: null
*/
console.log('message.js event:');
console.log(event);
console.log('message.js model:');
console.log(model);
console.log('message.js to:');
console.log(to);
console.log('message.js from:');
console.log(from);
/**
* 事件过滤处理,绑定到对应异步操作
* 除设备操作事件外Event > Mutations > State其他事件只做Action触发不直接刷新数据
* 具体流程Event > Actions > Mutations > State
*/
switch(event) {
/**
* 产品添加
* count: 1
* displayOrder: 0
* id: "5e3fb468-b7e7-b9d5-e2f2-cca918d28844"
* image: "/images/socket.png"
* name: "插座"
* number: "fbee:0009:01"
*/
case 'productEntityInserted':
//alert('getProducts')
//store.dispatch('getProducts', payload);
break;
// 设备移除
case 'deviceEntityDeleted':
// 设备新增
case 'deviceEntityInserted':
// 当当前为产品设备列表页面时,刷新产品设备列表 (由于每次进入页面都触发数据刷新,非当前页面则不做数据更新处理)
var number = getUrlKey('number');
if(router.currentRoute.path === '/pages/iot/product'){
if(number){
// 向产品设备列表中新增
store.dispatch('getProduct', {number:number});
}
}
// 产品列表
else if(router.currentRoute.path === '/pages/iot/products'){
// 更新产品列表设备数
store.dispatch('getProducts');
}
// 节点列表(智慧教室)
else if(router.currentRoute.path === '/pages/iot/nodes'){
// 更新产品列表设备数
store.dispatch('getNodes');
}
break;
default:
/**
* dataEntityUpdated
*/
//store.dispatch('update', payload);
}
});