Skip to content

Commit

Permalink
Merge branch 'master' into show-device-info-on-about-screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Davo36 authored Sep 2, 2019
2 parents 3fba359 + dc9f6d4 commit 6e1d3b0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 16 deletions.
10 changes: 7 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func (api *ManagementAPI) DeleteRecording(w http.ResponseWriter, r *http.Request
// TakeSnapshot will request a new snapshot to be taken by thermal-recorder
func (api *ManagementAPI) TakeSnapshot(w http.ResponseWriter, r *http.Request) {
conn, err := dbus.SystemBus()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
recorder := conn.Object("org.cacophony.thermalrecorder", "/org/cacophony/thermalrecorder")
err = recorder.Call("org.cacophony.thermalrecorder.TakeSnapshot", 0).Err
if err != nil {
Expand All @@ -160,8 +164,8 @@ func (api *ManagementAPI) TakeSnapshot(w http.ResponseWriter, r *http.Request) {
}
}

// Rename can change the devices name and gruop
func (api *ManagementAPI) Rename(w http.ResponseWriter, r *http.Request) {
// Reregister can change the devices name and gruop
func (api *ManagementAPI) Reregister(w http.ResponseWriter, r *http.Request) {
group := r.FormValue("group")
name := r.FormValue("name")
if group == "" && name == "" {
Expand All @@ -183,7 +187,7 @@ func (api *ManagementAPI) Rename(w http.ResponseWriter, r *http.Request) {
}

log.Printf("renaming with name: '%s' group: '%s'", name, group)
if err := apiClient.Rename(name, group); err != nil {
if err := apiClient.Reregister(name, group, randString(20)); err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, err.Error())
return
Expand Down
47 changes: 47 additions & 0 deletions api/randstring.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// go-api - Client for the Cacophony API server.
// Copyright (C) 2018, The Cacophony Project
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

package api

import (
"math/rand"
"time"
)

const (
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
charIdxBits = 6 // 6 bits to represent a char index
charIdxMax = 63 / charIdxBits // # of char indices fitting in 63 bits
charIdxMask = 1<<charIdxBits - 1 // All 1-bits, as many as charIdxBits
)

var randSrc = rand.NewSource(time.Now().UnixNano())

func randString(n int) string {
b := make([]byte, n)
// A randSrc.Int63() generates 63 random bits, enough for charIdxMax characters!
for i, cache, remain := n-1, randSrc.Int63(), charIdxMax; i >= 0; {
if remain == 0 {
cache, remain = randSrc.Int63(), charIdxMax
}
if idx := int(cache & charIdxMask); idx < len(chars) {
b[i] = chars[idx]
i--
}
cache >>= charIdxBits
remain--
}
return string(b)
}
2 changes: 1 addition & 1 deletion cmd/managementd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
apiRouter.HandleFunc("/recording/{id}", apiObj.DeleteRecording).Methods("DELETE")
apiRouter.HandleFunc("/camera/snapshot", apiObj.TakeSnapshot).Methods("PUT")
apiRouter.HandleFunc("/signal-strength", apiObj.GetSignalStrength).Methods("GET")
apiRouter.HandleFunc("/rename", apiObj.Rename).Methods("POST")
apiRouter.HandleFunc("/reregister", apiObj.Reregister).Methods("POST")
apiRouter.HandleFunc("/reboot", apiObj.Reboot).Methods("POST")
apiRouter.HandleFunc("/location", managementinterface.APILocationHandler).Methods("POST") // Set location via a POST request.
apiRouter.HandleFunc("/clock", managementinterface.APITimeHandler).Methods("POST") // Set times via a POST request.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/TheCacophonyProject/management-interface
go 1.12

require (
github.com/TheCacophonyProject/go-api v0.0.0-20190808052028-4af8cb3d4d33
github.com/TheCacophonyProject/go-api v0.0.0-20190901221455-8bdb00df1b38
github.com/gobuffalo/packr v1.30.1
github.com/godbus/dbus v4.1.0+incompatible
github.com/gorilla/context v1.1.1 // indirect
Expand Down
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/TheCacophonyProject/go-api v0.0.0-20190626053918-f75d61018103 h1:Y95eKfNi+SmG8HI/dfbTItiJpce6D9OEVO4HPdW+5cI=
github.com/TheCacophonyProject/go-api v0.0.0-20190626053918-f75d61018103/go.mod h1:tD8UcoBnVOb41Sgg+b5JOPVzdJMZnfxK7IpgGCNUvZw=
github.com/TheCacophonyProject/go-api v0.0.0-20190805073513-1b5de057d569 h1:XKVvBK9tDr7uwL+OsICxW7xzOz6svYCxIQ4PyVtXTxE=
github.com/TheCacophonyProject/go-api v0.0.0-20190805073513-1b5de057d569/go.mod h1:7OQjzdX6PbCJDxVRgTqNH9y12Ew+Q1sx9nwcRG7fj9s=
github.com/TheCacophonyProject/go-api v0.0.0-20190808010155-72c26da172c2 h1:KtCKSoSNT/YuXvaNQmqTWJRJR4Oqhpl+ifdk3/eV0eI=
github.com/TheCacophonyProject/go-api v0.0.0-20190808010155-72c26da172c2/go.mod h1:7OQjzdX6PbCJDxVRgTqNH9y12Ew+Q1sx9nwcRG7fj9s=
github.com/TheCacophonyProject/go-api v0.0.0-20190808023539-d072de86cdd4 h1:+o9Cv+ievinix9TeveJzEhRVw8TnJofrs85DMp1sEvs=
github.com/TheCacophonyProject/go-api v0.0.0-20190808023539-d072de86cdd4/go.mod h1:7OQjzdX6PbCJDxVRgTqNH9y12Ew+Q1sx9nwcRG7fj9s=
github.com/TheCacophonyProject/go-api v0.0.0-20190808052028-4af8cb3d4d33 h1:g6rvfHxeOw50gZa+gq2kfOGblJC3Uc/LXZoJEQgtkX0=
github.com/TheCacophonyProject/go-api v0.0.0-20190808052028-4af8cb3d4d33/go.mod h1:7OQjzdX6PbCJDxVRgTqNH9y12Ew+Q1sx9nwcRG7fj9s=
github.com/TheCacophonyProject/go-api v0.0.0-20190901221455-8bdb00df1b38 h1:fBnpPUmpLjotLgbojyezZEnb4aRtl8mevXEhrYxFr0I=
github.com/TheCacophonyProject/go-api v0.0.0-20190901221455-8bdb00df1b38/go.mod h1:7OQjzdX6PbCJDxVRgTqNH9y12Ew+Q1sx9nwcRG7fj9s=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
Expand Down
2 changes: 1 addition & 1 deletion static/js/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function rename() {
var data = $("#rename-form").serialize()

var xmlHttp = new XMLHttpRequest();
xmlHttp.open('POST', '/api/rename', true);
xmlHttp.open('POST', '/api/reregister', true);
xmlHttp.setRequestHeader("Authorization", "Basic "+btoa("admin:feathers"))
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlHttp.onload = async function() {
Expand Down

0 comments on commit 6e1d3b0

Please sign in to comment.