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.

146 lines
3.1 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.

import { Effect, Reducer, Subscription } from 'umi';
import { TagType } from './data.d';
import { queryTags,getClassInfo,execApi,execScene, } from './service';
import emitter from './events';
export interface StateType {
tags: TagType[];
classInfo:any
}
export interface ModelType {
namespace: string;
state: StateType;
effects: {
fetchTags: Effect;
getClassInfo: Effect;
exec: Effect;
execScene: Effect;
};
reducers: {
saveTags: Reducer<StateType>;
save: Reducer<StateType>;
};
subscriptions: { setup: Subscription };
}
const Model: ModelType = {
namespace: 'dashboardAndmonitor',
state: {
tags: [],
classInfo:{}
},
effects: {
*fetchTags(_, { call, put }) {
const response = yield call(queryTags);
yield put({
type: 'saveTags',
payload: response.list,
});
},
//获取教室设备信息通过Rest API
*getClassInfo({ payload, callback }, { call, put }) {
const response = yield call(getClassInfo, payload);
yield put({
type: 'save',
payload: {
classInfo: response,
},
});
},
//开关设备
*exec({ payload, callback }, { call, put }) {
const response = yield call(execApi, payload);
},
//场景开关
*execScene({ payload, callback }, { call, put }) {
const response = yield call(execScene, payload);
},
},
reducers: {
saveTags(state, action) {
return {
...state,
tags: action.payload,
};
},
save(state, { payload }) {
return {
...state,
...payload,
};
},
update(state:any, action:any) {
if(state.classInfo.devices && state.classInfo.devices.length > 0){
state.classInfo.devices.map((item:any)=>{
if(item.number == action.payload.model.device.number){
item.data.map((item1:any)=>{
if(item1.key == 'State'){
item1.value = action.payload.model.value
}
})
}
})
}
// alert(JSON.stringify(action.payload.model))
debugger
console.log(state)
let val = '';
// switch( action.payload.model.value ) {
// case '开':
// val = 'on';
// break;
// case '关':
// val = 'off';
// break;
// default:
// '';
// }
return {
...state,
status: val,
};
},
},
subscriptions: {
setup({ dispatch, history }): void {
emitter.on('message', function (payload) {
/**
* deviceId: "75bae534-a2ff-4355-97cf-06e7acbacc57"
* id: "f9f71b12-134e-4b18-ba33-335620ea6bc5"
*/
if(payload.event === "dataEntityUpdated"){
// debugger
if (dispatch) {
dispatch({
type: 'update',
payload
});
}
//console.log('message::::', payload)
}
})
},
},
};
export default Model;