From ff6838dbe686da8e81765d169dc7a46205c5170a Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Tue, 2 Oct 2012 17:55:15 +0300 Subject: [PATCH] removed console dependency --- console/balance.go | 43 +++++++++++++++++++------------------------ rater/responder.go | 41 ++++++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/console/balance.go b/console/balance.go index facd8215c..2e4b90cfe 100644 --- a/console/balance.go +++ b/console/balance.go @@ -4,34 +4,18 @@ package console import ( "fmt" + "github.com/cgrates/cgrates/rater" ) func init() { commands["get_balance"] = &CmdGetBalance{} } -// Data being sent to the rpc responder -type ArgsGetBalance struct { - Tenant string - User string - Direction string - BalanceId string -} - -// Received back from query -type ReplyGetBalance struct { - Tenant string - User string - Direction string - BalanceId string - Balance float64 -} - // Commander implementation type CmdGetBalance struct { rpcMethod string - rpcParams *ArgsGetBalance - rpcResult *ReplyGetBalance + rpcParams *rater.CallDescriptor + rpcResult *rater.CallCost } // name should be exec's name @@ -41,8 +25,8 @@ func (self *CmdGetBalance) Usage(name string) string { // set param defaults func (self *CmdGetBalance) defaults() error { - self.rpcMethod = "Responder.GetBalance" - self.rpcParams = &ArgsGetBalance{BalanceId: "MONETARYOUT", Direction: "OUT"} + self.rpcMethod = "Responder.GetMonetary" + self.rpcParams = &rater.CallDescriptor{Direction: "OUT"} return nil } @@ -54,9 +38,20 @@ func (self *CmdGetBalance) FromArgs(args []string) error { // Args look OK, set defaults before going further self.defaults() self.rpcParams.Tenant = args[2] - self.rpcParams.User = args[3] + self.rpcParams.Account = args[3] if len(args) > 4 { - self.rpcParams.BalanceId = args[4] + switch args[4] { + case "MONETARY": + self.rpcMethod = "Responder.GetMonetary" + case "SMS": + self.rpcMethod = "Responder.GetSMS" + case "INETRNET": + self.rpcMethod = "Responder.GetInternet" + case "INTERNET_TIME": + self.rpcMethod = "Responder.GetInternetTime" + case "MINUTES": + self.rpcMethod = "Responder.GetMonetary" + } } if len(args) > 5 { self.rpcParams.Direction = args[5] @@ -73,6 +68,6 @@ func (self *CmdGetBalance) RpcParams() interface{} { } func (self *CmdGetBalance) RpcResult() interface{} { - self.rpcResult = &ReplyGetBalance{} + self.rpcResult = &rater.CallCost{} return self.rpcResult } diff --git a/rater/responder.go b/rater/responder.go index 3effa1997..7aa34370f 100644 --- a/rater/responder.go +++ b/rater/responder.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "github.com/cgrates/cgrates/balancer2go" - "github.com/cgrates/cgrates/console" "net/rpc" "reflect" "runtime" @@ -176,25 +175,49 @@ func (rs *Responder) Shutdown(arg string, reply *string) (err error) { return } +func (rs *Responder) GetMonetary(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, CREDIT, reply) + return err +} + +func (rs *Responder) GetSMS(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, SMS, reply) + return err +} + +func (rs *Responder) GetInternet(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, TRAFFIC, reply) + return err +} + +func (rs *Responder) GetInternetTime(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, TRAFFIC_TIME, reply) + return err +} + +func (rs *Responder) GetMinutes(arg CallDescriptor, reply *CallCost) (err error) { + err = rs.getBalance(&arg, MINUTES, reply) + return err +} + // Get balance -func (rs *Responder) GetBalance(arg console.ArgsGetBalance, reply *console.ReplyGetBalance) (err error) { +func (rs *Responder) getBalance(arg *CallDescriptor, balanceId string, reply *CallCost) (err error) { if rs.Bal != nil { - return fmt.Errorf("No balancer supported for this command right now") + return errors.New("No balancer supported for this command right now") } - ubKey := arg.Direction + ":" + arg.Tenant + ":" + arg.User + ubKey := arg.Direction + ":" + arg.Tenant + ":" + arg.Account userBalance, err := storageGetter.GetUserBalance(ubKey) if err != nil { return err } - if balance, balExists := userBalance.BalanceMap[arg.BalanceId]; !balExists { + if balance, balExists := userBalance.BalanceMap[balanceId+arg.Direction]; !balExists { // No match, balanceId not found - return fmt.Errorf("-BALANCE_NOT_FOUND") + return errors.New("-BALANCE_NOT_FOUND") } else { reply.Tenant = arg.Tenant - reply.User = arg.User + reply.Account = arg.Account reply.Direction = arg.Direction - reply.BalanceId = arg.BalanceId - reply.Balance = balance + reply.Cost = balance } return nil }