Skip to content

Instantly share code, notes, and snippets.

@gatherKnowledge
Last active August 4, 2021 03:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gatherKnowledge/e83f884db92a12c73cfbd4d2a23278b9 to your computer and use it in GitHub Desktop.
Save gatherKnowledge/e83f884db92a12c73cfbd4d2a23278b9 to your computer and use it in GitHub Desktop.
const readline = require("readline");
const chalk = require("chalk");
const axios = require("axios");
const PROTOCOL = "http";
const URI = "localhost";
const PORT = 3000;
const SERVER = `${PROTOCOL}://${URI}:${PORT}`;
const PREFIX_PATH = "produce";
const TOKEN = "xxxxxxxxxxx";
const axiosInstance = axios.create({
baseURL: `${SERVER}`,
headers: { Authorization: `Bearer ${TOKEN}` },
timeout: 3000,
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
console.log(chalk.blue("BARCODE READER DEVICE TESTER"));
let dots = "";
const interval = setInterval(() => {
console.log(chalk.blue(`RUNNING${dots}`));
dots += ".";
if (dots.length === 10) {
clearInterval(interval);
console.clear();
rl.question("수량을 입력해주세요. ", (quantity) => {
const qty = parseInt(quantity);
console.clear();
if (isNaN(qty)) throw new Error("숫자만 입력가능합니다.");
console.log(chalk.blue(`입력 수량: ${qty}`));
console.log("준비 완료");
rl.on("line", async function (line) {
try {
const response = await axiosInstance.post(`/${PREFIX_PATH}`, {
itemCode: line,
qty,
});
const { status, data } = response;
if (status === 200 && data === "ok") {
console.log("추가 완료");
} else {
console.log("오류가 발생했습니다.");
}
} catch (e) {
console.error(e);
}
}).on("close", function () {
process.exit();
});
});
}
}, 100);
{
"name": "plc-sample",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "./node_modules/.bin/ts-node index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1",
"chalk": "^4.1.2",
"ts-node": "^10.1.0",
"typescript": "^4.3.5"
},
"devDependencies": {
"@types/node": "^16.4.10",
"tslib": "^2.3.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment