feat(tag reading): more tag reading updates, with more contorl now
station 1 and 2 are still pending but didnt have enough tags at the office trial it
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
/**
|
||||
* we will monitor shipped out pallets every hour if they get shipped out
|
||||
*/
|
||||
3
server/services/rfid/controller/stations/station1.ts
Normal file
3
server/services/rfid/controller/stations/station1.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
/**
|
||||
* station 1 will just check for 10 tags
|
||||
*/
|
||||
4
server/services/rfid/controller/stations/station2.ts
Normal file
4
server/services/rfid/controller/stations/station2.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* we will have 3 reader points here station2.1 will require 10 tags, 2.2 and 2.3 will require 1
|
||||
*
|
||||
*/
|
||||
27
server/services/rfid/controller/stations/station3.ts
Normal file
27
server/services/rfid/controller/stations/station3.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Phase 1 we link the tag to the line only the line3.x where x is the line number
|
||||
* Phase 2 we will generate a label to be reprinted at staion 4
|
||||
*/
|
||||
|
||||
import {createLog} from "../../../logger/logger.js";
|
||||
import type {TagData} from "../tagData.js";
|
||||
import {tagStuff} from "../tags/crudTag.js";
|
||||
|
||||
export const station3Tags = async (tagData: TagData[]) => {
|
||||
// make sure we only have one tag or dont update
|
||||
if (tagData.length != 1) {
|
||||
createLog(
|
||||
"error",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`There are ${tagData.length} tags, and ${tagData[0].reader} only allows 1 tag to create a label.`
|
||||
);
|
||||
// get tag data
|
||||
tagStuff(tagData);
|
||||
} else {
|
||||
//console.log("Generate the label and link it to the tag.");
|
||||
const tagdata = await tagStuff(tagData);
|
||||
|
||||
createLog("info", "rfid", "rfid", "Generate a label and link it to this tag.");
|
||||
}
|
||||
};
|
||||
43
server/services/rfid/controller/stations/wrappers.ts
Normal file
43
server/services/rfid/controller/stations/wrappers.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Phase 1 we will just follow the logic of printing a label when we are told requested to based on this tag.
|
||||
* Phase 2 we will just reprint the tag that was generated at the line
|
||||
*/
|
||||
|
||||
import {createLog} from "../../../logger/logger.js";
|
||||
import type {TagData} from "../tagData.js";
|
||||
import {tagStuff} from "../tags/crudTag.js";
|
||||
|
||||
export const wrapperStuff = async (tagData: TagData[]) => {
|
||||
if (tagData.length != 1) {
|
||||
createLog(
|
||||
"error",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`There are ${tagData.length} tags and this ${tagData[0].reader} only allows 1 tag to create a label.`
|
||||
);
|
||||
tagStuff(tagData);
|
||||
} else {
|
||||
const tagdata = await tagStuff(tagData);
|
||||
|
||||
/**
|
||||
* we want to make sure this pallet came from a line as its last spot if not we need to have a manual check.
|
||||
*/
|
||||
const station3 = tagdata.some((n: any) => n.lastareaIn.includes("line3"));
|
||||
|
||||
if (!station3) {
|
||||
createLog(
|
||||
"error",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`${tagdata.tag}, Did not come from a line please check the pallet and manually print the label.`
|
||||
);
|
||||
}
|
||||
|
||||
// check if a running number exists
|
||||
if (station3.runningNumber) {
|
||||
createLog("info", "rfid", "rfid", `Reprint label ${station3.runningNumber}`);
|
||||
} else {
|
||||
createLog("info", "rfid", "rfid", `A new labels will be created and linked to this ${tagdata.tag} tag`);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,4 +1,14 @@
|
||||
type TagData = {tagHex: string; reader: string; tag: string; timeStamp: Date};
|
||||
import {station3Tags} from "./stations/station3.js";
|
||||
import {wrapperStuff} from "./stations/wrappers.js";
|
||||
|
||||
export type TagData = {
|
||||
tagHex: string;
|
||||
reader: string;
|
||||
tag: string;
|
||||
timeStamp: Date;
|
||||
antenna: number;
|
||||
tagStrength: number;
|
||||
};
|
||||
export const tagData = async (data: TagData[]) => {
|
||||
/**
|
||||
* We will always update a tag
|
||||
@@ -14,7 +24,7 @@ export const tagData = async (data: TagData[]) => {
|
||||
const station3 = data.some((n) => n.reader.includes("line3"));
|
||||
|
||||
// at the wrapper
|
||||
const station4 = data.some((n) => n.reader.includes("wrapper"));
|
||||
const wrappers = data.some((n) => n.reader.includes("wrapper"));
|
||||
|
||||
// station checks
|
||||
if (station1 && data.length != 10) {
|
||||
@@ -26,23 +36,10 @@ export const tagData = async (data: TagData[]) => {
|
||||
}
|
||||
|
||||
if (station3) {
|
||||
// make sure we only have one tag or dont update
|
||||
if (data.length != 1) {
|
||||
console.log(`There are ${data.length} tags, and ${data[0].reader} only allows 1 tag to create a label.`);
|
||||
//throw new Error("There are more than 1 tag at this station and it is not allowed");
|
||||
} else {
|
||||
console.log("Generate the tag and link it to the tag.");
|
||||
}
|
||||
station3Tags(data);
|
||||
}
|
||||
|
||||
if (station4) {
|
||||
if (data.length != 1) {
|
||||
console.log(
|
||||
`There are ${data.length} tags and this ${data[0].reader} only allows 1 tag to create a label.`
|
||||
);
|
||||
//throw new Error("There are more than 1 tag at this station and it is not allowed");
|
||||
} else {
|
||||
console.log("reprint the label linked to the tag.");
|
||||
}
|
||||
if (wrappers) {
|
||||
wrapperStuff(data);
|
||||
}
|
||||
};
|
||||
|
||||
87
server/services/rfid/controller/tags/crudTag.ts
Normal file
87
server/services/rfid/controller/tags/crudTag.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import {eq} from "drizzle-orm";
|
||||
import {db} from "../../../../../database/dbclient.js";
|
||||
import {rfidTags} from "../../../../../database/schema/rfidTags.js";
|
||||
import type {TagData} from "../tagData.js";
|
||||
import {createLog} from "../../../logger/logger.js";
|
||||
|
||||
type ReturnTag = {
|
||||
success: boolean;
|
||||
tag: any;
|
||||
error: any;
|
||||
};
|
||||
export const tagStuff = async (tagData: TagData[]): Promise<any> => {
|
||||
const tags = await db.select().from(rfidTags);
|
||||
|
||||
// look through each tag and either add it to the db or update the area and other relevent data.
|
||||
for (let i = 0; i < tagData.length; i++) {
|
||||
// check if the tag already exists
|
||||
const tag = tags.filter((n) => n.tagHex === tagData[i].tagHex);
|
||||
if (tag.length === 0) {
|
||||
// add new tag
|
||||
const newTag = {
|
||||
tagHex: tagData[i].tagHex,
|
||||
tag: tagData[i].tag,
|
||||
lastRead: new Date(tagData[i].timeStamp),
|
||||
counts: [{area: tagData[i].reader, timesHere: 1}], //jsonb("counts").notNull(), //.default([{area: 1, timesHere: 5}]).notNull(),
|
||||
lastareaIn: tagData[i].reader,
|
||||
antenna: tagData[i].antenna,
|
||||
tagStrength: tagData[i].tagStrength,
|
||||
};
|
||||
try {
|
||||
// insert the tag with the onConflict update the tag
|
||||
const tag = await db.insert(rfidTags).values(newTag).returning({
|
||||
tag: rfidTags.tag,
|
||||
runningNumber: rfidTags.runningNumber,
|
||||
counts: rfidTags.counts,
|
||||
lastareaIn: rfidTags.lastareaIn,
|
||||
});
|
||||
createLog("info", "rfid", "rfid", `Tags were jusdt updated.`);
|
||||
return {success: true, tag};
|
||||
} catch (error) {
|
||||
createLog("error", "rfid", "rfid", `${JSON.stringify(error)}`);
|
||||
return {success: false, error};
|
||||
}
|
||||
} else {
|
||||
// update tag
|
||||
//console.log("Updating existing tag");
|
||||
// make sure we actually have an array here
|
||||
const countsArray = (tag[0]?.counts as {area: string; timesHere: number}[]) ?? [];
|
||||
|
||||
// check if the reader exists on the array
|
||||
const areaExists = countsArray.some((t) => t.area === tagData[0].reader);
|
||||
|
||||
// run the update on the array
|
||||
const updateCount = areaExists
|
||||
? countsArray.map((t) => {
|
||||
if (t.area === tagData[0].reader) {
|
||||
return {...t, timesHere: t.timesHere + 1};
|
||||
} else {
|
||||
return {...t, area: tagData[i].reader, timesHere: 1};
|
||||
}
|
||||
})
|
||||
: [...countsArray, {area: tagData[i].reader, timesHere: 1}];
|
||||
|
||||
const updateTag = {
|
||||
lastRead: new Date(tagData[i].timeStamp),
|
||||
counts: updateCount, //jsonb("counts").notNull(), //.default([{area: 1, timesHere: 5}]).notNull(),
|
||||
lastareaIn: tagData[i].reader,
|
||||
antenna: tagData[i].antenna,
|
||||
tagStrength: tagData[i].tagStrength,
|
||||
};
|
||||
|
||||
try {
|
||||
await db.update(rfidTags).set(updateTag).where(eq(rfidTags.tagHex, tagData[0].tagHex)).returning({
|
||||
tag: rfidTags.tag,
|
||||
runningNumber: rfidTags.runningNumber,
|
||||
counts: rfidTags.counts,
|
||||
lastareaIn: rfidTags.lastareaIn,
|
||||
});
|
||||
createLog("info", "rfid", "rfid", `Tags were jusdt updated.`);
|
||||
return {success: true, tag};
|
||||
} catch (error) {
|
||||
createLog("error", "rfid", "rfid", `${JSON.stringify(error)}`);
|
||||
return {success: false, error};
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user