Coder Social home page Coder Social logo

Comments (5)

joerghartmann avatar joerghartmann commented on August 23, 2024

@definitelyNotACatfish Issue is related with the indexer provided by Measurement.

You can remove the indexer as a workaround. Its main purpose is to ease the use of custom fragments. I'll provide a fix.

from cumulocity-clients-dotnet.

definitelyNotACatfish avatar definitelyNotACatfish commented on August 23, 2024

Hi @joerghartmann I'll wait for the fix. In the meantime I "implemented" the method in an own class for my specific needs

from cumulocity-clients-dotnet.

joerghartmann avatar joerghartmann commented on August 23, 2024

Could you please share the implementation with me?

from cumulocity-clients-dotnet.

definitelyNotACatfish avatar definitelyNotACatfish commented on August 23, 2024

Thats the method I created, basically just cp from your repo:

private async Task<bool> createMeasurementAsync(measurementsCollectionModel body)
{
	Uri url = new Uri($"{httpClient.BaseAddress}measurement/measurements");

	var request = new HttpRequestMessage 
	{
		Content = new StringContent(body.ToString(), Encoding.UTF8, "application/vnd.com.nsn.cumulocity.measurementcollection+json"),
		Method = HttpMethod.Post,
		RequestUri = url
	};
	request.Headers.TryAddWithoutValidation("Content-Type", "application/vnd.com.nsn.cumulocity.measurementcollection+json");
	request.Headers.TryAddWithoutValidation("Accept", "application/vnd.com.nsn.cumulocity.error+json, application/vnd.com.nsn.cumulocity.measurement+json, application/vnd.com.nsn.cumulocity.measurementcollection+json");
	
	var response = await httpClient.SendAsync(request);
	
	// Try to ensure successful measurement creation, else return false
	try
	{
		response.EnsureSuccessStatusCode();
	}
	catch
	{
		return false;
	}
	
	return true;
}

The collection model is a bit ugly, but I'm in a hurry and it works:

public class measurementsCollectionModel
{

	/// <summary>
	/// An array containing the measurements of the request.
	/// </summary>
	public List<object>? measurements { get; set; } = new List<object>();

	/// <summary>
	/// Constructor providing measurements list
	/// </summary>
	public measurementsCollectionModel(List<object> measurements)
	{
		this.measurements = measurements;
	}


	/// <summary>
	/// Print class as JSON, using custom JSON generation
	/// </summary>
	public override string ToString()
	{
		string json = "";

		if(this.measurements == null) return "{\n\"measurements\" : []\n}";

		// Create start tag
		json = "{\n\"measurements\" : [\n";

		// Call each measurement toString() in measurements
		foreach (var measurement in this.measurements)
		{
			json += measurement.ToString();
			json += ",";
		}
		
		// Cut away the uneccessary "," at the end
		json = json.Substring(0,json.Length-1);
		
		// Close JSON
		json += "]\n}";

		return json;
	}

}

The custom toString() here can be ignored, I have to do some magic in the measurement classes, which I can't show and so I had to print them seperatly here.

from cumulocity-clients-dotnet.

joerghartmann avatar joerghartmann commented on August 23, 2024

@definitelyNotACatfish We'll ignore the indexer in our converter implementation. Extensible objects (such as Measurement, Alarm, ....) are not convertible to JSON.

from cumulocity-clients-dotnet.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.