Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jutaz committed Mar 20, 2016
0 parents commit a65c5c1
Show file tree
Hide file tree
Showing 6 changed files with 1,178 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/getStationName.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"../src"
"fmt"
"net"
)

func main() {
airport := &airport.Airport{
Password: "", // Your password here.
Address: net.IPv4(10, 0, 1, 1), // Base station IP.
}
name, err := airport.GetStationName()
if nil != err {
panic(err)
}
fmt.Printf("Station name: %s\n", name)
}
19 changes: 19 additions & 0 deletions examples/reboot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"../src"
"fmt"
"net"
)

func main() {
airport := &airport.Airport{
Password: "", // Your password here.
Address: net.IPv4(10, 0, 1, 1), // Base station IP.
}
err := airport.Reboot()
if nil != err {
panic(err)
}
fmt.Println("Station rebooted")
}
100 changes: 100 additions & 0 deletions src/airport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package airport

import (
"net"
"bytes"
"io"
)

// Airport TODO
type Airport struct {
Password string
Address net.IP
}

//Reboot TODO
func (a *Airport) Reboot() error {
info := NewInfo(nil).Get("acRB").GetUpdateBytes()

return a.write(info)
}

// GetStationName TODO
func (a *Airport) GetStationName() (string, error) {
requestBytes := NewInfo(nil).GetRequestBytes()

info, err := a.read(requestBytes)

if nil != err {
return "", err
}

return string(info.Get("syNm").GetValue()), nil
}

func (a *Airport) read(requestPayload []byte) (*Info, error) {
requestMessage := NewMessage(MessageTypeRead, a.Password, requestPayload, len(requestPayload))
conn, err := a.createConnection()
if nil != err {
return nil, err
}

defer conn.Close()

_, err = conn.Write(requestMessage.GetBytes())
if nil != err {
return nil, err
}

_, err = conn.Write(requestPayload)
if nil != err {
return nil, err
}

responseHeader := make([]byte, 128)

_, err = conn.Read(responseHeader)
if nil != err {
return nil, err
}

responseBuffer := new(bytes.Buffer)
io.Copy(responseBuffer, conn)

return NewInfo(responseBuffer.Bytes()), nil
}

func (a *Airport) write(requestPayload []byte) error {
requestMessage := NewMessage(MessageTypeWrite, a.Password, requestPayload, len(requestPayload))
conn, err := a.createConnection()
if nil != err {
return err
}

defer conn.Close()

_, err = conn.Write(requestMessage.GetBytes())
if nil != err {
return err
}

_, err = conn.Write(requestPayload)
if nil != err {
return err
}

return nil
}

func (a *Airport) createConnection() (*net.TCPConn, error) {
address := &net.TCPAddr{
IP: a.Address,
Port: 5009,
}
conn, err := net.DialTCP("tcp", nil, address)
if nil != err {
return nil, err
}

return conn, nil
}
Loading

0 comments on commit a65c5c1

Please sign in to comment.