2018-09-24 08:49:21 +02:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2018-09-24 17:38:17 +02:00
|
|
|
"forge.cadoles.com/Pyxis/orion/openwrt"
|
2018-09-24 08:49:21 +02:00
|
|
|
"github.com/gorilla/rpc"
|
|
|
|
"github.com/gorilla/rpc/json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// OrionService is the JSON-RPC API
|
2018-09-24 17:38:17 +02:00
|
|
|
type OrionService struct {
|
|
|
|
UCI *openwrt.UCI
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOrionService create a new OrionService !
|
|
|
|
func NewOrionService() *OrionService {
|
|
|
|
uci := openwrt.NewUCI()
|
|
|
|
return &OrionService{
|
|
|
|
UCI: uci,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UCIArgs argument structure for exported method OwrtListWifiDevices
|
|
|
|
type UCIArgs struct{}
|
2018-09-24 08:49:21 +02:00
|
|
|
|
2018-09-24 17:38:17 +02:00
|
|
|
// UCIResponse is the response structure for exposed method OwrtListWifiDevices
|
|
|
|
type UCIResponse struct {
|
|
|
|
Devices []map[string]string
|
2018-09-24 08:49:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:38:17 +02:00
|
|
|
// OwrtListWifiDevices offers an RPC Method to list Wifi nics in a OpenWRT device.
|
|
|
|
func (o *OrionService) OwrtListWifiDevices(r *http.Request, args *UCIArgs, reply *UCIResponse) error {
|
|
|
|
o.UCI.LoadWirelessConf()
|
|
|
|
devs := o.UCI.GetWifiDevices()
|
|
|
|
reply.Devices = devs
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListIfaceArgs argument structure for exported method OwrtListWifiDevices
|
|
|
|
type ListIfaceArgs struct{}
|
|
|
|
|
|
|
|
// ListIfaceResponse is the response structure for exposed method OwrtListWifiDevices
|
|
|
|
type ListIfaceResponse struct {
|
|
|
|
Interfaces []*openwrt.UCIWirelessInterface
|
2018-09-24 08:49:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 17:38:17 +02:00
|
|
|
// OwrtListWifiInterfaces offers an RPC Method to list wifi interfaces in a OpenWRT device.
|
|
|
|
func (o *OrionService) OwrtListWifiInterfaces(r *http.Request, args *ListIfaceArgs, reply *ListIfaceResponse) error {
|
|
|
|
o.UCI.LoadWirelessConf()
|
|
|
|
reply.Interfaces = o.UCI.GetWifiIfaces()
|
2018-09-24 08:49:21 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer returns a new configured JSON-RPC server
|
|
|
|
func NewServer() *rpc.Server {
|
|
|
|
server := rpc.NewServer()
|
|
|
|
server.RegisterCodec(json.NewCodec(), "application/json")
|
|
|
|
if err := server.RegisterService(new(OrionService), "Orion"); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return server
|
|
|
|
}
|