diff --git a/lib/features/digitalRelay/implementations/piGpio/index.ts b/lib/features/digitalRelay/implementations/piGpio/index.ts new file mode 100644 index 0000000..dc72da9 --- /dev/null +++ b/lib/features/digitalRelay/implementations/piGpio/index.ts @@ -0,0 +1,46 @@ +import { exec } from 'child_process'; +import { promisify } from 'util'; + +const execAsync = promisify(exec); + +export class PiGpio implements DigitalRelay { + private pin: string + + constructor(){ + this.pin = process.env.GPIO_PIN || '26' + } + + async setup(): Promise { + try{ + console.log(`Setting up GPIO pin ${this.pin}...`) + await execAsync(`echo ${this.pin} > /sys/class/gpio/export || true`); + await execAsync(`echo out > /sys/class/gpio/gpio${this.pin}/direction`); + } catch(e){ + console.log(`Error during setup..`) + console.log(e) + } + } + + // Power on the DUT + async on(): Promise { + console.log(`Toggling gpio pin ${this.pin} on`) + await execAsync(`echo 1 > /sys/class/gpio/gpio${this.pin}/value`); + } + + // Power off the DUT + async off(): Promise { + console.log(`Toggling gpio pin ${this.pin} off`) + await execAsync(`echo 0 > /sys/class/gpio/gpio${this.pin}/value`); + } + + async getState(): Promise { + // TODO return state of power on/off + let res = await execAsync(`cat /sys/class/gpio/gpio${this.pin}/value`); + return res.stdout + } + + async teardown(): Promise { + await this.off(); + } + +} diff --git a/lib/features/digitalRelay/index.ts b/lib/features/digitalRelay/index.ts index a9ca201..943fa10 100644 --- a/lib/features/digitalRelay/index.ts +++ b/lib/features/digitalRelay/index.ts @@ -1,8 +1,10 @@ import { DigitalUSBRelay } from "./implementations/usb-relay"; import { DummyDigitalRelay } from "./implementations/dummy-digital-relay"; +import { PiGpio } from "./implementations/piGpio" const digitalRelayImplementations: {[key: string]: Type } = { usbRelay: DigitalUSBRelay, + gpio: PiGpio, dummyPower: DummyDigitalRelay, };