Skip to content

Commit 487be1c

Browse files
committed
fix
1 parent db89242 commit 487be1c

File tree

8 files changed

+21
-22
lines changed

8 files changed

+21
-22
lines changed

packages/server/client/balloon.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
checkReceiptStatus, convertToChinese, Logger, receiptPrint, sleep,
77
} from '../utils';
88

9+
const post = (url: string) => superagent.post(new URL(url, config.server).toString()).set('Accept', 'application/json');
910
const encoder = new EscPosEncoder();
1011

1112
const i18n = {
@@ -89,12 +90,12 @@ async function fetchTask(c) {
8990
if (timer) clearTimeout(timer);
9091
logger.info('Fetching balloon task from tools server...');
9192
try {
92-
const { body } = await superagent.post(`${c.server}/client/${c.token}/balloon`).send();
93+
const { body } = await post(`${c.server}/client/${c.token}/balloon`).send();
9394
if (body.balloons) {
9495
for (const doc of body.balloons) {
9596
logger.info(`Print balloon task ${doc.teamid}#${doc.balloonid}...`);
9697
await printBalloon(doc, config.receiptLang);
97-
await superagent.post(`${c.server}/client/${c.token}/doneballoon/${doc.balloonid}`);
98+
await post(`${c.server}/client/${c.token}/doneballoon/${doc.balloonid}`);
9899
logger.info(`Print task ${doc.teamid}#${doc.balloonid} completed.`);
99100
}
100101
} else {
@@ -109,7 +110,7 @@ async function fetchTask(c) {
109110
}
110111

111112
export async function apply() {
112-
printer = config.balloon;
113+
printer = { printer: config.balloon };
113114
if (config.token && config.server && config.balloon) await fetchTask(config);
114115
else logger.error('Config not found, please check the config.yaml');
115116
}

packages/server/client/printer.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { createTypstCompiler, generateTypst } from './typst';
99

1010
let compiler;
1111

12+
const post = (url: string) => superagent.post(new URL(url, config.server).toString()).set('Accept', 'application/json');
1213
const logger = new Logger('printer');
1314

1415
let timer = null;
@@ -36,7 +37,7 @@ export async function printFile(doc) {
3637
if (config.printers.length) {
3738
// eslint-disable-next-line no-constant-condition
3839
while (true) {
39-
const printersInfo = await getPrinters();
40+
const printersInfo: any[] = await getPrinters();
4041
const printers = printersInfo.filter((p) => config.printers.includes(p.printer));
4142
const randomP = printers[Math.floor(Math.random() * printers.length)];
4243
if (randomP.status === 'idle') {
@@ -65,8 +66,8 @@ async function fetchTask(c) {
6566
if (timer) clearTimeout(timer);
6667
logger.info('Fetching Task from tools server...');
6768
try {
68-
const printersInfo = await getPrinters();
69-
const { body } = await superagent.post(`${c.server}/client/${c.token}/print`)
69+
const printersInfo: any[] = await getPrinters();
70+
const { body } = await post(`${c.server}/client/${c.token}/print`)
7071
.send({
7172
printers: config.printers,
7273
printersInfo: JSON.stringify(printersInfo.map((p) => ({
@@ -83,7 +84,7 @@ async function fetchTask(c) {
8384
if (body.doc) {
8485
logger.info(`Print task ${body.doc.tid}#${body.doc._id}...`);
8586
await printFile(body.doc);
86-
await superagent.post(`${c.server}/client/${c.token}/doneprint/${body.doc._id}`);
87+
await post(`${c.server}/client/${c.token}/doneprint/${body.doc._id}`);
8788
logger.info(`Print task ${body.doc.tid}#${body.doc._id} completed.`);
8889
} else {
8990
logger.info('No print task, sleeping...');

packages/server/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ password:
3232
`;
3333
let printers = [];
3434
if (isClient) {
35-
printers = await getPrinters(true).catch(() => []);
36-
logger.info(printers.length, 'printers found:', printers.join(', '));
37-
await checkReceiptPrinter(printers);
35+
printers = (await getPrinters().catch(() => [])).map((p: any) => p.printer);
36+
logger.info(printers.length, 'printers found:', JSON.stringify(printers));
37+
await checkReceiptPrinter(await getPrinters(true));
3838
}
3939
const clientConfigDefault = yaml.dump({
4040
server: '',
4141
balloon: '',
4242
balloonLang: 'zh',
43-
printers: printers.map((p) => p.printer),
43+
printers,
4444
token: '',
4545
});
4646
fs.writeFileSync(configPath, isClient ? clientConfigDefault : serverConfigDefault);

packages/server/handler/commands.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ zenity --info --text "<span font='256'>$(cat ${config.seatFile})</span>"`);
3939
}
4040

4141
async postSetHostname() {
42-
this.response.body = await this.executeForAll(`hostnamectl hostname $(cat ${config.seatFile})`);
42+
this.response.body = await this.executeForAll(`hostnamectl hostname $(cat -- ${config.seatFile})`);
4343
}
4444

4545
async postAutologin({ }, autologin = false) {

packages/server/index.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ async function apply(ctx) {
4747
} else {
4848
await applyServer(ctx);
4949
}
50-
await require('./handler/misc').apply(ctx);
51-
await require('./handler/printer').apply(ctx);
52-
await require('./handler/monitor').apply(ctx);
53-
await require('./handler/client').apply(ctx);
54-
await require('./handler/balloon').apply(ctx);
5550
await ctx.lifecycle.flush();
5651
await ctx.parallel('app/listen');
5752
logger.success('Tools started');

packages/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@hydrooj/xcpc-tools",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A tools for XCPC contests",
55
"main": "index.ts",
66
"repository": "https://github.com/Hydro-dev/xcpc-tools",

packages/server/service/fetcher.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,5 @@ export async function apply(ctx) {
239239
}
240240
ctx.provide('fetcher', undefined, true);
241241
ctx.fetcher = await new fetcherList[config.type](ctx);
242+
ctx.fetcher.cron();
242243
}

packages/server/utils/receipt.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ export async function checkReceiptPrinter(printers: object[]) {
3636
export async function checkReceiptStatus(printer) {
3737
if (process.platform !== 'linux') {
3838
printer = { printer: printer.printer };
39-
return;
39+
return printer;
4040
}
4141
const lp = printer.printer.split('/').pop();
4242
const oldPrinter = printer;
4343
printer = {
4444
printer: printer.printer,
4545
info: fs.readFileSync(`/sys/class/usbmisc/${lp}/device/ieee1284_id`, 'utf8').trim(),
4646
};
47-
if (!oldPrinter || oldPrinter.info === printer.info) return;
47+
if (!oldPrinter || oldPrinter.info === printer.info) return printer;
4848
logger.info('Printer changed:', printer.printer, printer.info);
4949
const usbDevices = fs.readdirSync('/dev/usb');
5050
for (const f of usbDevices) {
@@ -54,14 +54,15 @@ export async function checkReceiptStatus(printer) {
5454
logger.info('Printer found:', f, ':', lpid);
5555
oldPrinter.printer = `/dev/usb/${f}`;
5656
printer = oldPrinter;
57-
break;
57+
return printer;
5858
}
5959
}
6060
}
6161
if (oldPrinter.info !== printer.info) throw Error('Printer not found, please check the printer connection.');
62+
return printer;
6263
}
6364

64-
export async function receiptPrint(text, printer) {
65+
export async function receiptPrint(printer, text) {
6566
fs.writeFileSync(path.resolve(process.cwd(), 'data', 'balloon.txt'), text);
6667
if (process.platform === 'win32') {
6768
exec(`COPY /B "${path.resolve(process.cwd(), 'data', 'balloon.txt')}" "${printer.printer}"`, (err, stdout, stderr) => {

0 commit comments

Comments
 (0)