// GetRoom retrieves a room using it's identifier func (lr *RoomRouter) GetRoom(params martini.Params, w http.ResponseWriter, roomModel *models.RoomModel, conn redis.Conn) { room, err := roomModel.Fetch(params["id"], conn) log.Infof(spew.Sprintf("room: %v", room)) if err == models.RecordNotFound { WriteServerErrorResponse(fmt.Sprintf("Unknown room id: %s", params["id"]), http.StatusNotFound, w) return } if err != nil { WriteServerErrorResponse("Unable to retrieve room", http.StatusInternalServerError, w) return } WriteServerResponse(room, http.StatusOK, w) }
// PutCalibrateRoom enables calibration for a specific room // // Request {"id":"1468fbcd-3ca6-4c6f-a742-ab91221e5462","device":"20CD39A0899C","reset":true} // Response 200 // func (lr *RoomRouter) PutCalibrateRoom(params martini.Params, w http.ResponseWriter, roomModel *models.RoomModel, conn redis.Conn) { room, err := roomModel.Fetch(params["id"], conn) log.Infof(spew.Sprintf("room: %v", room)) if err == models.RecordNotFound { WriteServerErrorResponse(fmt.Sprintf("Unknown room id: %s", params["id"]), http.StatusNotFound, w) return } if err != nil { WriteServerErrorResponse("Unable to retrieve room", http.StatusInternalServerError, w) return } // TODO: Need to send message over an RPC thing // LocationManager.calibrate(req.params.id, req.body.device, req.body.reset||false, function(err) { w.WriteHeader(http.StatusOK) }
// PutAppRoomMessage sends a message to the app passing the identifier of the room which it applies too func (lr *RoomRouter) PutAppRoomMessage(params martini.Params, r *http.Request, w http.ResponseWriter, roomModel *models.RoomModel, conn *ninja.Connection, rconn redis.Conn) { room, err := roomModel.Fetch(params["id"], rconn) log.Infof(spew.Sprintf("room: %v", room)) if err == models.RecordNotFound { WriteServerErrorResponse(fmt.Sprintf("Unknown room id: %s", params["id"]), http.StatusNotFound, w) return } if err != nil { WriteServerErrorResponse("Unable to retrieve room", http.StatusInternalServerError, w) return } appName := params["appName"] if appName == "" { WriteServerErrorResponse("appName required", http.StatusBadRequest, w) return } // get the request body body, err := GetJsonPayload(r) if err != nil { WriteServerErrorResponse("unable to parse json body", http.StatusBadRequest, w) return } topic := fmt.Sprintf("$node/%s/app/%s", NodeID, params["appName"]) // TODO: Need to send message over an RPC thing // req.bus.publish(topic,req.params.id,message,function(err,response){ conn.SendNotification(topic, room.ID, appName, body) w.WriteHeader(http.StatusOK) }
// GetRoom updates a room using it's identifier func (lr *RoomRouter) UpdateRoom(params martini.Params, r *http.Request, w http.ResponseWriter, roomModel *models.RoomModel, conn redis.Conn) { room, err := roomModel.Fetch(params["id"], conn) log.Infof(spew.Sprintf("room: %v", room)) if err == models.RecordNotFound { WriteServerErrorResponse(fmt.Sprintf("Unknown room id: %s", params["id"]), http.StatusNotFound, w) return } if err != nil { WriteServerErrorResponse("Unable to retrieve room", http.StatusInternalServerError, w) return } // get the request body body, err := GetJsonPayload(r) if err != nil { WriteServerErrorResponse("Unable to read body to update room", http.StatusInternalServerError, w) return } room.Name = body["name"].(string) room.Type = body["type"].(string) err = roomModel.Create(room, conn) if err != nil { WriteServerErrorResponse("Unable to update room", http.StatusInternalServerError, w) return } WriteServerResponse(room, http.StatusOK, w) }