Skip to content

Commit

Permalink
feat/cleanup-global: moved getState subscription to module
Browse files Browse the repository at this point in the history
  • Loading branch information
olzzon committed Dec 28, 2019
1 parent 965c372 commit 77d529a
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 246 deletions.
46 changes: 21 additions & 25 deletions server/utils/CasparCGConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@ export class CasparCGConnection {
oscCommandMap: { [key: string]: CommandChannelMap } = {};

constructor(mixerProtocol: ICasparCGMixerGeometry) {
this.store = store.getState() as IStore;
const unsubscribe = store.subscribe(() => {
this.store = store.getState() as IStore;
});

this.mixerProtocol = mixerProtocol

this.connection = new CasparCG({
autoReconnect: true,
autoReconnectAttempts: 20,
autoReconnectInterval: 3000,
host: this.store.settings[0].deviceIp,
port: parseInt(this.store.settings[0].devicePort + ''),
host: state.settings[0].deviceIp,
port: parseInt(state.settings[0].devicePort + ''),
})
console.log("Trying to connect to CasparCG...")
this.connection.onConnected = () => {
Expand All @@ -63,11 +59,11 @@ export class CasparCGConnection {

setupMixerConnection() {
if (!this.oscClient) {
const remotePort = parseInt(this.store.settings[0].devicePort + '') + 1000
const remotePort = parseInt(state.settings[0].devicePort + '') + 1000
this.oscClient = new osc.UDPPort({
localAddress: this.store.settings[0].localIp,
localAddress: state.settings[0].localIp,
localPort: remotePort,
remoteAddress: this.store.settings[0].deviceIp,
remoteAddress: state.settings[0].deviceIp,
remotePort
})
.on('ready', () => {
Expand All @@ -81,7 +77,7 @@ export class CasparCGConnection {
channel: index,
// CCG returns "produced" audio levels, before the Volume mixer transform
// We therefore want to premultiply this to show useful information about audio levels
level: Math.min(1, message.args[0] * this.store.faders[0].fader[index].faderLevel)
level: Math.min(1, message.args[0] * state.faders[0].fader[index].faderLevel)
});
} else if (this.mixerProtocol.sourceOptions) {
const m = message.address.split('/');
Expand Down Expand Up @@ -125,11 +121,11 @@ export class CasparCGConnection {
});

this.oscClient.open();
console.log("Listening for status on CasparCG: ", this.store.settings[0].deviceIp, remotePort)
console.log("Listening for status on CasparCG: ", state.settings[0].deviceIp, remotePort)
}

// Restore mixer values to the ones we have internally
this.store.faders[0].fader.forEach((channel, index) => {
state.faders[0].fader.forEach((channel, index) => {
this.updateFadeIOLevel(index, channel.faderLevel);
this.updatePflState(index);
})
Expand Down Expand Up @@ -232,10 +228,10 @@ export class CasparCGConnection {
}

updateChannelSetting(channelIndex: number, setting: string, value: string) {
if (this.mixerProtocol.sourceOptions && this.store.channels[0].channel[channelIndex].private) {
if (this.mixerProtocol.sourceOptions && state.channels[0].channel[channelIndex].private) {
const pair = this.mixerProtocol.sourceOptions.sources[channelIndex];
const producer = this.store.channels[0].channel[channelIndex].private!['producer'];
let filePath = String(this.store.channels[0].channel[channelIndex].private!['file_path']);
const producer = state.channels[0].channel[channelIndex].private!['producer'];
let filePath = String(state.channels[0].channel[channelIndex].private!['file_path']);
filePath = filePath.replace(/\.[\w\d]+$/, '')
this.controlChannelSetting(pair.channel, pair.layer, producer, filePath, setting, value);
}
Expand All @@ -246,13 +242,13 @@ export class CasparCGConnection {
return
}

if (this.store.faders[0].fader[channelIndex].pflOn === true) {
if (state.faders[0].fader[channelIndex].pflOn === true) {
// Enable SOLO on this channel on MONITOR
const pairs = this.mixerProtocol.toMixer.MONITOR_CHANNEL_FADER_LEVEL[channelIndex];
this.setAllLayers(pairs, this.store.faders[0].fader[channelIndex].faderLevel);
this.setAllLayers(pairs, state.faders[0].fader[channelIndex].faderLevel);

// Ensure that all other non-SOLO channels are muted on MONITOR
const others = this.store.faders[0].fader
const others = state.faders[0].fader
.map((i, index) => i.pflOn ? undefined : index)
.filter(i => (
i !== undefined &&
Expand All @@ -264,7 +260,7 @@ export class CasparCGConnection {
})
} else {
// check if any other channels are SOLO
const others = this.store.faders[0].fader
const others = state.faders[0].fader
.map((i, index) => i.pflOn ? index : undefined)
.filter(i => (
i !== undefined &&
Expand All @@ -277,13 +273,13 @@ export class CasparCGConnection {
this.setAllLayers(pairs, this.mixerProtocol.fader.min);
} else {
// There are no other SOLO channels, restore MONITOR to match PGM
this.store.channels[0].channel.forEach((i, index) => {
state.channels[0].channel.forEach((i, index) => {
if (index > this.mixerProtocol.toMixer.MONITOR_CHANNEL_FADER_LEVEL.length - 1) {
return
}

const pairs = this.mixerProtocol.toMixer.MONITOR_CHANNEL_FADER_LEVEL[index];
this.setAllLayers(pairs, this.store.channels[0].channel[index].outputLevel);
this.setAllLayers(pairs, state.channels[0].channel[index].outputLevel);
})
}
}
Expand Down Expand Up @@ -324,12 +320,12 @@ export class CasparCGConnection {
const pgmPairs = this.mixerProtocol.toMixer.PGM_CHANNEL_FADER_LEVEL[channelIndex]
this.setAllLayers(pgmPairs, outputLevel)

const anyPflOn = this.store.faders[0].fader.reduce((memo, i) => memo || i.pflOn, false)
const anyPflOn = state.faders[0].fader.reduce((memo, i) => memo || i.pflOn, false)
// Check if there are no SOLO channels on MONITOR or there are, but this channel is SOLO
if (!anyPflOn || (anyPflOn && this.store.faders[0].fader[channelIndex].pflOn)) {
if (!anyPflOn || (anyPflOn && state.faders[0].fader[channelIndex].pflOn)) {
const pairs = this.mixerProtocol.toMixer.MONITOR_CHANNEL_FADER_LEVEL[channelIndex];
if (this.store.faders[0].fader[channelIndex].pflOn) {
this.setAllLayers(pairs, this.store.faders[0].fader[channelIndex].faderLevel);
if (state.faders[0].fader[channelIndex].pflOn) {
this.setAllLayers(pairs, state.faders[0].fader[channelIndex].faderLevel);
} else {
this.setAllLayers(pairs, outputLevel);
}
Expand Down
39 changes: 17 additions & 22 deletions server/utils/EmberMixerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,12 @@ export class EmberMixerConnection {
this.sendOutMessage = this.sendOutMessage.bind(this);
this.pingMixerCommand = this.pingMixerCommand.bind(this);

this.store = store.getState();
const unsubscribe = store.subscribe(() => {
this.store = store.getState();
});

this.emberNodeObject = new Array(200);
this.mixerProtocol = mixerProtocol;

this.emberConnection = new DeviceTree(
this.store.settings[0].deviceIp,
this.store.settings[0].devicePort
state.settings[0].deviceIp,
state.settings[0].devicePort
);
let deviceRoot: any;
this.emberConnection.connect()
Expand All @@ -58,15 +53,15 @@ export class EmberMixerConnection {
console.log("Ember Connected");

let ch: number = 1;
this.store.settings[0].numberOfChannelsInType.forEach((numberOfChannels, typeIndex) => {
state.settings[0].numberOfChannelsInType.forEach((numberOfChannels, typeIndex) => {
for (let channelTypeIndex=0; channelTypeIndex < numberOfChannels ; channelTypeIndex++) {
this.subscribeFaderLevel(ch, typeIndex, channelTypeIndex);
ch++;
}
})

ch = 1;
this.store.settings[0].numberOfChannelsInType.forEach((numberOfChannels, typeIndex) => {
state.settings[0].numberOfChannelsInType.forEach((numberOfChannels, typeIndex) => {
for (let channelTypeIndex=0; channelTypeIndex < numberOfChannels ; channelTypeIndex++) {
this.subscribeChannelName(ch, typeIndex, channelTypeIndex);
ch++;
Expand Down Expand Up @@ -103,8 +98,8 @@ export class EmberMixerConnection {
.then((node: any) => {
this.emberNodeObject[ch-1] = node;
this.emberConnection.subscribe(node, (() => {
if (!this.store.channels[0].channel[ch-1].fadeActive
&& !this.store.channels[0].channel[ch - 1].fadeActive
if (!state.channels[0].channel[ch-1].fadeActive
&& !state.channels[0].channel[ch - 1].fadeActive
&& node.contents.value > this.mixerProtocol.channelTypes[typeIndex].fromMixer.CHANNEL_OUT_GAIN[0].min) {
store.dispatch({
type: SET_FADER_LEVEL,
Expand Down Expand Up @@ -178,21 +173,21 @@ export class EmberMixerConnection {
}

updateOutLevel(channelIndex: number) {
let channelType = this.store.channels[0].channel[channelIndex].channelType;
let channelTypeIndex = this.store.channels[0].channel[channelIndex].channelTypeIndex;
let channelType = state.channels[0].channel[channelIndex].channelType;
let channelTypeIndex = state.channels[0].channel[channelIndex].channelTypeIndex;
this.sendOutMessage(
this.emberNodeObject[channelIndex],
channelTypeIndex+1,
this.store.channels[0].channel[channelIndex].outputLevel,
state.channels[0].channel[channelIndex].outputLevel,
"f"
);
}

updatePflState(channelIndex: number) {
let channelType = this.store.channels[0].channel[channelIndex].channelType;
let channelTypeIndex = this.store.channels[0].channel[channelIndex].channelTypeIndex;
let channelType = state.channels[0].channel[channelIndex].channelType;
let channelTypeIndex = state.channels[0].channel[channelIndex].channelTypeIndex;

if (this.store.faders[0].fader[channelIndex].pflOn === true) {
if (state.faders[0].fader[channelIndex].pflOn === true) {
this.sendOutMessage(
this.mixerProtocol.channelTypes[channelType].toMixer.PFL_ON[0].mixerMessage,
channelTypeIndex+1,
Expand Down Expand Up @@ -239,8 +234,8 @@ export class EmberMixerConnection {
}

updateFadeIOLevel(channelIndex: number, outputLevel: number) {
let channelType = this.store.channels[0].channel[channelIndex].channelType;
let channelTypeIndex = this.store.channels[0].channel[channelIndex].channelTypeIndex;
let channelType = state.channels[0].channel[channelIndex].channelType;
let channelTypeIndex = state.channels[0].channel[channelIndex].channelTypeIndex;
this.sendOutMessage(
this.emberNodeObject[channelIndex],
channelTypeIndex+1,
Expand All @@ -250,9 +245,9 @@ export class EmberMixerConnection {
}

updateChannelName(channelIndex: number) {
let channelType = this.store.channels[0].channel[channelIndex].channelType;
let channelTypeIndex = this.store.channels[0].channel[channelIndex].channelTypeIndex;
let channelName = this.store.faders[0].fader[channelIndex].label;
let channelType = state.channels[0].channel[channelIndex].channelType;
let channelTypeIndex = state.channels[0].channel[channelIndex].channelTypeIndex;
let channelName = state.faders[0].fader[channelIndex].label;
this.sendOutMessage(
this.mixerProtocol.channelTypes[channelType].toMixer.CHANNEL_NAME[0].mixerMessage,
channelTypeIndex+1,
Expand Down
21 changes: 8 additions & 13 deletions server/utils/HuiMidiRemoteConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,10 @@ export class HuiMidiRemoteConnection {
this.convertToRemoteLevel = this.convertToRemoteLevel.bind(this);
this.updateRemoteFaderState = this.updateRemoteFaderState.bind(this);

this.store = store.getState();
const unsubscribe = store.subscribe(() => {
this.store = store.getState();
});

this.remoteProtocol = RemoteFaderPresets.hui;
this.mixerProtocol = MixerProtocolPresets[this.store.settings[0].mixerProtocol] || MixerProtocolPresets.genericMidi;
this.mixerProtocol = MixerProtocolPresets[state.settings[0].mixerProtocol] || MixerProtocolPresets.genericMidi;

if (!this.store.settings[0].enableRemoteFader) {
if (!state.settings[0].enableRemoteFader) {
return
}

Expand All @@ -46,13 +41,13 @@ export class HuiMidiRemoteConnection {
console.log("Remote MidiController connection could not be enabled.", err);
}

this.midiInput = WebMidi.getInputByName(this.store.settings[0].remoteFaderMidiInputPort);
this.midiOutput = WebMidi.getOutputByName(this.store.settings[0].remoteFaderMidiOutputPort);
this.midiInput = WebMidi.getInputByName(state.settings[0].remoteFaderMidiInputPort);
this.midiOutput = WebMidi.getOutputByName(state.settings[0].remoteFaderMidiOutputPort);

if (this.midiInput && this.midiOutput ) {
console.log("Remote Midi Controller connected on port")
console.log("Midi input :", this.store.settings[0].remoteFaderMidiInputPort)
console.log("Midi output :", this.store.settings[0].remoteFaderMidiOutputPort)
console.log("Midi input :", state.settings[0].remoteFaderMidiInputPort)
console.log("Midi output :", state.settings[0].remoteFaderMidiOutputPort)

this.setupRemoteFaderConnection();
} else {
Expand Down Expand Up @@ -161,7 +156,7 @@ export class HuiMidiRemoteConnection {
);
this.midiOutput.sendControlChange(
44,
1 + (64*this.store.faders[0].fader[channelIndex].pgmOn),
1 + (64*state.faders[0].fader[channelIndex].pgmOn),
1
);

Expand All @@ -173,7 +168,7 @@ export class HuiMidiRemoteConnection {
);
this.midiOutput.sendControlChange(
44,
3 + (64*this.store.faders[0].fader[channelIndex].pflOn),
3 + (64*(state.faders[0].fader[channelIndex].pflOn ? 1 : 0)),
1
);
}
Expand Down
Loading

0 comments on commit 77d529a

Please sign in to comment.