feat(rfid): work on the readers and there functions
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
//http://usday1vms006:4000/api/v1/zebra/wrapper1
|
||||
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
||||
import {readTags} from "../controller/readTags.js";
|
||||
import {createLog} from "../../logger/logger.js";
|
||||
import {responses} from "../../../globalUtils/routeDefs/responses.js";
|
||||
import {newHeartBeat} from "../controller/readerControl.js";
|
||||
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
||||
import { readTags } from "../controller/readTags.js";
|
||||
import { createLog } from "../../logger/logger.js";
|
||||
import { responses } from "../../../globalUtils/routeDefs/responses.js";
|
||||
import { newHeartBeat } from "../controller/readerControl.js";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
let lastGpiTimestamp = 0;
|
||||
|
||||
const ParamsSchema = z.object({
|
||||
reader: z
|
||||
.string()
|
||||
.min(3)
|
||||
.openapi({
|
||||
param: {
|
||||
name: "reader",
|
||||
in: "path",
|
||||
},
|
||||
example: "1212121",
|
||||
}),
|
||||
.string()
|
||||
.min(3)
|
||||
.openapi({
|
||||
param: {
|
||||
name: "reader",
|
||||
in: "path",
|
||||
},
|
||||
example: "1212121",
|
||||
}),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
@@ -33,31 +33,47 @@ app.openapi(
|
||||
responses: responses(),
|
||||
}),
|
||||
async (c) => {
|
||||
const {reader} = c.req.valid("param");
|
||||
const { reader } = c.req.valid("param");
|
||||
const body = await c.req.json();
|
||||
|
||||
if (body.type === "heartbeat") {
|
||||
const heart = await newHeartBeat(reader);
|
||||
return c.json({success: heart.success, message: heart.message}, 200);
|
||||
return c.json(
|
||||
{ success: heart.success, message: heart.message },
|
||||
200
|
||||
);
|
||||
}
|
||||
|
||||
if (body.type === "gpi" && body.data.state === "HIGH") {
|
||||
const eventTimestamp = new Date(body.timestamp).getTime(); // Convert ISO timestamp to milliseconds
|
||||
if (eventTimestamp - lastGpiTimestamp > 5 * 1000) {
|
||||
if (eventTimestamp - lastGpiTimestamp > 30 * 1000) {
|
||||
// Check if it's been more than 2ms
|
||||
lastGpiTimestamp = eventTimestamp; // Update last seen timestamp
|
||||
|
||||
createLog("info", "rfid", "rfid", `${reader} is reading a tag.`);
|
||||
createLog(
|
||||
"info",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`${reader} is reading a tag.`
|
||||
);
|
||||
await readTags(reader);
|
||||
} else {
|
||||
createLog("info", "rfid", "rfid", `A new trigger from ${reader} was to soon`);
|
||||
createLog(
|
||||
"info",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`A new trigger from ${reader} was to soon`
|
||||
);
|
||||
lastGpiTimestamp = eventTimestamp;
|
||||
}
|
||||
|
||||
//console.log(body);
|
||||
}
|
||||
|
||||
return c.json({success: true, message: `New info from ${reader}`}, 200);
|
||||
return c.json(
|
||||
{ success: true, message: `New info from ${reader}` },
|
||||
200
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
@@ -1,24 +1,27 @@
|
||||
//http://usday1vms006:4000/api/v1/zebra/wrapper1
|
||||
import {z, createRoute, OpenAPIHono} from "@hono/zod-openapi";
|
||||
import {ConsoleLogWriter} from "drizzle-orm";
|
||||
import {tagData} from "../controller/tagData.js";
|
||||
import {responses} from "../../../globalUtils/routeDefs/responses.js";
|
||||
import {noRead} from "../controller/noRead.js";
|
||||
import {badRead, goodRead} from "../controller/readerControl.js";
|
||||
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
|
||||
import { ConsoleLogWriter } from "drizzle-orm";
|
||||
import { tagData } from "../controller/tagData.js";
|
||||
import { responses } from "../../../globalUtils/routeDefs/responses.js";
|
||||
import { noRead } from "../controller/noRead.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";
|
||||
|
||||
const app = new OpenAPIHono();
|
||||
|
||||
const ParamsSchema = z.object({
|
||||
reader: z
|
||||
.string()
|
||||
.min(3)
|
||||
.openapi({
|
||||
param: {
|
||||
name: "reader",
|
||||
in: "path",
|
||||
},
|
||||
example: "1212121",
|
||||
}),
|
||||
.string()
|
||||
.min(3)
|
||||
.openapi({
|
||||
param: {
|
||||
name: "reader",
|
||||
in: "path",
|
||||
},
|
||||
example: "1212121",
|
||||
}),
|
||||
});
|
||||
|
||||
app.openapi(
|
||||
@@ -33,15 +36,25 @@ app.openapi(
|
||||
responses: responses(),
|
||||
}),
|
||||
async (c) => {
|
||||
const {reader} = c.req.valid("param");
|
||||
const { reader } = c.req.valid("param");
|
||||
createLog("info", "rfid", "rfid", `${reader} is sending us data.`);
|
||||
let tagdata: any = [];
|
||||
const body = await c.req.json();
|
||||
const { data: body, error: bodyError } = await tryCatch(c.req.json());
|
||||
|
||||
if (bodyError) {
|
||||
return c.json({ success: false, message: "missing data" }, 400);
|
||||
}
|
||||
//console.log(`Tag: ${Buffer.from(body.idHex, "hex").toString("utf-8")}, ${body[key].data.idHex}`);
|
||||
|
||||
for (let i = 0; i < body.length; i++) {
|
||||
const tag = Buffer.from(body[i].data.idHex, "hex").toString("utf-8");
|
||||
const tag = Buffer.from(body[i].data.idHex, "hex").toString(
|
||||
"utf-8"
|
||||
);
|
||||
//console.log("Raw value:", body[i].data.peakRssi, "Parsed:", parseInt(body[i].data.peakRssi));
|
||||
if (tag.includes("ALPLA") && parseInt(body[i].data.peakRssi) < -50) {
|
||||
if (
|
||||
tag.includes("ALPLA") &&
|
||||
parseInt(body[i].data.peakRssi) < -50
|
||||
) {
|
||||
tagdata = [
|
||||
...tagdata,
|
||||
{
|
||||
@@ -59,11 +72,18 @@ app.openapi(
|
||||
if (tagdata.length === 0) {
|
||||
noRead(reader);
|
||||
badRead(reader);
|
||||
return c.json({success: false, message: `There were no tags scanned.`}, 200);
|
||||
return c.json(
|
||||
{ success: false, message: `There were no tags scanned.` },
|
||||
200
|
||||
);
|
||||
} else {
|
||||
tagData(tagdata);
|
||||
goodRead(reader);
|
||||
return c.json({success: true, message: `New info from ${reader}`}, 200);
|
||||
|
||||
return c.json(
|
||||
{ success: true, message: `New info from ${reader}` },
|
||||
200
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user