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/store.js

228 lines
7.2 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.

Vue.use(Vuex);
const store = new Vuex.Store({
/**
* 采用单一状态树
* 组件状态在此声明
*/
state: {
accessToken: localStorage.getItem("accessToken"),
refreshToken: localStorage.getItem("refreshToken"),
user: null,
products: [],
nodes: [],
scenes: [],
timers: [],
tiggers: [],
product: null,
node: null,
device: null,
messages: []
},
/**
* 异步操作处理方法
*/
actions: {
/**
*
* @param {*} context
* @param {*} model
*
* model.event :
* 设备操作相应事件execApiResponse > dataEntityUpdated > deviceEntityUpdated
* 添加设备服务端根据设备信息标识自动创建产品同型号设备属于同一产品productEntityInserted > deviceEntityInserted > dataEntityInserted
*
*/
update(context, model) {
context.commit(model.event, model);
},
/**
* 获取产品列表通过Rest API
* 应用场景:
* 1. 页面首次加载通过API接口获取数据
* 2. websocket 接收到 productEntityInserted (由于从消息内提取数据,存在消息内数据项不全,数据不一致可能)
* @param {*} context
*
*/
getProducts(context){
axios.post('/IoTCenter/api/v1/product/getProducts').then(function (response) {
store.commit('setProducts', response.data);
}).catch(function (error) {
//
}).finally(function () {
//
});
},
/**
* 获取产品设备列表通过Rest API
* 应用场景:
* 1. 页面首次加载通过API接口获取数据
* 2. websocket 接收到 deviceEntityDeleted (由于从消息内提取数据,存在消息内数据项不全,数据不一致可能)
* @param {*} context
* @param {*} model
*/
getProduct(context, payload){
axios.post('/IoTCenter/api/v1/product/getProduct?number=' + payload.number).then(function (response) {
store.commit('setProduct', response.data);
}).catch(function (error) {
//
}).finally(function () {
//
});
},
/**
* 获取节点列表通过Rest API
* 应用场景:
* 1. 页面首次加载通过API接口获取数据
* 2. websocket 接收到 productEntityInserted (由于从消息内提取数据,存在消息内数据项不全,数据不一致可能)
* @param {*} context
* @param {*} model
*/
getNodes(context, payload){
axios.post('/IoTCenter/api/v1/node/getNodes').then(function (response) {
store.commit('setNodes', response.data);
}).catch(function (error) {
//
}).finally(function () {
//
});
},
},
/**
* 同步操作State方法
* @param {*} state
* @param {*} payload
*/
mutations: {
// 设置Tocken
setToken(state, data) {
state.accessToken = data.accessToken;
localStorage.setItem("accessToken", state.accessToken);
state.refreshToken = data.refreshToken;
localStorage.setItem("refreshToken", state.refreshToken);
},
// 退出登录
logout(state) {
state.accessToken = null;
state.refreshToken = null;
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
},
// 设置用户数据
setUser(state, user) {
state.user = user;
},
// 设置产品列表数据
setProducts(state, model) {
state.products = model;
},
// 设置产品列表数据
setNodes(state, model) {
state.nodes = model;
},
// 设置产品数据
setProduct(state, model) {
state.product = model;
},
// 设置设备数据
setDevice(state, model) {
state.device = model;
},
// 设置节点数据
setNode(state, model) {
state.node = model;
},
// 设置场景数据
setScenes(state, model) {
state.scenes = model;
},
/**
* 设置消息数据
* @param {*} state 原有状态
* @param {*} messages 新的消息数组
* @param {*} limit 默认条数限制
*/
setMessages(state, messages, limit = 99){
// 推出超出条数FIFO
state.messages.forEach(function(value,i){
if(i >= limit){
state.messages.shift()
}
})
// 消息列表追加
messages.forEach(function(value,i){
if(state.messages >= limit){
state.messages.shift()
}
state.messages.push(value);
})
},
nodeEntityUpdated(state, data) {
var model = data.model;
if (state.nodes) {
updateByNumber(state.nodes, model);
}
if (state.node) {
if (state.node.number === model.number) {
copy(model, state.node);
}
}
},
/**
* 产品更新已经废弃由于消息数据项不全且与API方式功能重复, 改为message > store处理方式
* @param {*} state
* @param {*} data
*/
/*
productEntityInserted(state, data){
state.products.push(data);
},*/
/**
* 更新数据(需要优化)
* @param {*} state
* @param {*} data
*/
dataEntityUpdated(state, data) {
var model = data.model;
// alert('dataEntityUpdated');
// 节点
if (state.node) {
// alert('dataEntityUpdated node !!')
let device = Enumerable.from(state.node.devices).where(function (o) { return o.id === model.deviceId; }).firstOrDefault();
if (device) {
updateByKey(device.data, model);
}
}
// 产品
if (state.product) {
//alert('dataEntityUpdated product !!')
let device = Enumerable.from(state.product.devices).where(function (o) { return o.id === model.deviceId; }).firstOrDefault();
if (device) {
updateByKey(device.data, model);
}
}
/**
* 设备操作状态变更
* 接收数据上报状态变更
*/
if (state.device) {
// alert('dataEntityUpdated device !!')
let device = state.device.id === model.deviceId ? state.device : null;
if (device) {
updateByKey(device.data, model);
}
}
// 消息
}
}
});