プロセスを整理

This commit is contained in:
Nebel 2019-10-15 20:56:02 +09:00
parent d79f14234a
commit 2e586e9be5
2 changed files with 12 additions and 20 deletions

View file

@ -44,7 +44,7 @@ exports.GPIOPortMap = GPIOPortMap;
class GPIOPort extends events_1.EventEmitter { class GPIOPort extends events_1.EventEmitter {
constructor(portNumber) { constructor(portNumber) {
super(); super();
this._portNumber = portNumber; this._portNumber = parseUint16(portNumber.toString());
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.");
@ -80,8 +80,8 @@ class GPIOPort extends events_1.EventEmitter {
} }
try { try {
clearInterval(this._timeout); clearInterval(this._timeout);
await fs_1.promises.writeFile(`/sys/class/gpio/export`, parseUint16(this.portNumber.toString()).toString()); await fs_1.promises.writeFile(`/sys/class/gpio/export`, String(this.portNumber));
await fs_1.promises.writeFile(`/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/direction`, direction); await fs_1.promises.writeFile(`/sys/class/gpio/gpio${this.portNumber}/direction`, direction);
if (direction === "in") { if (direction === "in") {
this._timeout = setInterval(this.read.bind(this), this._pollingInterval); this._timeout = setInterval(this.read.bind(this), this._pollingInterval);
} }
@ -95,7 +95,7 @@ class GPIOPort extends events_1.EventEmitter {
async unexport() { async unexport() {
clearInterval(this._timeout); clearInterval(this._timeout);
try { try {
await fs_1.promises.writeFile(`/sys/class/gpio/unexport`, parseUint16(this.portNumber.toString()).toString()); await fs_1.promises.writeFile(`/sys/class/gpio/unexport`, String(this.portNumber));
} }
catch (error) { catch (error) {
throw new OperationError(error); throw new OperationError(error);
@ -104,7 +104,7 @@ class GPIOPort extends events_1.EventEmitter {
} }
async read() { async read() {
try { try {
const buffer = await fs_1.promises.readFile(`/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/value`); const buffer = await fs_1.promises.readFile(`/sys/class/gpio/gpio${this.portNumber}/value`);
const value = parseUint16(buffer.toString()); const value = parseUint16(buffer.toString());
if (this._value !== value) { if (this._value !== value) {
this._value = value; this._value = value;
@ -118,7 +118,7 @@ class GPIOPort extends events_1.EventEmitter {
} }
async write(value) { async write(value) {
try { try {
await fs_1.promises.writeFile(`/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/value`, parseUint16(value.toString()).toString()); await fs_1.promises.writeFile(`/sys/class/gpio/gpio${this.portNumber}/value`, parseUint16(value.toString()).toString());
} }
catch (error) { catch (error) {
throw new OperationError(error); throw new OperationError(error);

View file

@ -88,7 +88,7 @@ export class GPIOPort extends EventEmitter {
constructor(portNumber: PortNumber) { constructor(portNumber: PortNumber) {
super(); super();
this._portNumber = portNumber; this._portNumber = parseUint16(portNumber.toString());
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.");
@ -129,14 +129,9 @@ export class GPIOPort extends EventEmitter {
try { try {
clearInterval(this._timeout as any); clearInterval(this._timeout as any);
await fs.writeFile(`/sys/class/gpio/export`, String(this.portNumber));
await fs.writeFile( await fs.writeFile(
`/sys/class/gpio/export`, `/sys/class/gpio/gpio${this.portNumber}/direction`,
parseUint16(this.portNumber.toString()).toString()
);
await fs.writeFile(
`/sys/class/gpio/gpio${parseUint16(
this.portNumber.toString()
)}/direction`,
direction direction
); );
if (direction === "in") { if (direction === "in") {
@ -157,10 +152,7 @@ export class GPIOPort extends EventEmitter {
clearInterval(this._timeout as any); clearInterval(this._timeout as any);
try { try {
await fs.writeFile( await fs.writeFile(`/sys/class/gpio/unexport`, String(this.portNumber));
`/sys/class/gpio/unexport`,
parseUint16(this.portNumber.toString()).toString()
);
} catch (error) { } catch (error) {
throw new OperationError(error); throw new OperationError(error);
} }
@ -171,7 +163,7 @@ export class GPIOPort extends EventEmitter {
async read() { async read() {
try { try {
const buffer = await fs.readFile( const buffer = await fs.readFile(
`/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/value` `/sys/class/gpio/gpio${this.portNumber}/value`
); );
const value = parseUint16(buffer.toString()) as GPIOValue; const value = parseUint16(buffer.toString()) as GPIOValue;
@ -190,7 +182,7 @@ export class GPIOPort extends EventEmitter {
async write(value: GPIOValue) { async write(value: GPIOValue) {
try { try {
await fs.writeFile( await fs.writeFile(
`/sys/class/gpio/gpio${parseUint16(this.portNumber.toString())}/value`, `/sys/class/gpio/gpio${this.portNumber}/value`,
parseUint16(value.toString()).toString() parseUint16(value.toString()).toString()
); );
} catch (error) { } catch (error) {