refactoring server.go
This commit is contained in:
parent
e6ee5d688f
commit
e0a32a2ecb
|
@ -160,10 +160,11 @@ func (o *OrionService) OwrtConnectWifiInterface(r *http.Request,
|
||||||
|
|
||||||
// OrionBox describe an fresh Orion box (base or rover)
|
// OrionBox describe an fresh Orion box (base or rover)
|
||||||
type OrionBox struct {
|
type OrionBox struct {
|
||||||
Address string
|
Address string
|
||||||
SSID string
|
NewAddress string
|
||||||
Security string
|
SSID string
|
||||||
WifiKey string
|
Security string
|
||||||
|
WifiKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
// OrionServer describe the Orion master server
|
// OrionServer describe the Orion master server
|
||||||
|
@ -200,9 +201,6 @@ func (o *OrionService) connectBox(box *OrionBox, server *OrionServer) error {
|
||||||
if box == nil {
|
if box == nil {
|
||||||
return fmt.Errorf("Box definitioni is emtpy")
|
return fmt.Errorf("Box definitioni is emtpy")
|
||||||
}
|
}
|
||||||
fmt.Println("DEBUG")
|
|
||||||
fmt.Println(server)
|
|
||||||
fmt.Println("DEBUG")
|
|
||||||
iface := server.ClientIface
|
iface := server.ClientIface
|
||||||
cells := iface.Scan()
|
cells := iface.Scan()
|
||||||
for _, cell := range cells {
|
for _, cell := range cells {
|
||||||
|
@ -217,59 +215,59 @@ func (o *OrionService) connectBox(box *OrionBox, server *OrionServer) error {
|
||||||
if dhres.CmdRes.ReturnCode != 0 {
|
if dhres.CmdRes.ReturnCode != 0 {
|
||||||
return fmt.Errorf("%s\n%s", cn.Stdout, cn.Stderr)
|
return fmt.Errorf("%s\n%s", cn.Stdout, cn.Stderr)
|
||||||
}
|
}
|
||||||
|
re := o.UCI.Reload()
|
||||||
|
if re.ReturnCode != 0 {
|
||||||
|
return fmt.Errorf("%s\n%s", re.Stdout, re.Stderr)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fmt.Errorf("Wifi cell with SSID %s is not available", box.SSID)
|
return fmt.Errorf("Wifi cell with SSID %s is not available", box.SSID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateOrionBox starts provisionning process for an Orion box (base or rover)
|
func (o *OrionService) discoverService() ([]emlid.Service, error) {
|
||||||
func (o *OrionService) UpdateOrionBox(r *http.Request,
|
service, err := emlid.Discover(20 * time.Second)
|
||||||
args *UpdateOrionBoxArgs,
|
if err != nil {
|
||||||
reply *UpdateOrionBoxResponse) error {
|
return service, err
|
||||||
|
}
|
||||||
|
if len(service) == 0 {
|
||||||
|
return service, fmt.Errorf("Discovery don't find any Orion Box")
|
||||||
|
}
|
||||||
|
return service, err
|
||||||
|
|
||||||
if err := o.connectBox(args.Box, args.Server); err != nil {
|
}
|
||||||
reply.Errors = append(reply.Errors, err.Error())
|
|
||||||
|
// setupMasterWifi connects to the box, add wifi network and join it !
|
||||||
|
func (o *OrionService) setupMasterWifi(rqContext context.Context, box *OrionBox, server *OrionServer) error {
|
||||||
|
if err := o.connectBox(box, server); err != nil {
|
||||||
return errors.Wrap(err, "Connect to box failed")
|
return errors.Wrap(err, "Connect to box failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
re := o.UCI.Reload()
|
if server.Security == "" {
|
||||||
if re.ReturnCode != 0 {
|
server.Security = string(updater.SecurityWPAPSK)
|
||||||
return fmt.Errorf("%s\n%s", re.Stdout, re.Stderr)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ssid := args.Server.SSID
|
var boxCli *updater.Client
|
||||||
security := args.Server.Security
|
service, err := o.discoverService()
|
||||||
wifiKey := args.Server.WifiKey
|
if err != nil {
|
||||||
|
boxCli = updater.NewClient(
|
||||||
if security == "" {
|
emlid.WithService(service[0]),
|
||||||
security = string(updater.SecurityWPAPSK)
|
)
|
||||||
|
} else {
|
||||||
|
boxCli = updater.NewClient(
|
||||||
|
emlid.WithEndpoint(box.Address, 80),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
//services, err := emlid.Discover(20 * time.Second)
|
|
||||||
//if err != nil {
|
|
||||||
// return err
|
|
||||||
//}
|
|
||||||
|
|
||||||
//if len(services) == 0 {
|
|
||||||
// return fmt.Errorf("Discovery don't find any Orion Box")
|
|
||||||
//}
|
|
||||||
|
|
||||||
//fmt.Println(services)
|
|
||||||
boxCli := updater.NewClient(
|
|
||||||
//emlid.WithService(services[0]),
|
|
||||||
emlid.WithEndpoint(args.Box.Address, 80),
|
|
||||||
)
|
|
||||||
|
|
||||||
if err := boxCli.Connect(); err != nil {
|
if err := boxCli.Connect(); err != nil {
|
||||||
return errors.Wrap(err, "Box connect to master failed")
|
return errors.Wrap(err, "Connecting to Box failed")
|
||||||
}
|
}
|
||||||
defer boxCli.Close()
|
defer boxCli.Close()
|
||||||
|
|
||||||
ctx, addWifiCancel := context.WithTimeout(r.Context(), 55*time.Second)
|
ctx, addWifiCancel := context.WithTimeout(rqContext, 55*time.Second)
|
||||||
defer addWifiCancel()
|
defer addWifiCancel()
|
||||||
|
|
||||||
done, err := boxCli.AddWifiNetwork(ctx, ssid, updater.WifiSecurity(security), wifiKey)
|
done, err := boxCli.AddWifiNetwork(ctx, server.SSID, updater.WifiSecurity(server.Security), server.WifiKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "AddWifiNetworkFailed")
|
return errors.Wrap(err, "AddWifiNetworkFailed")
|
||||||
}
|
}
|
||||||
|
@ -281,11 +279,32 @@ func (o *OrionService) UpdateOrionBox(r *http.Request,
|
||||||
ctx, joinWifiCancel := context.WithTimeout(ctx, 55*time.Second)
|
ctx, joinWifiCancel := context.WithTimeout(ctx, 55*time.Second)
|
||||||
defer joinWifiCancel()
|
defer joinWifiCancel()
|
||||||
|
|
||||||
if err := boxCli.JoinWifiNetwork(ctx, ssid, true); err != nil {
|
if err := boxCli.JoinWifiNetwork(ctx, server.SSID, true); err != nil {
|
||||||
return errors.Wrap(err, "Time sync failed")
|
return errors.Wrap(err, "Time sync failed")
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
ctx, timeSyncedCancel := context.WithTimeout(r.Context(), 55*time.Second)
|
// updateAndReboot connects to the box with the New adress and run basic tests on version dans updates
|
||||||
|
func (o *OrionService) updateAndReboot(rqContext context.Context, box *OrionBox, server *OrionServer, reply *UpdateOrionBoxResponse) error {
|
||||||
|
var boxCli *updater.Client
|
||||||
|
service, err := o.discoverService()
|
||||||
|
if err != nil {
|
||||||
|
boxCli = updater.NewClient(
|
||||||
|
emlid.WithService(service[0]),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
boxCli = updater.NewClient(
|
||||||
|
emlid.WithEndpoint(box.NewAddress, 80),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := boxCli.Connect(); err != nil {
|
||||||
|
return errors.Wrap(err, "Connecting to Box on master wifi network failed")
|
||||||
|
}
|
||||||
|
defer boxCli.Close()
|
||||||
|
|
||||||
|
ctx, timeSyncedCancel := context.WithTimeout(rqContext, 55*time.Second)
|
||||||
defer timeSyncedCancel()
|
defer timeSyncedCancel()
|
||||||
synced, err := boxCli.TimeSynced(ctx)
|
synced, err := boxCli.TimeSynced(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -293,7 +312,7 @@ func (o *OrionService) UpdateOrionBox(r *http.Request,
|
||||||
}
|
}
|
||||||
reply.Synced = synced
|
reply.Synced = synced
|
||||||
|
|
||||||
ctx, reachviewVersionCancel := context.WithTimeout(r.Context(), 55*time.Second)
|
ctx, reachviewVersionCancel := context.WithTimeout(rqContext, 55*time.Second)
|
||||||
defer reachviewVersionCancel()
|
defer reachviewVersionCancel()
|
||||||
version, err := boxCli.ReachViewVersion(ctx)
|
version, err := boxCli.ReachViewVersion(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -301,9 +320,22 @@ func (o *OrionService) UpdateOrionBox(r *http.Request,
|
||||||
}
|
}
|
||||||
reply.Version = version
|
reply.Version = version
|
||||||
|
|
||||||
ctx, rebootCancel := context.WithTimeout(r.Context(), 55*time.Second)
|
ctx, rebootCancel := context.WithTimeout(rqContext, 55*time.Second)
|
||||||
defer rebootCancel()
|
defer rebootCancel()
|
||||||
return boxCli.RebootNow(ctx, true)
|
return boxCli.RebootNow(ctx, true)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateOrionBox starts provisionning process for an Orion box (base or rover)
|
||||||
|
func (o *OrionService) UpdateOrionBox(r *http.Request,
|
||||||
|
args *UpdateOrionBoxArgs,
|
||||||
|
reply *UpdateOrionBoxResponse) error {
|
||||||
|
|
||||||
|
if err := o.setupMasterWifi(r.Context(), args.Box, args.Server); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return o.updateAndReboot(r.Context(), args.Box, args.Server, reply)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer returns a new configured JSON-RPC server
|
// NewServer returns a new configured JSON-RPC server
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
POST http://192.168.100.1:8888/rpc HTTP/1.1
|
||||||
|
content-type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"method": "Orion.OwrtCreateWifiInterface",
|
||||||
|
"params": [ {
|
||||||
|
"Iface": {
|
||||||
|
"Name": "PyxisNetwork",
|
||||||
|
"Network": "lan",
|
||||||
|
"Index": 0,
|
||||||
|
"Ssid": "Pyxis",
|
||||||
|
"SysDevName": "wlan0",
|
||||||
|
"Encryption": "psk2",
|
||||||
|
"Key": "A2Z3E4R5T6Y7U",
|
||||||
|
"Device": "radio0",
|
||||||
|
"DevicePath": "soc/soc:pcie/pci0000:00/0000:00:01.0/0000:01:00.0",
|
||||||
|
"Mode": "ap"
|
||||||
|
},
|
||||||
|
"Cleanup": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
###
|
||||||
|
POST http://192.168.100.1:8888/rpc HTTP/1.1
|
||||||
|
content-type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"method": "Orion.OwrtCreateWifiInterface",
|
||||||
|
"params": [ {
|
||||||
|
"Iface": {
|
||||||
|
"Name": "PyxisClient",
|
||||||
|
"Index": 1,
|
||||||
|
"SysDevName": "wlan1",
|
||||||
|
"Device": "radio1",
|
||||||
|
"DevicePath": "soc/soc:pcie/pci0000:00/0000:00:02.0/0000:02:00.0",
|
||||||
|
"Mode": "sta"
|
||||||
|
},
|
||||||
|
"Cleanup": false
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
|
||||||
|
###
|
||||||
|
POST http://192.168.100.1:8888/rpc HTTP/1.1
|
||||||
|
content-type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"method": "Orion.UpdateOrionBox",
|
||||||
|
"params": [ {
|
||||||
|
"Box": {
|
||||||
|
"Address": "192.168.42.1",
|
||||||
|
"NewAddress": "192.168.100.213",
|
||||||
|
"Security": "",
|
||||||
|
"SSID" : "reach:2a:03",
|
||||||
|
"WifiKey": "emlidreach"
|
||||||
|
},
|
||||||
|
"Server": {
|
||||||
|
"Address": "192.168.42.1",
|
||||||
|
"SSID": "Pyxis",
|
||||||
|
"Security": "",
|
||||||
|
"WifiKey": "A2Z3E4R5T6Y7U",
|
||||||
|
"ClientIface" : {
|
||||||
|
"Name": "PyxisClient",
|
||||||
|
"Index": 1,
|
||||||
|
"SysDevName": "wlan1",
|
||||||
|
"Device": "radio1",
|
||||||
|
"DevicePath": "soc/soc:pcie/pci0000:00/0000:00:02.0/0000:02:00.0",
|
||||||
|
"Mode": "sta"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} ]
|
||||||
|
}
|
Reference in New Issue