Skip to content

Commit

Permalink
added webinterface for single rotator and improved hub class
Browse files Browse the repository at this point in the history
  • Loading branch information
dh1tw committed Sep 10, 2017
1 parent c690400 commit 55e7ab1
Show file tree
Hide file tree
Showing 28 changed files with 2,000 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ remoteRotator
*.zip
*.tar.gz
*.tgz
hub/rice-box.go
14 changes: 14 additions & 0 deletions .remoteRotator.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
[tcp]
enabled = true
host = "127.0.0.1"
port = 3333

[http]
enabled = true
host = "127.0.0.1"
port = 7070

[discovery]
enabled = true

[rotator]
type = "ARS"
name = "myRotator"
Expand All @@ -10,3 +19,8 @@ baudrate = 9600
has-azimuth = true
has-elevation = false
pollingrate = "1s"
azimuth-min = 0
azimuth-max = 450
azimuth-stop = 0
elevation-min = 0
elevation-max = 180
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ matrix:
- GIMME_ARCH=arm
- HOST_OS=linux
go: 1.9
# - os: linux #temporarily removed since hashicorp/go.net doesn't compile
# env:
# - GIMME_OS=linux
# - GIMME_ARCH=arm64
# - HOST_OS=linux
# go: 1.9
- os: linux
env:
- GIMME_OS=linux
- GIMME_ARCH=arm64
- GIMME_OS=windows
- GIMME_ARCH=amd64
- HOST_OS=linux
go: 1.9
- os: linux
env:
- GIMME_OS=windows
- GIMME_ARCH=amd64
- GIMME_ARCH=386
- HOST_OS=linux
go: 1.9
- os: osx
Expand All @@ -45,6 +51,7 @@ before_install:
- git fetch --tags
- GOARCH=amd64 GOOS=$HOST_OS go get golang.org/x/tools/cmd/stringer
- GOARCH=amd64 GOOS=$HOST_OS go get github.com/mattn/goveralls
- GOARCH=amd64 GOOS=$HOST_OS go get github.com/GeertJohan/go.rice/rice
script:
- GOARCH=$GIMME_ARCH GOOS=$GIMME_OS go get ./...
- GOARCH=$GIMME_ARCH GOOS=$GIMME_OS ./ci/test.sh
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ build:

generate:
go generate ./...
cd hub; \
rice embed-go

dist:
go build -v -ldflags="-w -X github.com/dh1tw/remoteRotator/cmd.commitHash=${COMMIT} \
Expand All @@ -44,6 +46,8 @@ install:
-X github.com/dh1tw/remoteRotator/cmd.version=${VERSION}"

install-deps:
go get golang.org/x/tools/cmd/stringer
go get github.com/GeertJohan/go.rice/rice
go get ./...

windows:
Expand Down
103 changes: 95 additions & 8 deletions cmd/tcp_server.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cmd

import (
b64 "encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"os/signal"
"strings"
"time"

"github.com/hashicorp/mdns"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand All @@ -27,15 +30,26 @@ var tcpServerCmd = &cobra.Command{
func init() {
serverCmd.AddCommand(tcpServerCmd)

tcpServerCmd.Flags().StringP("host", "u", "127.0.0.1", "Host (use '0.0.0.0' for public access)")
tcpServerCmd.Flags().IntP("port", "p", 7373, "TCP Port")
tcpServerCmd.Flags().BoolP("tcp-enabled", "", true, "enable TCP Server")
tcpServerCmd.Flags().StringP("tcp-host", "u", "127.0.0.1", "Host (use '0.0.0.0' to listen on all network adapters)")
tcpServerCmd.Flags().IntP("tcp-port", "p", 7373, "TCP Port")
tcpServerCmd.Flags().BoolP("http-enabled", "", true, "enable HTTP Server")
tcpServerCmd.Flags().StringP("http-host", "w", "127.0.0.1", "Host (use '0.0.0.0' to listen on all network adapters)")
tcpServerCmd.Flags().IntP("http-port", "k", 7070, "Port for the HTTP access to the rotator")
tcpServerCmd.Flags().BoolP("discovery-enabled", "", true, "make rotator discoverable on the network")
tcpServerCmd.Flags().StringP("portname", "P", "/dev/ttyACM0", "portname / path to the rotator (e.g. COM1)")
tcpServerCmd.Flags().IntP("baudrate", "b", 9600, "baudrate")
tcpServerCmd.Flags().StringP("type", "t", "ARS", "Rotator type (supported: ARS")
tcpServerCmd.Flags().StringP("name", "n", "myRotator", "Name tag for the rotator")
tcpServerCmd.Flags().StringP("description", "d", "Yaesu G1000 with 4el 20m Yagi@18m ASL", "Description")
tcpServerCmd.Flags().BoolP("has-azimuth", "", true, "Indicate if the rotator supports Azimuth")
tcpServerCmd.Flags().BoolP("has-elevation", "", false, "Indicate if the rotator supports Elevation")
tcpServerCmd.Flags().DurationP("pollingrate", "", time.Second*1, "rotator polling rate")
tcpServerCmd.Flags().IntP("azimuth-min", "", 0, "metadata: minimum azimuth (in deg)")
tcpServerCmd.Flags().IntP("azimuth-max", "", 450, "metadata: maximum azimuth (in deg)")
tcpServerCmd.Flags().IntP("azimuth-stop", "", 0, "metadata: mechanical azimuth stop (in deg)")
tcpServerCmd.Flags().IntP("elevation-min", "", 0, "metadata: minimum elevation (in deg)")
tcpServerCmd.Flags().IntP("elevation-max", "", 180, "metadata: maximum elevation (in deg)")
}

func tcpServer(cmd *cobra.Command, args []string) {
Expand All @@ -59,15 +73,26 @@ func tcpServer(cmd *cobra.Command, args []string) {
// }

// bind the pflags to viper settings
viper.BindPFlag("tcp.host", cmd.Flags().Lookup("host"))
viper.BindPFlag("tcp.port", cmd.Flags().Lookup("port"))
viper.BindPFlag("tcp.enabled", cmd.Flags().Lookup("tcp-enabled"))
viper.BindPFlag("tcp.host", cmd.Flags().Lookup("tcp-host"))
viper.BindPFlag("tcp.port", cmd.Flags().Lookup("tcp-port"))
viper.BindPFlag("http.enabled", cmd.Flags().Lookup("http-enabled"))
viper.BindPFlag("http.host", cmd.Flags().Lookup("http-host"))
viper.BindPFlag("http.port", cmd.Flags().Lookup("http-port"))
viper.BindPFlag("discovery.enabled", cmd.Flags().Lookup("discovery-enabled"))
viper.BindPFlag("rotator.portname", cmd.Flags().Lookup("portname"))
viper.BindPFlag("rotator.baudrate", cmd.Flags().Lookup("baudrate"))
viper.BindPFlag("rotator.type", cmd.Flags().Lookup("type"))
viper.BindPFlag("rotator.name", cmd.Flags().Lookup("name"))
viper.BindPFlag("rotator.description", cmd.Flags().Lookup("description"))
viper.BindPFlag("rotator.has-azimuth", cmd.Flags().Lookup("has-azimuth"))
viper.BindPFlag("rotator.has-elevation", cmd.Flags().Lookup("has-elevation"))
viper.BindPFlag("rotator.pollingrate", cmd.Flags().Lookup("pollingrate"))
viper.BindPFlag("rotator.azimuth-min", cmd.Flags().Lookup("azimuth-min"))
viper.BindPFlag("rotator.azimuth-max", cmd.Flags().Lookup("azimuth-max"))
viper.BindPFlag("rotator.azimuth-stop", cmd.Flags().Lookup("azimuth-stop"))
viper.BindPFlag("rotator.elevation-min", cmd.Flags().Lookup("elevation-min"))
viper.BindPFlag("rotator.elevation-max", cmd.Flags().Lookup("elevation-max"))

// go func() {
// log.Println(http.ListenAndServe("0.0.0.0:6060", http.DefaultServeMux))
Expand All @@ -76,10 +101,16 @@ func tcpServer(cmd *cobra.Command, args []string) {
bcast := make(chan rotator.Status, 10)

var arsEventHandler = func(r rotator.Rotator, ev rotator.Event, value ...interface{}) {
fmt.Println(ev, value)
// fmt.Println(ev, value)
switch ev {
case rotator.Azimuth, rotator.Elevation:
bcast <- r.Serialize()
if len(value) == 0 {
return
}
switch value[0].(type) {
case rotator.Status:
bcast <- value[0].(rotator.Status)
}
default:
log.Printf("unknown event: %v with value(s): %v\n", ev, value)
}
Expand Down Expand Up @@ -115,22 +146,78 @@ func tcpServer(cmd *cobra.Command, args []string) {
go ars.Start(arsError, arsShutdown)

tcpError := make(chan bool)
go h.ListenTCP(viper.GetString("tcp.host"), viper.GetInt("tcp.port"), tcpError)

// start TCP server
if viper.GetBool("tcp.enabled") {
go h.ListenTCP(viper.GetString("tcp.host"), viper.GetInt("tcp.port"), tcpError)
}

wsError := make(chan bool)

// start HTTP server
if viper.GetBool("http.enabled") {
go h.ListenHTTP(viper.GetString("http.host"), viper.GetInt("http.port"), wsError)
}

// shutdownWg := sync.WaitGroup{}
mDNSServer := &mdns.Server{}
// start mDNS server
if viper.GetBool("discovery.enabled") {

i := rotator.Info{
Name: viper.GetString("rotator.name"),
Description: viper.GetString("rotator.description"),
HasAzimuth: viper.GetBool("rotator.has-azimuth"),
HasElevation: viper.GetBool("rotator.has-elevation"),
AzimuthMin: viper.GetInt("rotator.azimuth-min"),
AzimuthMax: viper.GetInt("rotator.azimuth-max"),
AzimuthStop: viper.GetInt("rotator.azimuth-stop"),
ElevationMin: viper.GetInt("rotator.elevation-min"),
ElevationMax: viper.GetInt("rotator.elevation-max"),
}

info, err := encodeInfo(i)
if err != nil {
fmt.Printf("unable to marshal rotator description: %s\n", err)
return
}

mDNSService, err := mdns.NewMDNSService(viper.GetString("rotator.name"),
"rotators.shackbus", "", "", viper.GetInt("http.port"), nil, []string{info})

if err != nil {
fmt.Printf("unable to start mDNS discovery service: %v", err)
return
}
mDNSServer, _ = mdns.NewServer(&mdns.Config{Zone: mDNSService})
defer mDNSServer.Shutdown()
}

for {
select {
case sig := <-osSignals:
if sig == os.Interrupt {
close(arsShutdown)
return
}
case msg := <-bcast:
h.Broadcast(msg)
case <-arsError:
return
case <-tcpError:
return
case <-wsError:
return
}
}

}

func encodeInfo(i rotator.Info) (string, error) {
res, err := json.Marshal(i)
if err != nil {
return "", err
}

uEnc := b64.URLEncoding.EncodeToString(res)
return uEnc, nil
}
40 changes: 40 additions & 0 deletions html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>RemoteRotator WebUI</title>
<link rel="stylesheet" href="/static/css/font-awesome.min.css">
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" href="/static/css/style.css">
</head>

<body>
<div id="app">
<div class="main-rotator">
<azimuth-rotator v-on:set-azimuth="setAzimuth" :name="azName" :heading="azHeading" :preset="azPreset" :enabled="azEnabled"
:canvas-size="azCanvasSize"></azimuth-rotator>
</div>
<div id="mini-rotators" class="hidden">
</div>
<div id="connection">
<p id="connected" class="bg-success" v-bind:class="{'hidden': hideConnectionMsg}" v-if="connected">
<i class="fa fa-check"></i> Connected to Server
</p>
<p id="no-connection" class="bg-danger" v-else="connected">
<i class="fa fa-spinner fa-spin" aria-hidden="true"></i> Connecting to Server
</p>
</div>
</div>
<script src="/static/js/vue.js"></script>
<script src="/static/js/vue-resource-1.3.4.min.js"></script>
<script src="/static/js/components/azimuth-rotator.js"></script>
<script src="/static/js/jquery-2.2.3.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/static/js/reconnecting-websocket.js"></script>
<script src="/static/js/app.js"></script>
</body>

</html>
5 changes: 5 additions & 0 deletions html/static/css/bootstrap.min.css

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions html/static/css/font-awesome.min.css

Large diffs are not rendered by default.

Loading

0 comments on commit 55e7ab1

Please sign in to comment.