Ejemplo n.º 1
0
func generator(t time.Duration) {
	for {
		var s conn.Setting
		s.GetSetting()
		if s.Isconnected {
			var k ftprotocol.KeepAlive
			k.SessionKey = []byte(s.Sessionkey)
			k.Sequence = byte(s.Sequence[0])

			//			log.Printf("session key : %X, sequence : %X\n", k.SessionKey, k.Sequence)

			//		log.Printf("send keepalive : %X", k.Message())

			var m conn.Message
			m.Messagetype = conn.REQUEST
			m.Info = hex.EncodeToString(k.Message())
			m.Status = conn.NONE

			m.InsertMessage()

			//			sequence = IncreaseSeq(sequence)

		}

		time.Sleep(time.Millisecond * time.Duration(t))
		//		time.Sleep(time.Millisecond * time.Duration(s.Writeinterval))

	}

}
Ejemplo n.º 2
0
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.")

}
Ejemplo n.º 3
0
func (c *MesssageController) Generate() {
	messageid, _ := strconv.ParseInt(c.GetString("messageid"), 16, 64)
	var setting conn.Setting
	setting.GetSetting()

	switch messageid {
	case ftprotocol.REQUESTSESSION:
		var rs ftprotocol.RequestSession
		//	rs.SessionKey, _ = hex.DecodeString(c.GetString("sessionkey"))

		//	rs.SessionKey = []byte(strings.ToUpper(c.GetString("sessionkey")))
		rs.SessionKey = []byte(strings.ToUpper(setting.Sessionkey))

		//	beego.Debug("SessionKey", rs.SessionKey)
		//	rs.MessageID, _ = hex.DecodeString(c.GetString("messageid"))
		rs.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		//		beego.Debug("MessageID", rs.MessageID)
		//		rs.Sequence = byte(c.GetString("sequence")[0])
		rs.Sequence = byte(setting.Sequence[0])

		//		beego.Debug("Sequence", c.GetString("sequence"), []byte(c.GetString("sequence")))

		deviceid, _ := strconv.ParseUint(c.GetString("deviceid"), 16, 32)

		rs.DeviceID = uint32(deviceid)

		protocalver, _ := c.GetInt32("protocolver")
		rs.ProtocolVersion = uint32(protocalver)

		rs.NoAck = true

		out := rs.Message()
		//		beego.Debug(out)
		c.Ctx.WriteString(hex.EncodeToString(out))

	case ftprotocol.GETSENSOR:
		var g ftprotocol.GetSensor

		g.SessionKey = []byte(strings.ToUpper(setting.Sessionkey))
		g.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		g.Sequence = byte(setting.Sequence[0])

		g.IsBroadcast, _ = c.GetBool("bbroadcastperiod")

		bpvalue, _ := c.GetInt32("broadcastperiodvalue")
		g.Broadcastperiod = uint32(bpvalue)
		g.IsAllSensorData, _ = c.GetBool("isallsensordata")

		//		log.Printf("%v, %d, %v", g.IsBroadcast, g.Broadcastperiod, g.IsAllSensorData)

		out := g.Message()
		c.Ctx.WriteString(hex.EncodeToString(out))

	case ftprotocol.KEEPALIVE:
		var ka ftprotocol.KeepAlive
		/*
			ka.SessionKey = []byte(strings.ToUpper(c.GetString("sessionkey")))
			ka.Sequence = byte(c.GetString("sequence")[0])
			ka.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		*/
		ka.SessionKey = []byte(strings.ToUpper(setting.Sessionkey))
		ka.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		ka.Sequence = byte(setting.Sequence[0])

		out := ka.Message()
		//      beego.Debug(out)
		c.Ctx.WriteString(hex.EncodeToString(out))

	case ftprotocol.GETRUNTIME:
		var g ftprotocol.GetRunTime
		/*
			g.SessionKey = []byte(strings.ToUpper(c.GetString("sessionkey")))
			g.Sequence = byte(c.GetString("sequence")[0])
			g.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		*/
		g.SessionKey = []byte(strings.ToUpper(setting.Sessionkey))
		g.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		g.Sequence = byte(setting.Sequence[0])

		out := g.Message()
		//      beego.Debug(out)
		c.Ctx.WriteString(hex.EncodeToString(out))
	case ftprotocol.DEVICENAMEREQUEST:
		var g ftprotocol.DeviceNameRequest
		/*
			g.SessionKey = []byte(strings.ToUpper(c.GetString("sessionkey")))
			g.Sequence = byte(c.GetString("sequence")[0])
			g.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		*/
		g.SessionKey = []byte(strings.ToUpper(setting.Sessionkey))
		g.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		g.Sequence = byte(setting.Sequence[0])

		out := g.Message()
		//      beego.Debug(out)
		c.Ctx.WriteString(hex.EncodeToString(out))
	case ftprotocol.GETVERSIONSREQUEST:
		var g ftprotocol.GetVersionsRequest
		/*
			g.SessionKey = []byte(strings.ToUpper(c.GetString("sessionkey")))
			g.Sequence = byte(c.GetString("sequence")[0])
			g.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		*/
		g.SessionKey = []byte(strings.ToUpper(setting.Sessionkey))
		g.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		g.Sequence = byte(setting.Sequence[0])

		out := g.Message()
		//      beego.Debug(out)
		c.Ctx.WriteString(hex.EncodeToString(out))
	case ftprotocol.SCREENPRESS:
		var g ftprotocol.ScreenPress

		g.SessionKey = []byte(strings.ToUpper(setting.Sessionkey))
		g.MessageID = []byte(strings.ToUpper(c.GetString("messageid")))
		g.Sequence = byte(setting.Sequence[0])

		screenid, _ := c.GetInt32("screenid")
		g.ScreenID = uint32(screenid)

		coorx, _ := c.GetInt32("coorx")
		g.X = uint32(coorx)

		coory, _ := c.GetInt32("coory")
		g.Y = uint32(coory)

		log.Println(g)

		out := g.Message()

		log.Println(out)

		c.Ctx.WriteString(hex.EncodeToString(out))

	default:
		c.Ctx.WriteString("Invalid Message")

	}

	/*

		switch messageid {
		case ftprotocol.REQUESTSESSION:
			var rs ftprotocol.RequestSession
			rs.SessionKey, _ = hex.DecodeString(c.GetString("sessionkey"))
			rs.MessageID, _ = hex.DecodeString(c.GetString("messageid"))

			out := rs.Message()
			c.Ctx.WriteString(hex.EncodeToString(out))
		default:

		}
	*/
}
Ejemplo n.º 4
0
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)

		}

	}

}