From e424876089a0f3d22293fa812552d1dba1162374 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 23 May 2023 00:44:12 +0200 Subject: [PATCH] scanner --- App.js | 4 ++- Pages/InitScanner/index.js | 20 +++++------- Pages/Nfc/index.js | 53 ++++++++++++++++++++++++------- Pages/Settings/index.js | 64 +++++++++++++++++++++++++++++++++++++- package-lock.json | 32 ++++++++++++++++--- package.json | 1 + utils.js | 5 +-- 7 files changed, 146 insertions(+), 33 deletions(-) diff --git a/App.js b/App.js index 5c6413f..91c967b 100644 --- a/App.js +++ b/App.js @@ -128,7 +128,9 @@ function App() { ]); const NfcRoute = () => ; - const SettingsRoute = () => ; + const SettingsRoute = () => ( + + ); const renderScene = BottomNavigation.SceneMap({ nfc: NfcRoute, diff --git a/Pages/InitScanner/index.js b/Pages/InitScanner/index.js index 5f4637b..44ce67a 100644 --- a/Pages/InitScanner/index.js +++ b/Pages/InitScanner/index.js @@ -1,26 +1,23 @@ -import {useRef, useState} from 'react'; +import {useState} from 'react'; 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 EncryptedStorage from 'react-native-encrypted-storage'; export default function InitScanner({setScannerSession}) { - const [viewMode, setViewMode] = useState(0); const [host, setHost] = useState('http://192.168.178.93:8080/v1/scanner'); const [scannerName, setScannerName] = useState(''); const onAddScanner = () => { - console.log(scannerName); - if ( - scannerName.length > Constants.GLOBALS.MIN_SCANNER_NAME_LENGTH && - scannerName.length < Constants.GLOBALS.MAX_SCANNER_NAME_LENGTH + scannerName.length >= Constants.GLOBALS.MIN_SCANNER_NAME_LENGTH && + scannerName.length <= Constants.GLOBALS.MAX_SCANNER_NAME_LENGTH ) { - console.log('valid'); - fetch(host, { method: 'POST', - headers: {'Content-Type': 'application/json'}, + headers: { + 'Content-Type': 'application/json', + }, body: JSON.stringify({ scannerName: scannerName, }), @@ -33,8 +30,7 @@ export default function InitScanner({setScannerSession}) { return Promise.reject(res.status); }) .then(async data => { - console.log('data', data); - + await EncryptedStorage.setItem('scanner_host', host); await EncryptedStorage.setItem('scanner_id', data.Id); setScannerSession(data.Session); }) diff --git a/Pages/Nfc/index.js b/Pages/Nfc/index.js index ade1db4..e070377 100644 --- a/Pages/Nfc/index.js +++ b/Pages/Nfc/index.js @@ -1,7 +1,9 @@ import {useEffect, useState} from 'react'; import {Text, View} from 'react-native'; +import EncryptedStorage from 'react-native-encrypted-storage'; import NfcManager, {Ndef, NfcEvents} from 'react-native-nfc-manager'; import {Button, Snackbar} from 'react-native-paper'; +import {Buffer} from 'buffer'; export default function Nfc() { const [hasNfc, setHasNFC] = useState(null); @@ -22,24 +24,53 @@ export default function Nfc() { checkIsSupported(); - NfcManager.setEventListener(NfcEvents.DiscoverTag, tag => { - console.log( - 'tag found', - tag, - Ndef.uri.decodePayload(tag.ndefMessage[0].payload), - ); + NfcManager.setEventListener(NfcEvents.DiscoverTag, async tag => { + const foundTag = Ndef.uri.decodePayload(tag.ndefMessage[0].payload); - setFoundNfcTag(Ndef.uri.decodePayload(tag.ndefMessage[0].payload)); + console.log('tag found', foundTag); + setFoundNfcTag(foundTag); setSnackbarVisible(true); - // fetch + try { + const scannerHost = await EncryptedStorage.getItem('scanner_host'); + const scannerSession = await EncryptedStorage.getItem( + 'scanner_session', + ); - setTimeout(() => { - setSnackbarVisible(false); - }, 2000); + console.log('scanner host found', scannerHost, scannerSession); + + 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 () => { NfcManager.setEventListener(NfcEvents.DiscoverTag, null); }; diff --git a/Pages/Settings/index.js b/Pages/Settings/index.js index 5ad6d23..fffa000 100644 --- a/Pages/Settings/index.js +++ b/Pages/Settings/index.js @@ -1,6 +1,65 @@ 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 ( Settings + ); } diff --git a/package-lock.json b/package-lock.json index 434bed2..88b2e03 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "@react-native-community/masked-view": "^0.1.11", "@react-navigation/bottom-tabs": "^6.5.7", "@react-navigation/native": "^6.1.6", + "buffer": "^6.0.3", "react": "18.2.0", "react-native": "0.71.8", "react-native-encrypted-storage": "^4.0.3", @@ -5952,6 +5953,29 @@ "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": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/bplist-creator/-/bplist-creator-0.1.0.tgz", @@ -6027,9 +6051,9 @@ } }, "node_modules/buffer": { - "version": "5.7.1", - "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", - "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", + "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==", "funding": [ { "type": "github", @@ -6046,7 +6070,7 @@ ], "dependencies": { "base64-js": "^1.3.1", - "ieee754": "^1.1.13" + "ieee754": "^1.2.1" } }, "node_modules/buffer-from": { diff --git a/package.json b/package.json index 78cfae0..697cf18 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "@react-native-community/masked-view": "^0.1.11", "@react-navigation/bottom-tabs": "^6.5.7", "@react-navigation/native": "^6.1.6", + "buffer": "^6.0.3", "react": "18.2.0", "react-native": "0.71.8", "react-native-encrypted-storage": "^4.0.3", diff --git a/utils.js b/utils.js index a152d4d..eb42991 100644 --- a/utils.js +++ b/utils.js @@ -46,10 +46,7 @@ export function UseScannerSession() { await EncryptedStorage.removeItem('scanner_session'); } else { console.log('here2'); - await EncryptedStorage.setItem( - 'scanner_session', - JSON.stringify(scannerSession), - ); + await EncryptedStorage.setItem('scanner_session', scannerSession); } };