bus_test.go 2.1 KB

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