Skip to content

Commit f9dfa64

Browse files
author
gferraro
committed
Merge remote-tracking branch 'origin/master' into ir-fix
2 parents 4d99645 + 5dcd1ca commit f9dfa64

File tree

4 files changed

+60
-18
lines changed

4 files changed

+60
-18
lines changed

api/api.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const (
4949
cptvGlob = "*.cptv"
5050
failedUploadsFolder = "failed-uploads"
5151
rebootDelay = time.Second * 5
52-
apiVersion = 7
52+
apiVersion = 8
5353
)
5454

5555
type ManagementAPI struct {
@@ -132,12 +132,18 @@ func (api *ManagementAPI) GetRecording(w http.ResponseWriter, r *http.Request) {
132132
cptvPath := getRecordingPath(cptvName, api.cptvDir)
133133
if cptvPath == "" {
134134
w.WriteHeader(http.StatusBadRequest)
135-
io.WriteString(w, "cptv file not found\n")
135+
io.WriteString(w, "file not found\n")
136136
return
137137
}
138138

139139
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, cptvName))
140-
w.Header().Set("Content-Type", "application/x-cptv")
140+
141+
ext := filepath.Ext(cptvName)
142+
if ext == ".cptv" {
143+
w.Header().Set("Content-Type", "application/x-cptv")
144+
} else {
145+
w.Header().Set("Content-Type", "application/json")
146+
}
141147
f, err := os.Open(cptvPath)
142148
if err != nil {
143149
w.WriteHeader(http.StatusInternalServerError)
@@ -152,13 +158,19 @@ func (api *ManagementAPI) GetRecording(w http.ResponseWriter, r *http.Request) {
152158
// DeleteRecording deletes the given cptv file
153159
func (api *ManagementAPI) DeleteRecording(w http.ResponseWriter, r *http.Request) {
154160
cptvName := mux.Vars(r)["id"]
155-
log.Printf("delete cptv '%s'", cptvName)
156161
recPath := getRecordingPath(cptvName, api.cptvDir)
157162
if recPath == "" {
158163
w.WriteHeader(http.StatusOK)
159164
io.WriteString(w, "cptv file not found\n")
160165
return
161166
}
167+
168+
metaFile := strings.TrimSuffix(recPath, filepath.Ext(recPath)) + ".txt"
169+
if _, err := os.Stat(metaFile); !os.IsNotExist(err) {
170+
log.Printf("deleting meta '%s'", metaFile)
171+
os.Remove(metaFile)
172+
}
173+
log.Printf("delete cptv '%s'", recPath)
162174
err := os.Remove(recPath)
163175
if os.IsNotExist(err) {
164176
w.WriteHeader(http.StatusOK)
@@ -189,6 +201,21 @@ func (api *ManagementAPI) TakeSnapshot(w http.ResponseWriter, r *http.Request) {
189201
}
190202
}
191203

204+
// TakeSnapshotRecording will request a new snapshot recording to be taken by thermal-recorder
205+
func (api *ManagementAPI) TakeSnapshotRecording(w http.ResponseWriter, r *http.Request) {
206+
conn, err := dbus.SystemBus()
207+
if err != nil {
208+
w.WriteHeader(http.StatusInternalServerError)
209+
return
210+
}
211+
recorder := conn.Object("org.cacophony.thermalrecorder", "/org/cacophony/thermalrecorder")
212+
err = recorder.Call("org.cacophony.thermalrecorder.TakeTestRecording", 0).Err
213+
if err != nil {
214+
w.WriteHeader(http.StatusInternalServerError)
215+
return
216+
}
217+
}
218+
192219
// Reregister can change the devices name and group
193220
func (api *ManagementAPI) Reregister(w http.ResponseWriter, r *http.Request) {
194221
group := r.FormValue("group")
@@ -374,21 +401,11 @@ func getCptvNames(dir string) []string {
374401
return names
375402
}
376403

377-
func getRecordingPath(cptv, dir string) string {
404+
func getRecordingPath(file, dir string) string {
378405
// Check that given file is a cptv file on the device.
379-
isCptvFile := false
380-
for _, name := range getCptvNames(dir) {
381-
if name == cptv {
382-
isCptvFile = true
383-
break
384-
}
385-
}
386-
if !isCptvFile {
387-
return ""
388-
}
389406
paths := []string{
390-
filepath.Join(dir, cptv),
391-
filepath.Join(dir, failedUploadsFolder, cptv),
407+
filepath.Join(dir, file),
408+
filepath.Join(dir, failedUploadsFolder, file),
392409
}
393410
for _, path := range paths {
394411
if _, err := os.Stat(path); !os.IsNotExist(err) {

cmd/managementd/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func main() {
106106
apiRouter.HandleFunc("/recording/{id}", apiObj.GetRecording).Methods("GET")
107107
apiRouter.HandleFunc("/recording/{id}", apiObj.DeleteRecording).Methods("DELETE")
108108
apiRouter.HandleFunc("/camera/snapshot", apiObj.TakeSnapshot).Methods("PUT")
109+
apiRouter.HandleFunc("/camera/snapshot-recording", apiObj.TakeSnapshotRecording).Methods("PUT")
109110
apiRouter.HandleFunc("/signal-strength", apiObj.GetSignalStrength).Methods("GET")
110111
apiRouter.HandleFunc("/reregister", apiObj.Reregister).Methods("POST")
111112
apiRouter.HandleFunc("/reboot", apiObj.Reboot).Methods("POST")

html/camera.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313

1414
<div class="container">
1515
<h2>Camera</h2>
16-
<button id="trigger-trap" style="position: relative" type="button">
16+
<button id="trigger-trap" style="position: relative;display: none" type="button">
1717
Trigger trap
1818
</button>
19+
<button id="take-snapshot-recording" style="position: relative" type="button">
20+
Take test recording
21+
</button>
1922
<div id="snapshot-stopped" style="display: none">
23+
2024
<p id="snapshot-stopped-message"></p>
2125
<button id="snapshot-restart" type="button">
2226
Continue viewing camera

static/js/camera.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ window.onload = function () {
9494
}
9595
document.getElementById("snapshot-restart")!.onclick = restartCameraViewing;
9696
document.getElementById("trigger-trap")!.onclick = triggerTrap;
97+
document.getElementById("take-snapshot-recording")!.onclick = takeTestRecording;
9798
cameraConnection = new CameraConnection(
9899
window.location.hostname,
99100
window.location.port,
@@ -102,6 +103,25 @@ window.onload = function () {
102103
);
103104
};
104105

106+
async function takeTestRecording() {
107+
document.getElementById("take-snapshot-recording")!.innerText = 'Making a test recording';
108+
document.getElementById("take-snapshot-recording")!.setAttribute("disabled", "true");
109+
console.log("making a test recording");
110+
fetch('/api/camera/snapshot-recording', {
111+
method: 'PUT',
112+
headers: {
113+
'Authorization': 'Basic YWRtaW46ZmVhdGhlcnM='
114+
}})
115+
116+
.then(response => console.log(response))
117+
.then(data => console.log(data))
118+
.catch(error => console.error(error))
119+
//TODO handle errors better and check that recording was made properly instead of just waiting..
120+
await new Promise(r => setTimeout(r, 3000));
121+
document.getElementById("take-snapshot-recording")!.removeAttribute("disabled");
122+
document.getElementById("take-snapshot-recording")!.innerText = 'Take test recording';
123+
}
124+
105125
function stopSnapshots(message: string) {
106126
if (cameraConnection) {
107127
cameraConnection.close();

0 commit comments

Comments
 (0)