fix(rfid): changed the tag reading to have a little more flexable
This commit is contained in:
@@ -1,98 +1,93 @@
|
|||||||
//http://usday1vms006:4000/api/v1/zebra/wrapper1
|
//http://usday1vms006:4000/api/v1/zebra/wrapper1
|
||||||
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
||||||
import { ConsoleLogWriter } from "drizzle-orm";
|
import { ConsoleLogWriter } from "drizzle-orm";
|
||||||
import { tagData } from "../controller/tagData.js";
|
import { apiHit } from "../../../globalUtils/apiHits.js";
|
||||||
import { responses } from "../../../globalUtils/routeDefs/responses.js";
|
import { responses } from "../../../globalUtils/routeDefs/responses.js";
|
||||||
|
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||||
|
import { createLog } from "../../logger/logger.js";
|
||||||
import { clearNoReadTimer, noRead } from "../controller/noRead.js";
|
import { clearNoReadTimer, noRead } from "../controller/noRead.js";
|
||||||
import { badRead, goodRead } from "../controller/readerControl.js";
|
import { badRead, goodRead } from "../controller/readerControl.js";
|
||||||
import { createLog } from "../../logger/logger.js";
|
|
||||||
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
|
||||||
import { stopRead } from "../controller/readTags.js";
|
import { stopRead } from "../controller/readTags.js";
|
||||||
import { apiHit } from "../../../globalUtils/apiHits.js";
|
import { tagData } from "../controller/tagData.js";
|
||||||
|
|
||||||
const app = new OpenAPIHono();
|
const app = new OpenAPIHono();
|
||||||
|
|
||||||
const ParamsSchema = z.object({
|
const ParamsSchema = z.object({
|
||||||
reader: z
|
reader: z
|
||||||
.string()
|
.string()
|
||||||
.min(3)
|
.min(3)
|
||||||
.openapi({
|
.openapi({
|
||||||
param: {
|
param: {
|
||||||
name: "reader",
|
name: "reader",
|
||||||
in: "path",
|
in: "path",
|
||||||
},
|
},
|
||||||
example: "1212121",
|
example: "1212121",
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
app.openapi(
|
app.openapi(
|
||||||
createRoute({
|
createRoute({
|
||||||
tags: ["rfid"],
|
tags: ["rfid"],
|
||||||
summary: "Tag info posted from the reader.",
|
summary: "Tag info posted from the reader.",
|
||||||
method: "post",
|
method: "post",
|
||||||
path: "/taginfo/{reader}",
|
path: "/taginfo/{reader}",
|
||||||
request: {
|
request: {
|
||||||
params: ParamsSchema,
|
params: ParamsSchema,
|
||||||
},
|
},
|
||||||
responses: responses(),
|
responses: responses(),
|
||||||
}),
|
}),
|
||||||
async (c) => {
|
async (c) => {
|
||||||
const { reader } = c.req.valid("param");
|
const { reader } = c.req.valid("param");
|
||||||
createLog("info", "rfid", "rfid", `${reader} is sending us data.`);
|
createLog("info", "rfid", "rfid", `${reader} is sending us data.`);
|
||||||
let tagdata: any = [];
|
let tagdata: any = [];
|
||||||
const { data: body, error: bodyError } = await tryCatch(c.req.json());
|
const { data: body, error: bodyError } = await tryCatch(c.req.json());
|
||||||
apiHit(c, { endpoint: `/taginfo/${reader}`, lastBody: body });
|
apiHit(c, { endpoint: `/taginfo/${reader}`, lastBody: body });
|
||||||
if (bodyError) {
|
if (bodyError) {
|
||||||
return c.json({ success: false, message: "missing data" }, 400);
|
return c.json({ success: false, message: "missing data" }, 400);
|
||||||
}
|
}
|
||||||
//console.log(`Tag: ${Buffer.from(body.idHex, "hex").toString("utf-8")}, ${body[key].data.idHex}`);
|
//console.log(`Tag: ${Buffer.from(body.idHex, "hex").toString("utf-8")}, ${body[key].data.idHex}`);
|
||||||
|
|
||||||
for (let i = 0; i < body.length; i++) {
|
for (let i = 0; i < body.length; i++) {
|
||||||
const tag = Buffer.from(body[i].data.idHex, "hex").toString(
|
const tag = Buffer.from(body[i].data.idHex, "hex").toString("utf-8");
|
||||||
"utf-8"
|
// console.log(
|
||||||
);
|
// "Raw value:",
|
||||||
// console.log(
|
// body[i].data.peakRssi,
|
||||||
// "Raw value:",
|
// "Parsed:",
|
||||||
// body[i].data.peakRssi,
|
// parseInt(body[i].data.peakRssi)
|
||||||
// "Parsed:",
|
// );
|
||||||
// parseInt(body[i].data.peakRssi)
|
if (
|
||||||
// );
|
tag.includes("ALPLA") &&
|
||||||
if (
|
parseInt(body[i].data.peakRssi) >= -80 // anything strong than this go ahead and read it
|
||||||
tag.includes("ALPLA") &&
|
) {
|
||||||
parseInt(body[i].data.peakRssi) < -30
|
tagdata = [
|
||||||
) {
|
...tagdata,
|
||||||
tagdata = [
|
{
|
||||||
...tagdata,
|
tagHex: body[i].data.idHex,
|
||||||
{
|
reader: reader,
|
||||||
tagHex: body[i].data.idHex,
|
tag: tag,
|
||||||
reader: reader,
|
timeStamp: body[i].timestamp,
|
||||||
tag: tag,
|
antenna: body[i].data.antenna,
|
||||||
timeStamp: body[i].timestamp,
|
tagStrength: body[i].data.peakRssi,
|
||||||
antenna: body[i].data.antenna,
|
},
|
||||||
tagStrength: body[i].data.peakRssi,
|
];
|
||||||
},
|
}
|
||||||
];
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (tagdata.length === 0) {
|
if (tagdata.length === 0) {
|
||||||
// noRead(reader);
|
// noRead(reader);
|
||||||
// badRead(reader);
|
// badRead(reader);
|
||||||
return c.json(
|
return c.json(
|
||||||
{ success: false, message: `There were no tags scanned.` },
|
{ success: false, message: `There were no tags scanned.` },
|
||||||
200
|
200,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
tagData(tagdata);
|
tagData(tagdata);
|
||||||
goodRead(reader);
|
goodRead(reader);
|
||||||
clearNoReadTimer();
|
clearNoReadTimer();
|
||||||
|
|
||||||
return c.json(
|
return c.json({ success: true, message: `New info from ${reader}` }, 200);
|
||||||
{ success: true, message: `New info from ${reader}` },
|
}
|
||||||
200
|
},
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
Reference in New Issue
Block a user