示例#1
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.")
			}
		}

	}

}