device_package_parse.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package v1
  4. import (
  5. "errors"
  6. "fmt"
  7. "github.com/jaryhe/gopkgs/cache"
  8. "strconv"
  9. "strings"
  10. "lift-monitor/timetask"
  11. )
  12. var MbTable = []uint16{
  13. 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
  14. 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
  15. 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
  16. 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
  17. 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
  18. 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
  19. 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
  20. 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
  21. 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
  22. 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
  23. 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
  24. 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
  25. 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
  26. 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
  27. 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
  28. 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
  29. 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
  30. 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
  31. 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
  32. 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
  33. 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
  34. 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
  35. 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
  36. 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
  37. 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
  38. 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
  39. 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
  40. 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
  41. 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
  42. 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
  43. 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
  44. 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040}
  45. func CheckSum(data []byte) uint16 {
  46. var crc16 uint16
  47. crc16 = 0xffff
  48. for _, v := range data {
  49. n := uint8(uint16(v) ^ crc16)
  50. crc16 >>= 8
  51. crc16 ^= MbTable[n]
  52. }
  53. return crc16
  54. }
  55. var crc8_tab = []byte{
  56. 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23,
  57. 0x2a, 0x2d,
  58. 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53,
  59. 0x5a, 0x5d,
  60. 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5, 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3,
  61. 0xca, 0xcd,
  62. 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85, 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3,
  63. 0xba, 0xbd,
  64. 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2, 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4,
  65. 0xed, 0xea,
  66. 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2, 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94,
  67. 0x9d, 0x9a,
  68. 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32, 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04,
  69. 0x0d, 0x0a,
  70. 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42, 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74,
  71. 0x7d, 0x7a,
  72. 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c, 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa,
  73. 0xa3, 0xa4,
  74. 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec, 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda,
  75. 0xd3, 0xd4,
  76. 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c, 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a,
  77. 0x43, 0x44,
  78. 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c, 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a,
  79. 0x33, 0x34,
  80. 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b, 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d,
  81. 0x64, 0x63,
  82. 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d,
  83. 0x14, 0x13,
  84. 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d,
  85. 0x84, 0x83,
  86. 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd,
  87. 0xf4, 0xf3,
  88. }
  89. func Crc8(buf []byte) byte {
  90. var crc byte
  91. crc = 0
  92. var index = 0
  93. for ; index < len(buf); index++ {
  94. tempByte := buf[index]
  95. crc = crc8_tab[(crc^tempByte)&0xff] & 0xff
  96. }
  97. return crc
  98. }
  99. func LiftParseMqtt(src []byte) (result []byte, completed bool) {
  100. length := len(src)
  101. if length < 44 {
  102. return nil, false
  103. }
  104. if src[0] != 0xA5 || src[1] != 0x5A {
  105. return nil, false
  106. }
  107. if src[length-4] != 0xcc || src[length-3] != 0x33 || src[length-2] != 0xc3 || src[length-1] != 0x3c {
  108. return nil, false
  109. }
  110. return src, true
  111. }
  112. func LiftParseUdp(src []byte) (result []byte, left []byte, completed bool, err error) {
  113. hexPrintf(src)
  114. length := len(src)
  115. if length < 44 {
  116. return nil, src, false, nil
  117. }
  118. if src[0] != 0xA5 || src[1] != 0x5A {
  119. return nil, nil, false, errors.New("data error1")
  120. }
  121. infoLength := int(src[37])<<8 + int(src[38])
  122. if length < infoLength+44 {
  123. return nil, src, false, nil
  124. }
  125. if infoLength > 1024 {
  126. return nil, nil, false, errors.New("data too long")
  127. }
  128. if src[infoLength+40] != 0xcc ||
  129. src[infoLength+41] != 0x33 ||
  130. src[infoLength+42] != 0xc3 ||
  131. src[infoLength+43] != 0x3c {
  132. return nil, nil, false, errors.New("data error2")
  133. }
  134. return src[:infoLength+44], src[infoLength+44:], true, nil
  135. }
  136. func hexPrintf(data []byte) {
  137. fmt.Printf("lenght:%d\n", len(data))
  138. for _, v := range data {
  139. fmt.Printf("0x%02x,", v)
  140. }
  141. fmt.Printf("\n")
  142. }
  143. func LiftParseTcp(src []byte) (result []byte, left []byte, completed bool, err error) {
  144. return LiftParseUdp(src)
  145. }
  146. func parseFloat(data string) float64 {
  147. if data == "" {
  148. return 0
  149. }
  150. ret, _ := strconv.ParseFloat(data, 64)
  151. return ret
  152. }
  153. const (
  154. // 身份认证请求
  155. LiftFrameAuthRequest = 0x00
  156. LiftFrameAuthResponse = 0x01
  157. // 基本属性信息
  158. LiftFrameBaseInfoRequest = 0x02
  159. LiftFrameBaseInfoResponse = 0x03
  160. // 传感器标定信息
  161. LiftFrameSensorConfRequest = 0x04
  162. LiftFrameSensorConfResponse = 0x05
  163. // 报警设置信息
  164. LiftFrameAlarmConfRequest = 0x06
  165. LiftFrameAlarmConfResponse = 0x07
  166. // 限位设置信息
  167. LiftFramePositionConfRequest = 0x08
  168. LiftFramePositionConfResponse = 0x09
  169. // 实时数据信息
  170. LiftFrameRealtimeDataRequest = 0x0a
  171. LiftFrameRealtimeDataResponse = 0x0b
  172. // 报警信息
  173. LiftFrameAlarmRequest = 0x0e
  174. LiftFrameAlarmResponse = 0x0f
  175. // 工作循环上报
  176. LiftFrameLoopDataRequest = 0x0c
  177. LiftFrameLoopDataResponse = 0x0d
  178. )
  179. var LiftFrameMap = map[byte]func(string, byte, byte, []byte) ([]byte, error){
  180. LiftFrameAuthRequest: LiftFramAuthRequestHandle,
  181. LiftFrameBaseInfoRequest: LiftFrameBaseInfoRequestHandle,
  182. //LiftFrameSensorConfRequest: LiftFrameSensorConfRequestHandle,
  183. LiftFrameAlarmConfRequest: LiftFrameAlarmConfRequestHandle,
  184. //LiftFramePositionConfRequest: LiftFramePositionConfRequestHandle,
  185. LiftFrameRealtimeDataRequest: LiftFrameRealtimeDataRequestHandle,
  186. LiftFrameAlarmRequest: LiftFrameAlarmRequestHandle,
  187. LiftFrameLoopDataRequest: LiftFrameLoopDataRequestHandle,
  188. }
  189. func makeResponse(sn string, liftNo byte, frameType byte, version byte, data []byte) []byte {
  190. ret := []byte{}
  191. // 开始标志
  192. start := []byte{0xA5, 0x5A}
  193. ret = append(ret, start...)
  194. // 版本
  195. ret = append(ret, version)
  196. // 帧类型
  197. ret = append(ret, frameType)
  198. // 塔机编号
  199. ret = append(ret, liftNo)
  200. // 设备代码
  201. ret = append(ret, []byte(sn)...)
  202. if len(sn) < 32 {
  203. // 填充空格
  204. spaceLength := 32 - len(sn)
  205. for i := 0; i < spaceLength; i++ {
  206. ret = append(ret, ' ')
  207. }
  208. }
  209. // 信息字段长度
  210. dataLength := int8(len(data))
  211. ret = append(ret, byte(dataLength>>8))
  212. ret = append(ret, byte(dataLength))
  213. // 信息段
  214. ret = append(ret, data...)
  215. // crc
  216. crc := Crc8(data)
  217. ret = append(ret, byte(crc))
  218. // 尾部
  219. ret = append(ret, 0xcc)
  220. ret = append(ret, 0x33)
  221. ret = append(ret, 0xc3)
  222. ret = append(ret, 0x3c)
  223. go timetask.UpdateLiftTime(sn)
  224. return ret
  225. }
  226. func checkAuth(sn string) (int64, string) {
  227. key := fmt.Sprintf("lift-%s", sn)
  228. str, _ := cache.Redis().Get(key)
  229. array := strings.Split(str, "----")
  230. if len(array) < 2 {
  231. return 0, ""
  232. }
  233. projectId, _ := strconv.ParseInt(array[0], 10, 64)
  234. if projectId > 0 {
  235. authCache(sn, projectId, array[1])
  236. }
  237. return projectId, array[1]
  238. }
  239. func authCache(sn string, projectId int64, name string) {
  240. key := fmt.Sprintf("lift-%s", sn)
  241. cache.Redis().SetEx(key, 3600, fmt.Sprintf("%d----%s", projectId, name))
  242. }
  243. func LiftFrameParse(data []byte) ([]byte, string, error) {
  244. dataLength := len(data)
  245. // 版本号
  246. version := data[2]
  247. // 帧类型
  248. frameType := data[3]
  249. // 塔机编号
  250. liftNo := data[4]
  251. // 设备sn码
  252. sn := string(data[5:37])
  253. // 信息段长度
  254. infolength := int(data[37])<<8 + int(data[38])
  255. // crc
  256. crc := Crc8(data[39 : 39+int(infolength)])
  257. originCrc := data[dataLength-5]
  258. if crc != originCrc {
  259. return nil, sn, errors.New("crc error")
  260. }
  261. // 帧类型检查
  262. handler, ok := LiftFrameMap[frameType]
  263. if ok == false {
  264. return nil, sn, errors.New(fmt.Sprintf("unknown frame %x", frameType))
  265. }
  266. sn = strings.TrimSpace(sn)
  267. // 帧处理
  268. res, err := handler(sn, liftNo, version, data[39:39+infolength])
  269. return res, sn, err
  270. }