Skip to content

Commit

Permalink
fix: device type and ensure settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Zainrax committed Feb 19, 2025
1 parent 041adcf commit c99c3e3
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 53 deletions.
6 changes: 4 additions & 2 deletions api/api/V1/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,6 @@ export default function (app: Application, baseUrl: string) {
const atTime =
(request.query["at-time"] as unknown as Date) ?? new Date();
const device = response.locals.device as Device;
debugger;
let deviceSettings: DeviceHistory | null = null;
if (request.query["latest-synced"]) {
deviceSettings = await models.DeviceHistory.latest(
Expand All @@ -1634,7 +1633,10 @@ export default function (app: Application, baseUrl: string) {
deviceSettings = await models.DeviceHistory.latest(
device.id,
device.GroupId,
atTime
atTime,
{
"settings.synced": { [Op.ne]: null },
}
);
}
if (deviceSettings) {
Expand Down
38 changes: 8 additions & 30 deletions browse-next/src/api/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import type {
DeviceEvent,
IsoFormattedString,
} from "@typedefs/api/event";
import type { DeviceEventType } from "@typedefs/api/consts";
import type {
DeviceEventType,
DeviceType,
DeviceTypeUnion,
} from "@typedefs/api/consts";
import type { ApiStationResponse as ApiLocationResponse } from "@typedefs/api/station";
import type { ApiRecordingResponse } from "@typedefs/api/recording";
import type { ApiTrackResponse } from "@typedefs/api/track";
Expand Down Expand Up @@ -552,11 +556,9 @@ export const getMaskRegionsForDevice = (

export const getSettingsForDevice = (
deviceId: DeviceId,
atTime?: Date,
lastSynced = false
) => {
const params = new URLSearchParams();
params.append("at-time", (atTime || new Date()).toISOString());
if (lastSynced) {
params.append("latest-synced", true.toString());
}
Expand Down Expand Up @@ -667,31 +669,7 @@ export const getLastKnownDeviceBatteryLevel = (
};

export const getDeviceModel = async (deviceId: DeviceId) => {
try {
const nodegroup = await getDeviceNodeGroup(deviceId);
if (nodegroup) {
const model = nodegroup.includes("tc2")
? "tc2"
: nodegroup.includes("pi")
? "pi"
: null;
if (model !== null) {
return model;
}
}
return await getLatestEventsByDeviceId(deviceId, {
type: "versionData",
limit: 1,
}).then((response) => {
if (response.success && response.result.rows.length) {
return response.result.rows[0].EventDetail.details["tc2-agent"]
? "tc2"
: "pi";
} else {
return null;
}
});
} catch (e) {
return null;
}
return CacophonyApi.get(`/api/v1/devices/${deviceId}/type`) as Promise<
FetchResult<{ type: DeviceTypeUnion }>
>;
};
39 changes: 25 additions & 14 deletions browse-next/src/components/DeviceRecordingSetup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import {
import Datepicker from "@vuepic/vue-datepicker";
import { projectDevicesLoaded } from "@models/LoggedInUser.ts";
import { resourceIsLoading } from "@/helpers/utils.ts";
import type { DeviceTypeUnion } from "@typedefs/api/consts";
type Time = { hours: number; minutes: number; seconds: number };
const devices = inject(selectedProjectDevices) as Ref<
ApiDeviceResponse[] | null
>;
const route = useRoute();
const deviceModel = ref<LoadedResource<"tc2" | "pi">>(null);
const deviceModel = ref<LoadedResource<DeviceTypeUnion>>(null);
// Device Settings
const settings = ref<LoadedResource<ApiDeviceHistorySettings>>(null);
const syncedSettings = ref<LoadedResource<ApiDeviceHistorySettings>>(null);
Expand Down Expand Up @@ -54,9 +55,10 @@ const device = computed<ApiDeviceResponse | null>(() => {
const settingsLoading = resourceIsLoading(settings);
const lastSyncedSettingsLoading = resourceIsLoading(lastSyncedSettings);
const nodeGroupInfoLoading = resourceIsLoading(deviceModel);
const isTc2Device = computed<boolean>(() => {
return deviceModel.value === "tc2";
return deviceModel.value === "hybrid-thermal-audio";
});
const defaultWindows = {
powerOn: "-30m",
Expand Down Expand Up @@ -154,15 +156,16 @@ const initialised = ref<boolean>(false);
onBeforeMount(async () => {
await projectDevicesLoaded();
await loadResource(settings, fetchSettings);
await loadResource(deviceModel, () => getDeviceModel(deviceId.value));
await loadResource(deviceModel, async () => {
const res = await getDeviceModel(deviceId.value);
if (res.success) {
return res.result.type;
}
});
initialised.value = true;
if (settings.value && !settings.value.synced) {
// Load last synced settings
const response = await getSettingsForDevice(
deviceId.value,
new Date(),
true
);
const response = await getSettingsForDevice(deviceId.value, true);
if (response && response.success && response.result.settings) {
syncedSettings.value = response.result.settings;
}
Expand All @@ -177,10 +180,13 @@ const useLowPowerMode = computed<boolean>({
);
},
set: (val: boolean) => {
(settings.value as ApiDeviceHistorySettings).thermalRecording = {
useLowPowerMode: val,
updated: new Date().toISOString(),
};
if (settings.value) {
(settings.value as ApiDeviceHistorySettings).thermalRecording = {
useLowPowerMode: val,
updated: new Date().toISOString(),
};
settings.value.synced = false;
}
},
});
const recordingWindowSetting = computed<"default" | "always" | "custom">({
Expand Down Expand Up @@ -235,6 +241,7 @@ const recordingWindowSetting = computed<"default" | "always" | "custom">({
updated: new Date().toISOString(),
};
}
settings.value.synced = false;
}
},
});
Expand All @@ -257,6 +264,7 @@ const customRecordingWindowStart = computed<Time>({
};
settings.value.windows.startRecording = timeObjToTimeStr(val);
settings.value.windows.updated = new Date().toISOString();
settings.value.synced = false;
}
},
});
Expand All @@ -280,6 +288,7 @@ const customRecordingWindowStop = computed<Time>({
};
settings.value.windows.stopRecording = timeObjToTimeStr(val);
settings.value.windows.updated = new Date().toISOString();
settings.value.synced = false;
}
},
});
Expand All @@ -306,6 +315,7 @@ const audioMode = computed<AudioModes>({
audioMode: val,
updated: new Date().toISOString(),
};
settings.value.synced = false;
}
},
});
Expand Down Expand Up @@ -336,8 +346,8 @@ function calculateTimePercentagePoints(
endTime: string
): Array<{ left: string; width: string }> {
if (startTime === "12:00" && endTime === "12:00") {
return [{ left: "0%", width: "100%" }];
}
return [{ left: "0%", width: "100%" }];
}
const startPercentage = timeToPercentage(startTime);
const endPercentage = timeToPercentage(endTime);
Expand Down Expand Up @@ -499,6 +509,7 @@ const audioSeed = computed<number>({
audioSeed: val,
updated: new Date().toISOString(),
};
settings.value.synced = false;
}
},
});
Expand Down
13 changes: 6 additions & 7 deletions browse-next/src/components/DeviceSetupReferencePhoto.vue
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ const moveHandle = (event: PointerEvent) => {
const singleFrame = ref<HTMLCanvasElement>();
const handleSingleFrameLoaded = (el: HTMLCanvasElement) => {
debugger;
singleFrame.value = el;
nextTick(() => {
positionHandles();
Expand All @@ -170,11 +169,11 @@ const constrainHandle = (
top: handleY,
} = handle.getBoundingClientRect();
if (clientX === undefined) {
clientX = handleX;
}
clientX = handleX;
}
if (clientY === undefined) {
clientY = handleY;
}
clientY = handleY;
}
const {
left: parentX,
Expand Down Expand Up @@ -505,8 +504,8 @@ const savingReferenceImage = ref<boolean>(false);
const saveReferenceImage = async () => {
const ctx = referenceImageSkew.value?.getContext("2d");
if (!ctx) {
return;
}
return;
}
savingReferenceImage.value = true;
renderSkewedImage(); // do one final draw at full opacity
Expand Down
1 change: 1 addition & 0 deletions types/api/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export enum DeviceType {
Hybrid = "hybrid-thermal-audio",
Unknown = "unknown",
}
export type DeviceTypeUnion = `${DeviceType}`;

export enum UserGlobalPermission {
Write = "write",
Expand Down

0 comments on commit c99c3e3

Please sign in to comment.