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)) } }
func (c *MesssageController) IsSencMessage() { b, _ := c.GetBool("issend") var s conn.Setting s.Isconnected = b err := s.UpdateIsConnected() if err != nil { c.Ctx.WriteString(err.Error()) } else { c.Ctx.WriteString("ok") } }
func (c *MesssageController) SendPwdKey() { messageid := ftprotocol.SCREENPRESS var setting conn.Setting setting.GetSetting() switch messageid { 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) var m conn.Message m.Messagetype = conn.REQUEST m.Info = hex.EncodeToString(out) m.Status = conn.NONE log.Println(m.Info) m.InsertMessage() c.Ctx.WriteString("ok") default: c.Ctx.WriteString("Invalid Message") } }
func IncreaseOneSequence() { var s conn.Setting s.GetSetting() // log.Printf(" seq :%x", byte(s.Sequence[0])) s.Sequence = string(IncreaseSeq(byte(s.Sequence[0]))) // log.Printf("Sequence : %s", s.Sequence) s.UpdateSequence() }
func (c *MesssageController) UpdateSetting() { var s conn.Setting s.GetSetting() result := make(map[string]string) result["sessionkey"] = string(s.Sessionkey) result["sequence"] = string(s.Sequence) result["deviceid"] = strconv.Itoa(int(s.Deviceid)) result["protocolver"] = s.Protocolver result["sessionstatus"] = strconv.Itoa(int(s.Sessionstatus)) result["sessiontimeout"] = strconv.Itoa(int(s.Sessiontimeout)) result["messagetimeout"] = strconv.Itoa(int(s.Messagetimeout)) result["maxretrycount"] = strconv.Itoa(int(s.Maxretrycount)) result["devicename"] = s.Devicename result["sensorbroadcastperiod"] = strconv.Itoa(int(s.Sensorbroadcastperiod)) js, _ := json.Marshal(result) c.Ctx.WriteString(string(js)) }
func writer(t time.Duration) { // get one request for { var s conn.Setting s.GetSetting() // time.Sleep(time.Millisecond * time.Duration(s.Writeinterval)) time.Sleep(time.Millisecond * time.Duration(t)) if s.Isconnected { var m conn.Message err := m.GetOneRequest() if err != nil { // log.Println("writer:", err.Error()) continue } // log.Printf("get one request :%v", m) b, _ := hex.DecodeString(m.Info) err = comm.Writer(b) if err != nil { log.Println(err.Error()) continue } log.Printf("Send:%X", b) IncreaseOneSequence() if bSoftDelete { m.Status = conn.DELETED m.UpdateStatus() } else { m.DeleteMessage() // log.Println("the request is deleted.") } } } }
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: } */ }
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.") } } } }