refactor(rfid): ratio resets implemeneted

This commit is contained in:
2025-07-14 12:36:36 -05:00
parent 4923b3c698
commit a7f8e39bac
5 changed files with 159 additions and 29 deletions

View File

@@ -1,4 +1,4 @@
import { Printer } from "lucide-react";
import { Printer, Tag } from "lucide-react";
import {
SidebarGroup,
SidebarGroupContent,
@@ -10,34 +10,6 @@ import {
import { hasPageAccess } from "@/utils/userAccess";
import { User } from "@/types/users";
const items = [
{
title: "One Click Print",
url: "/ocp",
icon: Printer,
role: ["viewer"],
module: "ocp",
active: true,
},
{
title: "RFID",
moduleName: "prodcution",
description: "RFID stuff",
url: "/rfid",
icon: "Tags",
active: true,
roles: [
"viewer",
"technician",
"supervisor",
"manager",
"admin",
"systemAdmin",
],
subSubModule: [],
},
];
export function ProductionSideBar({
user,
moduleID,
@@ -45,6 +17,26 @@ export function ProductionSideBar({
user: User | null;
moduleID: string;
}) {
const url: string = window.location.host.split(":")[0];
const items = [
{
title: "One Click Print",
url: "/ocp",
icon: Printer,
role: ["viewer"],
module: "ocp",
active: true,
},
{
title: "Rfid Readers",
url: "/rfid",
icon: Tag,
role: ["viewer"],
module: "production",
active:
url === "usday1vms006" || url === "localhost" ? true : false,
},
];
return (
<SidebarGroup>
<SidebarGroupLabel>Production</SidebarGroupLabel>

View File

@@ -1,6 +1,12 @@
//import { fixTime } from "@/utils/fixTime";
import { Button } from "@/components/ui/button";
import { getReaders } from "@/utils/querys/rfid/getReaders";
import { useQuery } from "@tanstack/react-query";
import { ColumnDef } from "@tanstack/react-table";
import axios from "axios";
import { format } from "date-fns-tz";
import { useState } from "react";
import { toast } from "sonner";
// This type is used to define the shape of our data.
// You can use a Zod schema here if you want.
@@ -94,4 +100,49 @@ export const readerColumns: ColumnDef<Readers>[] = [
);
},
},
{
accessorKey: "reset",
header: "Reset Reads",
cell: ({ row }) => {
const { refetch } = useQuery(getReaders());
const [readerReset, setReaderReset] = useState(false);
// const goodRatio =
// (parseInt(row.getValue("goodReads")) /
// (parseInt(row.getValue("goodReads")) +
// parseInt(row.getValue("badReads")))) *
// 100;
const name = row.getValue("reader");
const resetReads = async () => {
setReaderReset(true);
try {
const res = await axios.post("/api/rfid/resetRatio", {
reader: name,
});
if (res.status === 200) {
toast.success(res.data.message);
setReaderReset(false);
} else {
toast.error(res.data.message);
setReaderReset(false);
}
} catch (error: any) {
toast.error(error.data.message);
setReaderReset(false);
}
refetch();
};
return (
<div className="text-left font-medium">
<Button
className="h-4"
onClick={resetReads}
disabled={readerReset}
>
Reset Reads
</Button>
</div>
);
},
},
];

View File

@@ -6,6 +6,7 @@ import { eq, sql } from "drizzle-orm";
import { db } from "../../../../database/dbclient.js";
import { rfidReaders } from "../../../../database/schema/rfidReaders.js";
import { createLog } from "../../logger/logger.js";
import { tryCatch } from "../../../globalUtils/tryCatch.js";
export const newHeartBeat = async (reader: string) => {
/**
@@ -79,6 +80,36 @@ export const badRead = async (reader: string) => {
}
};
export const resetRatios = async (reader: string) => {
const { error } = await tryCatch(
db
.update(rfidReaders)
.set({
goodReads: 0,
badReads: 0,
})
.where(eq(rfidReaders.reader, reader))
);
if (error) {
createLog(
"error",
"rfid",
"rfid",
`${reader} encountered an error resetting the data.`
);
return {
success: false,
message: `${reader} encountered an error resetting the data.`,
};
}
createLog("info", "rfid", "rfid", `${reader} just had the tag data reset.`);
return {
success: true,
message: `${reader} just had the tag data reset.`,
};
};
export const goodRead = async (reader: string) => {
/**
* When we have a bad read we want to make sure the reader shows this was well.

View File

@@ -6,6 +6,7 @@ import addReader from "./route/addReader.js";
import updateReader from "./route/updateReader.js";
import manualTrigger from "./route/manualTagRead.js";
import getReaders from "./route/getReaders.js";
import resetRatio from "./route/resetRatio.js";
const app = new OpenAPIHono();
const routes = [
@@ -15,6 +16,7 @@ const routes = [
updateReader,
manualTrigger,
getReaders,
resetRatio,
] as const;
// app.route("/server", modules);

View File

@@ -0,0 +1,54 @@
import { z, createRoute, OpenAPIHono } from "@hono/zod-openapi";
import { addReader } from "../controller/addReader.js";
import { authMiddleware } from "../../auth/middleware/authMiddleware.js";
import { responses } from "../../../globalUtils/routeDefs/responses.js";
import type { User } from "../../../types/users.js";
import { verify } from "hono/jwt";
import { apiHit } from "../../../globalUtils/apiHits.js";
import { resetRatios } from "../controller/readerControl.js";
const app = new OpenAPIHono();
export const ReaderBody = z.object({
reader: z.string().openapi({ example: "wrapper1" }),
});
app.openapi(
createRoute({
tags: ["rfid"],
summary: "Resets the ratio on the reader",
method: "post",
path: "/resetRatio",
//middleware: authMiddleware,
//description: "Adding in a new reader to add to the network.",
request: {
body: { content: { "application/json": { schema: ReaderBody } } },
},
responses: responses(),
}),
async (c) => {
const body = await c.req.json();
apiHit(c, { endpoint: "/resetRatio", lastBody: body });
try {
const reset = await resetRatios(body.reader);
return c.json(
{
success: reset.success,
message: reset.message,
},
200
);
} catch (error) {
return c.json(
{
success: false,
message: `${body.name} encountered an error while trying to reset the readers ratio`,
},
400
);
}
}
);
export default app;