implement pairing storage

This commit is contained in:
Michele Balistreri 2021-09-30 16:36:28 +03:00 committed by Iuri Matias
parent 899c342414
commit 5cf58a78e5
3 changed files with 40 additions and 1 deletions

View File

@ -1,6 +1,6 @@
import NimQml, chronicles, std/wrapnils import NimQml, chronicles, std/wrapnils
import status/[signals, status, keycard] import status/[signals, status, keycard]
import view import view, pairing
logScope: logScope:
topics = "keycard-model" topics = "keycard-model"
@ -24,6 +24,12 @@ proc reset*(self: KeycardController) =
discard discard
proc attemptOpenSecureChannel(self: KeycardController) : bool = proc attemptOpenSecureChannel(self: KeycardController) : bool =
let pairing = self.view.pairings.getPairing(self.view.appInfo.instanceUID)
if pairing == "":
return false
# actually open secure channel
return false return false
proc getCardState(self: KeycardController) = proc getCardState(self: KeycardController) =

View File

@ -0,0 +1,30 @@
import json, os, std/wrapnils
import ../../constants
let PAIRINGSTORE = joinPath(DATADIR, "keycard-pairings.json")
type KeycardPairingController* = ref object
store: JsonNode
proc newPairingController*(): KeycardPairingController =
result = KeycardPairingController()
if fileExists(PAIRINGSTORE):
result.store = parseJSON(readFile(PAIRINGSTORE))
else:
result.store = %*{}
proc save(self: KeycardPairingController) =
writeFile(PAIRINGSTORE, $self.store)
proc addPairing*(self: KeycardPairingController, instanceUID: string, pairing: string) =
self.store[instanceUID] = %* pairing
self.save()
proc removePairing*(self: KeycardPairingController, instanceUID: string) =
self.store.delete(instanceUID)
self.save()
proc getPairing*(self: KeycardPairingController, instanceUID: string): string =
let node = self.store{instanceUID}
return ?.node.getStr()

View File

@ -1,6 +1,7 @@
import NimQml, chronicles import NimQml, chronicles
import status/[status, keycard] import status/[status, keycard]
import status/types/keycard as keycardtypes import status/types/keycard as keycardtypes
import pairing
logScope: logScope:
topics = "keycard-model" topics = "keycard-model"
@ -20,6 +21,7 @@ type
QtObject: QtObject:
type KeycardView* = ref object of QObject type KeycardView* = ref object of QObject
status*: Status status*: Status
pairings*: KeycardPairingController
cardState*: CardState cardState*: CardState
appInfo*: KeycardApplicationInfo appInfo*: KeycardApplicationInfo
@ -32,6 +34,7 @@ QtObject:
proc newKeycardView*(status: Status): KeycardView = proc newKeycardView*(status: Status): KeycardView =
new(result, delete) new(result, delete)
result.status = status result.status = status
result.pairings = newPairingController()
result.setup result.setup
proc cardConnected*(self: KeycardView) {.signal.} proc cardConnected*(self: KeycardView) {.signal.}