func (c *MesssageController) Parse() { b, _ := hex.DecodeString(c.GetString("response")) if b[0] == 0x06 { b = b[2:] } msgid, err := ftprotocol.GetMessageID(b) result := make(map[string]string) if err != nil { result["reason"] = "invalid message format" } else { switch msgid { case ftprotocol.REQUESTSESSION: case ftprotocol.KEEPALIVE: case ftprotocol.REQUESTSESSIONRESPONSE: var response ftprotocol.RequestSessionResponse response.Parse(b) beego.Debug(response) beego.Debug(response.DeviceID) beego.Debug(response.ProtocolVersion) beego.Debug(response.SessionStatus) result["reason"] = "ok" result["sessionkey"] = string(response.SessionKey) result["sequence"] = string(response.Sequence) result["messageid"] = string(response.MessageID) result["deviceid"] = strconv.Itoa(int(response.DeviceID)) result["protocolver"] = strconv.Itoa(int(response.ProtocolVersion)) result["sessionstatus"] = strconv.Itoa(int(response.SessionStatus)) result["sessiontimeout"] = strconv.Itoa(int(response.SessionTimeout)) result["messagetimeout"] = strconv.Itoa(int(response.MessageTimeout)) result["maxretrycount"] = strconv.Itoa(int(response.MaxRetryCount)) default: result["reason"] = "invalid message id" } } js, _ := json.Marshal(result) c.Ctx.WriteString(string(js)) }
func (c *MesssageController) KeepAlive() { count := 0 var sequence byte = 0x30 // var ack []byte // open session for count < 1000 { if count == 0 { var rs ftprotocol.RequestSession rs.SessionKey = []byte{0x46, 0x46} rs.MessageID = []byte{0x31, 0x31} rs.Sequence = sequence rs.DeviceID = 0xD8 rs.ProtocolVersion = 0x2729 if count == 0 { rs.NoAck = true } input := rs.Message() output := comm.Sender(input) log.Printf("RECEIVE:%X", output) // beego.Debug(output) // sequence = IncreaseSeq(sequence) if len(output) > 50 { var res ftprotocol.RequestSessionResponse res.Parse(output) // sequence = res.Sequence log.Printf("%v:", res) log.Println("session status : ", res.SessionStatus) log.Println("device id : ", res.DeviceID) log.Println("protocol version : ", res.ProtocolVersion) } beego.Debug("sequence : ", sequence) if len(output) < 10 { if output[0] == 0x15 { // sequence = 0x30 } } // ack = []byte{0x06, sequence} // connection.Sender(ack) // beego.Debug(output) } var k ftprotocol.KeepAlive k.Sequence = sequence k.SessionKey = []byte{0x46, 0x46} k.MessageID = []byte{0x30, 0x30} kinput := k.Message() koutput := comm.Sender(kinput) log.Printf("RECEIVE KeepAlive:%X", koutput) sequence = IncreaseSeq(sequence) // ack = []byte{0x06, sequence} // connection.Sender(ack) /* var nr ftprotocol.DeviceNameRequest nr.Sequence = sequence nr.SessionKey = []byte{0x46, 0x46} nr.MessageID = []byte{0x31, 0x44} ninput := nr.Message() noutput := connection.Sender(ninput) beego.Debug(noutput) sequence = IncreaseSeq(sequence) */ var gs ftprotocol.GetSensor gs.Sequence = sequence gs.SessionKey = []byte{0x46, 0x46} gs.MessageID = []byte{0x33, 0x42} gs.IsBroadcast = true gs.Broadcastperiod = 100 gs.IsAllSensorData = true ginput := gs.Message() goutput := comm.Sender(ginput) log.Printf("RECEIVE GetSensor:%X", goutput) sequence = IncreaseSeq(sequence) /* var gcd ftprotocol.GetActivationHistogram gcd.Sequence = sequence gcd.SessionKey = []byte{0x46, 0x46} gcd.MessageID = []byte{0x32, 0x42} gcdinput := gcd.Message() gcdoutput := connection.Sender(gcdinput) beego.Debug(gcdoutput) sequence = IncreaseSeq(sequence) var gcd ftprotocol.GetCriticalData gcd.Sequence = sequence gcd.SessionKey = []byte{0x46, 0x46} gcd.MessageID = []byte{0x33, 0x37} gcd.DataStoreNameSize = true ginput := gcd.Message() goutput := connection.Sender(ginput) beego.Debug(goutput) sequence = IncreaseSeq(sequence) */ // ack = []byte{0x06, sequence} // connection.Sender(ack) count = count + 1 time.Sleep(time.Microsecond * 100) } c.Ctx.WriteString("done.") }
func Test_GetSensorData() { // var serial comm.Serial err := comm.OpenSerial() if err != nil { log.Println(err.Error()) } var sessionkey []byte = []byte{0x46, 0x46} var sequence byte = 0x30 for i := 0; ; i++ { if i == 0 { var r ftprotocol.RequestSession r.DeviceID = 0xD8 r.ProtocolVersion = 0x2729 r.SessionKey = sessionkey r.Sequence = sequence r.NoAck = true log.Printf("send requestsession : %X", r.Message()) comm.Writer(r.Message()) res, _ := comm.Reader() log.Printf("received requestsession:%X", res) if len(res) > 13 { var s ftprotocol.RequestSessionResponse s.Parse(res[2:]) sessionkey = s.SessionKey sequence = s.Sequence log.Println("session status:", s.SessionStatus) log.Println("device id:", s.DeviceID) log.Println("protocol version:", s.ProtocolVersion) } } var k ftprotocol.KeepAlive k.SessionKey = sessionkey k.Sequence = sequence log.Printf("send keepalive : %X", k.Message()) comm.Writer(k.Message()) res, _ := comm.Reader() log.Printf("received keepalive:%X", res) if len(res) > 1 { if res[0] == 0x06 { sequence = res[1] } } sequence = IncreaseSeq(sequence) if i == 10 { var s ftprotocol.GetSensor s.SessionKey = sessionkey s.Sequence = sequence s.IsBroadcast = true s.Broadcastperiod = 10 s.IsAllSensorData = true log.Printf("send GetSensor : %X", s.Message()) comm.Writer(s.Message()) res, _ := comm.Reader() log.Printf("received GetSensor:%X", res) sequence = IncreaseSeq(sequence) } if i == 20 { var s ftprotocol.GetSensor s.SessionKey = sessionkey s.Sequence = sequence s.IsBroadcast = false s.Broadcastperiod = 10 s.IsAllSensorData = true log.Printf("send GetSensor : %X", s.Message()) comm.Writer(s.Message()) res, _ := comm.Reader() log.Printf("received GetSensor:%X", res) sequence = IncreaseSeq(sequence) } } }