-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: NFC support for canteen cards to query the balance
- Loading branch information
Showing
23 changed files
with
623 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
save-exact=true | ||
|
||
@capawesome-team:registry=https://npm.pkg.github.com |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/app/core/services/capacitor/capacitor-nfc/capacitor-nfc.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { Injectable, NgZone } from '@angular/core'; | ||
import { Capacitor } from '@capacitor/core'; | ||
import { | ||
ConnectOptions, | ||
Nfc, | ||
NfcTag, | ||
PollingOption, | ||
TransceiveOptions, | ||
TransceiveResult, | ||
WriteOptions, | ||
} from '@capawesome-team/capacitor-nfc'; | ||
import { Observable, ReplaySubject, Subject } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class CapacitorNfcService { | ||
private readonly scannedTagSubject = new Subject<NfcTag>(); | ||
private readonly lastScannedTagSubject = new ReplaySubject<NfcTag>(1); | ||
private readonly sessionCanceledSubject = new Subject<void>(); | ||
private readonly sessionErrorSubject = new Subject<string>(); | ||
|
||
constructor(private readonly ngZone: NgZone) { | ||
void Nfc.removeAllListeners().then(() => { | ||
void Nfc.addListener('nfcTagScanned', event => { | ||
this.ngZone.run(() => { | ||
this.scannedTagSubject.next(event.nfcTag); | ||
this.lastScannedTagSubject.next(event.nfcTag); | ||
}); | ||
}); | ||
void Nfc.addListener('scanSessionCanceled', () => { | ||
this.ngZone.run(() => { | ||
this.sessionCanceledSubject.next(); | ||
}); | ||
}); | ||
void Nfc.addListener('scanSessionError', event => { | ||
this.ngZone.run(() => { | ||
this.sessionErrorSubject.next(event.message); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
public get scannedTag$(): Observable<NfcTag> { | ||
return this.scannedTagSubject.asObservable(); | ||
} | ||
|
||
public get lastScannedTag$(): Observable<NfcTag> { | ||
return this.lastScannedTagSubject.asObservable(); | ||
} | ||
|
||
public get sessionCanceled$(): Observable<void> { | ||
return this.sessionCanceledSubject.asObservable(); | ||
} | ||
|
||
public get sessionError$(): Observable<string> { | ||
return this.sessionErrorSubject.asObservable(); | ||
} | ||
|
||
public async startScanSession(): Promise<void> { | ||
await Nfc.startScanSession({ | ||
pollingOptions: [PollingOption.iso14443, PollingOption.iso15693], | ||
}); | ||
} | ||
|
||
public async stopScanSession(): Promise<void> { | ||
await Nfc.stopScanSession(); | ||
} | ||
|
||
public async write(options: WriteOptions): Promise<void> { | ||
await Nfc.write(options); | ||
} | ||
|
||
public async erase(): Promise<void> { | ||
await Nfc.erase(); | ||
} | ||
|
||
public async format(): Promise<void> { | ||
await Nfc.format(); | ||
} | ||
|
||
public transceive(options: TransceiveOptions): Promise<TransceiveResult> { | ||
return Nfc.transceive(options); | ||
} | ||
|
||
public connect(options: ConnectOptions): Promise<void> { | ||
return Nfc.connect(options); | ||
} | ||
|
||
public async close(): Promise<void> { | ||
await Nfc.close(); | ||
} | ||
|
||
public async openSettings(): Promise<void> { | ||
await Nfc.openSettings(); | ||
} | ||
|
||
public async isSupported(): Promise<boolean> { | ||
const { isSupported } = await Nfc.isSupported(); | ||
return isSupported; | ||
} | ||
|
||
public async isEnabled(): Promise<boolean> { | ||
const isAndroid = Capacitor.getPlatform() === 'android'; | ||
if (!isAndroid) { | ||
return true; | ||
} | ||
const { isEnabled } = await Nfc.isEnabled(); | ||
return isEnabled; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './capacitor-app/capacitor-app.service'; | ||
export * from './capacitor-nfc/capacitor-nfc.service'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './nfc-helper/nfc-helper.service'; | ||
export * from './nfc/nfc.service'; |
23 changes: 23 additions & 0 deletions
23
src/app/core/services/nfc/nfc-helper/nfc-helper.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { NfcUtils } from '@capawesome-team/capacitor-nfc'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class NfcHelperService { | ||
private readonly nfcUtilsInstance = new NfcUtils(); | ||
|
||
constructor() {} | ||
|
||
public convertHexToBytes(hex: string): number[] { | ||
return this.nfcUtilsInstance.convertHexToBytes({ hex }).bytes; | ||
} | ||
|
||
public convertBytesToHex(bytes: number[]): string { | ||
return this.nfcUtilsInstance.convertBytesToHex({ bytes }).hex; | ||
} | ||
|
||
public convertHexToNumber(hex: string): number { | ||
return this.nfcUtilsInstance.convertHexToNumber({ hex }).number; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { NdefMessage, NfcTag, NfcTagTechType } from '@capawesome-team/capacitor-nfc'; | ||
import { Observable } from 'rxjs'; | ||
import { CapacitorNfcService } from '../../capacitor'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class NfcService { | ||
constructor(private readonly capacitorNfcService: CapacitorNfcService) {} | ||
|
||
public get scannedTag$(): Observable<NfcTag> { | ||
return this.capacitorNfcService.scannedTag$; | ||
} | ||
|
||
public get lastScannedTag$(): Observable<NfcTag> { | ||
return this.capacitorNfcService.lastScannedTag$; | ||
} | ||
|
||
public async startScanSession(): Promise<void> { | ||
const isSupported = await this.isSupported(); | ||
if (!isSupported) { | ||
throw this.createNotSupportedError(); | ||
} | ||
const isEnabled = await this.isEnabled(); | ||
if (!isEnabled) { | ||
throw this.createNotEnabledError(); | ||
} | ||
await this.capacitorNfcService.startScanSession(); | ||
} | ||
|
||
public async stopScanSession(): Promise<void> { | ||
const isSupported = await this.isSupported(); | ||
if (!isSupported) { | ||
return; | ||
} | ||
const isEnabled = await this.isEnabled(); | ||
if (!isEnabled) { | ||
return; | ||
} | ||
await this.capacitorNfcService.stopScanSession(); | ||
} | ||
|
||
public async write(message: NdefMessage): Promise<void> { | ||
const isSupported = await this.isSupported(); | ||
if (!isSupported) { | ||
throw this.createNotSupportedError(); | ||
} | ||
const isEnabled = await this.isEnabled(); | ||
if (!isEnabled) { | ||
throw this.createNotEnabledError(); | ||
} | ||
await this.capacitorNfcService.write({ | ||
message, | ||
}); | ||
} | ||
|
||
public async erase(): Promise<void> { | ||
const isSupported = await this.isSupported(); | ||
if (!isSupported) { | ||
throw this.createNotSupportedError(); | ||
} | ||
const isEnabled = await this.isEnabled(); | ||
if (!isEnabled) { | ||
throw this.createNotEnabledError(); | ||
} | ||
await this.capacitorNfcService.erase(); | ||
} | ||
|
||
public async format(): Promise<void> { | ||
const isSupported = await this.isSupported(); | ||
if (!isSupported) { | ||
throw this.createNotSupportedError(); | ||
} | ||
const isEnabled = await this.isEnabled(); | ||
if (!isEnabled) { | ||
throw this.createNotEnabledError(); | ||
} | ||
await this.capacitorNfcService.format(); | ||
} | ||
|
||
public async transceive(techType: NfcTagTechType, data: number[]): Promise<number[]> { | ||
const isSupported = await this.isSupported(); | ||
if (!isSupported) { | ||
throw this.createNotSupportedError(); | ||
} | ||
const isEnabled = await this.isEnabled(); | ||
if (!isEnabled) { | ||
throw this.createNotEnabledError(); | ||
} | ||
const { response } = await this.capacitorNfcService.transceive({ | ||
techType, | ||
data, | ||
}); | ||
return response; | ||
} | ||
|
||
public async connect(techType: NfcTagTechType): Promise<void> { | ||
const isSupported = await this.isSupported(); | ||
if (!isSupported) { | ||
throw this.createNotSupportedError(); | ||
} | ||
const isEnabled = await this.isEnabled(); | ||
if (!isEnabled) { | ||
throw this.createNotEnabledError(); | ||
} | ||
await this.capacitorNfcService.connect({ | ||
techType, | ||
}); | ||
} | ||
|
||
public async close(): Promise<void> { | ||
const isSupported = await this.isSupported(); | ||
if (!isSupported) { | ||
return; | ||
} | ||
const isEnabled = await this.isEnabled(); | ||
if (!isEnabled) { | ||
return; | ||
} | ||
await this.capacitorNfcService.close(); | ||
} | ||
|
||
public isSupported(): Promise<boolean> { | ||
return this.capacitorNfcService.isSupported(); | ||
} | ||
|
||
public isEnabled(): Promise<boolean> { | ||
return this.capacitorNfcService.isEnabled(); | ||
} | ||
|
||
private createNotSupportedError(): Error { | ||
const message = 'Dein Gerät unterstützt kein NFC.'; | ||
return new Error(message); | ||
} | ||
|
||
private createNotEnabledError(): Error { | ||
const message = 'Bitte aktiviere NFC in den Einstellungen deines Geräts.'; | ||
return new Error(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Injectable } from '@angular/core'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class TimeoutService { | ||
public timeout<T>(callback: () => T, timeoutMs: number): Promise<T> { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
const result = callback(); | ||
resolve(result); | ||
}, timeoutMs); | ||
}); | ||
} | ||
} |
Oops, something went wrong.