Exemplo n.º 1
0
// GetThing retrieves a thing using it's identifier
func (lr *ThingRouter) GetThing(params martini.Params, w http.ResponseWriter, thingModel *models.ThingModel, conn redis.Conn) {

	thing, err := thingModel.Fetch(params["id"], conn)

	log.Infof(spew.Sprintf("thing: %v", thing))

	if err == models.RecordNotFound {
		WriteServerErrorResponse(fmt.Sprintf("Unknown thing id: %s", params["id"]), http.StatusNotFound, w)
		return
	}

	if err != nil {
		WriteServerErrorResponse("Unable to retrieve thing", http.StatusInternalServerError, w)
		return
	}

	WriteServerResponse(thing, http.StatusOK, w)
}
Exemplo n.º 2
0
// PutThingLocation assigns or clears the location for a thing, this is currently a room identifier sent in the payload
func (lr *ThingRouter) PutThingLocation(params martini.Params, r *http.Request, w http.ResponseWriter, thingModel *models.ThingModel, conn redis.Conn) {

	thing, err := thingModel.Fetch(params["id"], conn)

	log.Infof(spew.Sprintf("thing: %v", thing))

	if err == models.RecordNotFound {
		WriteServerErrorResponse(fmt.Sprintf("Unknown thing id: %s", params["id"]), http.StatusNotFound, w)
		return
	}

	if err != nil {
		WriteServerErrorResponse("Unable to retrieve thing", http.StatusInternalServerError, w)
		return
	}

	// get the request body
	body, err := GetJsonPayload(r)

	if err != nil {
		WriteServerErrorResponse("Unable to parse body", http.StatusInternalServerError, w)
		return
	}

	roomID := body["id"].(string)

	// not a big fan of this magic
	if roomID == "" {
		err = thingModel.SetLocation(params["id"], nil, conn)
	} else {
		err = thingModel.SetLocation(params["id"], &roomID, conn)
	}

	if err != nil {
		WriteServerErrorResponse("Unable to save thing location", http.StatusInternalServerError, w)
		return
	}

	w.WriteHeader(http.StatusOK)
}