From 4e25298fb87b68641b77a12e921773cfbb18ea16 Mon Sep 17 00:00:00 2001 From: Satoru Takagi Date: Fri, 3 Sep 2021 15:46:55 +0900 Subject: [PATCH] Fix issue #30 (#31) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit EACCES: permission deniedエラーが出る問題の解決 Fix #30 --- index.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/index.ts b/index.ts index f3b2755..5ab3818 100644 --- a/index.ts +++ b/index.ts @@ -81,6 +81,7 @@ export class GPIOPort extends EventEmitter { private readonly _pollingInterval: number; private _direction: DirectionMode | OperationError; private _exported: boolean | OperationError; + private _exportRetry: number; private _value: GPIOValue | undefined; private _timeout: ReturnType | undefined; onchange: GPIOChangeEventHandler | undefined; @@ -92,6 +93,7 @@ export class GPIOPort extends EventEmitter { this._pollingInterval = PollingInterval; this._direction = new OperationError("Unknown direction."); this._exported = new OperationError("Unknown export."); + this._exportRetry = 0; this.on("change", (event: GPIOChangeEvent): void => { if (this.onchange !== undefined) this.onchange(event); @@ -153,7 +155,14 @@ export class GPIOPort extends EventEmitter { ); } } catch (error) { - throw new OperationError(error); + if ( this._exportRetry == 0 ){ + await sleep(100); + console.warn("May be the first time port access. Retry.."); + ++ this._exportRetry; + await this.export(direction); + } else { + throw new OperationError(error); + } } this._direction = direction; @@ -244,3 +253,9 @@ export async function requestGPIOAccess(): Promise { return new GPIOAccess(ports); } + +function sleep(ms: number) { + return new Promise((resolve) => { + return setTimeout(resolve, ms); + }); +} \ No newline at end of file