diff --git a/lib/features/power/implementations/crelay/README.md b/lib/features/power/implementations/crelay/README.md index be59e0e..c83aeac 100644 --- a/lib/features/power/implementations/crelay/README.md +++ b/lib/features/power/implementations/crelay/README.md @@ -5,10 +5,11 @@ This implementation is to be used when a relay is being used that is controlled ## Dependencies -- alpine distribution +- alpine distribution - git ## Configuration +- `CRELAY_SERIAL`: the relay serial number, in case crelay detects more than one available device - `CRELAY_POWER_CHANNEL`: the relay channel connected to the DUT power - `USB_RELAY_CONN`: `NC` or `NO` - for normally open or normally closed relay configuration \ No newline at end of file diff --git a/lib/features/power/implementations/crelay/index.ts b/lib/features/power/implementations/crelay/index.ts index e3606de..9fa61d3 100644 --- a/lib/features/power/implementations/crelay/index.ts +++ b/lib/features/power/implementations/crelay/index.ts @@ -4,18 +4,20 @@ import { promisify } from 'util'; const execAsync = promisify(exec); export class Crelay implements Power { - private relayId: number + private relaySerial + private relayChannel: number private connOn: string private connOff: string constructor(){ - this.relayId = Number(process.env.CRELAY_POWER_CHANNEL) || 1; // indexing of relay channels starts at 1 for this device. + this.relaySerial = process.env.CRELAY_SERIAL || ''; + this.relayChannel = Number(process.env.CRELAY_POWER_CHANNEL) || 1; // indexing of relay channels starts at 1 for this device. this.connOn = (process.env.USB_RELAY_CONN === 'NC') ? 'OFF': 'ON' // if the user specifies they have set up the connection to be NC this.connOff = (process.env.USB_RELAY_CONN === 'NC') ? 'ON': 'OFF' // if the user specifies they have set up the connection to be NC } async setup(): Promise { - // install craly tooling + // install crelay tooling try { await execAsync('apk add make build-base'); await execAsync('cd /usr/app/ && git clone https://github.com/balena-io-hardware/crelay.git'); @@ -23,8 +25,8 @@ export class Crelay implements Power { await execAsync('cp /usr/app/crelay/src/crelay /usr/local/bin/crelay'); // this retrieves info about the relay being used, and its serial - let info = await execAsync(`crelay -i ${this.relayId}`); - console.log(`Crelay controlled relay being used for power, channel: ${this.relayId}.`); + let info = await execAsync(`crelay -i`); + console.log(`Crelay controlled relay being used for power, serial: ${this.relaySerial}, channel: ${this.relayChannel}.`); console.log(info.stdout); } catch(e){ console.log(e) @@ -35,18 +37,34 @@ export class Crelay implements Power { // Power on the DUT async on(voltage?: number): Promise { console.log(`Powering on DUT...`); - await execAsync(`crelay ${this.relayId} ${this.connOn}`); + if(this.relaySerial) { + await execAsync(`crelay -s ${this.relaySerial} ${this.relayChannel} ${this.connOn}`); + } else { + await execAsync(`crelay ${this.relayChannel} ${this.connOn}`); + } + } // Power off the DUT async off(): Promise { console.log(`Powering off DUT...`); - await execAsync(`crelay ${this.relayId} ${this.connOff}`); + if(this.relaySerial) { + await execAsync(`crelay -s ${this.relaySerial} ${this.relayChannel} ${this.connOff}`); + } else { + await execAsync(`crelay ${this.relayChannel} ${this.connOff}`); + } + } async getState(): Promise { // TODO return state of power on/off - let state = await execAsync(`crelay ${this.relayId}`); + if(this.relaySerial) { + let state = await execAsync(`crelay -s ${this.relaySerial} ${this.relayChannel}`); + return state.stdout; + } + + // No serial. + let state = await execAsync(`crelay ${this.relayChannel}`); return state.stdout; }