import { getIPAddress } from './utils' ; import { SoundModes } from "./types" ; import { constants } from './constants' ; import { startBalenaService , stopBalenaService , restartBalenaService } from './utils' ; import BalenaAudio from 'balena-audio' ; interface MultiRoomConfig { master : String ; forced : boolean ; } interface DeviceConfig { ip : string ; type : string ; } export default class SoundConfig { public mode : SoundModes = constants . mode ; public device : DeviceConfig = { ip : getIPAddress ( ) ?? 'localhost' , type : constants . balenaDeviceType } ; public multiroom : MultiRoomConfig = { master : constants . multiroom . master ?? this . device . ip , forced : constants . multiroom . forced } ; private audioBlock : BalenaAudio ; bindAudioBlock ( audioBlock ) { this . audioBlock = audioBlock ; } setMultiRoomMaster ( master ) { this . multiroom . master = master ; restartBalenaService ( 'multiroom-client' ) ; } setMode ( mode ) { let oldMode = this . mode ; let modeUpdated = mode !== oldMode ; if ( mode && Object . values ( SoundModes ) . includes ( mode ) ) { this . mode = SoundModes [ mode ] ; if ( modeUpdated ) { switch ( this . mode ) { case SoundModes . MULTI_ROOM : startBalenaService ( 'multiroom-server' ) ; startBalenaService ( 'multiroom-client' ) ; startBalenaService ( 'airplay' ) ; startBalenaService ( 'spotify' ) ; startBalenaService ( 'upnp' ) ; startBalenaService ( 'bluetooth' ) ; this . audioBlock . moveSinkInput ( 0 , 3 ) ; break ; case SoundModes . MULTI_ROOM_CLIENT : stopBalenaService ( 'multiroom-server' ) ; stopBalenaService ( 'airplay' ) ; stopBalenaService ( 'spotify' ) ; stopBalenaService ( 'upnp' ) ; stopBalenaService ( 'bluetooth' ) ; startBalenaService ( 'multiroom-client' ) ; break ; case SoundModes . STANDALONE : stopBalenaService ( 'multiroom-server' ) ; stopBalenaService ( 'multiroom-client' ) ; startBalenaService ( 'airplay' ) ; startBalenaService ( 'spotify' ) ; startBalenaService ( 'upnp' ) ; startBalenaService ( 'bluetooth' ) ; this . audioBlock . moveSinkInput ( 0 , 2 ) ; break ; default : break ; } } } else { console . log ( `Error setting mode, invalid mode: ${ mode }` ) ; } return modeUpdated ; } isMultiRoomEnabled ( ) { let mrModes = [ SoundModes . MULTI_ROOM , SoundModes . MULTI_ROOM_CLIENT ] ; return mrModes . includes ( this . mode ) ; } isMultiRoomServer ( ) { let mrModes = [ SoundModes . MULTI_ROOM ] ; return mrModes . includes ( this . mode ) ; } isMultiRoomMaster ( ) { return this . isMultiRoomServer ( ) && this . device . ip === this . multiroom . master ; } isNewMultiRoomMaster ( master ) { return this . isMultiRoomEnabled ( ) && this . multiroom . master !== master ; } }