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 ParseSearchContent(search string) *models.Deviceinfo {
	beego.Debug("searchCondition=", search)

	searchInfo := make(map[string]string)
	searchs := strings.Split(search, "&&")
	for _, item := range searchs {
		if item == "" {
			continue
		} else {
			info := strings.Split(item, "=")
			searchInfo[info[0]] = info[1]
		}
	}
	beego.Debug("searchInfo=", searchInfo)

	ds_flag := false

	var device models.Deviceinfo
	for key, value := range searchInfo {
		//去掉所有空格
		key = strings.Replace(key, " ", "", -1)
		value = strings.Replace(value, " ", "", -1)
		beego.Debug("key=", key, "value=", value)

		switch key {
		case "mac":
			device.Mac = value
		case "ip":
			device.IpAddress = value
		case "dsn":
			device.HostSn = value
		case "lc":
			device.IpLocation = value
		case "fr":
			if value != "" {
				t, err := time.Parse(TimeFormat, value)
				if err != nil {
					beego.Error(err)
				}
				device.FirstRegistrationTime = t
			}
		case "lr":
			if value != "" {
				t, err := time.Parse(TimeFormat, value)
				if err != nil {
					beego.Error(err)
				}
				device.LastRegistrationTime = t
			}
		case "fw":
			device.FirmwareVersion = value
		case "ds":
			ds_flag = true
			device.State, _ = strconv.Atoi(value)
		default:
		}
	}
	if !ds_flag {
		device.State = -1
	}
	return &device
}