Skip to content

Commit

Permalink
Merge pull request #77 from Davo36/show-device-info-on-about-screen
Browse files Browse the repository at this point in the history
Show device info on about screen
  • Loading branch information
mjs authored Sep 2, 2019
2 parents dc9f6d4 + 6e1d3b0 commit e37bdef
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 62 deletions.
1 change: 1 addition & 0 deletions cmd/managementd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func main() {
router.HandleFunc("/location", managementinterface.LocationHandler).Methods("GET", "POST") // Form to view and/or set location manually.
router.HandleFunc("/clock", managementinterface.TimeHandler).Methods("GET", "POST") // Form to view and/or adjust time settings.
router.HandleFunc("/about", managementinterface.AboutHandler).Methods("GET")

router.HandleFunc("/advanced", managementinterface.AdvancedMenuHandler).Methods("GET")
router.HandleFunc("/camera", managementinterface.CameraHandler).Methods("GET")
router.HandleFunc("/camera/snapshot", managementinterface.CameraSnapshot).Methods("GET")
Expand Down
127 changes: 81 additions & 46 deletions html/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,60 +8,95 @@
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
</head>

<body>
{{template "navbar"}}
<body>
{{template "navbar"}}

<div class="container">
{{if ne .ErrorMessage ""}}
<div class="alert alert-danger">
{{.ErrorMessage}}
</div>
{{end}}
<div class="container">
{{if ne .ErrorMessage ""}}
<div class="alert alert-danger">
{{.ErrorMessage}}
</div>
{{end}}

<div class="container pt-5 pl-0">
<h2>Installed Packages<br></h2>
</div>
<div class="container pt-5 pl-0">
<h2>About<br></h2>
</div>
<hr>

<div class="row">
<div class="col-xs-12" >
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Package</th>
<th>Version</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
{{range .PackageDataRows}}
<tr>
<td>{{index . 0}}</td>
<td class="text-center">{{index . 1}}</td>
<td></td>
<td></td>
</tr>
{{end}}
</tbody>
</table>
</div>

<div class="container pt-5 pl-0">
<h3>Device<br></h3>
</div>

<div class="row">
<div class="col-xs-8">
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td>Name</td>
<td>{{DeviceName}}</td>
</tr>
<tr>
<td>Group</td>
<td id="Group">{{.Group}}</td>
</tr>
<tr>
<td>Serial Number</td>
<td>{{.RaspberryPiSerialNumber}}</td>
</tr>
<tr>
<td>API Identifier</td>
<td id="APIID">{{.DeviceID}}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>



<div class="container pt-5 pb-4 pl-0">
<h3>Installed Packages<br></h3>
</div>

<div class="row">
<div class="col-xs-8">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>Package</th>
<th>Version</th>
</tr>
</thead>
<tbody>
{{range .PackageDataRows}}
<tr>
<td>{{index . 0}}</td>
<td class="text-center">{{index . 1}}</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
</div>

<script src="/static/js/jquery-3.3.1.slim.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>

<script>
$(document).ready(function(){
document.getElementById("btnBack").href="/advanced";
document.getElementById("navDeviceName").href="/advanced";
});
</script>
</div>

<script src="/static/js/jquery-3.3.1.slim.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>

<script>
$(document).ready(function () {
document.getElementById("btnBack").href = "/advanced";
document.getElementById("navDeviceName").href = "/advanced";
});
</script>


</body>
</body>

</html>
</html>
6 changes: 6 additions & 0 deletions html/rename.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ <h2>Device Rename<br></h2>
<script src="/static/js/jquery-3.3.1.slim.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/static/js/datalist-polyfill.min.js"></script>
<script>
$(document).ready(function(){
document.getElementById("btnBack").href="/advanced";
document.getElementById("navDeviceName").href="/advanced";
});
</script>
</body>

</html>
96 changes: 80 additions & 16 deletions management-interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import (
"strconv"
"strings"

goapi "github.com/TheCacophonyProject/go-api"

"github.com/gobuffalo/packr"
"github.com/gorilla/mux"
yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -116,6 +118,40 @@ func getDeviceName() string {
return strings.SplitN(name, ".", 2)[0]
}

// Return the serial number for the Raspberr Pi in the device.
func getRaspberryPiSerialNumber() string {

if runtime.GOOS == "windows" {
return ""
}

// The /proc/cpuinfo file normally contains a serial number.
file, err := os.Open("/proc/cpuinfo")
if err != nil {
return ""
}
defer file.Close()
out, err := ioutil.ReadAll(file)
if err != nil {
return ""
}

// Extract the serial number.
serialNumber := ""
rows := strings.Split(string(out), "\n")
for _, row := range rows {
parts := strings.Split(row, ":")
if len(parts) == 2 {
field := strings.ToUpper(strings.TrimSpace(parts[0]))
if field == "SERIAL" {
return strings.TrimSpace(parts[1])
}
}
}

return serialNumber
}

// Get the directory of where this executable was started.
func getExecutablePath() string {
ex, err := os.Executable()
Expand Down Expand Up @@ -386,12 +422,12 @@ func addWPANetwork(ssid string, password string) error {
return fmt.Errorf("SSID %s already exists", ssid)
}

networkId, err := addNewNetwork()
networkID, err := addNewNetwork()
if err != nil {
return err
}

err = setWPANetworkDetails(ssid, password, networkId)
err = setWPANetworkDetails(ssid, password, networkID)
if err != nil {
return err
}
Expand All @@ -407,10 +443,10 @@ func addWPANetwork(ssid string, password string) error {
// addNewNetwork adds a new network in the wpa_supplication configuration and returns the new network id
func addNewNetwork() (int, error) {
out, err := exec.Command("wpa_cli", "add_network").Output()
var networkId int = -1
var networkID = -1

if err != nil {
return networkId, fmt.Errorf("error executing wpa_cli add_network - error %s output %s", err, out)
return networkID, fmt.Errorf("error executing wpa_cli add_network - error %s output %s", err, out)
}
stdOut := string(out)

Expand All @@ -419,26 +455,26 @@ func addNewNetwork() (int, error) {
scanner.Scan() //skip interface line
if scanner.Scan() {
line := scanner.Text()
networkId, err = strconv.Atoi(line)
networkID, err = strconv.Atoi(line)
if err != nil {
return -1, fmt.Errorf("could not find network id - error %s from stdout %s", err, stdOut)
}
}
return networkId, err
return networkID, err
}

// setWPANetworkDetails sets the ssid and password of the specified networkID in the wpa_supplication configuration
func setWPANetworkDetails(ssid string, password string, networkId int) error {
func setWPANetworkDetails(ssid string, password string, networkID int) error {
cmd := exec.Command("wpa_cli")
stdin, err := cmd.StdinPipe()
if err != nil {
return fmt.Errorf("error getting stdin pipe from cmd: %s", err)
}

defer stdin.Close()
io.WriteString(stdin, fmt.Sprintf("set_network %d ssid \"%s\"\n", networkId, ssid))
io.WriteString(stdin, fmt.Sprintf("set_network %d psk \"%s\"\n", networkId, password))
io.WriteString(stdin, fmt.Sprintf("enable_network %d\n", networkId))
io.WriteString(stdin, fmt.Sprintf("set_network %d ssid \"%s\"\n", networkID, ssid))
io.WriteString(stdin, fmt.Sprintf("set_network %d psk \"%s\"\n", networkID, password))
io.WriteString(stdin, fmt.Sprintf("enable_network %d\n", networkID))
io.WriteString(stdin, "quit\n")
out, err := cmd.CombinedOutput()
if err != nil {
Expand Down Expand Up @@ -578,16 +614,43 @@ func getInstalledPackages() (string, error) {
// AboutHandler shows the currently installed packages on the device.
func AboutHandler(w http.ResponseWriter, r *http.Request) {

type packagesResponse struct {
PackageDataRows [][]string
ErrorMessage string
type aboutResponse struct {
RaspberryPiSerialNumber string
Group string
DeviceID int
PackageDataRows [][]string
ErrorMessage string
}

packagesData, err := getInstalledPackages()
// Create response
resp := aboutResponse{
RaspberryPiSerialNumber: getRaspberryPiSerialNumber(),
}

// Get the device group from the API
config, err := goapi.LoadConfig()
if err != nil {
log.Println("failed to read device config from API:", err)
} else {
resp.Group = config.Group
}

// Get the device ID from the device-priv.yaml file locally
privConfig, err := goapi.LoadPrivateConfig()
if err != nil {
tmpl.ExecuteTemplate(w, "about.html", packagesResponse{ErrorMessage: errorMessage(err)})
log.Println("error loading private config:", err)
} else {
if privConfig != nil {
resp.DeviceID = privConfig.DeviceID
}
}

// Get installed packages.
packagesData, err := getInstalledPackages()
if err != nil {
resp.ErrorMessage = errorMessage(err)
tmpl.ExecuteTemplate(w, "about.html", resp)
}
// Want to separate this into separate fields so that can display in a table in HTML
dataRows := [][]string{}
rows := strings.Split(packagesData, "\n")
Expand All @@ -599,8 +662,9 @@ func AboutHandler(w http.ResponseWriter, r *http.Request) {
words := strings.Split(strings.TrimSpace(row), "|")
dataRows = append(dataRows, words[:2])
}
resp.PackageDataRows = dataRows

tmpl.ExecuteTemplate(w, "about.html", packagesResponse{PackageDataRows: dataRows})
tmpl.ExecuteTemplate(w, "about.html", resp)
}

// CheckInterfaceHandler checks an interface to see if it is up or down.
Expand Down

0 comments on commit e37bdef

Please sign in to comment.