refactor(rfid): cleaned up contorller folder
This commit is contained in:
@@ -1,51 +1,85 @@
|
||||
import axios from "axios";
|
||||
import {createLog} from "../../logger/logger.js";
|
||||
import {db} from "../../../../database/dbclient.js";
|
||||
import {rfidReaders} from "../../../../database/schema/rfidReaders.js";
|
||||
import {eq} from "drizzle-orm";
|
||||
|
||||
const authData = btoa("admin:Zebra123!");
|
||||
let token: string;
|
||||
let ip: string;
|
||||
|
||||
export const readTags = async (reader: string) => {
|
||||
/**
|
||||
* Start the read for x seconds then auto stop it
|
||||
*/
|
||||
|
||||
let token: string;
|
||||
|
||||
const readers = [{reader: "reader1", readerIP: "10.10.1.222", lastHeartBeat: new Date()}];
|
||||
const readers = await db.select().from(rfidReaders).where(eq(rfidReaders.active, true));
|
||||
if (readers.length === 0) {
|
||||
createLog("error", "rfid", "rfid", `There are no active readers right now maybe one forgot to be activated`);
|
||||
return;
|
||||
}
|
||||
// get the auth token
|
||||
const ip = readers.find((r) => r.reader === reader)?.readerIP;
|
||||
ip = readers.find((r) => r.reader === reader)?.readerIP as string;
|
||||
|
||||
try {
|
||||
const res = await axios.get(`https://${ip}/cloud/localRestLogin`, {
|
||||
headers: {Authorization: `Basic ${btoa("admin:Zebra123!")}`},
|
||||
headers: {Authorization: `Basic ${authData}`},
|
||||
});
|
||||
token = res.data.message;
|
||||
// start the read
|
||||
try {
|
||||
const res = await axios.put(
|
||||
`https://${ip}/cloud/start`,
|
||||
{},
|
||||
{
|
||||
headers: {Authorization: `Bearer ${token}`},
|
||||
}
|
||||
);
|
||||
|
||||
// stop after 5 seconds
|
||||
|
||||
try {
|
||||
const res = await axios.put(
|
||||
`https://${ip}/cloud/stop`,
|
||||
{},
|
||||
{
|
||||
headers: {Authorization: `Bearer ${token}`},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
createLog("error", "rfid", "rfid", `There was an error Stopping the read ${error}`);
|
||||
}
|
||||
} catch (error) {
|
||||
createLog("error", "rfid", "rfid", `There was an error Starting the read ${error}`);
|
||||
}
|
||||
} catch (error) {
|
||||
createLog("error", "rfid", "rfid", `There was an error Getting the token the read ${error}`);
|
||||
startRead();
|
||||
} catch (error: any) {
|
||||
console.log(error);
|
||||
createLog(
|
||||
"error",
|
||||
"rfid",
|
||||
"rfid",
|
||||
`There was an error Getting the token the read: ${error.response?.data.message}`
|
||||
);
|
||||
}
|
||||
|
||||
// start the read
|
||||
};
|
||||
|
||||
const startRead = async () => {
|
||||
try {
|
||||
const res = await axios.put(
|
||||
`https://${ip}/cloud/start`,
|
||||
{},
|
||||
{
|
||||
headers: {Authorization: `Bearer ${token}`},
|
||||
}
|
||||
);
|
||||
|
||||
//console.log(res.data);
|
||||
|
||||
if (res.status === 200) {
|
||||
setTimeout(() => {
|
||||
stopRead();
|
||||
}, 5 * 1000);
|
||||
}
|
||||
|
||||
// stop after 5 seconds
|
||||
} catch (error: any) {
|
||||
if (error.response.data.code === 3) {
|
||||
stopRead();
|
||||
setTimeout(() => {
|
||||
startRead();
|
||||
}, 1000);
|
||||
}
|
||||
createLog("error", "rfid", "rfid", `There was an error Starting the read: ${error.response.data.message}`);
|
||||
}
|
||||
};
|
||||
const stopRead = async () => {
|
||||
try {
|
||||
const res = await axios.put(
|
||||
`https://${ip}/cloud/stop`,
|
||||
{},
|
||||
{
|
||||
headers: {Authorization: `Bearer ${token}`},
|
||||
}
|
||||
);
|
||||
} catch (error: any) {
|
||||
createLog("error", "rfid", "rfid", `There was an error Stopping the read: ${error.response.data.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
60
server/services/rfid/controller/readerControl.ts
Normal file
60
server/services/rfid/controller/readerControl.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* While in production we will monitor the readers if we have not gotten a heartbeat in the last 5 min we will send a reboot command along with an email.
|
||||
*/
|
||||
|
||||
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";
|
||||
|
||||
export const newHeartBeat = async (reader: string) => {
|
||||
/**
|
||||
* When a heat beat is sent over for a reader we want to update the reader.
|
||||
*/
|
||||
|
||||
try {
|
||||
const heatBeat = await db
|
||||
.update(rfidReaders)
|
||||
.set({lastHeartBeat: sql`NOW()`})
|
||||
.where(eq(rfidReaders.reader, reader));
|
||||
createLog("info", "rfid", "rfid", `${reader} just updated its heatBeat.`);
|
||||
return {success: true, message: `${reader} just updated its heatBeat.`};
|
||||
} catch (error) {
|
||||
createLog("error", "rfid", "rfid", `${reader} encountered an error while updating the heatbeat, ${error}`);
|
||||
return {success: false, message: `${reader} encountered an error while updating the heatbeat, ${error}`};
|
||||
}
|
||||
};
|
||||
|
||||
export const badRead = async (reader: string) => {
|
||||
/**
|
||||
* When we have a bad read we want to make sure the reader shows this was well.
|
||||
*/
|
||||
try {
|
||||
const badRead = await db
|
||||
.update(rfidReaders)
|
||||
.set({lastTrigger: sql`NOW()`, lastTriggerGood: false})
|
||||
.where(eq(rfidReaders.reader, reader));
|
||||
createLog("info", "rfid", "rfid", `${reader} just Triggered a bad read.`);
|
||||
return {success: true, message: `${reader} just Triggered a bad read.`};
|
||||
} catch (error) {
|
||||
createLog("error", "rfid", "rfid", `${reader} encountered an error while updating the heatbeat, ${error}`);
|
||||
return {success: false, message: `${reader} encountered an error while updating the heatbeat, ${error}`};
|
||||
}
|
||||
};
|
||||
|
||||
export const goodRead = async (reader: string) => {
|
||||
/**
|
||||
* When we have a bad read we want to make sure the reader shows this was well.
|
||||
*/
|
||||
try {
|
||||
const goodRead = await db
|
||||
.update(rfidReaders)
|
||||
.set({lastTrigger: sql`NOW()`, lastTriggerGood: true})
|
||||
.where(eq(rfidReaders.reader, reader));
|
||||
createLog("info", "rfid", "rfid", `${reader} just Triggered a good read.`);
|
||||
return {success: true, message: `${reader} just Triggered a good read.`};
|
||||
} catch (error) {
|
||||
createLog("error", "rfid", "rfid", `${reader} encountered an error while updating the heatbeat, ${error}`);
|
||||
return {success: false, message: `${reader} encountered an error while updating the heatbeat, ${error}`};
|
||||
}
|
||||
};
|
||||
@@ -1,3 +0,0 @@
|
||||
/**
|
||||
* we will monitor shipped out pallets every hour if they get shipped out
|
||||
*/
|
||||
@@ -1,3 +0,0 @@
|
||||
/**
|
||||
* station 1 will just check for 10 tags
|
||||
*/
|
||||
@@ -1,4 +0,0 @@
|
||||
/**
|
||||
* we will have 3 reader points here station2.1 will require 10 tags, 2.2 and 2.3 will require 1
|
||||
*
|
||||
*/
|
||||
@@ -1,4 +0,0 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
@@ -1,4 +0,0 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
Reference in New Issue
Block a user