From b4ae25eb576bcc996d1d56379b2e0648d5e2bcd3 Mon Sep 17 00:00:00 2001 From: Tau Date: Sun, 28 Apr 2019 21:00:05 -0400 Subject: [PATCH] aimedb: Wire up backend --- src/aimedb/handler.ts | 121 ++++++++++++++++++++++++++++++------------ src/aimedb/index.ts | 8 ++- 2 files changed, 95 insertions(+), 34 deletions(-) diff --git a/src/aimedb/handler.ts b/src/aimedb/handler.ts index c847833..9eee352 100644 --- a/src/aimedb/handler.ts +++ b/src/aimedb/handler.ts @@ -1,49 +1,104 @@ -import { AimeRequest } from "./request"; -import { AimeResponse } from "./response"; +import { Repositories } from "./repo"; +import * as Req from "./request"; +import * as Res from "./response"; -export function dispatch(req: AimeRequest): AimeResponse | undefined { +function hello( + rep: Repositories, + req: Req.HelloRequest, + now: Date +): Res.HelloResponse { + console.log("Aimedb: Hello"); + + return { type: req.type, status: 1 }; +} + +function campaign( + rep: Repositories, + req: Req.CampaignRequest, + now: Date +): Res.CampaignResponse { + console.log("Aimedb: Campaign stuff"); + + return { type: req.type, status: 1 }; +} + +async function lookup( + rep: Repositories, + req: Req.LookupRequest, + now: Date +): Promise { + console.log("Aimedb: Mifare lookup v1", req.luid); + + return { + type: req.type, + status: 1, + aimeId: await rep.cards().lookup(req.luid, now), + registerLevel: "none", + }; +} + +async function lookup2( + rep: Repositories, + req: Req.LookupRequest2, + now: Date +): Promise { + console.log("Aimedb: Mifare lookup v2", req.luid); + + return { + type: req.type, + status: 1, + aimeId: await rep.cards().lookup(req.luid, now), + registerLevel: "none", + }; +} + +async function register( + rep: Repositories, + req: Req.RegisterRequest, + now: Date +): Promise { + console.log("Aimedb: Mifare register", req.luid); + + return { + type: req.type, + status: 1, + aimeId: await rep.cards().register(req.luid, now), + }; +} + +function log( + rep: Repositories, + req: Req.LogRequest, + now: Date +): Res.LogResponse { + console.log("Aimedb: Log message"); + + return { type: req.type, status: 1 }; +} + +export async function dispatch( + rep: Repositories, + req: Req.AimeRequest, + now: Date +): Promise { switch (req.type) { case "hello": - console.log("Aimedb: Hello"); - - return { type: req.type, status: 1 }; + return hello(rep, req, now); case "campaign": - console.log("Aimedb: Campaign stuff"); - - return { type: req.type, status: 1 }; + return campaign(rep, req, now); case "lookup": - console.log("Aimedb: Mifare lookup v1", req.luid); - - return { - type: req.type, - status: 1, - aimeId: 0x55667788, - registerLevel: "none", - }; + return lookup(rep, req, now); case "lookup2": - console.log("Aimedb: Mifare lookup v2", req.luid); - - return { - type: req.type, - status: 1, - aimeId: 0x55667788, - registerLevel: "none", - }; + return lookup2(rep, req, now); case "register": - // We get sent here if lookup does not return an aimeId - - console.log("Aimedb: Mifare register", req.luid); - - return { type: req.type, status: 1, aimeId: 0x55667788 }; + return register(rep, req, now); case "log": - console.log("Aimedb: Log message"); - - return { type: req.type, status: 1 }; + return log(rep, req, now); case "goodbye": console.log("Aimedb: Goodbye"); diff --git a/src/aimedb/index.ts b/src/aimedb/index.ts index 83af27c..f378acf 100644 --- a/src/aimedb/index.ts +++ b/src/aimedb/index.ts @@ -3,25 +3,31 @@ import { Socket } from "net"; import { dispatch } from "./handler"; import { AimeRequest } from "./request"; import { setup } from "./pipeline"; +import { beginDbSession } from "./db"; export default async function aimedb(socket: Socket) { console.log("Aimedb: Connection opened"); const { input, output } = setup(socket); + const txn = await beginDbSession(); try { for await (const obj of input) { console.log("Aimedb: Decode", obj); + const now = new Date(); const req = obj as AimeRequest; - const res = dispatch(req); + const res = await dispatch(txn, req, now); if (res !== undefined) { output.write(res); } } + + txn.commit(); } catch (e) { console.log("Aimedb: Connection error:\n", e); + txn.rollback(); } console.log("Aimedb: Connection closed\n");