コード例 #1
0
ファイル: heartbeat.go プロジェクト: tcdog001/LMS
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))
}
コード例 #2
0
ファイル: alarm.go プロジェクト: tcdog001/LMS
func (this *AlarmController) Post() {
	ret := AlarmData{}
	//用户身份认证
	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))
	alarm := models.Alarm{
		Time: time.Now(),
	}
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &alarm)
	if err != nil {
		beego.Error(err)
		ret.Code = -2
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}

	//将alarm信息写入数据库
	ok = models.AddAlarm(&alarm)
	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
}
コード例 #3
0
ファイル: unregister.go プロジェクト: tcdog001/szm_lms
/*返回值
格式:{"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
}
コード例 #4
0
ファイル: register.go プロジェクト: tcdog001/LMS
/*返回值
格式:{"code":0/-1/-2}
( 0) success
(-1) user/password error
(-2) other error
*/
func (this *RegisterController) Post() {
	//用户认证
	ret := RegisterData{}
	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!")

	//从请求中获取客户端ip
	/*
		clientAddr := this.Ctx.Request.RemoteAddr
		index := strings.Index(clientAddr, ":")
		clientIp := clientAddr[:index]
		beego.Debug("client IP=", clientIp)
	*/
	clientIp := this.Ctx.Input.IP()
	//获取ip归属地
	iplocation := SelectIpLocation(clientIp)
	beego.Debug("client IpLocation=", iplocation)

	//获取请求的json数据
	beego.Info("request body=", string(this.Ctx.Input.RequestBody))
	deviceinfo := models.Deviceinfo{
		IpAddress:  clientIp,
		IpLocation: iplocation,
	}
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &deviceinfo)
	if err != nil {
		ret.Code = -2
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	} else {
		//注册设备到数据库
		if !models.RegisterDeivce(&deviceinfo) {
			ret.Code = -2
			writeContent, _ := json.Marshal(ret)
			this.Ctx.WriteString(string(writeContent))
			return
		}
	}
	beego.Info("insert table deviceinfo success!")

	//注册设备到历史记录表
	historyinfo := models.Historyinfo{
		IpAddress:  clientIp,
		IpLocation: iplocation,
	}
	json.Unmarshal(this.Ctx.Input.RequestBody, &historyinfo)
	if !models.RegisterHistory(&historyinfo) {
		ret.Code = -2
		writeContent, _ := json.Marshal(ret)
		this.Ctx.WriteString(string(writeContent))
		return
	}
	beego.Info("insert table historyinfo success!")

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

	//返回注册成功
	ret.Code = 0
	writeContent, _ := json.Marshal(ret)
	this.Ctx.WriteString(string(writeContent))
	beego.Info(string(writeContent))
	return
}
コード例 #5
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))
}
コード例 #6
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
}