migrated to async service

This commit is contained in:
didier amyot committed 2023-10-19 11:59:05 -04:00
1 parent d7711ba95f
commit db36a61ffd
1 file changed
+39 -39
+39 -39
View File
@@ -25,18 +25,18 @@ type LiftAction = {
class ButtonService{ class ButtonService{
private readonly liftRequests : LiftRequest[] = []; private readonly liftRequests : LiftRequest[] = [];
getLiftRequests(): LiftRequest[]{ getLiftRequests(): Promise<LiftRequest[]>{
return this.liftRequests; return Promise.resolve(this.liftRequests);
} }
addLiftRequest(liftRequest: LiftRequest): boolean{ addLiftRequest(liftRequest: LiftRequest): Promise<boolean>{
for(const lr of this.liftRequests){ for(const lr of this.liftRequests){
if(lr.level === liftRequest.level && lr.direction === liftRequest.direction){ if(lr.level === liftRequest.level && lr.direction === liftRequest.direction){
return false; return Promise.resolve(false);
} }
} }
this.liftRequests.push(liftRequest); this.liftRequests.push(liftRequest);
return true; return Promise.resolve(true);
} }
} }
@@ -48,11 +48,11 @@ class LiftService {
this.lifts = lifts; this.lifts = lifts;
} }
getAllLifts(): Lift[] { getAllLifts(): Promise<Lift[]> {
return this.lifts; return Promise.resolve(this.lifts);
} }
private computeDirection(lift: Lift): string{ private computeDirection(lift: Lift): string {
if(lift.destinations.length === 0){ if(lift.destinations.length === 0){
return "IDLE"; return "IDLE";
} }
@@ -96,26 +96,26 @@ class LiftService {
} }
processAddDestination(liftId: number, level: number): boolean { async processAddDestination(liftId: number, level: number): Promise<boolean> {
const lift = this.getElevatorById(liftId); const lift = await this.getElevatorById(liftId);
if(!lift){ if(!lift){
return false; return Promise.resolve(false);
} }
lift.destinations = this.addDestination(lift.destinations, lift.level, level); lift.destinations = this.addDestination(lift.destinations, lift.level, level);
lift.direction = this.computeDirection(lift); lift.direction = this.computeDirection(lift);
return true; return Promise.resolve(true);
} }
processDoorOpen(liftId: number, level: number): boolean { async processDoorOpen(liftId: number, level: number): Promise<boolean> {
const lift = this.getElevatorById(liftId); const lift = await this.getElevatorById(liftId);
if(!lift){ if(!lift){
return false; return Promise.resolve(false);
} }
lift.destinations = this.removeFromArray(lift.destinations, level); lift.destinations = this.removeFromArray(lift.destinations, level);
lift.direction = this.computeDirection(lift); lift.direction = this.computeDirection(lift);
return true; return Promise.resolve(true);
} }
removeFromArray(lst: number[], toRemove: number): number[] { private removeFromArray(lst: number[], toRemove: number): number[] {
const ret: number[] = []; const ret: number[] = [];
for(const e of lst){ for(const e of lst){
if(e !== toRemove){ if(e !== toRemove){
@@ -125,13 +125,13 @@ class LiftService {
return ret return ret
} }
processLiftMovement(liftId: number, level: number): boolean { async processLiftMovement(liftId: number, level: number): Promise<boolean> {
const lift = this.getElevatorById(liftId); const lift = await this.getElevatorById(liftId);
if(!lift){ if(!lift){
return false; return Promise.resolve(false);
} }
lift.level = level; lift.level = level;
return true; return Promise.resolve(true);
} }
private matches(lift: Lift, liftFilterCriteria: LiftFilterCriteron) { private matches(lift: Lift, liftFilterCriteria: LiftFilterCriteron) {
@@ -154,23 +154,23 @@ class LiftService {
return true; return true;
} }
getAllListsMatching(liftFilterCriteria: LiftFilterCriteron): Lift[] { getAllListsMatching(liftFilterCriteria: LiftFilterCriteron): Promise<Lift[]> {
const ret: Lift[] = []; const ret: Lift[] = [];
for (const lift of this.lifts) { for (const lift of this.lifts) {
if (this.matches(lift, liftFilterCriteria)) { if (this.matches(lift, liftFilterCriteria)) {
ret.push(lift); ret.push(lift);
} }
} }
return ret; return Promise.resolve(ret);
} }
getElevatorById(id: number): Lift | null { getElevatorById(id: number): Promise<Lift | null> {
for (const lift of this.lifts) { for (const lift of this.lifts) {
if (lift.id === id) { if (lift.id === id) {
return lift; return Promise.resolve(lift);
} }
} }
return null; return Promise.resolve(null);
} }
} }
@@ -208,25 +208,25 @@ function send400(res: Response): void {
res.send({}); res.send({});
} }
app.get('/api/v1/lifts', (req: Request, res: Response): void => { app.get('/api/v1/lifts', async (req: Request, res: Response): Promise<void> => {
const query = req.query as LiftSearchRequestParams; const query = req.query as LiftSearchRequestParams;
const criterion = liftSearchRequestParamsToLiftFilterCriteria(query); const criterion = liftSearchRequestParamsToLiftFilterCriteria(query);
const ret = liftService.getAllListsMatching(criterion); const ret = await liftService.getAllListsMatching(criterion);
res.send({lifts: toSmallRepresentation(ret)}) res.send({lifts: toSmallRepresentation(ret)})
}); });
app.get('/api/v1/lifts/:id', (req: Request<{ id: string }>, res: Response): void => { app.get('/api/v1/lifts/:id', async (req: Request<{ id: string }>, res: Response): Promise<void> => {
const id = req.params.id; const id = req.params.id;
const ret = liftService.getElevatorById(parseInt(id)); const ret = await liftService.getElevatorById(parseInt(id));
if (ret == null) { if (ret == null) {
return send404(res); return send404(res);
} }
res.send(ret) res.send(ret)
}); });
app.post('/api/v1/lifts/:id', (req: Request<{ id: string }>, res: Response): void => { app.post('/api/v1/lifts/:id', async (req: Request<{ id: string }>, res: Response): Promise<void> => {
const id = parseInt(req.params.id); const id = parseInt(req.params.id);
const ret = liftService.getElevatorById(id); const ret = await liftService.getElevatorById(id);
if (ret == null) { if (ret == null) {
return send404(res); return send404(res);
} }
@@ -235,24 +235,24 @@ app.post('/api/v1/lifts/:id', (req: Request<{ id: string }>, res: Response): voi
return send400(res) return send400(res)
} }
if(action.type === 'lift-move'){ if(action.type === 'lift-move'){
liftService.processLiftMovement(id, action.level!); await liftService.processLiftMovement(id, action.level!);
} }
if(action.type === 'door-open'){ if(action.type === 'door-open'){
liftService.processDoorOpen(id, action.level!); await liftService.processDoorOpen(id, action.level!);
} }
if(action.type === 'add-destination'){ if(action.type === 'add-destination'){
liftService.processAddDestination(id, action.destination!); await liftService.processAddDestination(id, action.destination!);
} }
res.send(ret) res.send(ret)
}); });
app.get('/api/v1/lift-requests', (req: Request, res: Response): void => { app.get('/api/v1/lift-requests', async (_req: Request, res: Response): Promise<void> => {
res.send(buttonService.getLiftRequests()); res.send(await buttonService.getLiftRequests());
}); });
app.post('/api/v1/lift-requests', (req: Request, res: Response): void => { app.post('/api/v1/lift-requests', async (req: Request, res: Response): Promise<void> => {
const changed = buttonService.addLiftRequest(req.body as LiftRequest); const changed = await buttonService.addLiftRequest(req.body as LiftRequest);
res.send({changed: changed}); res.send({changed: changed});
}); });