Skip to content

Commit

Permalink
Merge pull request #112 from TheCacophonyProject/ir-fix
Browse files Browse the repository at this point in the history
Ir fix
  • Loading branch information
gferraro authored Feb 1, 2023
2 parents 5dcd1ca + 3eb6c8d commit c8e6de6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
15 changes: 15 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,21 @@ func (api *ManagementAPI) DeleteEvents(w http.ResponseWriter, r *http.Request) {
}
}

// Trigger trap
func (api *ManagementAPI) TriggerTrap(w http.ResponseWriter, r *http.Request) {
log.Println("triggering trap")
err := eventclient.AddEvent(eventclient.Event{
Timestamp: time.Now(),
Type: "trapped",
Details: map[string]interface{}{"test": true},
})

if err != nil {
badRequest(&w, err)
return
}
}

// CheckSaltConnection will try to ping the salt server and return the response
func (api *ManagementAPI) CheckSaltConnection(w http.ResponseWriter, r *http.Request) {
log.Println("pinging salt server")
Expand Down
3 changes: 2 additions & 1 deletion cmd/managementd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func main() {
apiRouter.HandleFunc("/event-keys", apiObj.GetEventKeys).Methods("GET")
apiRouter.HandleFunc("/events", apiObj.GetEvents).Methods("GET")
apiRouter.HandleFunc("/events", apiObj.DeleteEvents).Methods("DELETE")
apiRouter.HandleFunc("/trigger-trap", apiObj.TriggerTrap).Methods("PUT")
apiRouter.HandleFunc("/check-salt-connection", apiObj.CheckSaltConnection).Methods("GET")
apiRouter.HandleFunc("/salt-update", apiObj.StartSaltUpdate).Methods("POST")
apiRouter.HandleFunc("/salt-update", apiObj.GetSaltUpdateState).Methods("GET")
Expand Down Expand Up @@ -185,7 +186,7 @@ func WebsocketServer(ws *websocket.Conn) {
}
socketsLock.Unlock()
if firstSocket {
log.Print("Git new client register")
log.Print("Get new client register")
haveClients <- true
}
}
Expand Down
5 changes: 4 additions & 1 deletion html/camera.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

<div class="container">
<h2>Camera</h2>
<button id="trigger-trap" style="position: relative;display: none" type="button">
Trigger trap
</button>
<button id="take-snapshot-recording" style="position: relative" type="button">
Take test recording
</button>
<div id="snapshot-stopped" style="display: none">

<p id="snapshot-stopped-message"></p>
<button id="snapshot-restart" type="button">
Continue viewing camera
Expand Down
71 changes: 58 additions & 13 deletions static/js/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,23 @@ function restartCameraViewing() {
}
}

async function takeTestRecording() {
document.getElementById("take-snapshot-recording")!.innerText = 'Making a test recording';
document.getElementById("take-snapshot-recording")!.setAttribute("disabled", "true");
console.log("making a test recording");
fetch('/api/camera/snapshot-recording', {
async function triggerTrap() {
document.getElementById("trigger-trap")!.innerText = 'Triggering trap';
document.getElementById("trigger-trap")!.setAttribute("disabled", "true");
console.log("triggering trap");
fetch('/api/trigger-trap', {
method: 'PUT',
headers: {
'Authorization': 'Basic YWRtaW46ZmVhdGhlcnM='
}})

.then(response => console.log(response))
.then(data => console.log(data))
.catch(error => console.error(error))
//TODO handle errors better and check that recording was made properly instead of just waiting..
await new Promise(r => setTimeout(r, 3000));
document.getElementById("take-snapshot-recording")!.removeAttribute("disabled");
document.getElementById("take-snapshot-recording")!.innerText = 'Take test recording';
document.getElementById("trigger-trap")!.removeAttribute("disabled");
document.getElementById("trigger-trap")!.innerText = 'Trigger trap';
}

window.onload = function () {
Expand All @@ -93,6 +93,7 @@ window.onload = function () {
snapshotLimit = Number.MAX_SAFE_INTEGER;
}
document.getElementById("snapshot-restart")!.onclick = restartCameraViewing;
document.getElementById("trigger-trap")!.onclick = triggerTrap;
document.getElementById("take-snapshot-recording")!.onclick = takeTestRecording;
cameraConnection = new CameraConnection(
window.location.hostname,
Expand All @@ -102,6 +103,25 @@ window.onload = function () {
);
};

async function takeTestRecording() {
document.getElementById("take-snapshot-recording")!.innerText = 'Making a test recording';
document.getElementById("take-snapshot-recording")!.setAttribute("disabled", "true");
console.log("making a test recording");
fetch('/api/camera/snapshot-recording', {
method: 'PUT',
headers: {
'Authorization': 'Basic YWRtaW46ZmVhdGhlcnM='
}})

.then(response => console.log(response))
.then(data => console.log(data))
.catch(error => console.error(error))
//TODO handle errors better and check that recording was made properly instead of just waiting..
await new Promise(r => setTimeout(r, 3000));
document.getElementById("take-snapshot-recording")!.removeAttribute("disabled");
document.getElementById("take-snapshot-recording")!.innerText = 'Take test recording';
}

function stopSnapshots(message: string) {
if (cameraConnection) {
cameraConnection.close();
Expand Down Expand Up @@ -170,26 +190,51 @@ function drawRectWithText(

async function processFrame(frame: Frame) {
const canvas = document.getElementById("frameCanvas") as HTMLCanvasElement;

const trackCanvas = document.getElementById(
"trackCanvas"
) as HTMLCanvasElement;

if (canvas == null) {
return;
}
if( canvas.width != frame.frameInfo.Camera.ResX){
canvas.width = frame.frameInfo.Camera.ResX
trackCanvas.width = frame.frameInfo.Camera.ResX
}
if(canvas.height != frame.frameInfo.Camera.ResY){
canvas.height = frame.frameInfo.Camera.ResY
trackCanvas.height = frame.frameInfo.Camera.ResY
}
const context = canvas.getContext("2d") as CanvasRenderingContext2D;
const imgData = context.getImageData(
0,
0,
frame.frameInfo.Camera.ResX,
frame.frameInfo.Camera.ResY
);
const max = Math.max(...frame.frame);
const min = Math.min(...frame.frame);
const range = max - min;
// gp hack to see if ir camera, dbus from python makes dictionary have to be all int type
let irCamera = frame.frameInfo.Camera.Model=="2";
if(irCamera){
document.getElementById("trigger-trap")!.style.display = "";
}else{
document.getElementById("trigger-trap")!.style.display = "none";
}
let max=0;
let min=0;
let range=0;
if (!irCamera){
max = Math.max(...frame.frame);
min = Math.min(...frame.frame);
range = max - min;
}
let maxI = 0;
for (let i = 0; i < frame.frame.length; i++) {
const pix = Math.min(255, ((frame.frame[i] - min) / range) * 255.0);
let pix = 0
if(irCamera){
pix = frame.frame[i]
}else{
pix = Math.min(255, ((frame.frame[i] - min) / range) * 255.0);
}
let index = i * 4;
imgData.data[index] = pix;
imgData.data[index + 1] = pix;
Expand Down

0 comments on commit c8e6de6

Please sign in to comment.