Fix issue #30 (#31)

EACCES: permission deniedエラーが出る問題の解決
Fix #30
This commit is contained in:
Satoru Takagi 2021-09-03 15:46:55 +09:00 committed by GitHub
parent 137f9363d5
commit 4e25298fb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -81,6 +81,7 @@ export class GPIOPort extends EventEmitter {
private readonly _pollingInterval: number; private readonly _pollingInterval: number;
private _direction: DirectionMode | OperationError; private _direction: DirectionMode | OperationError;
private _exported: boolean | OperationError; private _exported: boolean | OperationError;
private _exportRetry: number;
private _value: GPIOValue | undefined; private _value: GPIOValue | undefined;
private _timeout: ReturnType<typeof setInterval> | undefined; private _timeout: ReturnType<typeof setInterval> | undefined;
onchange: GPIOChangeEventHandler | undefined; onchange: GPIOChangeEventHandler | undefined;
@ -92,6 +93,7 @@ export class GPIOPort extends EventEmitter {
this._pollingInterval = PollingInterval; this._pollingInterval = PollingInterval;
this._direction = new OperationError("Unknown direction."); this._direction = new OperationError("Unknown direction.");
this._exported = new OperationError("Unknown export."); this._exported = new OperationError("Unknown export.");
this._exportRetry = 0;
this.on("change", (event: GPIOChangeEvent): void => { this.on("change", (event: GPIOChangeEvent): void => {
if (this.onchange !== undefined) this.onchange(event); if (this.onchange !== undefined) this.onchange(event);
@ -153,7 +155,14 @@ export class GPIOPort extends EventEmitter {
); );
} }
} catch (error) { } 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; this._direction = direction;
@ -244,3 +253,9 @@ export async function requestGPIOAccess(): Promise<GPIOAccess> {
return new GPIOAccess(ports); return new GPIOAccess(ports);
} }
function sleep(ms: number) {
return new Promise((resolve) => {
return setTimeout(resolve, ms);
});
}