예제 #1
0
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))
}
예제 #2
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)

		}

	}

}
예제 #3
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.")

}
예제 #4
0
func worker(t time.Duration) {

	for {
		time.Sleep(time.Millisecond * t)

		//		log.Println("working....")
		var m conn.Message
		err := m.GetOneResponse()
		if err != nil {
			//		log.Println("worker:", err.Error())
			continue
			//			log.Printf("get one response :%v", m)
		}

		//		log.Println(m.Info)

		b, err := hex.DecodeString(m.Info)
		if err != nil {
			log.Println(err.Error())
			m.Status = conn.INVALID
			m.UpdateStatus()
			continue
		}
		var f ftprotocol.Frame
		//		log.Printf("%X", b)
		s, err := f.Parse(b)
		if err != nil {
			log.Printf("%X:%s\n", b, err.Error())
			//	m.DeleteMessage()
			m.Status = conn.INVALID
			m.UpdateStatus()

			continue
		}

		msgid, _ := strconv.ParseInt(string(f.MessageID), 16, 32)

		switch msgid {
		case ftprotocol.REQUESTSESSIONRESPONSE:
			var sessionres ftprotocol.RequestSessionResponse
			sessionres.Frame = f
			err := sessionres.ParseMessageData(f.MessageData)
			if err != nil {
				log.Println(err.Error())
				m.Status = conn.INVALID
				m.UpdateStatus()
				continue
			}

			var s conn.Setting
			s.Sessionstatus = sessionres.SessionStatus
			s.UpdateSessionStatus()

			s.Sessiontimeout = sessionres.SessionTimeout
			s.UpdateSessiontimeout()

			s.Deviceid = conn.DeviceID(sessionres.DeviceID)
			s.UpdateDeviceid()

			s.Protocolver = strconv.FormatUint(uint64(sessionres.ProtocolVersion), 10)
			s.UpdateProtocolVer()

			s.Sessionkey = string(sessionres.SessionKey)
			s.UpdateSessionKey()

			s.Sequence = string(sessionres.Sequence)
			s.UpdateSequence()

			s.Messagetimeout = sessionres.MessageTimeout
			s.UpdateMessagetimeout()

			s.Maxretrycount = sessionres.MaxRetryCount
			s.UpdateMaxretrycount()

		case ftprotocol.DEVICENAMERESPONSE:
			var res ftprotocol.DeviceNameResponse
			res.Frame = f
			err := res.ParseMessageData(f.MessageData)
			if err != nil {
				log.Println(err.Error())
				m.Status = conn.INVALID
				m.UpdateStatus()
				continue
			}
			var s conn.Setting
			//		log.Println("device  name : ", res.StringName)
			s.Devicename = res.StringName
			err = s.UpdateDevicename()
			if err != nil {
				log.Println("UpdateDevicename:", err.Error())
			}

		case ftprotocol.GETSENSORRESPONSE:
			var res ftprotocol.GetSensorResponse
			res.Frame = f
			err := res.ParseMessageData(f.MessageData)
			if err != nil {
				log.Println("GETSENSORRESPONSE:", err.Error())
				m.Status = conn.INVALID
				m.UpdateStatus()
				continue
			}
			var s conn.Setting
			s.Sensorbroadcastperiod = res.BroadcastPeriod
			err = s.UpdateSensorbroadcastperiod()
			if err != nil {
				log.Println("UpdateSensorbroadcastperiod :", err.Error())
			}

		case ftprotocol.DSP1SENSORDATA:
			var res ftprotocol.Dsp1SensorData
			res.Frame = f
			err := res.ParseMessageData(f.MessageData)
			if err != nil {
				log.Println("DSP1SENSORDATA:", err.Error())
				m.Status = conn.INVALID
				m.UpdateStatus()
				continue
			}
			var s conn.Sensordata
			s.Isvaliddata = res.IsValidData
			s.Sequencenumber = res.SequenceNumber
			s.Isactivatingflag = res.IsActivatingFlag
			s.Vavg = res.Vavg
			s.Iavg = res.Iavg
			s.Pavg = res.Pavg
			s.Vrms = res.Vrms
			s.Irms = res.Irms
			s.Viphase = res.Viphase
			s.Vpk = res.Vpk
			s.Ipk = res.Ipk
			s.Vcf = res.Vcf
			s.Icf = res.Icf
			s.Zload = res.Zload
			s.T1 = res.T1
			s.T2 = res.T2
			s.Leakage = res.Leakage
			s.Stimpos = res.Stimpos
			s.Stimneg = res.Stimneg
			s.Oltarget = res.Oltarget

			s.Createtime = time.Now()

			s.InsertSensordata()

		case ftprotocol.EMPTY:
			var s conn.Setting
			s.Sequence = string(f.Sequence)
			//			log.Printf("sequence : %X", s.Sequence)
			s.UpdateSequence()
		default:
			log.Printf("unsupported message : %X", msgid)

		}
		if len(s) > 0 {
			m.Info = s
			m.UpdateInfo()
		} else {
			//			log.Println(m)
			if bSoftDelete {
				m.Status = conn.DELETED
				m.UpdateStatus()
			} else {
				m.DeleteMessage()
				//		log.Println("the request is deleted.")
			}
		}

	}

}