Example #1
0
func (this *HeartBeatController) Post() {
	ret := HeartData{}
	//用户认证
	uname, pwd, ok := this.Ctx.Request.BasicAuth()
	if !ok {
		beego.Info("get client  Request.BasicAuth failed!")
		ret.Code = -1
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	user := models.Userinfo{
		Username: uname,
		Password: pwd,
	}
	ok = models.CheckAccount(&user)
	if !ok {
		beego.Info("user/pwd not matched!")
		ret.Code = -1
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	beego.Info("user/pwd matched!")

	//接收请求信息
	beego.Debug("requestBody=", string(this.Ctx.Input.RequestBody))
	deviceinfo := models.Deviceinfo{}
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &deviceinfo)
	beego.Debug("deviceinfo=", deviceinfo)
	if err != nil {
		beego.Error(err)
		ret.Code = -2
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}

	//心跳接收成功,更新设备状态
	deviceinfo.State = 1
	deviceinfo.LastKeepaliveTime = time.Now()
	if !models.UpdateDeviceStatus(&deviceinfo) {
		ret.Code = -2
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}

	//插入Listener中
	device := DeviceListener{
		State:         1,
		LastAliveTime: time.Now(),
	}
	Listener[deviceinfo.Mac] = device

	//返回给设备处理结果
	ret.Code = 0
	writeContent, _ := json.Marshal(ret)
	this.Ctx.WriteString(string(writeContent))
}
Example #2
0
/*返回值
格式:{"code":0/-1/-2}
( 0) success
(-1) user/password error
(-2) other error
*/
func (this *UnRegisterController) Post() {
	ret := UnRegisterData{}
	//用户认证
	uname, pwd, ok := this.Ctx.Request.BasicAuth()
	if !ok {
		beego.Info("get client  Request.BasicAuth failed!")
		ret.Code = -1
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	user := models.Userinfo{
		Username: uname,
		Password: pwd,
	}
	ok = models.CheckAccount(&user)
	if !ok {
		beego.Info("user/pwd not matched!")
		ret.Code = -1
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	beego.Info("user/pwd matched!")

	//获取请求的json数据
	beego.Info("request body=", string(this.Ctx.Input.RequestBody))
	deviceinfo := models.Deviceinfo{}
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &deviceinfo)
	if err != nil {
		ret.Code = -2
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}

	//更新数据库中设备的状态为离线,并删除状态监听器中对应项
	deviceinfo.State = 0
	models.UpdateDeviceStatus(&deviceinfo)
	delete(Listener, deviceinfo.Mac)

	//返回注销成功
	ret.Code = 0
	writeContent, _ := json.Marshal(ret)
	this.Ctx.WriteString(string(writeContent))
	beego.Info(string(writeContent))
	return
}
Example #3
0
func init() {
	Listener = make(map[string]DeviceListener)

	go func() {
		for {
			for k, v := range Listener {
				beego.Debug("k=%s,v=%v", k, v)
				//判断是否超时
				if time.Now().Sub(v.LastAliveTime) >= time.Duration(TIMEOUT_INTERVAL)*time.Minute {
					//更新数据库中设备的状态为离线
					device := models.Deviceinfo{
						Mac:   k,
						State: 0,
					}
					models.UpdateDeviceStatus(&device)
					delete(Listener, k)
				}
			}
			beego.Debug("Listener GC runing...")
			time.Sleep(GC_INTERVAL * time.Minute)
		}
	}()
}
Example #4
0
func (this *GetCommandController) Post() {
	ret := CommandData{}
	//身份认证
	uname, pwd, ok := this.Ctx.Request.BasicAuth()
	if !ok {
		beego.Info("get client  Request.BasicAuth failed!")
		ret.Cmd = ""
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	user := models.Userinfo{
		Username: uname,
		Password: pwd,
	}
	ok = models.CheckAccount(&user)
	if !ok {
		beego.Info("user/pwd not matched!")
		ret.Cmd = ""
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	beego.Info("user/pwd matched!")

	//获取请求信息
	deviceinfo := models.Deviceinfo{
		State:             1,
		LastKeepaliveTime: time.Now(),
	}
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &deviceinfo)
	if err != nil {
		ret.Cmd = ""
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	//设备取命令的动作做为一次心跳,更新设备状态
	models.UpdateDeviceStatus(&deviceinfo)

	//插入Listener中
	device := DeviceListener{
		State:         1,
		LastAliveTime: time.Now(),
	}
	Listener[deviceinfo.Mac] = device

	//取命令
	command := models.Command{
		Executed: false,
	}
	json.Unmarshal(this.Ctx.Input.RequestBody, &command)
	ok, com := models.GetCommand(&command)
	if !ok {
		//该设备没有对应的命令,返回命令为空
		ret.Cmd = ""
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	beego.Info(com)

	//设备取出命令,更新状态为(等待下发命令)
	com.Executed = true
	ok = models.UpdateDeviceCommand(com)
	if !ok {
		ret.Cmd = ""
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}

	//更新操作记录状态为(已执行)
	/*
		record := models.OperationRecord{
			Mac:      com.Mac,
			Command:  com.Command,
			Executed: true,
			ExecTime: time.Now(),
		}*/
	record := com.Operecord
	record.Executed = true
	record.ExecTime = time.Now()
	ok = models.UpdateOperationRecord(record)
	if !ok {
		ret.Cmd = ""
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}

	//将命令发送给设备
	ret.Id = com.Id
	ret.Cmd = base.EncodeToString([]byte(com.Command))
	writeContent, _ := json.Marshal(ret)
	this.Ctx.WriteString(string(writeContent))
}