import * as os from 'os' ; import axios , { AxiosResponse } from 'axios' ; export function getIPAddresses ( ) { let networkInterfaces = os . networkInterfaces ( ) ; let addresses = Object . keys ( networkInterfaces ) . filter ( i => i . includes ( 'wlan' ) || i . includes ( 'eth' ) ) . map ( i => networkInterfaces [ i ] ) . flat ( ) . filter ( i => ! i . internal && i . family === 'IPv4' ) . map ( i => i . address ) ; return addresses ; } export function getIPAddress ( ) { return getIPAddresses ( ) [ 0 ] ?? null ; } export function restartBalenaService ( service ) { return executeBalenaServiceAction ( service , 'restart' ) ; } export function startBalenaService ( service ) { return executeBalenaServiceAction ( service , 'start' ) ; } export function stopBalenaService ( service ) { return executeBalenaServiceAction ( service , 'stop' ) ; } function executeBalenaServiceAction ( service , action ) { try { let url = `${ process . env . BALENA_SUPERVISOR_ADDRESS }/v2/applications/${ process . env . BALENA_APP_ID }/${ action }-service?apikey=${ process . env . BALENA_SUPERVISOR_API_KEY }` ; return axios . post ( url , { serviceName : service } ) ; } catch ( error ) { console . log ( error . message ) ; return Promise . reject ( error . message ) ; } } export function rebootDevice ( ) { return axios . post ( `${ process . env . BALENA_SUPERVISOR_ADDRESS }/v1/reboot?apikey=${ process . env . BALENA_SUPERVISOR_API_KEY }` ) ; } export function shutdownDevice ( ) { return axios . post ( `${ process . env . BALENA_SUPERVISOR_ADDRESS }/v1/shutdown?apikey=${ process . env . BALENA_SUPERVISOR_API_KEY }` ) ; } export function restartDevice ( ) { return axios . post ( `${ process . env . BALENA_SUPERVISOR_ADDRESS }/v1/restart?apikey=${ process . env . BALENA_SUPERVISOR_API_KEY }` , { appId : process . env . BALENA_APP_ID } ) ; }