Skip to content

Commit

Permalink
tidy up as per suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
gferraro committed Aug 27, 2024
1 parent ecca03e commit 3543503
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 43 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ build: install-packr install-typescript

.PHONY: install-typescript
install-typescript:
npm install -g typescript
npm install -g rollup
npm install typescript
npm install rollup
npm i @types/jquery
npx tsc

Expand Down
57 changes: 34 additions & 23 deletions static/js/audiorecording.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
"use strict";

let intervalId: number | null = null;
let lastState: number | null = null;
class AudioState {
constructor(
public intervalId: number | null,
public lastState: number | null
) {}
clearInterval() {
if (this.intervalId) {
clearInterval(this.intervalId as number);
}
this.intervalId = null;
this.lastState = null;
}
startPolling() {
this.clearInterval();
this.intervalId = setInterval(getAudioStatus, 1000);
}
}
const audioState = new AudioState(null, null);
let countdown = 0;
async function getAudioStatus() {
var xmlHttp = new XMLHttpRequest();
Expand All @@ -18,10 +34,8 @@ async function getAudioStatus() {
let statusText = "";
if (state == 1) {
countdown = 2;
if (lastState) {
clearInterval(intervalId as number);
lastState = null;
intervalId = null;
if (audioState) {
audioState.clearInterval();
statusText = "";
document
.getElementById("audio-test-button")
Expand All @@ -32,24 +46,24 @@ async function getAudioStatus() {
}
} else if (state == 2) {
countdown = 2;
lastState = state;
audioState.lastState = state;
statusText = "Test Recording Pending";
if (!intervalId) {
intervalId = setInterval(getAudioStatus, 1000);
if (!audioState.intervalId) {
audioState.startPolling();
document
.getElementById("audio-test-button")
?.setAttribute("disabled", "true");
}
} else if (state == 3) {
lastState = state;
audioState.lastState = state;
if (countdown == 0) {
statusText = "Taking Test Recording";
} else {
statusText = `Taking Test Recording in ${countdown}s`;
countdown -= 1;
}
if (!intervalId) {
intervalId = setInterval(getAudioStatus, 1000);
if (!audioState.intervalId) {
audioState.startPolling();
document
.getElementById("audio-test-button")
?.setAttribute("disabled", "true");
Expand All @@ -58,24 +72,22 @@ async function getAudioStatus() {
countdown = 2;
let recType = mode == 1 ? "an audio" : "a thermal";
statusText = `Already Taking ${recType} Recording`;
if (lastState != 4) {
clearInterval(intervalId as number);
if (audioState.lastState != 4) {
document
.getElementById("audio-test-button")
?.setAttribute("disabled", "true");
intervalId = setInterval(getAudioStatus, 10000);
audioState.startPolling();
//need to tell tc2 agent to poll state
testAPICall(false);
(
document.getElementById("audio-test-button") as HTMLElement
).innerText = "Waiting for Recording to finish";
}
lastState = state;
audioState.lastState = state;
} else {
countdown = 0;
statusText = "unknow state";
clearInterval(intervalId as number);
intervalId = null;
audioState.clearInterval();
document
.getElementById("audio-test-button")
?.removeAttribute("disabled");
Expand All @@ -96,9 +108,8 @@ async function getAudioStatus() {
}

function enableRecButton() {
if (intervalId) {
clearInterval(intervalId);
}
audioState.clearInterval();

document.getElementById("audio-test-button")?.removeAttribute("disabled");
(document.getElementById("audio-test-button") as HTMLElement).innerText =
"Take Test Recording";
Expand Down Expand Up @@ -126,8 +137,8 @@ async function testAPICall(checkResponse: boolean) {
enableRecButton();
updateAudioError(xmlHttp);
} else {
clearInterval(intervalId as number);
intervalId = setInterval(getAudioStatus, 1000);
audioState.clearInterval();
audioState.startPolling();
}
} else {
enableRecButton();
Expand Down
31 changes: 13 additions & 18 deletions static/js/camera.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { FrameInfo, Frame, Region, CameraInfo } from "../../api/types";

let audioOnly = false;
async function getAudioMode() {
var xmlHttp = new XMLHttpRequest();
xmlHttp.responseType = "json";
xmlHttp.open("GET", "/api/audiorecording", true);
xmlHttp.setRequestHeader("Authorization", "Basic " + btoa("admin:feathers"));
var success = false;
xmlHttp.onload = async function () {
if (xmlHttp.status == 200) {
audioOnly = xmlHttp.response["audio-mode"] == "AudioOnly";
}
};
xmlHttp.onerror = async function () {
console.log("Error getting audio status");
};
await xmlHttp.send();
return fetch("/api/audiorecording", {
method: "GET", // Default is 'get'
headers: new Headers({
Authorization: "Basic " + btoa("admin:feathers"),
"Content-Type": "application/json",
}),
})
.then((response) => response.json())
.then((json) => json["audio-mode"] == "AudioOnly");
}

async function getAudioStatus() {
Expand Down Expand Up @@ -326,7 +320,7 @@ async function processFrame(frame: Frame) {

export class CameraConnection {
private closing: boolean;

private audioOnly: boolean;
constructor(
public host: string,
public port: string,
Expand All @@ -335,6 +329,7 @@ export class CameraConnection {
connectionState: CameraConnectionState
) => void
) {
this.audioOnly = false;
this.closing = false;
this.connect();
}
Expand Down Expand Up @@ -427,12 +422,12 @@ export class CameraConnection {
this.onFrame((await this.parseFrame(event.data as Blob)) as Frame);
} else {
if (event.data == "disconnected") {
await getAudioMode();
this.audioOnly = await getAudioMode();
this.thermalConnected = false;
document
.getElementById("take-snapshot-recording")!
.setAttribute("disabled", "true");
if (audioOnly == true) {
if (this.audioOnly == true) {
document.getElementById("snapshot-stopped-message")!.innerText =
'In Audio only mode, change the mode in the "Audio Recording" section';

Expand Down

0 comments on commit 3543503

Please sign in to comment.