#朔联科技react开发库 #1.创建store import { createStore } from 'axa-rc-lib'; let store = createStore( 'web', { serverURL: 'https://axalent/zdk/services/zamapi/', socketIP: '111.111.111.111', socketPort: 1001, appId: 199, } );
#2.调用接口 store.api.userLogin(name, password, { onSuccess: () => { //成功 }, onFail: (error) => { //失败 } }); #3.设置cloud监听 class HomePage extends Component {
unTreeListener = null;
unDeviceListeners = [];
unDeviceAttributeListeners = [];
unAddDeviceListener = null;
unDeleteDeviceListener = null;
constructor(props) {
}
componentDidMount() {
this.loadDevices();
}
componentWillUnmount() {
this.unBindListener();
}
loadDevices = () => {
this.props.store.api.getDeviceList({
onSuccess: (devices) => {
//成功
this.bindListener(devices);
},
onFail: (error) => {
//失败
}
});
}
bindListener = (devices) => {
this.unTreeListener = this.props.store.setDeviceTreeListener((event) => {
console.log('received deviceTreeListener:');
console.log(event);
});
this.unAddDeviceListener = this.props.store.setAddDeviceListener((device, error) => {
console.log('received addDeviceListener:');
if (!error) {
console.log('new device:');
console.log(device);
} else {
console.log('add failed.');
console.log(error);
}
});
this.unDeleteDeviceListener = this.props.store.setDeleteDeviceListener((device, error) => {
console.log('received deleteDeviceListener:');
if (!error) {
console.log('delete device:');
console.log(device);
} else {
console.log('delete failed.');
console.log(error);
}
});
devices.forEach((device) => {
let unDeviceListener = this.props.store.setDeviceListener(device.devId, (event) => {
console.log('received deviceListener:');
console.log(event);
});
this.unDeviceListeners.push(unDeviceListener);
let unDeviceAttributeListener = this.props.store.setDeviceAttributeListener(device.devId, 'custom_name', (event) => {
console.log('received deviceAttributeListener:');
console.log(event);
});
this.unDeviceAttributeListeners.push(unDeviceAttributeListener);
});
}
unBindListener() {
this.UnTreeListener();
this.unAddDeviceListener();
this.unDeleteDeviceListener();
this.unDeviceListeners.forEach((unDeviceListener) => unDeviceListener());
this.unDeviceAttributeListeners.forEach((unDeviceAttributeListener) => unDeviceAttributeListener());
}
}