"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrackingWorkspace = void 0;
const integration_base_1 = require("@things-factory/integration-base");
const utils_1 = require("@things-factory/utils");
const robotics_types_1 = require("../../controllers/robotics-types");
const tracking_engine_1 = require("../../controllers/tracking-engine");
class TrackingWorkspace {
    async ready(connectionConfigs) {
        await Promise.all(connectionConfigs.map(this.connect.bind(this)));
        integration_base_1.ConnectionManager.logger.info('tracking-workspace connections are ready');
    }
    async connect(connection) {
        var { name } = connection;
        var engine = new tracking_engine_1.TrackingEngineImpl(name);
        integration_base_1.ConnectionManager.addConnectionInstance(connection, Object.assign(Object.assign({ discriminator: robotics_types_1.ROBOTICS_OBJECT_TYPES.WORKSPACE }, connection), { engine }));
        /* IMPROVE-ME 최초 시작시점에 다른 커넥션들이 완료될 시간을 1초 정도 벌어주자. */
        await (0, utils_1.sleep)(1000);
        engine.start({
            onstdout: stdout => {
                integration_base_1.ConnectionManager.logger.info(stdout.toString('utf8'));
            },
            onstderr: stderr => {
                integration_base_1.ConnectionManager.logger.error(stderr.toString('utf8'));
                console.error(stderr.toString('utf8'));
            },
            onexit: exitcode => {
                /* 비정상적 종료시에는 커넥션을 직접 끊어준다. */
                integration_base_1.ConnectionManager.logger.error(`tracking-workspace('${name}') connection will be disconnected due to onexit from tracking engine.`);
                integration_base_1.ConnectionManager.getConnectionInstance(connection) && this.disconnect(connection);
            }
        });
        integration_base_1.ConnectionManager.logger.info(`tracking-workspace connection(${connection.name}:${connection.endpoint}) is connected`);
    }
    async disconnect(connection) {
        var { engine } = integration_base_1.ConnectionManager.removeConnectionInstance(connection);
        engine && engine.stop();
        integration_base_1.ConnectionManager.logger.info(`tracking-workspace connection(${connection.name}) is disconnected`);
    }
    get parameterSpec() {
        return [
            {
                type: 'checkbox',
                label: 'camera stream',
                name: 'checkVideoStream'
            },
            {
                type: 'select',
                label: 'detection-method',
                name: 'detectionMethod',
                property: {
                    options: ['', 'Aruco', 'Box', 'Pack']
                }
            },
            {
                type: 'offset-pose-simple',
                name: 'poseThreshold',
                label: 'pose-threshold'
            },
            {
                type: 'workspace-status',
                label: 'workspace status',
                name: 'workspaceStatus'
            }
        ];
    }
    get taskPrefixes() {
        return ['tracking-ws'];
    }
    get help() {
        return 'robotics/connector/workspace';
    }
}
exports.TrackingWorkspace = TrackingWorkspace;
integration_base_1.ConnectionManager.registerConnector('tracking-workspace', new TrackingWorkspace());
//# sourceMappingURL=tracking-workspace.js.map