|
1 | | -import { ControllerDefinition, KuzzleRequest } from "kuzzle"; |
| 1 | +import { BadRequestError, ControllerDefinition, KuzzleRequest } from "kuzzle"; |
2 | 2 |
|
3 | 3 | import { DecodersRegister } from "./DecodersRegister"; |
4 | 4 | import { PayloadService } from "./PayloadService"; |
@@ -32,6 +32,15 @@ export class DecodersController { |
32 | 32 | { path: "device-manager/decoders/_prunePayloads", verb: "delete" }, |
33 | 33 | ], |
34 | 34 | }, |
| 35 | + route: { |
| 36 | + handler: this.route.bind(this), |
| 37 | + http: [ |
| 38 | + { |
| 39 | + path: "device-manager/decoders/route", |
| 40 | + verb: "post", |
| 41 | + }, |
| 42 | + ], |
| 43 | + }, |
35 | 44 | }, |
36 | 45 | }; |
37 | 46 | } |
@@ -61,4 +70,32 @@ export class DecodersController { |
61 | 70 |
|
62 | 71 | return { deleted }; |
63 | 72 | } |
| 73 | + |
| 74 | + async route(request: KuzzleRequest): Promise<{ valid: boolean }> { |
| 75 | + const payload = request.getBody(); |
| 76 | + const model = |
| 77 | + payload.deviceModel ?? request.getString("deviceModel", "unknownModel"); |
| 78 | + const apiAction = `${request.input.controller}:${request.input.action}`; |
| 79 | + if (model === "unknownModel") { |
| 80 | + app.log.warn( |
| 81 | + "Received payload without a device model: routing to receiveUnknown", |
| 82 | + ); |
| 83 | + this.payloadService.receiveUnknown(model, payload, apiAction); |
| 84 | + throw new BadRequestError( |
| 85 | + "Payload must specify the deviceModel for proper routing", |
| 86 | + ); |
| 87 | + } |
| 88 | + const decoder = this.decodersRegister.decoders.find( |
| 89 | + (d) => d.deviceModel === model, |
| 90 | + ); |
| 91 | + if (!decoder) { |
| 92 | + app.log.warn( |
| 93 | + `Received payload from ${model} model, no associated decoder found: routing to receiveUnknown`, |
| 94 | + ); |
| 95 | + this.payloadService.receiveUnknown(model, payload, apiAction); |
| 96 | + throw new BadRequestError("The specified device model is unknown"); |
| 97 | + } |
| 98 | + app.log.debug("Routing payload to decoder for " + decoder.deviceModel); |
| 99 | + return this.payloadService.receive(request, decoder); |
| 100 | + } |
64 | 101 | } |
0 commit comments