Skip to content

Commit

Permalink
patch: allow crelay to specify the serial number.
Browse files Browse the repository at this point in the history
This allows specifying which device to use when crelay detects more than
one on a system. Can happen when a FTDI USB-serial converter is present.

Changelog-Entry: crelay: allow crelay to specify the serial number
Signed-By: Lisandro Pérez Meyer <[email protected]>
  • Loading branch information
perezmeyer committed Apr 17, 2024
1 parent 48251d6 commit 33106e3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/features/power/implementations/crelay/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 26 additions & 8 deletions lib/features/power/implementations/crelay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ 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<void> {
// 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');
await execAsync('cd /usr/app/crelay/src && make && make install');
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)
Expand All @@ -35,18 +37,34 @@ export class Crelay implements Power {
// Power on the DUT
async on(voltage?: number): Promise<void> {
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<void> {
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<string> {
// 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;
}

Expand Down

0 comments on commit 33106e3

Please sign in to comment.