Skip to content

Commit 4d99645

Browse files
committed
Added button to trigger trap
1 parent f619dc3 commit 4d99645

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

.goreleaser.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ builds:
1818
- "7"
1919
ldflags: -s -w -X main.version={{.Version}}
2020
hooks:
21-
pre: packr
21+
pre:
22+
- packr
23+
- tsc
2224
post: packr clean
2325
- id: "signal-strength"
2426
binary: "signal-strength"

api/api.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,21 @@ func (api *ManagementAPI) DeleteEvents(w http.ResponseWriter, r *http.Request) {
453453
}
454454
}
455455

456+
// Trigger trap
457+
func (api *ManagementAPI) TriggerTrap(w http.ResponseWriter, r *http.Request) {
458+
log.Println("triggering trap")
459+
err := eventclient.AddEvent(eventclient.Event{
460+
Timestamp: time.Now(),
461+
Type: "trapped",
462+
Details: map[string]interface{}{"test": true},
463+
})
464+
465+
if err != nil {
466+
badRequest(&w, err)
467+
return
468+
}
469+
}
470+
456471
// CheckSaltConnection will try to ping the salt server and return the response
457472
func (api *ManagementAPI) CheckSaltConnection(w http.ResponseWriter, r *http.Request) {
458473
log.Println("pinging salt server")

cmd/managementd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func main() {
119119
apiRouter.HandleFunc("/event-keys", apiObj.GetEventKeys).Methods("GET")
120120
apiRouter.HandleFunc("/events", apiObj.GetEvents).Methods("GET")
121121
apiRouter.HandleFunc("/events", apiObj.DeleteEvents).Methods("DELETE")
122+
apiRouter.HandleFunc("/trigger-trap", apiObj.TriggerTrap).Methods("PUT")
122123
apiRouter.HandleFunc("/check-salt-connection", apiObj.CheckSaltConnection).Methods("GET")
123124
apiRouter.HandleFunc("/salt-update", apiObj.StartSaltUpdate).Methods("POST")
124125
apiRouter.HandleFunc("/salt-update", apiObj.GetSaltUpdateState).Methods("GET")

html/camera.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
<div class="container">
1515
<h2>Camera</h2>
16+
<button id="trigger-trap" style="position: relative" type="button">
17+
Trigger trap
18+
</button>
1619
<div id="snapshot-stopped" style="display: none">
1720
<p id="snapshot-stopped-message"></p>
1821
<button id="snapshot-restart" type="button">

static/js/camera.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,32 @@ function restartCameraViewing() {
6868
}
6969
}
7070

71+
async function triggerTrap() {
72+
document.getElementById("trigger-trap")!.innerText = 'Triggering trap';
73+
document.getElementById("trigger-trap")!.setAttribute("disabled", "true");
74+
console.log("triggering trap");
75+
fetch('/api/trigger-trap', {
76+
method: 'PUT',
77+
headers: {
78+
'Authorization': 'Basic YWRtaW46ZmVhdGhlcnM='
79+
}})
80+
81+
.then(response => console.log(response))
82+
.then(data => console.log(data))
83+
.catch(error => console.error(error))
84+
//TODO handle errors better and check that recording was made properly instead of just waiting..
85+
await new Promise(r => setTimeout(r, 3000));
86+
document.getElementById("trigger-trap")!.removeAttribute("disabled");
87+
document.getElementById("trigger-trap")!.innerText = 'Trigger trap';
88+
}
89+
7190
window.onload = function () {
7291
const urlParams = new URLSearchParams(window.location.search);
7392
if (urlParams.get("timeout") == "off") {
7493
snapshotLimit = Number.MAX_SAFE_INTEGER;
7594
}
7695
document.getElementById("snapshot-restart")!.onclick = restartCameraViewing;
96+
document.getElementById("trigger-trap")!.onclick = triggerTrap;
7797
cameraConnection = new CameraConnection(
7898
window.location.hostname,
7999
window.location.port,

0 commit comments

Comments
 (0)