main
alex 2023-05-23 00:44:12 +02:00
parent dada119106
commit e424876089
7 changed files with 146 additions and 33 deletions

4
App.js
View File

@ -128,7 +128,9 @@ function App() {
]); ]);
const NfcRoute = () => <Nfc />; const NfcRoute = () => <Nfc />;
const SettingsRoute = () => <Settings />; const SettingsRoute = () => (
<Settings setScannerSession={setScannerSession} />
);
const renderScene = BottomNavigation.SceneMap({ const renderScene = BottomNavigation.SceneMap({
nfc: NfcRoute, nfc: NfcRoute,

View File

@ -1,26 +1,23 @@
import {useRef, useState} from 'react'; import {useState} from 'react';
import {Constants} from '../../utils'; import {Constants} from '../../utils';
import {Button, HelperText, Text, TextInput} from 'react-native-paper'; import {Button, HelperText, TextInput} from 'react-native-paper';
import {View} from 'react-native'; import {View} from 'react-native';
import EncryptedStorage from 'react-native-encrypted-storage'; import EncryptedStorage from 'react-native-encrypted-storage';
export default function InitScanner({setScannerSession}) { export default function InitScanner({setScannerSession}) {
const [viewMode, setViewMode] = useState(0);
const [host, setHost] = useState('http://192.168.178.93:8080/v1/scanner'); const [host, setHost] = useState('http://192.168.178.93:8080/v1/scanner');
const [scannerName, setScannerName] = useState(''); const [scannerName, setScannerName] = useState('');
const onAddScanner = () => { const onAddScanner = () => {
console.log(scannerName);
if ( if (
scannerName.length > Constants.GLOBALS.MIN_SCANNER_NAME_LENGTH && scannerName.length >= Constants.GLOBALS.MIN_SCANNER_NAME_LENGTH &&
scannerName.length < Constants.GLOBALS.MAX_SCANNER_NAME_LENGTH scannerName.length <= Constants.GLOBALS.MAX_SCANNER_NAME_LENGTH
) { ) {
console.log('valid');
fetch(host, { fetch(host, {
method: 'POST', method: 'POST',
headers: {'Content-Type': 'application/json'}, headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ body: JSON.stringify({
scannerName: scannerName, scannerName: scannerName,
}), }),
@ -33,8 +30,7 @@ export default function InitScanner({setScannerSession}) {
return Promise.reject(res.status); return Promise.reject(res.status);
}) })
.then(async data => { .then(async data => {
console.log('data', data); await EncryptedStorage.setItem('scanner_host', host);
await EncryptedStorage.setItem('scanner_id', data.Id); await EncryptedStorage.setItem('scanner_id', data.Id);
setScannerSession(data.Session); setScannerSession(data.Session);
}) })

View File

@ -1,7 +1,9 @@
import {useEffect, useState} from 'react'; import {useEffect, useState} from 'react';
import {Text, View} from 'react-native'; import {Text, View} from 'react-native';
import EncryptedStorage from 'react-native-encrypted-storage';
import NfcManager, {Ndef, NfcEvents} from 'react-native-nfc-manager'; import NfcManager, {Ndef, NfcEvents} from 'react-native-nfc-manager';
import {Button, Snackbar} from 'react-native-paper'; import {Button, Snackbar} from 'react-native-paper';
import {Buffer} from 'buffer';
export default function Nfc() { export default function Nfc() {
const [hasNfc, setHasNFC] = useState(null); const [hasNfc, setHasNFC] = useState(null);
@ -22,24 +24,53 @@ export default function Nfc() {
checkIsSupported(); checkIsSupported();
NfcManager.setEventListener(NfcEvents.DiscoverTag, tag => { NfcManager.setEventListener(NfcEvents.DiscoverTag, async tag => {
console.log( const foundTag = Ndef.uri.decodePayload(tag.ndefMessage[0].payload);
'tag found',
tag,
Ndef.uri.decodePayload(tag.ndefMessage[0].payload),
);
setFoundNfcTag(Ndef.uri.decodePayload(tag.ndefMessage[0].payload)); console.log('tag found', foundTag);
setFoundNfcTag(foundTag);
setSnackbarVisible(true); setSnackbarVisible(true);
// fetch try {
const scannerHost = await EncryptedStorage.getItem('scanner_host');
const scannerSession = await EncryptedStorage.getItem(
'scanner_session',
);
setTimeout(() => { console.log('scanner host found', scannerHost, scannerSession);
setSnackbarVisible(false);
}, 2000); fetch(scannerHost + '/scan', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Authorization': scannerSession,
},
body: JSON.stringify({
scanResult: Buffer.from(foundTag).toString('base64'),
}),
})
.then(res => {
if (res.status === 200) {
setSnackbarMessage('Sent to dashboard');
return res.text();
}
setSnackbarMessage('Failed to send');
return Promise.reject(res.status);
})
.then(data => console.log('data', data))
.catch(err => console.error(err));
} catch (err) {
console.error(err);
}
}); });
setTimeout(() => {
setSnackbarVisible(false);
}, 2000);
return () => { return () => {
NfcManager.setEventListener(NfcEvents.DiscoverTag, null); NfcManager.setEventListener(NfcEvents.DiscoverTag, null);
}; };

View File

@ -1,6 +1,65 @@
import {Text, View} from 'react-native'; import {Text, View} from 'react-native';
import {Button} from 'react-native-paper';
import EncryptedStorage from 'react-native-encrypted-storage';
export default function Settings({setScannerSession}) {
const onSignOut = async () => {
try {
const scannerHost = await EncryptedStorage.getItem('scanner_host');
const scannerSession = await EncryptedStorage.getItem('scanner_session');
await EncryptedStorage.removeItem('scanner_host');
console.log('after await ', scannerHost, scannerSession);
setScannerSession();
const response = await fetch(scannerHost, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'X-Authorization': scannerSession,
},
});
const result = await response.text();
console.log('result', result);
} catch (error) {
console.error(error);
}
/*EncryptedStorage.getItem('scanner_host')
.then(scannerHost => {
console.log('scanner host', scannerHost);
fetch(scannerHost, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
'X-Authorization': userSession,
},
})
.then(res => {
if (res.status === 200) {
setSnackbarMessage('Sent to dashboard');
return res.text();
}
setSnackbarMessage('Failed to send');
return Promise.reject(res.status);
})
.then(data => {
console.log('data', data);
})
.catch(err => {
console.error(err);
});
})
.catch(err => console.error(err));*/
};
export default function Settings() {
return ( return (
<View <View
style={{ style={{
@ -9,6 +68,9 @@ export default function Settings() {
alignItems: 'center', alignItems: 'center',
}}> }}>
<Text style={{color: '#fff'}}>Settings</Text> <Text style={{color: '#fff'}}>Settings</Text>
<Button mode="contained" onPress={() => onSignOut()}>
Sign out
</Button>
</View> </View>
); );
} }

32
package-lock.json generated
View File

@ -11,6 +11,7 @@
"@react-native-community/masked-view": "^0.1.11", "@react-native-community/masked-view": "^0.1.11",
"@react-navigation/bottom-tabs": "^6.5.7", "@react-navigation/bottom-tabs": "^6.5.7",
"@react-navigation/native": "^6.1.6", "@react-navigation/native": "^6.1.6",
"buffer": "^6.0.3",
"react": "18.2.0", "react": "18.2.0",
"react-native": "0.71.8", "react-native": "0.71.8",
"react-native-encrypted-storage": "^4.0.3", "react-native-encrypted-storage": "^4.0.3",
@ -5952,6 +5953,29 @@
"readable-stream": "^3.4.0" "readable-stream": "^3.4.0"
} }
}, },
"node_modules/bl/node_modules/buffer": {
"version": "5.7.1",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz",
"integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"dependencies": {
"base64-js": "^1.3.1",
"ieee754": "^1.1.13"
}
},
"node_modules/bplist-creator": { "node_modules/bplist-creator": {
"version": "0.1.0", "version": "0.1.0",
"resolved": "https://registry.npmjs.org/bplist-creator/-/bplist-creator-0.1.0.tgz", "resolved": "https://registry.npmjs.org/bplist-creator/-/bplist-creator-0.1.0.tgz",
@ -6027,9 +6051,9 @@
} }
}, },
"node_modules/buffer": { "node_modules/buffer": {
"version": "5.7.1", "version": "6.0.3",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz",
"integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==",
"funding": [ "funding": [
{ {
"type": "github", "type": "github",
@ -6046,7 +6070,7 @@
], ],
"dependencies": { "dependencies": {
"base64-js": "^1.3.1", "base64-js": "^1.3.1",
"ieee754": "^1.1.13" "ieee754": "^1.2.1"
} }
}, },
"node_modules/buffer-from": { "node_modules/buffer-from": {

View File

@ -13,6 +13,7 @@
"@react-native-community/masked-view": "^0.1.11", "@react-native-community/masked-view": "^0.1.11",
"@react-navigation/bottom-tabs": "^6.5.7", "@react-navigation/bottom-tabs": "^6.5.7",
"@react-navigation/native": "^6.1.6", "@react-navigation/native": "^6.1.6",
"buffer": "^6.0.3",
"react": "18.2.0", "react": "18.2.0",
"react-native": "0.71.8", "react-native": "0.71.8",
"react-native-encrypted-storage": "^4.0.3", "react-native-encrypted-storage": "^4.0.3",

View File

@ -46,10 +46,7 @@ export function UseScannerSession() {
await EncryptedStorage.removeItem('scanner_session'); await EncryptedStorage.removeItem('scanner_session');
} else { } else {
console.log('here2'); console.log('here2');
await EncryptedStorage.setItem( await EncryptedStorage.setItem('scanner_session', scannerSession);
'scanner_session',
JSON.stringify(scannerSession),
);
} }
}; };