Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
safetcut-app
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
atichat
safetcut-app
Commits
7f354269
Commit
7f354269
authored
Sep 25, 2019
by
HaOuiha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use apollo gql call direct api from the app
parent
d36fdaa6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
62 additions
and
361 deletions
+62
-361
reduxStore/actions/allMainDeviceAction.js
+4
-9
reduxStore/actions/currentSelectedAction.js
+5
-57
reduxStore/actions/timersAction.js
+0
-261
screens/Public/AuthLoadingScreen.js
+14
-3
screens/Public/LoginScreen.js
+0
-12
utils/apollo/apollo.js
+39
-19
No files found.
reduxStore/actions/allMainDeviceAction.js
View file @
7f354269
import
app
,
{
fireStore
,
baseURL
}
from
'../../firebase'
;
import
axios
from
'axios'
;
import
_
from
'lodash'
;
import
{
apolloQuery
,
READ_SHADOW
}
from
'../../utils/apollo/apollo'
;
// import { useQuery } from '@apollo/react-hooks';
export
const
GET_ALL_MAIN_DEVICE_INFO
=
'GET_ALL_MAIN_DEVICE_INFO'
;
export
const
GET_ALL_MAIN_DEVICE_INFO_LOADING
=
'GET_ALL_MAIN_DEVICE_INFO_LOADING'
;
...
...
@@ -87,24 +88,18 @@ export const getAllMainDeviceInfo = () => async (dispatch, getState) => {
};
const
getBreakerStatus
=
async
deviceId
=>
{
const
URL
=
`
${
baseURL
}
/shadow/
${
deviceId
}
`
;
const
idToken
=
await
app
.
auth
().
currentUser
.
getIdToken
(
true
);
const
options
=
{
headers
:
{
Authorization
:
`Token
${
idToken
}
`
},
url
:
URL
,
};
try
{
const
{
data
:
{
shadow
:
{
value
},
},
}
=
await
a
xios
(
options
);
}
=
await
a
polloQuery
(
READ_SHADOW
,
{
deviceid
:
deviceId
}
);
const
tempBreakerStatus
=
_
.
pick
(
value
,
[
'SM1'
]);
const
breakerStatus
=
tempBreakerStatus
.
SM1
.
BK_S
;
return
breakerStatus
;
}
catch
(
e
)
{
console
.
log
(
e
||
'error'
);
//
console.log(e || 'error');
return
0
;
}
};
reduxStore/actions/currentSelectedAction.js
View file @
7f354269
import
app
,
{
fireStore
,
baseURL
}
from
'../../firebase'
;
import
axios
from
'axios'
;
import
_
from
'lodash'
;
import
{
apolloQuery
,
apolloMutation
,
READ_SHADOW
,
WRITE_SHADOW
}
from
'../../utils/apollo/apollo'
;
import
{
getExistedConnectedDeviceAction
}
from
'./timersAction'
;
export
const
GET_CURRENT_SELECTED_DATA
=
'GET_CURRENT_SELECTED_DATA'
;
...
...
@@ -47,18 +47,12 @@ export const getCurrentSelectedShadow = deviceId => async (dispatch, getState) =
const
{
currentSelectedData
}
=
currentSelectedDeviceReducer
;
const
selectedDeviceId
=
currentSelectedData
.
deviceId
;
const
URL
=
`
${
baseURL
}
/shadow/
${
deviceId
||
selectedDeviceId
}
`
;
const
idToken
=
await
app
.
auth
().
currentUser
.
getIdToken
(
true
);
const
options
=
{
headers
:
{
Authorization
:
`Token
${
idToken
}
`
},
url
:
URL
,
};
try
{
const
{
data
:
{
shadow
:
{
value
},
},
}
=
await
a
xios
(
options
);
}
=
await
a
polloQuery
(
READ_SHADOW
,
{
deviceid
:
deviceId
||
selectedDeviceId
}
);
const
tempBreakersStatus
=
_
.
pick
(
value
,
[
'CMD_MSG'
,
'SM1'
,
'MD_STA'
,
'ML0'
,
'ML1'
,
'ML2'
,
'ML3'
,
'ML4'
]);
const
breakersStatus
=
{
...
tempBreakersStatus
,
SM1
:
{
BK_S
:
tempBreakersStatus
.
SM1
.
BK_S
}
};
...
...
@@ -71,13 +65,6 @@ export const getCurrentSelectedShadow = deviceId => async (dispatch, getState) =
};
export
const
getCurrentSelectedData
=
deviceId
=>
async
dispatch
=>
{
const
URL
=
`
${
baseURL
}
/shadow/
${
deviceId
}
`
;
const
idToken
=
await
app
.
auth
().
currentUser
.
getIdToken
(
true
);
const
options
=
{
headers
:
{
Authorization
:
`Token
${
idToken
}
`
},
url
:
URL
,
};
try
{
//FIRESTORE
dispatch
(
loadingAction
(
true
));
...
...
@@ -94,7 +81,7 @@ export const getCurrentSelectedData = deviceId => async dispatch => {
data
:
{
shadow
:
{
value
},
},
}
=
await
a
xios
(
options
);
}
=
await
a
polloQuery
(
READ_SHADOW
,
{
deviceid
:
deviceId
||
selectedDeviceId
}
);
const
tempBreakersStatus
=
_
.
pick
(
value
,
[
'CMD_MSG'
,
'SM1'
,
'MD_STA'
,
'ML0'
,
'ML1'
,
'ML2'
,
'ML3'
,
'ML4'
]);
const
breakersStatus
=
{
...
tempBreakersStatus
,
SM1
:
{
BK_S
:
tempBreakersStatus
.
SM1
.
BK_S
}
};
...
...
@@ -150,9 +137,7 @@ export const setMainStatus = value => async (dispatch, getState) => {
const
{
currentSelectedDeviceReducer
}
=
getState
();
const
{
currentSelectedData
,
breakerStatus
}
=
currentSelectedDeviceReducer
;
const
selectedDeviceId
=
currentSelectedData
.
deviceId
;
const
URL
=
`
${
baseURL
}
/shadow/
${
selectedDeviceId
}
`
;
const
idToken
=
await
app
.
auth
().
currentUser
.
getIdToken
(
true
);
const
command
=
()
=>
{
switch
(
value
)
{
case
true
:
...
...
@@ -163,22 +148,14 @@ export const setMainStatus = value => async (dispatch, getState) => {
};
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
{
// await axios(options);
const
{
data
:
{
writeShadow
:
{
value
:
{
CMD_MSG
},
},
},
}
=
await
a
xios
(
options
);
}
=
await
a
polloMutation
(
WRITE_SHADOW
,
{
deviceid
:
selectedDeviceId
,
data
:
data
}
);
console
.
log
(
value
);
console
.
log
(
'CMD_MSG'
,
CMD_MSG
);
const
desiredBreakerStatus
=
{
...
breakerStatus
,
CMD_MSG
,
SM1
:
{
...
breakerStatus
.
SM1
,
BK_S
:
value
?
1
:
0
}
};
...
...
@@ -194,9 +171,7 @@ export const setSubBreakerStatus = (value, MainIndex, SubIndex) => async (dispat
const
{
currentSelectedDeviceReducer
}
=
getState
();
const
{
currentSelectedData
,
breakerStatus
}
=
currentSelectedDeviceReducer
;
const
selectedDeviceId
=
currentSelectedData
.
deviceId
;
const
URL
=
`
${
baseURL
}
/shadow/
${
selectedDeviceId
}
`
;
const
idToken
=
await
app
.
auth
().
currentUser
.
getIdToken
(
true
);
const
command
=
()
=>
{
switch
(
value
)
{
case
true
:
...
...
@@ -207,22 +182,14 @@ export const setSubBreakerStatus = (value, MainIndex, SubIndex) => async (dispat
};
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
{
// await axios(options);
const
{
data
:
{
writeShadow
:
{
value
:
{
CMD_MSG
},
},
},
}
=
await
a
xios
(
options
);
}
=
await
a
polloMutation
(
WRITE_SHADOW
,
{
deviceid
:
selectedDeviceId
,
data
:
data
}
);
console
.
log
(
value
);
console
.
log
(
'CMD_MSG'
,
CMD_MSG
);
const
desiredBreakerStatus
=
{
...
...
@@ -308,22 +275,3 @@ export const deleteConnectedDevice = data => async (dispatch, getState) => {
console
.
log
(
error
);
}
};
// export const getCurrentSelectedData = (deviceId, mcbLinkId, subBreakerId) => (dispatch, getState) => {
// const { currentSelectedDeviceReducer } = getState();
// const { selectedDeviceId } = currentSelectedDeviceReducer;
// const currentSelectedDeviceId = deviceId || selectedDeviceId;
// try {
// const stopListener = fireStore
// .collection('device')
// .doc(currentSelectedDeviceId)
// .onSnapshot(mainDeviceSanpshot => {
// dispatch(getCurrentSelectedDataAction(mainDeviceSanpshot.data()));
// });
// return stopListener;
// } catch (e) {
// alert(e);
// }
// };
reduxStore/actions/timersAction.js
View file @
7f354269
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'
;
...
...
@@ -412,260 +408,3 @@ export const deleteTimer = selectedData => async (dispatch, getState) => {
alert
(
e
);
}
};
// 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(groupTypeData);
// console.log('------------------------------');
// console.log(tempMainBreakerTimersData);
// console.log(tempSubBreakerTimersData);
// //convert Timer to shadow format
// const mainBreakerTimersData = tempMainBreakerTimersData.MAB1T.reduce((accumulator, timer, index) => {
// const isActive = timer.isActive ? '1' : '0';
// const isPowerOn = timer.isPowerOn ? '1' : '0';
// const repeatOn = () => {
// let binaryFormat = timer.repeatOn
// .map(item => (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');
// let timerShadowFormat = { [`T${index + 1}`]: `${isActive},${isPowerOn},${repeatOn()},${hour},${minute}` };
// return { ...accumulator, ...timerShadowFormat };
// }, {});
// console.log('mainBreakerTimersData', mainBreakerTimersData);
// 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;
// const { mcbLinkId, subBreakerId, key } = selectedData;
// let docRef = null;
// try {
// dispatch(loadingTimersAction(true));
// if (key) {
// if (mcbLinkId && subBreakerId) {
// docRef = fireStore
// .collection('device')
// .doc(selectedDeviceId)
// .collection('mcbLinks')
// .doc(mcbLinkId)
// .collection('subBreakers')
// .doc(subBreakerId)
// .collection('Timers')
// .doc(key);
// } else {
// docRef = fireStore
// .collection('device')
// .doc(selectedDeviceId)
// .collection('Timers')
// .doc(key);
// }
// await docRef.delete();
// }
// } catch (e) {
// alert(e);
// }
// dispatch(loadingTimersAction(false));
// };
// export const getTimers = () => async (dispatch, getState) => {
// const { currentSelectedDeviceReducer } = getState();
// const { selectedDeviceId } = currentSelectedDeviceReducer;
// try {
// dispatch(loadingTimersAction(true));
// let allTimersData = [];
// let mainDeviceTimers = [];
// let mcbLinksTimers = [];
// let subBreakersTimers = [];
// //main device section
// /*-------------------------------------*/
// const selectedDevice = await fireStore
// .collection('device')
// .doc(selectedDeviceId)
// .get();
// const selectedDeviceData = await selectedDevice.data();
// mainDeviceTimers.push(selectedDeviceData.timers);
// selectedDeviceData.timers.map(timer =>
// allTimersData.push(Object.assign(timer, { type: selectedDeviceData.type, name: selectedDeviceData.name }))
// );
// //mcblink section
// /*-------------------------------------*/
// const selectedDeviceMcbLinks = await fireStore
// .collection('device')
// .doc(selectedDeviceId)
// .collection('mcbLinks')
// .get();
// let existedMcbLinks = [];
// let selectedDeviceMcbLinksData = [];
// selectedDeviceMcbLinks.docs.map(mcbLink => {
// existedMcbLinks.push(mcbLink.data().id);
// selectedDeviceMcbLinksData.push(mcbLink.data());
// let mcbLinkData = mcbLink.data();
// mcbLinksTimers.push(mcbLinkData.timers);
// mcbLinkData.timers.map(timer => {
// allTimersData.push(
// Object.assign(timer, {
// id: mcbLink.data().id,
// type: mcbLink.data().type,
// name: mcbLink.data().name,
// })
// );
// });
// });
// //subbreaker section
// /*-------------------------------------*/
// let existedSubBreakers = [];
// let selectedDeviceSubBreakersData = [];
// selectedDeviceMcbLinksData.map(async mcbLink => {
// let selectedDeviceSubBreakers = await fireStore
// .collection('device')
// .doc(selectedDeviceId)
// .collection('mcbLinks')
// .doc(mcbLink.id.toString())
// .collection('subBreakers')
// .get();
// return selectedDeviceSubBreakers.docs.map(subBreaker => {
// existedSubBreakers.push([mcbLink.id, subBreaker.data().id]);
// selectedDeviceSubBreakersData.push(subBreaker.data());
// let subBreakerData = subBreaker.data();
// subBreakersTimers.push(subBreakerData.timers);
// subBreakerData.timers.map(timer => {
// allTimersData.push(
// Object.assign(timer, {
// mcbLinkId: mcbLink.id,
// id: subBreaker.data().id,
// type: subBreaker.data().type,
// name: subBreaker.data().name,
// })
// );
// });
// });
// });
// console.log('selectedDeviceMcbLinksData', selectedDeviceMcbLinksData);
// const existedData = { existedMcbLinks, existedSubBreakers };
// // console.log(mainDeviceTimers, mcbLinksTimers, subBreakersTimers);
// dispatch(getExistedDataAction(existedData));
// dispatch(getMainDeviceTImersAction(mainDeviceTimers));
// dispatch(getMcbLinksTimersAction(mcbLinksTimers));
// dispatch(getSubBreakersTimersAction(subBreakersTimers));
// dispatch(getTimersAction(allTimersData));
// } catch (error) {
// dispatch(errorTimersAction(error.message || error || 'Error'));
// }
// dispatch(loadingTimersAction(false));
// };
// export const getTimers = () => async (dispatch, getState) => {
// dispatch(loadingTimersAction(true));
// const { currentSelectedDeviceReducer } = getState();
// const { selectedDeviceId } = currentSelectedDeviceReducer;
// try {
// const allSelectedDevice = await fireStore
// .collection('device')
// .doc(selectedDeviceId)
// .get();
// let allSelectedDeviceData = await allSelectedDevice.data();
// let allTimersData = [];
// allSelectedDeviceData.timers.map(timer =>
// allTimersData.push(
// Object.assign(timer, { type: allSelectedDeviceData.type, name: allSelectedDeviceData.name })
// )
// );
// allSelectedDeviceData.mcbLinks.map(mcbLink =>
// mcbLink.timers.map(timer =>
// allTimersData.push(
// Object.assign(timer, {
// id: mcbLink.id,
// type: mcbLink.type,
// name: mcbLink.name,
// })
// )
// )
// );
// allSelectedDeviceData.mcbLinks.map(mcbLink =>
// mcbLink.subBreakers.map(subBreaker =>
// subBreaker.timers.map(timer =>
// allTimersData.push(
// Object.assign(timer, {
// mcbId: mcbLink.id,
// id: subBreaker.id,
// type: subBreaker.type,
// name: subBreaker.name,
// })
// )
// )
// )
// );
// // console.log('allTimersData', allTimersData);
// dispatch(getTimersAction(allTimersData));
// dispatch(loadingTimersAction(false));
// } catch (error) {
// dispatch(errorTimersAction(error.message || error || 'Error'));
// dispatch(loadingTimersAction(false));
// }
// };
screens/Public/AuthLoadingScreen.js
View file @
7f354269
import
React
,
{
Component
}
from
'react'
;
import
{
ActivityIndicator
,
View
,
StatusBar
,
Image
}
from
'react-native'
;
import
AsyncStorage
from
'@react-native-community/async-storage'
;
//expo not yet support
import
AsyncStorage
from
'@react-native-community/async-storage'
;
import
logo
from
'../../assets/images/logo2.png'
;
import
{
width
}
from
'../../constants/Layout'
;
import
app
from
'../../firebase'
;
...
...
@@ -29,19 +29,30 @@ class AuthLoadingScreen extends Component {
},
900
);
// 1200
};
getToken
=
async
()
=>
{
const
{
uid
}
=
app
.
auth
().
currentUser
;
const
user
=
await
fireStore
.
collection
(
'user'
)
.
doc
(
uid
)
.
get
();
const
{
token
}
=
user
.
data
();
await
AsyncStorage
.
setItem
(
'token'
,
token
);
};
bootstrapAsync
=
async
()
=>
{
this
.
showLogoThenIndicator
();
try
{
// Preload data from an external API && Preload data using AsyncStorage
const
alreadyLaunched
=
await
AsyncStorage
.
getItem
(
'alreadyLaunched'
);
const
RememberedLogin
=
await
AsyncStorage
.
getItem
(
'RememberedLogin'
);
console
.
log
(
'alreadyLaunched'
,
alreadyLaunched
);
console
.
log
(
'RememberedLogin'
,
RememberedLogin
);
//
console.log('alreadyLaunched', alreadyLaunched);
//
console.log('RememberedLogin', RememberedLogin);
app
.
auth
().
onAuthStateChanged
(
async
user
=>
{
if
(
user
)
{
console
.
log
(
user
);
await
this
.
props
.
getCurrentUser
(
user
);
await
this
.
getToken
();
this
.
props
.
navigation
.
navigate
(
RememberedLogin
===
'true'
?
'Main'
:
'Login'
);
// createMockData();
}
else
{
...
...
screens/Public/LoginScreen.js
View file @
7f354269
...
...
@@ -41,18 +41,6 @@ export default class LoginScreen extends Component {
}
};
// componentDidMount = async () => {
// const currentUser = await app.auth().currentUser;
// if (currentUser) {
// try {
// await app.auth().signOut();
// await AsyncStorage.removeItem('RememberedLogin');
// } catch (error) {
// alert(error);
// }
// }
// };
render
()
{
return
(
<
KeyboardAvoidingView
style
=
{
theme
.
introContainer
}
>
...
...
utils/apollo/apollo.js
View file @
7f354269
import
ApolloClient
from
'apollo-boost'
;
import
gql
from
'graphql-tag'
;
import
AsyncStorage
from
'@react-native-community/async-storage'
;
const
BASE_URL
=
'https://gqlv2.nexpie.io'
;
const
BASE_URL
=
'https://gqlv1.nexpie.io'
;
const
client
=
new
ApolloClient
({
uri
:
BASE_URL
,
request
:
async
operation
=>
{
const
token
=
await
AsyncStorage
.
getItem
(
'token'
);
operation
.
setContext
({
headers
:
{
authorization
:
token
?
`Bearer
${
token
}
`
:
''
,
},
});
},
});
//https://www.apollographql.com/docs/react/api/apollo-client/
const
apolloQuery
=
async
(
query
,
variables
)
=>
{
return
await
client
.
query
({
query
,
variables
,
fetchPolicy
:
'network-only'
,
errorPolicy
:
'all'
,
});
};
const
apolloMutation
=
async
(
mutation
,
variables
)
=>
{
return
await
client
.
mutate
({
mutation
,
variables
,
errorPolicy
:
'all'
,
});
};
export
{
client
,
apolloQuery
,
apolloMutation
};
/*----------------------------------------------------------------- */
const
READ_SHADOW
=
gql
`
query
readShadow
($deviceid: String!) {
query($deviceid: String!) {
shadow(deviceid: $deviceid) {
deviceid
value
...
...
@@ -13,26 +48,11 @@ const READ_SHADOW = gql`
`
;
const
WRITE_SHADOW
=
gql
`
mutation
writeShadow($deviceid: String!, $data: String
!) {
mutation
($deviceid: String!, $data: JSON
!) {
writeShadow(deviceid: $deviceid, value: $data) {
deviceid
value
}
}
`
;
const
client
=
new
ApolloClient
({
uri
:
BASE_URL
,
request
:
async
operation
=>
{
// const token = localStorage.getItem('token');
operation
.
setContext
({
headers
:
{
authorization
:
token
?
`Bearer
${
token
}
`
:
''
,
},
});
},
});
export
{
client
};
export
{
READ_SHADOW
,
WRITE_SHADOW
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment