refactor(rfid): ratio resets implemeneted
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Printer } from "lucide-react";
|
import { Printer, Tag } from "lucide-react";
|
||||||
import {
|
import {
|
||||||
SidebarGroup,
|
SidebarGroup,
|
||||||
SidebarGroupContent,
|
SidebarGroupContent,
|
||||||
@@ -10,6 +10,14 @@ import {
|
|||||||
import { hasPageAccess } from "@/utils/userAccess";
|
import { hasPageAccess } from "@/utils/userAccess";
|
||||||
import { User } from "@/types/users";
|
import { User } from "@/types/users";
|
||||||
|
|
||||||
|
export function ProductionSideBar({
|
||||||
|
user,
|
||||||
|
moduleID,
|
||||||
|
}: {
|
||||||
|
user: User | null;
|
||||||
|
moduleID: string;
|
||||||
|
}) {
|
||||||
|
const url: string = window.location.host.split(":")[0];
|
||||||
const items = [
|
const items = [
|
||||||
{
|
{
|
||||||
title: "One Click Print",
|
title: "One Click Print",
|
||||||
@@ -20,31 +28,15 @@ const items = [
|
|||||||
active: true,
|
active: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: "RFID",
|
title: "Rfid Readers",
|
||||||
moduleName: "prodcution",
|
|
||||||
description: "RFID stuff",
|
|
||||||
url: "/rfid",
|
url: "/rfid",
|
||||||
icon: "Tags",
|
icon: Tag,
|
||||||
active: true,
|
role: ["viewer"],
|
||||||
roles: [
|
module: "production",
|
||||||
"viewer",
|
active:
|
||||||
"technician",
|
url === "usday1vms006" || url === "localhost" ? true : false,
|
||||||
"supervisor",
|
|
||||||
"manager",
|
|
||||||
"admin",
|
|
||||||
"systemAdmin",
|
|
||||||
],
|
|
||||||
subSubModule: [],
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export function ProductionSideBar({
|
|
||||||
user,
|
|
||||||
moduleID,
|
|
||||||
}: {
|
|
||||||
user: User | null;
|
|
||||||
moduleID: string;
|
|
||||||
}) {
|
|
||||||
return (
|
return (
|
||||||
<SidebarGroup>
|
<SidebarGroup>
|
||||||
<SidebarGroupLabel>Production</SidebarGroupLabel>
|
<SidebarGroupLabel>Production</SidebarGroupLabel>
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
//import { fixTime } from "@/utils/fixTime";
|
//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 { ColumnDef } from "@tanstack/react-table";
|
||||||
|
import axios from "axios";
|
||||||
import { format } from "date-fns-tz";
|
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.
|
// This type is used to define the shape of our data.
|
||||||
// You can use a Zod schema here if you want.
|
// 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>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import { eq, sql } from "drizzle-orm";
|
|||||||
import { db } from "../../../../database/dbclient.js";
|
import { db } from "../../../../database/dbclient.js";
|
||||||
import { rfidReaders } from "../../../../database/schema/rfidReaders.js";
|
import { rfidReaders } from "../../../../database/schema/rfidReaders.js";
|
||||||
import { createLog } from "../../logger/logger.js";
|
import { createLog } from "../../logger/logger.js";
|
||||||
|
import { tryCatch } from "../../../globalUtils/tryCatch.js";
|
||||||
|
|
||||||
export const newHeartBeat = async (reader: string) => {
|
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) => {
|
export const goodRead = async (reader: string) => {
|
||||||
/**
|
/**
|
||||||
* When we have a bad read we want to make sure the reader shows this was well.
|
* When we have a bad read we want to make sure the reader shows this was well.
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import addReader from "./route/addReader.js";
|
|||||||
import updateReader from "./route/updateReader.js";
|
import updateReader from "./route/updateReader.js";
|
||||||
import manualTrigger from "./route/manualTagRead.js";
|
import manualTrigger from "./route/manualTagRead.js";
|
||||||
import getReaders from "./route/getReaders.js";
|
import getReaders from "./route/getReaders.js";
|
||||||
|
import resetRatio from "./route/resetRatio.js";
|
||||||
const app = new OpenAPIHono();
|
const app = new OpenAPIHono();
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
@@ -15,6 +16,7 @@ const routes = [
|
|||||||
updateReader,
|
updateReader,
|
||||||
manualTrigger,
|
manualTrigger,
|
||||||
getReaders,
|
getReaders,
|
||||||
|
resetRatio,
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
// app.route("/server", modules);
|
// app.route("/server", modules);
|
||||||
|
|||||||
54
server/services/rfid/route/resetRatio.ts
Normal file
54
server/services/rfid/route/resetRatio.ts
Normal 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;
|
||||||
Reference in New Issue
Block a user