1
+ /* eslint-disable no-await-in-loop */
1
2
import path from 'path' ;
2
3
import EscPosEncoder from '@freedom_sky/esc-pos-encoder' ;
3
4
import superagent from 'superagent' ;
4
5
import { config } from '../config' ;
5
6
import {
7
+ convertToChinese ,
6
8
fs , Logger , sleep ,
7
9
} from '../utils' ;
8
10
9
11
const encoder = new EscPosEncoder ( ) ;
10
12
13
+ const i18n = {
14
+ zh : {
15
+ receipt : '气球打印单' ,
16
+ location : '座位' ,
17
+ problem : '题目' ,
18
+ color : '颜色' ,
19
+ comment : '备注' ,
20
+ team : '队伍' ,
21
+ status : '队伍当前气球状态' ,
22
+ } ,
23
+ en : {
24
+ receipt : 'Balloon Receipt' ,
25
+ location : 'Location' ,
26
+ problem : 'Balloon' ,
27
+ color : 'Color' ,
28
+ comment : 'Comment' ,
29
+ team : 'Team' ,
30
+ status : 'Team Balloon Status' ,
31
+ } ,
32
+ } ;
33
+
11
34
export const receiptText = (
12
- id : number , location : string , problem : string , color : string , comment : string , teamname : string , status : string ,
35
+ id : number , location : string , problem : string , color : string , comment : string , teamname : string , status : string , lang : 'zh' | 'en' = 'zh' ,
13
36
) => encoder
14
37
. initialize ( )
15
38
. codepage ( 'cp936' )
16
39
. setPinterType ( 80 ) // wrong typo in the library
17
40
. align ( 'center' )
18
41
. bold ( true )
19
42
. size ( 2 )
20
- . line ( '气球打印单' )
43
+ . line ( i18n [ lang ] . receipt )
21
44
. emptyLine ( 1 )
22
45
. line ( `ID: ${ id } ` )
23
46
. emptyLine ( 1 )
24
47
. bold ( false )
25
48
. size ( 1 )
26
49
. line ( '===========================================' )
27
50
. emptyLine ( 1 )
28
- . oneLine ( '座位' , location )
29
- . oneLine ( '气球' , problem )
30
- . oneLine ( '颜色' , color )
31
- . oneLine ( '备注' , comment )
51
+ . oneLine ( i18n [ lang ] . location , location )
52
+ . oneLine ( i18n [ lang ] . problem , problem )
53
+ . oneLine ( i18n [ lang ] . color , color )
54
+ . oneLine ( i18n [ lang ] . comment , comment )
32
55
. emptyLine ( 1 )
33
56
. align ( 'center' )
34
57
. bold ( true )
35
58
. line ( '===========================================' )
36
59
. emptyLine ( 2 )
37
60
. size ( 0 )
38
- . line ( `队伍 : ${ teamname } ` )
39
- . line ( '队伍当前气球状态:' )
61
+ . line ( `${ i18n [ lang ] . team } : ${ teamname } ` )
62
+ . line ( ` ${ i18n [ lang ] . status } :` )
40
63
. line ( `${ status } ` )
41
64
. emptyLine ( 2 )
42
65
. line ( 'Powered by hydro-dev/xcpc-tools' )
43
66
. emptyLine ( 3 )
44
67
. cut ( )
45
68
. encode ( ) ;
46
69
47
- const logger = new Logger ( 'fetcher ' ) ;
70
+ const logger = new Logger ( 'balloon ' ) ;
48
71
49
72
let timer = null ;
50
73
let printer = null ;
@@ -53,18 +76,18 @@ async function getReceiptStatus(receipt) {
53
76
const lp = receipt . split ( '/' ) . pop ( ) ;
54
77
const oldPrinter = printer ;
55
78
printer = {
56
- printer : lp ,
57
- info : fs . readFileSync ( `/sys/class/usb /${ lp } /device/ieee1284_id` , 'utf8' ) . trim ( ) ,
79
+ printer : receipt ,
80
+ info : fs . readFileSync ( `/sys/class/usbmisc /${ lp } /device/ieee1284_id` , 'utf8' ) . trim ( ) ,
58
81
} ;
59
82
if ( ! oldPrinter || oldPrinter . info === printer . info ) return ;
60
83
logger . info ( 'Printer changed:' , printer . printer , printer . info ) ;
61
84
const usbDevices = fs . readdirSync ( '/dev/usb' ) ;
62
85
for ( const f of usbDevices ) {
63
86
if ( f . startsWith ( 'lp' ) ) {
64
- const lpid = fs . readFileSync ( `/sys/class/usb /${ f } /device/ieee1284_id` , 'utf8' ) . trim ( ) ;
87
+ const lpid = fs . readFileSync ( `/sys/class/usbmisc /${ f } /device/ieee1284_id` , 'utf8' ) . trim ( ) ;
65
88
if ( lpid === oldPrinter . info ) {
66
89
logger . info ( 'Printer found:' , f , ':' , lpid ) ;
67
- oldPrinter . printer = f ;
90
+ oldPrinter . printer = `/dev/usb/ ${ f } ` ;
68
91
printer = oldPrinter ;
69
92
break ;
70
93
}
@@ -73,34 +96,37 @@ async function getReceiptStatus(receipt) {
73
96
if ( oldPrinter . info !== printer . info ) throw Error ( 'Printer not found, please check the printer connection.' ) ;
74
97
}
75
98
76
- async function printBalloon ( doc ) {
99
+ async function printBalloon ( doc , lang ) {
77
100
const bReceipt = receiptText (
78
101
doc . balloonid ,
79
102
doc . location ? doc . location : 'N/A' ,
80
103
doc . problem ,
81
- doc . contestproblem . color ,
104
+ lang === 'zh' ? convertToChinese ( doc . contestproblem . color ) : doc . contestproblem . color ,
82
105
doc . awards ? doc . awards : 'N/A' ,
83
106
doc . team ,
84
107
doc . total ? Object . keys ( doc . total ) . map ( ( k ) => `- ${ k } : ${ doc . total [ k ] . color } ` ) . join ( '\n' ) : 'N/A' ,
108
+ lang ,
85
109
) ;
86
110
if ( printer ) {
87
111
await getReceiptStatus ( printer . printer ) ;
88
- fs . writeFileSync ( path . resolve ( printer ) , bReceipt ) ;
112
+ fs . writeFileSync ( path . resolve ( printer . printer ) , bReceipt ) ;
89
113
}
90
114
}
91
115
92
116
async function fetchTask ( c ) {
93
117
if ( timer ) clearTimeout ( timer ) ;
94
- logger . info ( 'Fetching Task from tools server...' ) ;
118
+ logger . info ( 'Fetching balloon task from tools server...' ) ;
95
119
try {
96
120
const { body } = await superagent . post ( `${ c . server } /client/${ c . token } /balloon` ) . send ( ) ;
97
- if ( body . doc ) {
98
- logger . info ( `Print task ${ body . doc . tid } #${ body . doc . _id } ...` ) ;
99
- await printBalloon ( body . doc ) ;
100
- await superagent . post ( `${ c . server } /client/${ c . token } /doneballoon/${ body . doc . _id } ` ) ;
101
- logger . info ( `Print task ${ body . doc . tid } #${ body . doc . _id } completed.` ) ;
121
+ if ( body . balloons ) {
122
+ for ( const doc of body . balloons ) {
123
+ logger . info ( `Print balloon task ${ doc . teamid } #${ doc . balloonid } ...` ) ;
124
+ await printBalloon ( doc , config . receiptLang ) ;
125
+ await superagent . post ( `${ c . server } /client/${ c . token } /doneballoon/${ doc . balloonid } ` ) ;
126
+ logger . info ( `Print task ${ doc . teamid } #${ doc . balloonid } completed.` ) ;
127
+ }
102
128
} else {
103
- logger . info ( 'No print task, sleeping...' ) ;
129
+ logger . info ( 'No balloon task, sleeping...' ) ;
104
130
await sleep ( 5000 ) ;
105
131
}
106
132
} catch ( e ) {
@@ -113,4 +139,5 @@ async function fetchTask(c) {
113
139
export async function apply ( ) {
114
140
await getReceiptStatus ( config . balloon ) ;
115
141
if ( config . token && config . server && config . balloon ) await fetchTask ( config ) ;
142
+ else logger . error ( 'Config not found, please check the config.yaml' ) ;
116
143
}
0 commit comments