Commit 8d120271 by HaOuiha

update

parent bf95de6e
......@@ -10,15 +10,17 @@ class MeterCard extends Component {
state = {
isPowerOn: false,
};
getShadow = async () => {
getShadow = () => {
const deviceId = this.props.item.deviceId;
await this.props.getCurrentSelectedShadow(deviceId);
this.props.getCurrentSelectedShadow(deviceId);
};
componentDidMount = async () => {
await this.getShadow();
console.log('meter props', this.props);
};
componentDidUpdate = async (prevProps, prevState) => {
if (prevProps.item !== this.props.item) {
this.getShadow();
}
if (prevProps.shadow !== this.props.shadow) {
await this.setInitState();
}
......@@ -30,7 +32,7 @@ class MeterCard extends Component {
});
};
render() {
const item = this.props.item;
const { item } = this.props;
const { isPowerOn } = this.state;
return Platform.OS === 'android' ? (
<BaseButton onPress={this.props.onPressEachCard} rippleColor={'#fcc5c5'} style={styles.cardContainer}>
......
import app, { fireStore } from '../../firebase';
import axios from 'axios';
import qs from 'qs';
const baseURL = 'https://us-central1-safetcut-20cdf.cloudfunctions.net';
......@@ -30,8 +29,12 @@ export const getCurrentSelectedShadowAction = shadow => ({
shadow,
});
export const getCurrentSelectedShadow = deviceId => async dispatch => {
const URL = `${baseURL}/shadow/${deviceId}`;
export const getCurrentSelectedShadow = () => async (dispatch, getState) => {
const { currentSelectedDeviceReducer } = getState();
const { currentSelectedData } = currentSelectedDeviceReducer;
const selectedDeviceId = currentSelectedData.deviceId;
const URL = `${baseURL}/shadow/${selectedDeviceId}`;
const idToken = await app.auth().currentUser.getIdToken(true);
const options = {
headers: { Authorization: `Token ${idToken}` },
......@@ -72,6 +75,7 @@ export const updateDetail = (type, value) => async (dispatch, getState) => {
const { currentSelectedDeviceReducer } = getState();
const { currentSelectedData } = currentSelectedDeviceReducer;
const selectedDeviceId = currentSelectedData.deviceId;
//cut deviceId
const { deviceId, ...currentSelectedMainData } = currentSelectedData;
const getNewData = () => {
......
import { fireStore } from '../../firebase';
import app, { fireStore } from '../../firebase';
import _ from 'lodash';
import axios from 'axios';
import moment from 'moment';
const baseURL = 'https://us-central1-safetcut-20cdf.cloudfunctions.net';
export const GET_TIMERS = 'GET_TIMERS';
export const GET_TIMERS_LOADING = 'GET_TIMERS_LOADING';
......@@ -39,7 +43,7 @@ export const loadingTimersAction = bool => ({
isLoading: bool,
});
export const errorAction = error => ({
export const errorTimersAction = error => ({
type: GET_TIMERS_ERROR,
error,
});
......@@ -161,7 +165,7 @@ export const getTimers = () => async (dispatch, getState) => {
allTimersData = [...mainDeviceTimers, ...subBreakersTimers];
dispatch(getTimersAction(allTimersData));
} catch (error) {
dispatch(errorAction(error.message || error || 'Error'));
dispatch(errorTimersAction(error.message || error || 'Error'));
}
dispatch(loadingTimersAction(false));
};
......@@ -223,7 +227,8 @@ export const createNewTimer = (mcbLinkId, subBreakerId, settingTime) => async (d
const docRef = getDocRef();
const currentNumTimers = countingTimers();
if (currentNumTimers < 5) {
// limit timers
if (currentNumTimers < 6) {
const resRef = await docRef.add(settingTime);
//assign more timers details
......@@ -373,6 +378,70 @@ export const deleteTimer = selectedData => async (dispatch, getState) => {
}
};
export const writeTimersToShadow = () => async (dispatch, getState) => {
const { currentSelectedDeviceReducer, timersReducer } = getState();
const { allTimers } = timersReducer;
const { currentSelectedData } = currentSelectedDeviceReducer;
const selectedDeviceId = currentSelectedData.deviceId;
const URL = `${baseURL}/shadow/${selectedDeviceId}`;
const idToken = await app.auth().currentUser.getIdToken(true);
const groupTypeData = _.groupBy(allTimers, 'type');
const tempSubBreakerTimersData = _.groupBy(
groupTypeData.subBreaker,
tempSubBreakerTimersData =>
'L' + tempSubBreakerTimersData.mcbLinkId + 'B' + tempSubBreakerTimersData.subBreakerId + 'T'
);
const tempMainBreakerTimersData = { MAB1T: groupTypeData.main };
console.log('mainBreakerTimersData', mainBreakerTimersData);
console.log('tempSubBreakerTimersData', tempSubBreakerTimersData);
//convert Timer to shadow format
const mainBreakerTimersData = tempMainBreakerTimersData.MAB1T.map((timer, index) => {
const isActive = timer.isActive ? '1' : '0';
const isPowerOn = timer.isPowerOn ? '1' : '0';
const repeatOn = () => {
let binaryFormat = timer.repeatOn
.map(item => {
return item.isRepeat ? +'1' : +'0';
})
.toString()
.replace(/\,/g, '');
const decimalFormat = parseInt(Number(binaryFormat), 2);
return decimalFormat;
};
const hour = moment(timer.timer).format('HH');
const minute = moment(timer.timer).format('mm');
console.log({ [`T${index + 1}`]: `${isActive},${isPowerOn},${repeatOn()},${hour},${minute}` });
const writingData = { [`T${index + 1}`]: `${isActive},${isPowerOn},${repeatOn()},${hour},${minute}` };
return writingData;
});
console.log(mainBreakerTimersData[0]);
const data = { data: { L1B1T: { T1: '1,1,127,8,0' } } };
const options = {
method: 'PUT',
headers: { Authorization: `Token ${idToken}`, 'content-Type': 'text/plain' },
url: URL,
data: JSON.stringify(data).replace(/\"/g, ''),
};
try {
// const response = await axios(options);
// console.log(response);
} catch (error) {
console.log(error);
}
};
// export const deleteTimer2 = selectedData => async (dispatch, getState) => {
// const { currentSelectedDeviceReducer } = getState();
// const { selectedDeviceId } = currentSelectedDeviceReducer;
......@@ -511,7 +580,7 @@ export const deleteTimer = selectedData => async (dispatch, getState) => {
// dispatch(getTimersAction(allTimersData));
// } catch (error) {
// dispatch(errorAction(error.message || error || 'Error'));
// dispatch(errorTimersAction(error.message || error || 'Error'));
// }
// dispatch(loadingTimersAction(false));
// };
......@@ -564,7 +633,7 @@ export const deleteTimer = selectedData => async (dispatch, getState) => {
// dispatch(getTimersAction(allTimersData));
// dispatch(loadingTimersAction(false));
// } catch (error) {
// dispatch(errorAction(error.message || error || 'Error'));
// dispatch(errorTimersAction(error.message || error || 'Error'));
// dispatch(loadingTimersAction(false));
// }
// };
......@@ -10,7 +10,7 @@ import {
loadingAction,
setMainStatus,
} from '../../../reduxStore/actions/currentSelectedAction';
import { getTimers, loadingTimersAction } from '../../../reduxStore/actions/timersAction';
import { getTimers, loadingTimersAction, errorTimersAction } from '../../../reduxStore/actions/timersAction';
import { isIphoneX } from '../../../utils/isPhoneX';
import { withNavigationFocus } from 'react-navigation';
......@@ -85,7 +85,7 @@ class SmartMeterDetailScreen extends Component {
await this.props.setMainStatus(value);
let test = setInterval(() => {
this.props.getCurrentSelectedShadow(deviceId);
this.props.getCurrentSelectedShadow();
console.log('getShadow');
}, 2000);
......@@ -108,7 +108,7 @@ class SmartMeterDetailScreen extends Component {
componentWillUnmount = () => {
this._mcbLinksListMounted = false;
//reset new loading to redux fix blink screen
//reset new loading to redux; fix blink screen
this.props.loadingAction(true);
this.props.loadingTimersAction(true);
};
......@@ -304,7 +304,10 @@ class SmartMeterDetailScreen extends Component {
refreshControl={
<RefreshControl
refreshing={this._mcbLinksListMounted && (isLoadingList || isLoadingValues)}
onRefresh={async () => await this.getData()}
onRefresh={async () => {
await this.getData();
this.props.errorTimersAction(null);
}}
title="Pull to refresh"
tintColor={color.primary}
titleColor={color.darkGrey}
......@@ -374,6 +377,7 @@ const mapDispatchToProps = {
loadingTimersAction,
getTimers,
setMainStatus,
errorTimersAction,
};
const withNavSmartMeterDetailScreen = withNavigationFocus(SmartMeterDetailScreen);
......
......@@ -25,6 +25,7 @@ import {
createNewTimer,
editTimer,
deleteTimer,
writeTimersToShadow,
} from '../../../reduxStore/actions/timersAction';
import Modal from 'react-native-modalbox';
import moment from 'moment';
......@@ -250,6 +251,7 @@ class TimerScreen extends PureComponent {
selectedSubBreaker: null,
});
}
this.props.writeTimersToShadow();
}
if (prevProps.existedMcbLinksData !== this.props.existedMcbLinksData) {
this.setPickerSelectData();
......@@ -712,6 +714,7 @@ const mapDispatchToProps = {
createNewTimer,
editTimer,
deleteTimer,
writeTimersToShadow,
};
export default connect(
......
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