package rpc import ( "net/http" "forge.cadoles.com/Pyxis/orion/openwrt" "github.com/gorilla/rpc" "github.com/gorilla/rpc/json" ) // OrionService is the JSON-RPC API 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{} // UCIResponse is the response structure for exposed method OwrtListWifiDevices type UCIResponse struct { Devices []map[string]string } // 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 } // 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() 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 }