Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix global handling of current tally data #718

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/_models/TallyData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export type AddressTallyData = {
}

export type SourceTallyData = {
[sourceId: string]: string[];
[deviceSourceId: string]: string[];
}

export type DeviceTallyData = {
Expand Down
85 changes: 27 additions & 58 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,52 +983,33 @@ function TSLClients_UpdateAll() {

function getDeviceStates(deviceId?: string): DeviceState[] {
let deviceStateObj = devices.filter((d) => deviceId ? d.id == deviceId : true).flatMap((d) => currentConfig.bus_options.map((b) => {
const deviceSources = device_sources.filter((s) => s.deviceId == d.id);
const device = GetDeviceByDeviceId(d.id);
const deviceSourcesForDevice = device_sources.filter((s) => s.deviceId == d.id);

// Check if buss is linked, if linked all sources must be in this bus
const device = GetDeviceByDeviceId(d.id);
if ((device.linkedBusses || []).includes(b.id)) {

// Check if all sources are in the buss, if not return [] for sources.
// Count number of sources in bus
// TODO: Check if this can be replaced with deviceSources.findIndex((s) and refactored to reduce duplicated code.
let num = 0;
for (let i = 0; i < deviceSources.length; i++) {
if (currentSourceTallyData?.[deviceSources[i].sourceId]?.includes(b.type)) {
num++
}
}
if (num === deviceSources.length) {
if (currentDeviceTallyData?.[device.id]?.includes(b.type)) {
return {
busId: b.id,
deviceId: d.id,
sources: deviceSources.filter(
(s) => Object.entries(SourceClients[s.sourceId]?.tally?.value || [])
.filter(([address, busses]) => address == s.address)
.findIndex(([address, busses]: [string, string[]]) => busses.includes(b.type)) !== -1).map((s) => s.id),
}
sources: deviceSourcesForDevice.filter(deviceSource => currentSourceTallyData?.[deviceSource.id]?.includes(b.type)).map((s) => s.id),
}
} else {
return {
busId: b.id,
deviceId: d.id,
sources: [],
}
}
}
} else {
return {
busId: b.id,
deviceId: d.id,
sources: deviceSources.filter(
(s) => Object.entries(SourceClients[s.sourceId]?.tally?.value || [])
.filter(([address, busses]) => address == s.address)
.findIndex(([address, busses]: [string, string[]]) => busses.includes(b.type)) !== -1).map((s) => s.id),
sources: deviceSourcesForDevice.filter(deviceSource => currentSourceTallyData?.[deviceSource.id]?.includes(b.type)).map((s) => s.id),
}
}
}
}));

//console.log('*** device state obj ***')
//console.log(deviceStateObj)

return deviceStateObj;
}

Expand Down Expand Up @@ -1166,39 +1147,27 @@ function UpdateDeviceState(deviceId: string) {

const deviceSources = device_sources.filter((d) => d.deviceId == deviceId);
for (const bus of currentConfig.bus_options) {
let isDeviceActiveOnCurrentBus: boolean = false
if ((device.linkedBusses || []).includes(bus.id)) {
// bus is linked, which means all sources must be in this bus

// Count number of sources in bus
// TODO: This should be replaced with deviceSources.findIndex((s).
let num = 0;
for (let i = 0; i < deviceSources.length; i++) {
if (currentSourceTallyData?.[deviceSources[i].sourceId]?.includes(bus.type)) {
num++
}
}
// bus is linked, which means all configured sources for the current device must have this bus active

if (num === deviceSources.length) {
currentDeviceTallyData[device.id].push(bus.id);
if (!previousBusses.includes(bus.id)) {
RunAction(deviceId, bus.id, true);
}
} else {
if (previousBusses.includes(bus.id)) {
RunAction(deviceId, bus.id, false);
}
}
let deviceSourcesForCurrentDevice = deviceSources.filter(devSrc => devSrc.deviceId = device.id)
let deviceSourcesForCurrentDeviceActiveOnCurrentBus = deviceSourcesForCurrentDevice.filter(devSrc => currentSourceTallyData?.[devSrc.id]?.includes(bus.type))

isDeviceActiveOnCurrentBus = deviceSourcesForCurrentDevice.length === deviceSourcesForCurrentDeviceActiveOnCurrentBus.length
} else {
// bus is unlinked
if (deviceSources.findIndex((s) => currentSourceTallyData?.[s.sourceId]?.includes(bus.type)) !== -1) {
currentDeviceTallyData[device.id].push(bus.id);
if (!previousBusses.includes(bus.id)) {
RunAction(deviceId, bus.id, true);
}
} else {
if (previousBusses.includes(bus.id)) {
RunAction(deviceId, bus.id, false);
}
isDeviceActiveOnCurrentBus = deviceSources.findIndex((s) => s.deviceId == device.id && currentSourceTallyData?.[s.id]?.includes(bus.type)) !== -1
}

if (isDeviceActiveOnCurrentBus) {
currentDeviceTallyData[device.id].push(bus.id);
if (!previousBusses.includes(bus.id)) {
RunAction(deviceId, bus.id, true);
}
} else {
if (previousBusses.includes(bus.id)) {
RunAction(deviceId, bus.id, false);
}
}
}
Expand Down Expand Up @@ -1325,8 +1294,8 @@ function initializeSource(source: Source): TallyInput {
const tallyData: SourceTallyData = {};
for (const [sourceAddress, busses] of Object.entries(tallyDataWithAddresses)) {
let device_source = device_sources.find((s) => s.sourceId == source.id && s.address == sourceAddress);
if(device_source) {
tallyData[device_source.sourceId] = busses;
if (device_source) {
tallyData[device_source.id] = busses;
}
}
SendCloudSourceTallyData(source.id, tallyData);
Expand Down
Loading