// Copyright 2019 github.com. All rights reserved. // Use of this source code is governed by github.com. package v1 import ( "errors" "fmt" "github.com/jaryhe/gopkgs/cache" "strconv" "strings" "lift-monitor/timetask" ) var MbTable = []uint16{ 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841, 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40, 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41, 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641, 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040, 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240, 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441, 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41, 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840, 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41, 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40, 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640, 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041, 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240, 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441, 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41, 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840, 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41, 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40, 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640, 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041, 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241, 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440, 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40, 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841, 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40, 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41, 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641, 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040} func CheckSum(data []byte) uint16 { var crc16 uint16 crc16 = 0xffff for _, v := range data { n := uint8(uint16(v) ^ crc16) crc16 >>= 8 crc16 ^= MbTable[n] } return crc16 } var crc8_tab = []byte{ 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62,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x63, 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83, 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3, } func Crc8(buf []byte) byte { var crc byte crc = 0 var index = 0 for ; index < len(buf); index++ { tempByte := buf[index] crc = crc8_tab[(crc^tempByte)&0xff] & 0xff } return crc } func LiftParseMqtt(src []byte) (result []byte, completed bool) { length := len(src) if length < 44 { return nil, false } if src[0] != 0xA5 || src[1] != 0x5A { return nil, false } if src[length-4] != 0xcc || src[length-3] != 0x33 || src[length-2] != 0xc3 || src[length-1] != 0x3c { return nil, false } return src, true } func LiftParseUdp(src []byte) (result []byte, left []byte, completed bool, err error) { hexPrintf(src) length := len(src) if length < 44 { return nil, src, false, nil } if src[0] != 0xA5 || src[1] != 0x5A { return nil, nil, false, errors.New("data error1") } infoLength := int(src[37])<<8 + int(src[38]) if length < infoLength+44 { return nil, src, false, nil } if infoLength > 1024 { return nil, nil, false, errors.New("data too long") } if src[infoLength+40] != 0xcc || src[infoLength+41] != 0x33 || src[infoLength+42] != 0xc3 || src[infoLength+43] != 0x3c { return nil, nil, false, errors.New("data error2") } return src[:infoLength+44], src[infoLength+44:], true, nil } func hexPrintf(data []byte) { fmt.Printf("lenght:%d\n", len(data)) for _, v := range data { fmt.Printf("0x%02x,", v) } fmt.Printf("\n") } func LiftParseTcp(src []byte) (result []byte, left []byte, completed bool, err error) { return LiftParseUdp(src) } func parseFloat(data string) float64 { if data == "" { return 0 } ret, _ := strconv.ParseFloat(data, 64) return ret } const ( // 身份认证请求 LiftFrameAuthRequest = 0x00 LiftFrameAuthResponse = 0x01 // 基本属性信息 LiftFrameBaseInfoRequest = 0x02 LiftFrameBaseInfoResponse = 0x03 // 传感器标定信息 LiftFrameSensorConfRequest = 0x04 LiftFrameSensorConfResponse = 0x05 // 报警设置信息 LiftFrameAlarmConfRequest = 0x06 LiftFrameAlarmConfResponse = 0x07 // 限位设置信息 LiftFramePositionConfRequest = 0x08 LiftFramePositionConfResponse = 0x09 // 实时数据信息 LiftFrameRealtimeDataRequest = 0x0a LiftFrameRealtimeDataResponse = 0x0b // 报警信息 LiftFrameAlarmRequest = 0x0e LiftFrameAlarmResponse = 0x0f // 工作循环上报 LiftFrameLoopDataRequest = 0x0c LiftFrameLoopDataResponse = 0x0d ) var LiftFrameMap = map[byte]func(string, byte, byte, []byte) ([]byte, error){ LiftFrameAuthRequest: LiftFramAuthRequestHandle, LiftFrameBaseInfoRequest: LiftFrameBaseInfoRequestHandle, //LiftFrameSensorConfRequest: LiftFrameSensorConfRequestHandle, LiftFrameAlarmConfRequest: LiftFrameAlarmConfRequestHandle, //LiftFramePositionConfRequest: LiftFramePositionConfRequestHandle, LiftFrameRealtimeDataRequest: LiftFrameRealtimeDataRequestHandle, LiftFrameAlarmRequest: LiftFrameAlarmRequestHandle, LiftFrameLoopDataRequest: LiftFrameLoopDataRequestHandle, } func makeResponse(sn string, liftNo byte, frameType byte, version byte, data []byte) []byte { ret := []byte{} // 开始标志 start := []byte{0xA5, 0x5A} ret = append(ret, start...) // 版本 ret = append(ret, version) // 帧类型 ret = append(ret, frameType) // 塔机编号 ret = append(ret, liftNo) // 设备代码 ret = append(ret, []byte(sn)...) if len(sn) < 32 { // 填充空格 spaceLength := 32 - len(sn) for i := 0; i < spaceLength; i++ { ret = append(ret, ' ') } } // 信息字段长度 dataLength := int8(len(data)) ret = append(ret, byte(dataLength>>8)) ret = append(ret, byte(dataLength)) // 信息段 ret = append(ret, data...) // crc crc := Crc8(data) ret = append(ret, byte(crc)) // 尾部 ret = append(ret, 0xcc) ret = append(ret, 0x33) ret = append(ret, 0xc3) ret = append(ret, 0x3c) go timetask.UpdateLiftTime(sn) return ret } func checkAuth(sn string) (int64, string) { key := fmt.Sprintf("lift-%s", sn) str, _ := cache.Redis().Get(key) array := strings.Split(str, "----") if len(array) < 2 { return 0, "" } projectId, _ := strconv.ParseInt(array[0], 10, 64) if projectId > 0 { authCache(sn, projectId, array[1]) } return projectId, array[1] } func authCache(sn string, projectId int64, name string) { key := fmt.Sprintf("lift-%s", sn) cache.Redis().SetEx(key, 3600, fmt.Sprintf("%d----%s", projectId, name)) } func LiftFrameParse(data []byte) ([]byte, string, error) { dataLength := len(data) // 版本号 version := data[2] // 帧类型 frameType := data[3] // 塔机编号 liftNo := data[4] // 设备sn码 sn := string(data[5:37]) // 信息段长度 infolength := int(data[37])<<8 + int(data[38]) // crc crc := Crc8(data[39 : 39+int(infolength)]) originCrc := data[dataLength-5] if crc != originCrc { return nil, sn, errors.New("crc error") } // 帧类型检查 handler, ok := LiftFrameMap[frameType] if ok == false { return nil, sn, errors.New(fmt.Sprintf("unknown frame %x", frameType)) } sn = strings.TrimSpace(sn) // 帧处理 res, err := handler(sn, liftNo, version, data[39:39+infolength]) return res, sn, err }