|
|
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: [],
|
|
|
deviceStatus: {online: 0, offline: 0}, // 设备在线情况
|
|
|
environment: {
|
|
|
temperature: { minimum: 0.00, maximum: 0.00, average: 0.00, newest:0.00 }, // 温度统计
|
|
|
humidity: { minimum: 0.00, maximum: 0.00, average: 0.00, newest:0.00 }// 湿度统计
|
|
|
}
|
|
|
|
|
|
},
|
|
|
/**
|
|
|
* 异步操作处理方法
|
|
|
*/
|
|
|
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 () {
|
|
|
//
|
|
|
});
|
|
|
},
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取在线设备数量(通过遍历产品中的设备)
|
|
|
* 应用场景:
|
|
|
* 1. 页面显示在线设备数据
|
|
|
*
|
|
|
* @param {*} context
|
|
|
* @param String[] payload
|
|
|
*/
|
|
|
getDeviceStatus(context, payload = null){
|
|
|
var online = 0;
|
|
|
var offline = 0;
|
|
|
// 获取产品列表
|
|
|
axios.post('/IoTCenter/api/v1/product/getProducts').then(function (response) {
|
|
|
console.log('response->>>>>',response)
|
|
|
var requests = [];
|
|
|
$.each(response.data, function(i, item){
|
|
|
console.log('requests->',item)
|
|
|
requests.push(
|
|
|
axios.post('/IoTCenter/api/v1/product/getProduct?number=' + item.number).then(function (response) {
|
|
|
$.each(response.data.devices, function(n, val){
|
|
|
console.log('isOnline->', val.isOnline)
|
|
|
if(val.isOnline) {
|
|
|
online++;
|
|
|
}else{
|
|
|
offline++;
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
)
|
|
|
})
|
|
|
console.log('requests->>>>',requests)
|
|
|
axios.all( requests ).then(function (response) {
|
|
|
store.commit('setDeviceStatus', {online:online, offline:offline})
|
|
|
});
|
|
|
}).catch(function (error) {
|
|
|
//
|
|
|
}).finally(function () {
|
|
|
//
|
|
|
});
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 获取环境汇总信息(温度/适度/关照)
|
|
|
* 应用场景:
|
|
|
* 环境汇总信息(温湿度)
|
|
|
*
|
|
|
* @param {*} context
|
|
|
* @param String payload
|
|
|
* 温湿度传感器: fbee:0302:01
|
|
|
* 光强检测器: fbee:0106:01
|
|
|
*/
|
|
|
getEnvironment(context, payload = 'fbee:0302:01'){
|
|
|
var temperature = { minimum: null, maximum: null, average: null, newest: null, timestamp: null }; // 温度统计
|
|
|
var humidity = { minimum: null, maximum: null, average: null, newest: null, timestamp: null }; // 湿度统计
|
|
|
var temperatureSum = 0;
|
|
|
var humiditySum = 0;
|
|
|
var count = 0;
|
|
|
axios.post('/IoTCenter/api/v1/product/getProduct?number=' + payload).then(function (response) {
|
|
|
console.log('getEnvironment>>>>>', response.data.devices);
|
|
|
count = response.data.devices.length;
|
|
|
// KEY: Temperature | Humidity
|
|
|
$.each(response.data.devices, function(i, item){
|
|
|
$.each(item.data, function(k, d){
|
|
|
var val = parseFloat(d.value)
|
|
|
if(d.key === 'Temperature') {
|
|
|
// 计算和
|
|
|
temperatureSum += val;
|
|
|
// 设定最小值
|
|
|
if(temperature.minimum === null || (val < temperature.minimum)){
|
|
|
temperature.minimum = val;
|
|
|
}
|
|
|
// 设定最大值
|
|
|
if(temperature.maximum === null || (val > temperature.maximum)){
|
|
|
temperature.maximum = val;
|
|
|
}
|
|
|
// 设定时间
|
|
|
if(temperature.timestamp === null || (d.timestamp > temperature.timestamp)){
|
|
|
temperature.timestamp = d.timestamp;
|
|
|
temperature.newest = val;
|
|
|
}
|
|
|
}
|
|
|
if(d.key === 'Humidity') {
|
|
|
// 计算和
|
|
|
humiditySum += val;
|
|
|
// 设定最小值
|
|
|
if(humidity.minimum === null || (val < humidity.minimum)){
|
|
|
humidity.minimum = val;
|
|
|
}
|
|
|
// 设定最大值
|
|
|
if(humidity.maximum === null || (val > humidity.maximum)){
|
|
|
humidity.maximum = val;
|
|
|
}
|
|
|
// 设定时间
|
|
|
if(humidity.timestamp === null || (d.timestamp > humidity.timestamp)){
|
|
|
humidity.timestamp = d.timestamp;
|
|
|
humidity.newest = val;
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
})
|
|
|
// 设置平均
|
|
|
temperature.average = (temperatureSum / count).toFixed(2);
|
|
|
humidity.average = (humiditySum / count).toFixed(2);
|
|
|
store.commit('setEnvironment', {temperature:temperature, humidity:humidity});
|
|
|
}).catch(function (error) {
|
|
|
//
|
|
|
}).finally(function () {
|
|
|
//
|
|
|
});
|
|
|
|
|
|
},
|
|
|
|
|
|
/**
|
|
|
* 获取平台场景列表(通过Rest API)
|
|
|
* 应用场景:
|
|
|
* 1. 页面首次加载通过API接口获取数据
|
|
|
* 2. websocket 接收到 ? (由于从消息内提取数据,存在消息内数据项不全,数据不一致可能)
|
|
|
* @param {*} context
|
|
|
* @param {*} model
|
|
|
*/
|
|
|
getScenes(context, payload){
|
|
|
axios.post('/IoTCenter/api/v1/Scene/getScenes').then(function (response) {
|
|
|
store.commit('setScenes', 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;
|
|
|
},
|
|
|
// 设置设备状态
|
|
|
setDeviceStatus(state, model) {
|
|
|
state.deviceStatus.online = model.online || 0; // 在线数
|
|
|
state.deviceStatus.offline = model.offline || 0; // 离线数
|
|
|
},
|
|
|
/* 设置环境信息
|
|
|
* temperature: {minimum: 4, maximum: 5, average: 6, newest:20 }
|
|
|
* humidity: {minimum: 4, maximum: 5, average: 6, newest:20 }
|
|
|
*/
|
|
|
setEnvironment(state, model) {
|
|
|
state.environment.temperature = model.temperature, // 温度统计
|
|
|
state.environment.humidity = model.humidity // 湿度统计
|
|
|
},
|
|
|
/**
|
|
|
* 设置消息数据
|
|
|
* @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);
|
|
|
}
|
|
|
}
|
|
|
// 消息
|
|
|
}
|
|
|
}
|
|
|
}); |