Commit 8181b820 by Tonk

update switch on/off subbreaker

parent bf95de6e
......@@ -14,16 +14,16 @@ class MeterCard extends Component {
const deviceId = this.props.item.deviceId;
await this.props.getCurrentSelectedShadow(deviceId);
};
componentDidMount = async () => {
await this.getShadow();
componentDidMount = () => {
this.getShadow();
console.log('meter props', this.props);
};
componentDidUpdate = async (prevProps, prevState) => {
componentDidUpdate = (prevProps, prevState) => {
if (prevProps.shadow !== this.props.shadow) {
await this.setInitState();
this.setInitState();
}
};
setInitState = async () => {
setInitState = () => {
const { shadow } = this.props;
this.setState({
isPowerOn: shadow.SM1.BK_S,
......@@ -73,6 +73,11 @@ class MeterCard extends Component {
</View>
</Row>
</View>
<View style={[theme.rowContainer, { position: 'absolute', top: 10, right: 5 }]}>
<Icon name="wifi" type="FontAwesome" style={styles.iconStyle} />
<Icon name={item.isSharing ? 'users' : 'user'} type="FontAwesome" style={styles.iconStyle} />
<View style={[styles.online, { backgroundColor: item.isOnline ? '#10ca88' : color.primary }]} />
</View>
</BaseButton>
) : (
<TouchableOpacity onPress={this.props.onPressEachCard} style={styles.cardContainer}>
......
......@@ -121,13 +121,6 @@ export const setMainStatus = value => async (dispatch, getState) => {
};
const data = { data: { CMD_MSG: { MSG1: command() } } };
// console.log('value', value, 'command', command(), 'qs', qs.stringify({ data: { CMD_MSG: { MSG1: command() } } }));
// console.log('selectedDeviceId', selectedDeviceId);
// console.log(idToken);
// console.log(URL);
// console.log(data);
// console.log(JSON.stringify(data).replace(/\"/g, ''));
const options = {
method: 'PUT',
headers: { Authorization: `Token ${idToken}`, 'content-Type': 'text/plain' },
......@@ -154,6 +147,49 @@ export const setMainStatus = value => async (dispatch, getState) => {
}
};
export const setSubBreakerStatus = (value, MainIndex, SubIndex) => async (dispatch, getState) => {
const { currentSelectedDeviceReducer } = getState();
const { currentSelectedData, shadow } = currentSelectedDeviceReducer;
const selectedDeviceId = currentSelectedData.deviceId;
const URL = `${baseURL}/shadow/${selectedDeviceId}`;
const idToken = await app.auth().currentUser.getIdToken(true);
const command = () => {
switch (value) {
case true:
return `L${MainIndex + 1}B${SubIndex + 1}_ON`;
case false:
return `L${MainIndex + 1}B${SubIndex + 1}_OFF`;
}
};
const data = { data: { CMD_MSG: { MSG1: command() } } };
const options = {
method: 'PUT',
headers: { Authorization: `Token ${idToken}`, 'content-Type': 'text/plain' },
url: URL,
data: JSON.stringify(data).replace(/\"/g, ''),
};
try {
const {
data: {
writeShadow: {
value: { CMD_MSG },
},
},
} = await axios(options);
// console.log(value);
// console.log('ori', shadow);
// const newShadow = { ...shadow, CMD_MSG, SM1: { ...shadow.SM1, BK_S: value ? 1 : 0 } };
// console.log('new', newShadow);
// dispatch(getCurrentSelectedShadowAction(newShadow));
} catch (error) {
console.log(error);
}
};
// export const getCurrentSelectedData = (deviceId, mcbLinkId, subBreakerId) => (dispatch, getState) => {
// const { currentSelectedDeviceReducer } = getState();
// const { selectedDeviceId } = currentSelectedDeviceReducer;
......
......@@ -9,6 +9,7 @@ import {
getCurrentSelectedShadow,
loadingAction,
setMainStatus,
setSubBreakerStatus,
} from '../../../reduxStore/actions/currentSelectedAction';
import { getTimers, loadingTimersAction } from '../../../reduxStore/actions/timersAction';
import { isIphoneX } from '../../../utils/isPhoneX';
......@@ -219,12 +220,32 @@ class SmartMeterDetailScreen extends Component {
this.setState(prevState => (mcbLink.isExpand = !prevState.mcbLinksInfo[indexMcbLinks].isExpand));
const renderSubBreaker = (subBreaker, indexSubBreakers) => {
const handleOnPressSubBreakerSwitch = () =>
const handleOnPressSubBreakerSwitch = async value => {
this.setState(
prevState =>
(subBreaker.isPowerOn = !prevState.subBreakersInfo[indexMcbLinks][indexSubBreakers].isPowerOn)
);
const deviceId = this.props.navigation.getParam('deviceId');
await this.props.setSubBreakerStatus(value, indexMcbLinks, indexSubBreakers);
let test = setInterval(() => {
this.props.getCurrentSelectedShadow(deviceId);
console.log('getShadow');
}, 2000);
if (this.state.prevShadow !== props.shadow) {
clearInterval(test);
return { isWaiting: false };
} else {
setTimeout(() => {
clearInterval(test);
}, 6000);
}
};
const isSubOn = this.props.shadow[`ML${indexMcbLinks + 1}`][
`L${indexMcbLinks + 1}B${indexSubBreakers + 1}`
];
return (
<View
style={{
......@@ -250,7 +271,11 @@ class SmartMeterDetailScreen extends Component {
</View>
</Left>
<Right>
<Switch value={subBreaker.isPowerOn} onChange={handleOnPressSubBreakerSwitch} />
<Switch
disabled={this.props.shadow.SM1.BK_S === 0 ? true : false}
value={isSubOn === 1 ? true : false}
onValueChange={value => handleOnPressSubBreakerSwitch(value)}
/>
</Right>
</Row>
</View>
......@@ -374,6 +399,7 @@ const mapDispatchToProps = {
loadingTimersAction,
getTimers,
setMainStatus,
setSubBreakerStatus,
};
const withNavSmartMeterDetailScreen = withNavigationFocus(SmartMeterDetailScreen);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment