Skip to content

Commit fb46bfb

Browse files
committed
rename to divide balloon and printer
1 parent 78f5b0f commit fb46bfb

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

packages/server/handler/client.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ClientControlHandler extends AuthHandler {
3535
}
3636
}
3737

38-
class ClientConnectHandler extends Handler {
38+
class ClientPrintConnectHandler extends Handler {
3939
async post(params) {
4040
const client = await this.ctx.db.client.findOne({ id: params.cid });
4141
if (!client) throw new AccessDeniedError('Client', null, 'Client not found');
@@ -84,8 +84,37 @@ class ClientPrintDoneHandler extends Handler {
8484
}
8585
}
8686

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+
87114
export async function apply(ctx: Context) {
88115
ctx.Route('client_control', '/client', ClientControlHandler);
89-
ctx.Route('client_fetch', '/client/:cid', ClientConnectHandler);
116+
ctx.Route('client_print_fetch', '/client/:cid/print', ClientPrintConnectHandler);
90117
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);
91120
}

packages/server/init.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ server: \ntoken: \nusername: \npassword: \nsecretRoute: ${String.random(12)}`);
1212
throw new Error('Config file generated, please fill in the config.yaml');
1313
}
1414
const {
15-
type, viewPass, server, token, username, password, port, cors, secretRoute, oldMonitor, videoProxy,
15+
type, viewPass, server, token, username, password, port, cors, secretRoute, oldMonitor, videoProxy, freezeEncourage,
1616
} = yaml.load(fs.readFileSync(configPath, 'utf8').toString()) as any;
1717
global.Tools = {
1818
config: {
@@ -25,6 +25,7 @@ server: \ntoken: \nusername: \npassword: \nsecretRoute: ${String.random(12)}`);
2525
secretRoute,
2626
oldMonitor,
2727
videoProxy,
28+
freezeEncourage,
2829
},
2930
version: require('./package.json').version,
3031
};

0 commit comments

Comments
 (0)