예제 #1
0
파일: getcommand.go 프로젝트: tcdog001/LMS
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))
}
예제 #2
0
파일: getscript.go 프로젝트: tcdog001/LMS
func (this *GetScriptController) Post() {
	ret := GetScriptData{}
	//用户身份认证
	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!")

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

	//获取对应脚本信息
	sc, ok := models.GetScript(&script)
	if !ok {
		ret.Code = -2
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	beego.Debug(sc)
	//修改状态
	sc.Downloaded = true
	sc.DowonlodTime = time.Now()
	ok = models.UpdateScript(sc)
	if !ok {
		ret.Code = -2
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	} else {
		ret.Path = URL + sc.FilePath
	}
	//更新操作记录状态为(已执行)
	record := models.OperationRecord{
		Mac:      sc.Mac,
		Command:  "",
		Script:   sc.FilePath,
		Executed: true,
		ExecTime: time.Now(),
	}
	ok = models.UpdateOperationRecord(&record)
	if !ok {
		ret.Code = -2
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	//返回成功
	ret.Code = 0
	writeContent, _ := json.Marshal(ret)
	this.Ctx.WriteString(string(writeContent))
	beego.Info(string(writeContent))
	return
}