"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a, _b, _c, _d, _e, _f;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrackingCameraMutation = void 0;
const type_graphql_1 = require("type-graphql");
const tracking_camera_type_1 = require("./tracking-camera-type");
const robotarm_type_1 = require("../robotarm/robotarm-type");
const env_1 = require("@things-factory/env");
const robotics_types_1 = require("../../controllers/robotics-types");
const integration_base_1 = require("@things-factory/integration-base");
const data_bridge_1 = require("../../controllers/data-bridge/data-bridge");
const data_bridge_manager_1 = require("../../controllers/data-bridge/data-bridge-manager");
const await_spawn_1 = __importDefault(require("await-spawn"));
// extend type Mutation {
//   detectTrackingCameraROIs(name: String!): [ROI]
//   calibrateHandeyeMatrix(name: String!): Matrix
//   calibrateCameraParameter(name: String!): CameraParameter
//   captureCalibrateResult(name: String!): Boolean
//   getCalibrateResult(name: String!): Boolean
//   finishCameraCalibrate(name: String!): Boolean
//   finishHandeyeCalibrate(name: String!): Boolean
//   startHandeyeCalibrate(name: String!): Boolean
//   getHandeyeResult(name: String!): Boolean
// }
const visionConfig = env_1.config.get('vision', {});
const cameraCalibratorProgram = (_b = (_a = visionConfig.camera) === null || _a === void 0 ? void 0 : _a.cameraCalibrator) === null || _b === void 0 ? void 0 : _b.program;
const ROIDetectorProgram = (_d = (_c = visionConfig.camera) === null || _c === void 0 ? void 0 : _c.ROIDetector) === null || _d === void 0 ? void 0 : _d.program;
const handEyeCalibratorProgram = (_f = (_e = visionConfig.camera) === null || _e === void 0 ? void 0 : _e.handEyeCalibrator) === null || _f === void 0 ? void 0 : _f.program;
let TrackingCameraMutation = class TrackingCameraMutation {
    async detectTrackingCameraROIs(name, context) {
        const { domain } = context.state;
        var camera = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, name);
        if (!camera || camera.discriminator !== robotics_types_1.ROBOTICS_OBJECT_TYPES.CAMERA) {
            throw Error(`Tracking Camera '${name}' Not Found`);
        }
        var executable = ROIDetectorProgram[0];
        var params = [...ROIDetectorProgram.slice(1), name];
        var rois = await (0, await_spawn_1.default)(executable, params);
        return JSON.parse(rois.toString());
    }
    async calibrateHandeyeMatrix(name, context) {
        const { domain } = context.state;
        var camera = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, name);
        if (!camera || camera.discriminator !== robotics_types_1.ROBOTICS_OBJECT_TYPES.CAMERA) {
            throw Error(`Tracking Camera '${name}' Not Found`);
        }
        var executable = handEyeCalibratorProgram[0];
        var params = [...handEyeCalibratorProgram.slice(1), name];
        var calibrationMatrix = await (0, await_spawn_1.default)(executable, params);
        env_1.logger.info(`handeye matrix:\n'${calibrationMatrix === null || calibrationMatrix === void 0 ? void 0 : calibrationMatrix.toString()}'`);
        return JSON.parse(calibrationMatrix.toString());
    }
    async calibrateCameraParameter(name, context) {
        const { domain } = context.state;
        var camera = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, name);
        if (!camera || camera.discriminator !== robotics_types_1.ROBOTICS_OBJECT_TYPES.CAMERA) {
            throw Error(`Tracking Camera '${name}' Not Found`);
        }
        let activeApp = data_bridge_manager_1.DataBridgeConnectionManager.getActive();
        if (activeApp === '' || activeApp === name) {
            // TODO: control to run multiple python applications here
            data_bridge_manager_1.DataBridgeConnectionManager.set(name, 'start');
        }
        var executable = cameraCalibratorProgram[0];
        var params = [...cameraCalibratorProgram.slice(1), name];
        var cameraParameter = await (0, await_spawn_1.default)(executable, params);
        env_1.logger.info(`calibration matrix:\n'${cameraParameter === null || cameraParameter === void 0 ? void 0 : cameraParameter.toString()}'`);
        return JSON.parse(cameraParameter.toString());
    }
    async captureCalibrateResult(name, context) {
        const { domain } = context.state;
        var camera = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, name);
        if (!camera || camera.discriminator !== robotics_types_1.ROBOTICS_OBJECT_TYPES.CAMERA) {
            throw Error(`Tracking Camera '${name}' Not Found`);
        }
        var dataBridgeName = 'cameracalib' + ':' + name;
        data_bridge_1.dataBridge.sendCmdToExt('snapshot', dataBridgeName);
        // TODO: check if this kina return value is available
        return true;
    }
    async getCalibrateResult(name, context) {
        const { domain } = context.state;
        var camera = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, name);
        if (!camera || camera.discriminator !== robotics_types_1.ROBOTICS_OBJECT_TYPES.CAMERA) {
            throw Error(`Tracking Camera '${name}' Not Found`);
        }
        var dataBridgeName = 'cameracalib' + ':' + name;
        data_bridge_1.dataBridge.sendCmdToExt('result', dataBridgeName);
        // TODO: check if this kina return value is available
        return true;
    }
    async finishCameraCalibrate(name, context) {
        const { domain } = context.state;
        var camera = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, name);
        if (!camera || camera.discriminator !== robotics_types_1.ROBOTICS_OBJECT_TYPES.CAMERA) {
            throw Error(`Tracking Camera '${name}' Not Found`);
        }
        var dataBridgeName = 'cameracalib' + ':' + name;
        data_bridge_1.dataBridge.sendCmdToExt('exit', dataBridgeName);
        // TODO: check if this kina return value is available
        return true;
    }
    async finishHandeyeCalibrate(name, context) {
        const { domain } = context.state;
        var camera = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, name);
        if (!camera || camera.discriminator !== robotics_types_1.ROBOTICS_OBJECT_TYPES.CAMERA) {
            throw Error(`Tracking Camera '${name}' Not Found`);
        }
        var dataBridgeName = 'handeyecalib' + ':' + name;
        data_bridge_1.dataBridge.sendCmdToExt('exit', dataBridgeName);
        // TODO: check if this kina return value is available
        return true;
    }
    async startHandeyeCalibrate(name, context) {
        const { domain } = context.state;
        var camera = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, name);
        if (!camera || camera.discriminator !== robotics_types_1.ROBOTICS_OBJECT_TYPES.CAMERA) {
            throw Error(`Tracking Camera '${name}' Not Found`);
        }
        var dataBridgeName = 'handeyecalib' + ':' + name;
        data_bridge_1.dataBridge.sendCmdToExt('start', dataBridgeName);
        // TODO: check if this kina return value is available
        return true;
    }
    async getHandeyeResult(name, context) {
        const { domain } = context.state;
        var camera = integration_base_1.ConnectionManager.getConnectionInstanceByName(domain, name);
        if (!camera || camera.discriminator !== robotics_types_1.ROBOTICS_OBJECT_TYPES.CAMERA) {
            throw Error(`Tracking Camera '${name}' Not Found`);
        }
        var dataBridgeName = 'handeyecalib' + ':' + name;
        data_bridge_1.dataBridge.sendCmdToExt('result', dataBridgeName);
        // TODO: check if this kina return value is available
        return true;
    }
};
__decorate([
    (0, type_graphql_1.Mutation)(returns => [robotarm_type_1.ROI], { description: 'To start roi configuration and get the result' }),
    __param(0, (0, type_graphql_1.Arg)('name', type => String, { nullable: false })),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], TrackingCameraMutation.prototype, "detectTrackingCameraROIs", null);
__decorate([
    (0, type_graphql_1.Mutation)(returns => robotarm_type_1.Matrix, { description: 'To start handeye calibration and get the result' }),
    __param(0, (0, type_graphql_1.Arg)('name', type => String, { nullable: false })),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], TrackingCameraMutation.prototype, "calibrateHandeyeMatrix", null);
__decorate([
    (0, type_graphql_1.Mutation)(returns => robotarm_type_1.CameraParameter, { description: 'To start handeye calibration and get the result' }),
    __param(0, (0, type_graphql_1.Arg)('name', type => String, { nullable: false })),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], TrackingCameraMutation.prototype, "calibrateCameraParameter", null);
__decorate([
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To send camera calibration snapshot to the python module' }),
    __param(0, (0, type_graphql_1.Arg)('name', type => String, { nullable: false })),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], TrackingCameraMutation.prototype, "captureCalibrateResult", null);
__decorate([
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To send camera calibration snapshot to the python module' }),
    __param(0, (0, type_graphql_1.Arg)('name', type => String, { nullable: false })),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], TrackingCameraMutation.prototype, "getCalibrateResult", null);
__decorate([
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To finish the current camera calibration with python module' }),
    __param(0, (0, type_graphql_1.Arg)('name', type => String, { nullable: false })),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], TrackingCameraMutation.prototype, "finishCameraCalibrate", null);
__decorate([
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To finish the current handeye calibration with python module' }),
    __param(0, (0, type_graphql_1.Arg)('name', type => String, { nullable: false })),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], TrackingCameraMutation.prototype, "finishHandeyeCalibrate", null);
__decorate([
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To start handeye calibration' }),
    __param(0, (0, type_graphql_1.Arg)('name', type => String, { nullable: false })),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], TrackingCameraMutation.prototype, "startHandeyeCalibrate", null);
__decorate([
    (0, type_graphql_1.Mutation)(returns => Boolean, { description: 'To get the result of the current handeye calibration' }),
    __param(0, (0, type_graphql_1.Arg)('name', type => String, { nullable: false })),
    __param(1, (0, type_graphql_1.Ctx)()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [String, Object]),
    __metadata("design:returntype", Promise)
], TrackingCameraMutation.prototype, "getHandeyeResult", null);
TrackingCameraMutation = __decorate([
    (0, type_graphql_1.Resolver)(tracking_camera_type_1.TrackingCamera)
], TrackingCameraMutation);
exports.TrackingCameraMutation = TrackingCameraMutation;
//# sourceMappingURL=tracking-camera-mutation.js.map