Skip to content

Commit

Permalink
add dialout and set_state
Browse files Browse the repository at this point in the history
  • Loading branch information
BreakDimbo committed Mar 28, 2018
1 parent e113a70 commit 038ff11
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 149 deletions.
51 changes: 39 additions & 12 deletions api/apier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"fmt"
"strings"

"github.com/satori/go.uuid"

Expand All @@ -12,19 +13,44 @@ import (

func Execute(cmd string, c *wbclient.Client) error {
msgID := uuid.NewV4().String()
switch cmd {
case "login":
switch strings.TrimSpace(cmd) {
case code.LoginMethod:
send(c, code.REQ, code.LoginMethod, msgID, &map[string]interface{}{
"agent_id": "1441",
"agent_id": "1443",
"app_id": "f781fd92-9e96-4195-7a7e-257307edb4ad",
"state": "idle",
"token": "tmp:token",
})
case "set_state_idle":
send(c, code.REQ, code.SetStateMethod, msgID, &map[string]interface{}{
"agent_id": "1443",
"app_id": "f781fd92-9e96-4195-7a7e-257307edb4ad",
"state": "idle",
"token": "tmp_token",
})
case "set_state_busy":
send(c, code.REQ, code.SetStateMethod, msgID, &map[string]interface{}{
"agent_id": "1443",
"app_id": "f781fd92-9e96-4195-7a7e-257307edb4ad",
"state": "busy",
"token": "tmp_token",
})

case "dialout":
send(c, code.REQ, code.DailoutMethod, msgID, &map[string]interface{}{
"agent_id": "1443",
"app_id": "f781fd92-9e96-4195-7a7e-257307edb4ad",
"called_display": "057126200670",
"caller": "92795089691002",
"called": "13240345451",
})
default:
fmt.Println("[Error] wrong api command not match")
}
return nil
}

func send(c *wbclient.Client, typ string, method string, msgID string, custom *map[string]interface{}) error {
func send(c *wbclient.Client, typ string, method string, msgID string, custom *map[string]interface{}) (err error) {
rep, err := NewReport(&NewParam{
SeqID: int32(1),
MsgID: msgID,
Expand All @@ -33,21 +59,22 @@ func send(c *wbclient.Client, typ string, method string, msgID string, custom *m
Custom: *custom,
})
if err != nil {
fmt.Errorf("new report error : %s", err)
return err
fmt.Printf("[Error] new report error : %s\n", err)
return
}
payload, err := rep.GetPayload()
if err != nil {
fmt.Errorf("reporter.GetPayload error: %s", err)
return err
fmt.Printf("reporter.GetPayload error: %s\n", err)
return
}

// 向 ws 写数据
err = c.SendMessage(payload)
if err != nil {
fmt.Errorf("ws write error: %s", err)
return err
fmt.Printf("ws write error: %s\n", err)
return
}
log.Debugf("send msg to sig: %s", payload)
return nil

log.Debugf("send msg to server: %s\n", payload)
return
}
10 changes: 0 additions & 10 deletions api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ type LoginReq struct {
Token string `json:"token"`
}

// LoginRep 登录响应
type LoginRep struct {
rpBase
UserID string `json:"user_id,omitempty"`
UID string `json:"uid,omitempty"`
CurState string `json:"cur_state,omitempty"`
Rcode string `json:"rcode,omitempty"`
Rdesc string `json:"rdesc,omitempty"`
}

func newLoginReq(rpb rpBase, params map[string]interface{}) (retRp *LoginReq, retErr error) {
defer func() {
if r := recover(); r != nil {
Expand Down
39 changes: 39 additions & 0 deletions api/originate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package api

import (
"encoding/json"
"fmt"
)

type OriginateReq struct {
rpBase
Caller string `json:"Caller" alias:"Caller"`
Called string `json:"DestNumber" alias:"DestNumber"`
CalledDisplay string `json:"SpNumber" alias:"SpNumber"`
AgentId string `json:"AgentId" alias:"AgentId"`
AppId string `json:"app_id" alias:"app_id"`
}

func newOriginate(rpb rpBase, params map[string]interface{}) (retRp *OriginateReq, retErr error) {
defer func() {
if r := recover(); r != nil {
fmt.Errorf("recover: %+v", r)
retErr = fmt.Errorf("recover : %+v", r)
retRp = nil
}
}()
rp := OriginateReq{
rpBase: rpb,
AgentId: params["agent_id"].(string),
CalledDisplay: params["called_display"].(string),
Called: params["called"].(string),
Caller: params["caller"].(string),
AppId: params["app_id"].(string),
}
return &rp, nil
}

// GetPayload : 返回需要发送的字符串
func (rp *OriginateReq) GetPayload() ([]byte, error) {
return json.Marshal(rp)
}
10 changes: 5 additions & 5 deletions api/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type NewParam struct {
func NewReport(param *NewParam) (Reporter, error) {
rpb := rpBase{
Ver: "1.0",
Ua: "stress-test",
Ua: "robot-dan",
SeqID: param.SeqID,
MsgID: param.MsgID,
MsgType: param.MsgType,
Expand All @@ -40,10 +40,10 @@ func NewReport(param *NewParam) (Reporter, error) {
switch {
case equal(code.REQ, code.LoginMethod, msgType, methodStr):
return newLoginReq(rpb, param.Custom)
// case equal(code.REQ, code.GetChannelTokenMethod, msgType, methodStr):
// return newGetChannelTokenReq(rpb, param.Custom)
// case equal(code.REQ, code.InviteMethod, msgType, methodStr):
// return newInviteReq(rpb, param.Custom)
case equal(code.REQ, code.SetStateMethod, msgType, methodStr):
return newSetState(rpb, param.Custom)
case equal(code.REQ, code.DailoutMethod, msgType, methodStr):
return newOriginate(rpb, param.Custom)
// case equal(code.ACK, code.InviteMethod, msgType, methodStr):
// return newInviteAck(rpb, param.Custom)
// case equal(code.REQ, code.AnswerMethod, msgType, methodStr):
Expand Down
38 changes: 38 additions & 0 deletions api/set_state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package api

import (
"encoding/json"
"fmt"
)

// SetStateReq 登录请求
type SetStateReq struct {
rpBase
AgentId string `json:"agent_id,omitempty"`
AppID string `json:"app_id,omitempty"`
State string `json:"state,omitempty"`
Token string `json:"token"`
}

func newSetState(rpb rpBase, params map[string]interface{}) (retRp *SetStateReq, retErr error) {
defer func() {
if r := recover(); r != nil {
fmt.Errorf("recover: %+v", r)
retErr = fmt.Errorf("recover : %+v", r)
retRp = nil
}
}()
rp := SetStateReq{
rpBase: rpb,
AgentId: params["agent_id"].(string),
AppID: params["app_id"].(string),
State: params["state"].(string),
Token: params["token"].(string),
}
return &rp, nil
}

// GetPayload : 返回需要发送的字符串
func (rp *SetStateReq) GetPayload() ([]byte, error) {
return json.Marshal(rp)
}
34 changes: 0 additions & 34 deletions api/start_media.go

This file was deleted.

34 changes: 0 additions & 34 deletions api/stop_media.go

This file was deleted.

39 changes: 5 additions & 34 deletions code/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,15 @@ package code

// 方法枚举值
const (
GetAttrMethod = "get_attr"
GetAttrAllMethod = "get_attr_all"
GetChannelTokenMethod = "get_channel_token"
GetStateMethod = "get_state"
GetUserStateMethod = "get_user_state"
LoginMethod = "login"
LogoutMethod = "logout"
SetAttrMethod = "set_attr"
SetStateMethod = "set_state"
StartMediaMethod = "start_media"
StopMediaMethod = "stop_media"
InviteMethod = "invite"
AnswerMethod = "answer"
ByeMethod = "bye"
CancelMethod = "cancel"

DisconnectMethod = "disconnect"
StartRecordMethod = "start_record"
StopRecordMethod = "stop_record"
LoginMethod = "login"
LogoutMethod = "logout"
SetStateMethod = "set_state"
DailoutMethod = "dailout"
)

var MethodList = []string{
GetAttrMethod,
GetAttrAllMethod,
GetChannelTokenMethod,
GetStateMethod,
GetUserStateMethod,
LoginMethod,
LogoutMethod,
SetAttrMethod,
SetStateMethod,
StartMediaMethod,
StopMediaMethod,
InviteMethod,
AnswerMethod,
ByeMethod,
CancelMethod,
DisconnectMethod,
StartRecordMethod,
StopRecordMethod,
DailoutMethod,
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"os"
"time"

"github.com/breakD/wb_client/api"
"github.com/breakD/wb_client/wbclient"
Expand All @@ -28,12 +29,17 @@ func main() {
c.Connect()

for c.WsConn() == nil {
// waiting for the ws connecting
time.Sleep(time.Second)
}

api.Execute("login", c)
fmt.Println("logging")
time.Sleep(time.Second)

for {
reader := bufio.NewReader(os.Stdin)
time.Sleep(time.Second)
fmt.Print("Enter api: ")
text, _ := reader.ReadString('\n')
fmt.Println(text)
Expand Down
Binary file modified wb_client
Binary file not shown.
Loading

0 comments on commit 038ff11

Please sign in to comment.