bus_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package bus
  2. import (
  3. "encoding/hex"
  4. "reflect"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/ammeter/util"
  10. "github.com/yuguorong/go/log"
  11. )
  12. var dbgChan IChannel = nil
  13. var dbgBus IBus = nil
  14. var dbgtx = []byte{
  15. 0xfe, 0xfe, 0xfe, 0xfe, 0x68, 0x04, 0x00, 0x42, 0x05, 0x08, 0x21, 0x68, 0x11, 0x04, 0x33, 0x33, 0x33, 0x33, 0x25, 0x16,
  16. }
  17. //fefefefe68040042050821681104333333332516
  18. func testrecive(chnExit chan bool, r []chan interface{}) {
  19. tmr := time.NewTimer(time.Second * 10)
  20. tmr.Stop()
  21. loop := true
  22. for loop {
  23. select {
  24. case msg := <-r[0]:
  25. switch reflect.TypeOf(msg).Kind() {
  26. case reflect.Slice:
  27. b := msg.([]byte)
  28. log.Info(hex.EncodeToString(b))
  29. if b[0] == 0x23 && b[1] == 0x23 {
  30. log.Info(string(b))
  31. }
  32. case reflect.String:
  33. log.Info(msg.(string))
  34. tmr.Reset(time.Second * 10)
  35. }
  36. case <-chnExit:
  37. loop = false
  38. case <-tmr.C:
  39. if dbgChan != nil && dbgBus != nil {
  40. log.Info(dbgChan.ID(), " in timer command send")
  41. dbgBus.Send(dbgChan, dbgtx)
  42. tmr.Reset(time.Second * 20)
  43. }
  44. }
  45. }
  46. }
  47. func mountbus(name string, param ...interface{}) IBus {
  48. return MountBus(name, param)
  49. }
  50. func testDispath(buf []byte, param interface{}) interface{} {
  51. log.Info(string(buf))
  52. log.Info(hex.EncodeToString(buf))
  53. if hex.EncodeToString(buf) == "9521003697" {
  54. return hex.EncodeToString(buf)
  55. }
  56. return nil
  57. }
  58. var deviceList map[string][]string
  59. func TestDtuServer(T *testing.T) {
  60. deviceList = map[string][]string{
  61. "9521003872": {"2108", "05420005", "05420003", "05420002"}, //6f
  62. "9521003712": {"2108", "05420001"}, //1F
  63. "9521003534": {"2108", "05420006"}, //-1F
  64. "9521003697": {"2108", "05420004"}, //5c
  65. }
  66. TransRadio := "150A/5A"
  67. TransDiv := 1.0
  68. Trans := strings.Split(TransRadio, "/")
  69. TransDeno, _ := strconv.ParseFloat(util.NumberFilter(Trans[0], util.NUMBER_CONTINUE), 64)
  70. if len(Trans) > 1 {
  71. TransDiv, _ = strconv.ParseFloat(util.NumberFilter(Trans[1], util.NUMBER_CONTINUE), 64)
  72. }
  73. log.Info(TransDeno, ",", TransDiv)
  74. router := make([]chan interface{}, 2)
  75. router[0] = make(chan interface{}, 16)
  76. router[1] = make(chan interface{}, 4)
  77. chExit := make(chan bool, 2)
  78. go testrecive(chExit, router)
  79. dbgBus = mountbus("dtuFtpServer", 10010)
  80. dbgBus.Init()
  81. dbgChan = dbgBus.MountChannel("9521003697", router)
  82. time.Sleep(time.Second * 40)
  83. chExit <- false
  84. dbgBus.FreeChannel(dbgChan)
  85. time.Sleep(time.Second * 10)
  86. dbgBus.Uninit()
  87. }