@@ -35,7 +35,7 @@ class ClientControlHandler extends AuthHandler {
35
35
}
36
36
}
37
37
38
- class ClientConnectHandler extends Handler {
38
+ class ClientPrintConnectHandler extends Handler {
39
39
async post ( params ) {
40
40
const client = await this . ctx . db . client . findOne ( { id : params . cid } ) ;
41
41
if ( ! client ) throw new AccessDeniedError ( 'Client' , null , 'Client not found' ) ;
@@ -84,8 +84,37 @@ class ClientPrintDoneHandler extends Handler {
84
84
}
85
85
}
86
86
87
+ class ClientBallloonConnectHandler extends Handler {
88
+ async post ( params ) {
89
+ const client = await this . ctx . db . client . findOne ( { id : params . cid } ) ;
90
+ if ( ! client ) throw new AccessDeniedError ( 'Client' , null , 'Client not found' ) ;
91
+ const ip = this . request . ip . replace ( '::ffff:' , '' ) ;
92
+ logger . info ( `Client ${ client . name } (${ ip } ) connected.` ) ;
93
+ const balloons = await this . ctx . db . balloon . find ( { printDone : 0 , shouldPrint : true } ) . sort ( { time : 1 } ) ;
94
+ this . response . body = { balloons } ;
95
+ logger . info ( `Client ${ client . name } connected, print ${ balloons . length } tasks sent.` ) ;
96
+ await this . ctx . db . client . updateOne ( { id : params . cid } , { $set : { updateAt : new Date ( ) . getTime ( ) , ip } } ) ;
97
+ await this . ctx . db . balloon . update ( { balloonid : { $in : balloons . map ( ( b ) => b . balloonid ) } } ,
98
+ { $set : { receivedAt : new Date ( ) . getTime ( ) } } , { multi : true } ) ;
99
+ }
100
+ }
101
+ class ClientBalloonDoneHandler extends Handler {
102
+ async post ( params ) {
103
+ const client = await this . ctx . db . client . findOne ( { id : params . cid } ) ;
104
+ if ( ! client ) throw new AccessDeniedError ( 'Client' , null , 'Client not found' ) ;
105
+ const balloon = await this . ctx . db . balloon . findOne ( { balloonid : + params . tid } ) ;
106
+ if ( ! balloon ) throw new ValidationError ( 'Balloon' , params . tid , 'Balloon not found' ) ;
107
+ await this . ctx . db . balloon . updateOne ( { balloonid : + params . tid } , { $set : { printDone : 1 , printDoneAt : new Date ( ) . getTime ( ) } } ) ;
108
+ if ( ! balloon . done ) await this . ctx . fetcher . setBalloonDone ( balloon . balloonid ) ;
109
+ this . response . body = { code : 1 } ;
110
+ logger . info ( `Client ${ client . name } connected, print task ${ balloon . teamid } #${ balloon . balloonid } completed.` ) ;
111
+ }
112
+ }
113
+
87
114
export async function apply ( ctx : Context ) {
88
115
ctx . Route ( 'client_control' , '/client' , ClientControlHandler ) ;
89
- ctx . Route ( 'client_fetch ' , '/client/:cid' , ClientConnectHandler ) ;
116
+ ctx . Route ( 'client_print_fetch ' , '/client/:cid/print ' , ClientPrintConnectHandler ) ;
90
117
ctx . Route ( 'client_print_done' , '/client/:cid/doneprint/:tid' , ClientPrintDoneHandler ) ;
118
+ ctx . Route ( 'client_balloon_fetch' , '/client/:cid/balloon' , ClientBallloonConnectHandler ) ;
119
+ ctx . Route ( 'client_balloon_done' , '/client/:cid/doneballoon/:tid' , ClientBalloonDoneHandler ) ;
91
120
}
0 commit comments