Skip to content

Commit

Permalink
feat(barcode-publisher): 🎉 add barcode-publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
gokhangunduz committed Apr 16, 2024
1 parent 1d930d5 commit 865231f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
9 changes: 5 additions & 4 deletions app/src/jobs/rosBarcodeListener.job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ export default async function rosBarcodeListenerJob() {
"barcode",
JSON.stringify({
time: Math.floor(+new Date() / 1000),
scanner_id: Number(message.scanner_id),
sensorId: Number(message.sensorId),
barcode: message.barcode,
location_x: location.location_x,
location_y: location.location_y,
location_z: location.location_z,
location_x: message.waypoint.x,
location_y: message.waypoint.y,
location_z: message.waypoint.z,
yaw: message.waypoint.yaw,
})
);
} catch (error) {
Expand Down
70 changes: 70 additions & 0 deletions app/src/tests/barcodePublisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-disable no-useless-escape */
import { exec } from "child_process";

const initialData = {
robotId: 1,
fleetId: 1,
sensorId: 1,
waypoint: {
x: 1,
y: 1,
z: 0,
yaw: 0.0,
},
};

let barcodes: string[] = [];

function commandGenerator(commandData: {
robotId?: number;
fleetId?: number;
sensorId?: number;
x: number;
y: number;
z: number;
}) {
return `ros2 topic pub --once /barcode std_msgs/msg/String '{\"data\": \"{\\\"robotId\\\": \\\"${commandData.robotId || initialData.robotId}\\\", \\\"fleetId\\\": \\\"${commandData.fleetId || initialData.fleetId}\\\", \\\"sensorId\\\": \\\"${commandData.sensorId || initialData.sensorId}\\\", \\\"barcode\\\": \\\"${randomBarcodeGenerator()}\\\", \\\"waypoint\\\": {\\\"x\\\": ${commandData.x}, \\\"y\\\": ${commandData.y}, \\\"z\\\": ${commandData.z}, \\\"yaw\\\": 0.0}}\"}'`;
}

function randomBarcodeGenerator() {
return Math.random().toString(36).substr(2, 8);
}

for (let x = 1; x < 13; x++) {
for (let y = 1; y < 13; y++) {
for (let z = 0; z < 6; z++) {
const command = commandGenerator({
x: initialData.waypoint.x + x,
y: initialData.waypoint.y + y,
z: initialData.waypoint.z + z,
});
barcodes.push(command);
}
}
}

function runCommands() {
if (barcodes.length === 0) {
console.log("All commands executed!");
return;
}

const command = barcodes.shift();

console.log("WORK:", command);
exec(command!, (error, stdout, stderr) => {
if (error) {
console.error(`JSERR: ${error.message}`);
return;
}
if (stderr) {
console.error(`STDERR: ${stderr}`);
return;
}
console.log(`STDOUT: ${stdout}`);
});

setTimeout(runCommands, 100);
}

runCommands();
2 changes: 1 addition & 1 deletion app/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type Environments = {
};

export type Barcode = {
scanner_id: number;
sensorId: number;
time: number;
barcode: string;
location_x: number;
Expand Down

0 comments on commit 865231f

Please sign in to comment.