diff --git a/src/components/Camera.ts b/src/components/Camera.ts index 06b6da0..19b1424 100644 --- a/src/components/Camera.ts +++ b/src/components/Camera.ts @@ -1,5 +1,4 @@ -import {GPCameraCaptureType, GPCodes} from "../driver"; -import {PointerCamera, RefCamera, StructCameraText} from "../driver/modules"; +import {GPCameraCaptureType, GPCodes, PointerCamera, RefCamera, StructCameraText} from "../driver"; import {ILiveviewOptions} from "../interfaces"; import {CameraAbilities} from "./CameraAbilities"; import {CameraFile} from "./CameraFile"; @@ -13,12 +12,13 @@ import {PortInfo} from "./PortInfo"; export class Camera extends PointerWrapper { private initialized: boolean = false; private closed: boolean = false; - private _widgets: CameraWidgets; constructor() { super({method: "gp_camera", refType: RefCamera}); } + private _widgets: CameraWidgets; + get widgets(): CameraWidgets { this.checkNotClosed(); @@ -87,24 +87,6 @@ export class Camera extends PointerWrapper { return this; } - /** - * - */ - private checkNotClosed() { - if (this.closed) { - throw new Error("Invalid state: closed"); - } - } - - /** - * - */ - private checkIsInitialized() { - if (!this.initialized) { - throw new Error("Invalid state: not initialized"); - } - } - public liveview(options: ILiveviewOptions): Liveview { this.checkNotClosed(); this.checkIsInitialized(); @@ -218,62 +200,6 @@ export class Camera extends PointerWrapper { return this.captureAsync(GPCameraCaptureType.GP_CAPTURE_SOUND); } - /** - * - * @param {GPCameraCaptureType} type - * @param path - * @returns {CameraFile | undefined} - */ - protected capture(type: GPCameraCaptureType, path?: string): CameraFile | undefined { - this.checkNotClosed(); - const cFilePath = new CameraFilePath(); - - this.call("capture", type, cFilePath.buffer, Context.get().pointer); - - const cFile = cFilePath.newFile(this.pointer); - - if (path && cFile) { - try { - cFile.save(path); - } finally { - this.deinitialize(); - this.initialize(); - cFile.closeQuietly(); - cFilePath.close(); - } - } - - return cFile; - } - - /** - * - * @param {GPCameraOperation} type - * @param path - * @returns {CameraFile | undefined} - */ - protected async captureAsync(type: GPCameraCaptureType, path?: string): Promise { - this.checkNotClosed(); - const cFilePath = new CameraFilePath(); - - await this.callAsync("capture", type, cFilePath.buffer, Context.get().pointer); - - const cFile = await cFilePath.newFileAsync(this.pointer); - - if (path && cFile) { - try { - await cFile.saveAsync(path); - } finally { - this.deinitialize(); - this.initialize(); - cFile.closeQuietly(); - cFilePath.close(); - } - } - - return cFile; - } - /** * */ @@ -383,4 +309,78 @@ export class Camera extends PointerWrapper { toString(): string { return "Camera: " + this.getAbilities().model; } + + /** + * + * @param {GPCameraCaptureType} type + * @param path + * @returns {CameraFile | undefined} + */ + protected capture(type: GPCameraCaptureType, path?: string): CameraFile | undefined { + this.checkNotClosed(); + const cFilePath = new CameraFilePath(); + + this.call("capture", type, cFilePath.buffer, Context.get().pointer); + + const cFile = cFilePath.newFile(this.pointer); + + if (path && cFile) { + try { + cFile.save(path); + } finally { + this.deinitialize(); + this.initialize(); + cFile.closeQuietly(); + cFilePath.close(); + } + } + + return cFile; + } + + /** + * + * @param {GPCameraOperation} type + * @param path + * @returns {CameraFile | undefined} + */ + protected async captureAsync(type: GPCameraCaptureType, path?: string): Promise { + this.checkNotClosed(); + const cFilePath = new CameraFilePath(); + + await this.callAsync("capture", type, cFilePath.buffer, Context.get().pointer); + + const cFile = await cFilePath.newFileAsync(this.pointer); + + if (path && cFile) { + try { + await cFile.saveAsync(path); + } finally { + this.deinitialize(); + this.initialize(); + cFile.closeQuietly(); + cFilePath.close(); + } + } + + return cFile; + } + + /** + * + */ + private checkNotClosed() { + if (this.closed) { + throw new Error("Invalid state: closed"); + } + } + + /** + * + */ + private checkIsInitialized() { + if (!this.initialized) { + throw new Error("Invalid state: not initialized"); + } + } } diff --git a/src/driver/GPhoto2Driver.ts b/src/driver/GPhoto2Driver.ts index d979a49..d2a1c6b 100644 --- a/src/driver/GPhoto2Driver.ts +++ b/src/driver/GPhoto2Driver.ts @@ -18,6 +18,7 @@ import { } from "./modules"; const ffi = require("ffi-napi"); +const util = require("util"); /** * @@ -32,7 +33,7 @@ export interface GPhoto2Driver IGPPortInfoModule {} // tslint:disable-next-line -export const GPhoto2Driver: GPhoto2Driver = ffi.Library("libgphoto2", { +export const GPhoto2Driver: GPhoto2Driver & {[key: string]: any} = ffi.Library("libgphoto2", { // CONTEXT ...GPContextModuleDescription, @@ -55,13 +56,6 @@ export const GPhoto2Driver: GPhoto2Driver = ffi.Library("libgphoto2", { ...GPWidgetModuleDescription }); -[].concat(GP_CAMERA_MODULE_ASYNC_KEYS as any, GP_FILE_MODULE_ASYNC_KEYS as any).forEach(key => { - // console.debug("[GPDRIVER] Bind async method", key); - (GPhoto2Driver as any)[key + "_async"] = function async(...args: any[]) { - // console.debug("[GPDRIVER] Call async method", key, args.length); - - return new Promise(resolve => { - (GPhoto2Driver as any)[key].async(...args, resolve); - }); - }; +[...GP_CAMERA_MODULE_ASYNC_KEYS, ...GP_FILE_MODULE_ASYNC_KEYS].forEach(key => { + GPhoto2Driver[`${key}_async`] = util.promisify(GPhoto2Driver[key].async); }); diff --git a/test/mocha.opts b/test/mocha.opts index 1e2ed56..31c5dea 100644 --- a/test/mocha.opts +++ b/test/mocha.opts @@ -1,5 +1,4 @@ --require ts-node/register/transpile-only ---require tsconfig-paths/register --require scripts/mocha/register --recursive --reporter dot