Exemple #1
0
//DownLink (DL) messages
func setReportingInterval(response http.ResponseWriter, request *http.Request) *utilities.Error {
	// Ensure this is a POST request
	if request.Method != "POST" {
		log.Printf("%s RECEIVED UNSUPPORTED REST REQUEST %s %s\n", logTag, request.Method, request.URL)
		return utilities.ClientError("UNSUPPORTED METHOD", http.StatusBadRequest)
	}

	// Get the minutes interval
	var mins uint32
	err := json.NewDecoder(request.Body).Decode(&mins)
	if err != nil {
		log.Printf("%s UNABLE TO EXTRACT THE REPORTING INTERVAL FROM REQUEST %s: %s\n", logTag, request.URL, err.Error())
		return utilities.ClientError("UNABLE TO DECIPHER REPORTING INTERVAL", http.StatusBadRequest)
	}

	// Encode and enqueue the requested data
	err = encodeAndEnqueueReportingInterval(uint32(mins))
	if err != nil {
		log.Printf("%s UNABLE TO ENCODE AND ENQUEUE REPORTING INTERVAL UPDATE FOR UTM-API %s\n", logTag, request.URL)
		return utilities.ClientError("UANBLE TO ENCODE AND ENQUEUE REPORTING INTERVAL", http.StatusBadRequest)
	}

	// Success
	response.WriteHeader(http.StatusOK)
	return nil
}
Exemple #2
0
func getLatestState(response http.ResponseWriter, request *http.Request) *utilities.Error {
	// Ensure this is a GET request
	if (request.Method != "GET") ||
		(request.Method == "") {
		log.Printf("%s RECEIVED UNSUPPORTED REST REQUEST %s %s\n", logTag, request.Method, request.URL)
		return utilities.ClientError("UNSUPPORTED METHOD", http.StatusBadRequest)
	}

	// Get the latest state; only one response will come back before the requesting channel is closed
	get := make(chan LatestState)
	StateTableCmds <- &get
	state := <-get

	//Graddually group our units
	AddUuidToMap(state.LatestDisplayRow)

	//Recyle map with new state
	UuidMap = RecycleMap(UuidMap, state)

	//Copy unit into slice, for encoding
	UuidSlice = ConvertMapToSlice(UuidMap)

	sort.Sort(ByUuid(UuidSlice))

	//Store our message in MongoDB
	db := utilities.GetDB(request)
	UtmMessages := models.UtmMsgs{}

	if state.LatestDisplayRow != nil {
		out, err := json.Marshal(state.LatestDisplayRow)
		if err == nil {
			UtmMessages.Insert(db, state.LatestDisplayRow.Uuid, string(out))
		}
	}

	// Send the requested data
	response.Header().Set("Content-Type", "application/json")
	response.WriteHeader(http.StatusOK)
	err := json.NewEncoder(response).Encode(UuidSlice)
	if err != nil {
		log.Printf("%s RECEIVED REST REQUEST %s BUT ATTEMPTING TO SERIALISE THE RESULT %s YIELDED ERROR %s\n", logTag, request.URL, spew.Sdump(state), err.Error())
		return utilities.ServerError(err)
	}
	//log.Printf("%s Received rest request %s and responding with %s\n", logTag, request.URL, spew.Sdump(state))

	return nil
}