screens_service.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package protocol
  2. import (
  3. "fmt"
  4. "github.com/valyala/bytebufferpool"
  5. "strconv"
  6. "time"
  7. )
  8. // 认证登录 (这里设置了默认心跳时间为60s)
  9. func (x AuthDataPack) AuthLogin() *bytebufferpool.ByteBuffer {
  10. buf := bytebufferpool.Get()
  11. x.Head = []byte{0xFE, 0x5C, 0x4B, 0x89}
  12. x.Len = []byte{0x2A, 0x00, 0x00, 0x00}
  13. x.Type = 0x62
  14. x.Id = []byte{0x00, 0x00, 0x00, 0x00}
  15. x.DataLen = []byte{0x17, 0x00, 0x00, 0x00}
  16. x.Result = 0x31
  17. x.Separator1 = 0x23
  18. x.Separator2 = 0x23
  19. x.HeartBeat = []byte{0x30, 0x36, 0x30} //默认060, 60秒发一次心跳
  20. x.EndSeparator = 0x23
  21. x.End = []byte{0xFF, 0xFF}
  22. buf.Write(x.Head)
  23. buf.Write(x.Len)
  24. buf.WriteByte(x.Type)
  25. buf.Write(x.Id)
  26. buf.Write(x.DataLen)
  27. buf.WriteByte(x.Result)
  28. buf.WriteByte(x.Separator1)
  29. slice := GetNowDate()
  30. TmpData := make([]byte, len(slice))
  31. for i, s := range slice {
  32. TmpData[i] = byte(s) + 0x30
  33. }
  34. x.Time = TmpData
  35. buf.Write(x.Time)
  36. buf.WriteByte(x.Separator2)
  37. buf.Write(x.HeartBeat)
  38. buf.WriteByte(x.EndSeparator)
  39. buf.Write(x.End)
  40. //fmt.Println("TmpData: ", hex.EncodeToString(buf.Bytes()))
  41. //fmt.Println("TmpData: ", buf.Bytes())
  42. return buf
  43. }
  44. func GetNowDate() (dateSlice [16]int) {
  45. now := time.Now()
  46. year, month, day := now.Date()
  47. hour, minute, second := now.Clock()
  48. weekday := int(now.Weekday()) // 0表示星期天,1表示星期一,依此类推
  49. if weekday == 0 {
  50. weekday = 7 // 将星期天设为07
  51. }
  52. // 格式化
  53. Y := fmt.Sprintf("%d", year)
  54. M := fmt.Sprintf("%02d", month) // 将月份转换为两位数字
  55. D := fmt.Sprintf("%02d", day) // 将日期转换为两位数字
  56. H := fmt.Sprintf("%02d", hour) // 将小时转换为两位数字
  57. Min := fmt.Sprintf("%02d", minute) // 将分钟转换为两位数字
  58. S := fmt.Sprintf("%02d", second) // 将秒数转换为两位数字
  59. W := fmt.Sprintf("%02d", weekday) // 将星期几转换为两位数字
  60. dateStr := Y + M + D + W + H + Min + S
  61. for i, _ := range dateStr {
  62. num, _ := strconv.Atoi(string(dateStr[i]))
  63. dateSlice[i] = num
  64. }
  65. return dateSlice
  66. }