All files / dist/controller tn-communication-controller.js

100% Statements 40/40
81.25% Branches 13/16
100% Functions 9/9
100% Lines 40/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107    5x 5x 5x 5x 5x     17x 17x               17x     13x   16x             16x 16x 16x   16x       17x         17x 18x 18x 18x 18x   18x 13x     5x         18x 5x         18x       13x   15x 15x 15x                 18x     18x 18x   18x 5x 5x   18x 13x     5x                   5x  
"use strict";
/*! Copyright (c) 2023 Siemens AG. Licensed under the MIT License. */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TnCommunicationController = void 0;
const core_1 = require("@coaty/core");
const operators_1 = require("rxjs/operators");
const tn_base_controller_1 = require("./tn-base-controller");
class TnCommunicationController extends tn_base_controller_1.TnBaseController {
    publishChannel(event) {
        const { id, data, sourceId } = event;
        const coatyObject = {
            name: "Auto-created from Protobuf Any",
            objectId: this.runtime.newUuid(),
            coreType: "CoatyObject",
            objectType: data.type_url,
            value: this.byteArrayToBase64(data.value),
            sourceId,
        };
        this.communicationManager.publishChannel(core_1.ChannelEvent.withObject(id, coatyObject));
    }
    observeChannel(selector, onCancelled$) {
        return this.communicationManager.observeChannel(selector.id)
            .pipe((0, operators_1.takeUntil)(onCancelled$), (0, operators_1.map)(evt => {
            const pbChannelEvt = {
                id: evt.channelId,
                data: {
                    type_url: evt.data.object.objectType,
                    value: this.base64ToByteArray(evt.data.object["value"]),
                },
            };
            const sourceId = evt.data.object["sourceId"];
            Eif (sourceId !== undefined) {
                pbChannelEvt.sourceId = sourceId;
            }
            return pbChannelEvt;
        }));
    }
    publishCall(event, onCancelled$) {
        const paramaters = {
            type_url: event.parameters.type_url,
            value: this.byteArrayToBase64(event.parameters.value),
            sourceId: event.sourceId,
        };
        return this.communicationManager.publishCall(core_1.CallEvent.with(event.operation, paramaters))
            .pipe((0, operators_1.takeUntil)(onCancelled$), (0, operators_1.map)(returnEvent => returnEvent.data), (0, operators_1.map)(data => {
            const evt = {};
            Eif (data.executionInfo.sourceId !== undefined) {
                evt.sourceId = data.executionInfo.sourceId;
            }
            if (data.isError) {
                evt.error = data.error;
            }
            else {
                evt.data = {
                    type_url: data.result.type_url,
                    value: this.base64ToByteArray(data.result["value"]),
                };
            }
            if ("type_url" in data.executionInfo && "value" in data.executionInfo) {
                evt.executionInfo = {
                    type_url: data.executionInfo.type_url,
                    value: this.base64ToByteArray(data.executionInfo["value"]),
                };
            }
            return evt;
        }));
    }
    observeCall(selector, onCancelled$) {
        return this.communicationManager.observeCall(selector.operation)
            .pipe((0, operators_1.takeUntil)(onCancelled$), (0, operators_1.map)(evt => {
            const correlationId = this.runtime.newUuid();
            const sourceId = evt.data.getParameterByName("sourceId");
            return {
                operation: evt.operation,
                parameters: {
                    type_url: evt.data.getParameterByName("type_url"),
                    value: this.base64ToByteArray(evt.data.getParameterByName("value")),
                },
                sourceId,
                correlationId,
                responseCallback: pbReturnEvent => {
                    const execInfo = {
                        correlationId,
                    };
                    Eif (pbReturnEvent.sourceId !== undefined) {
                        execInfo.sourceId = pbReturnEvent.sourceId;
                    }
                    if (!!pbReturnEvent.executionInfo) {
                        execInfo.type_url = pbReturnEvent.executionInfo.type_url;
                        execInfo.value = this.byteArrayToBase64(pbReturnEvent.executionInfo.value);
                    }
                    if (pbReturnEvent.error !== undefined) {
                        evt.returnEvent(core_1.ReturnEvent.withError(pbReturnEvent.error.code, pbReturnEvent.error.message, execInfo));
                    }
                    else {
                        evt.returnEvent(core_1.ReturnEvent.withResult({
                            type_url: pbReturnEvent.data.type_url,
                            value: this.byteArrayToBase64(pbReturnEvent.data.value),
                        }, execInfo));
                    }
                },
            };
        }));
    }
}
exports.TnCommunicationController = TnCommunicationController;