API calls using example code from documentation fail


#1

Hello SGP experts,

I am currently implementing a little application in c# which should call the SGP API to start imaging. I found the code snippet in the documentation and thought, I give it a try. But I never get any answer from the software when I send a request to it. Here is my little modified code. The request.Resource is different, but I also tried to use the original code, which was also not working. Any idea what I am missing? The code compiles, but when executed I never get past the "SgCaptureImageResponse good is always “null” problem.

Thx for your help
Ralf

		string temp_image_URL = @"C:\Users\Ralf\Desktop\MM\mm_exposure.fit";
		SgImage img = new SgImage() { BinningMode = 1, ExposureLength = 5, FrameType = "Light", Path = temp_image_URL };
		Guid imgReceipt;
		if (!TakeImage(img, out imgReceipt))
		   return;



	private static bool TakeImage(SgImage img, out Guid receipt)
	{

		RestSharp.IRestClient client = new RestClient();
		var request = new RestRequest(Method.POST);
		request.Resource = "http://localhost:59590/json/reply/SgCaptureImage";
		request.RequestFormat = DataFormat.Json;
		request.AddHeader("Content-Type", "application/json");
		request.AddBody(img);
	
	   IRestResponse response = client.Execute(request);
	
	   SgCaptureImageResponse good = JsonConvert.DeserializeObject<SgCaptureImageResponse>(response.Content);

	   if (!good.Success)
	   {
	       Console.WriteLine(good.Message);
	       receipt = Guid.Empty;
	       return false;
	   }
	
	   receipt = good.Receipt;
	   return true;
	}
	
	private static bool GetImagePath(Guid receipt, out string path)
	{
	   
		const string BaseUrl = "http://localhost:59590/";
		
		RestClient client = new RestClient(BaseUrl);
		
		// RestSharp.IRestClient client = new RestClient();

		var request = new RestRequest(Method.GET);
	   request.Resource = BaseUrl + "imagepath/" + receipt;
	   request.RequestFormat = DataFormat.Json;
	   request.AddHeader("Accept", "application/json");
	
	   IRestResponse response = client.Execute(request);
	   SgGenericResponse good = JsonConvert.DeserializeObject<SgGenericResponse>(response.Content);
	
	   if (!good.Success)
	   {
	       Console.WriteLine(good.Message);
	       path = "NA";
	       return false;
	   }
	
	   // When successful, Message will contain the path to the image (this might not be the same path you passed in)
	   path = good.Message;
	   return true;
	}
	
	// DTOs
	public class SgGenericResponse
	{
	   public bool Success { get; set; }
	   public string Message { get; set; }
	}

	public class SgCaptureImageResponse
	{
	   public bool Success { get; set; }
	   public string Message { get; set; }
	   public Guid Receipt { get; set; }
	}

	
	public class SgImage
	{
	   public int BinningMode { get; set; }
	   public int IsoMode { get; set; }
	   public int ExposureLength { get; set; }
	   public string Gain { get; set; }
	   public string Speed { get; set; }
	   public string FrameType { get; set; }
	   public string Path { get; set; }
	}

#2

Hello RaKo,

Looks like a race condition to me.

I haven’t tried the code you reference but I think you would need some delay between the time you send the exposure request to the camera (you are asking for a 5 second exposure) and when you look for the response.

BobT


#3

Hello BobT,

thx for your reply. I tested again with Thread.sleep(10000) and it did not change the result. If I look at SGP interface, it looks like SGP isn’t doing anything, when the request is sent…

Thx
Ralf


#4

With SGP running can you get to the API documentation by browsing to http://localhost:59590/ ?

If not it could be that something (like a firewall) is blocking access.

Thanks,
Jared


#5

Hi Jared,

in the chrome browser I can easily go to http://localhost:59590/metadata. I also tried to execute http://localhost:59590/json/reply/SgCaptureImage in the browser and got the expected response that no path is specified…

{
Success: false,
Message: “Invalid image directory! Der Wert darf nicht NULL sein. Parametername: path”,
Receipt: “00000000000000000000000000000000”
}

So a firewall seems not to be the problem.

Thx
Ralf


#6

Hi all,

do I need to specify some encoding like UTF8 somewhere??

Thx
Ralf


#7

Hi Ralf,

You got my curiosity up about the API so I spent some time with the C# example. There is a class definition missing (SgImageResponse) and I had to change the way the URL is put together in TakeImage() and GetImagePath() but it works fine now. Here are the changes:

Added:
public class SgImageResponse
{
public bool Success { get; set; }
public string Message { get; set; }
public Guid Receipt { get; set; }
}

Modified:

private static bool GetImagePath(Guid receipt, out string path)
{
RestSharp.IRestClient client = new RestClient(“http://localhost:59590/”);
var request = new RestRequest(Method.GET);
request.Resource = “imagepath/” + receipt;
request.RequestFormat = DataFormat.Json;
request.AddHeader(“Accept”, “application/json”);

        IRestResponse response = client.Execute(request);
        SgGenericResponse good = JsonConvert.DeserializeObject<SgGenericResponse>(response.Content);

        if(!good.Success)
        {
            Console.WriteLine(good.Message);
            path = "NA";
            return false;
        }

        path = good.Message;
        return true;
    }


    private static bool TakeImage(SgImage img, out Guid receipt)
    {
        RestSharp.IRestClient client = new RestClient("http://localhost:59590/");

        var request = new RestRequest(Method.POST);
        request.Resource = "image";
        request.RequestFormat = DataFormat.Json;
        request.AddHeader("Accept", "application/json");

        request.AddBody(img);

        IRestResponse response = client.Execute(request);
        SgImageResponse good = JsonConvert.DeserializeObject<SgImageResponse>(response.Content);

        if (!good.Success)
        {
            Console.WriteLine(good.Message);
            receipt = Guid.Empty;
            return false;
        }

        receipt = good.Receipt;
        return true;
    }

The URL changes may just be do to the browser I use (MS Edge) and the missing class definition was probably a cut and paste error when the web page was created.

Works fine for me now, hope this helps your effort.

BobT


#8

Hello BobT,

thanks a lot for your help! When I use your code, than the image is not found in my case. I also noticed, that the new RestClient needs some URL in order to work propoerly, but all possible variants I could think of did not work. The response to the request “IRestResponse response = client.Execute(request);” always came back with “File not found”…

Yesterday I also worked again on the problem and found another solution to it.
The missing public class SgImageResponse I had also found and implemented, otherwise the code did not compile at all.

This is my code now :slight_smile:

		const string BaseUrl = "http://localhost:59590/json/reply/";
		
		RestSharp.IRestClient client = new RestClient(BaseUrl);

		var request = new RestRequest(Method.GET);
		request.Resource = BaseUrl + "SgGetImagePath";
	   	request.RequestFormat = DataFormat.Json;
	   	request.AddHeader("Content-Type", "application/json");
	   	request.AddParameter("Receipt", receipt);
	
	   	IRestResponse response = client.Execute(request);
	   	SgGenericResponse good = JsonConvert.DeserializeObject<SgGenericResponse>(response.Content); 

This works in my case, I get the file path and everything is fine :slight_smile:

Next step is getting SGP to solve the image …

It would be also good to have an API call available, which allows to select and start the CCD from the external software. In my application I check, whether SGP is already up and running, before trying to execute the request to the API. In case it is not running, I am starting SGP, but I can’t connect automatically to a specific CCD… So for now I simply notify the user, that he has to set the CCD manually in SGP instead of doing it for the user automatically. Maybe that can be added to the API in the future.

Thx
Ralf


#9

The current API allows for this.


#10

Hi Ken,

how can I do that? Do I need a version higher than 2.5.1.17 for that?

Thx
Ralf


#11

Apologies,

This functionality was written some time ago, but, for some unknown reason, was made part of the private API. I have moved it to the public API and it will be available in the 2.5.2.3 beta (soon).

http://127.0.0.1:59590/enumdevices/Camera/

then

http://127.0.0.1:59590/connectdevice/Camera/Camera%20V2%20simulator


#12

Hi Ralph,

you can look to https://github.com/mworion/mount.git get a wrapper class for interfacing SGPro (in python) there is all official API in.

Hop this helps.

@Ken: many thanks for shifting that API to public, looked for that as well.

Michel


#13

Hi Michel,

thanks for pointing me to your github project! This already quite advanced!! Congrats.

I am not using Python but C#, but it doesn’t make a huge difference :slight_smile:
As soon as the camera starting thing is available in the public part of the API, I will be happy!

Best regards
Ralf


www.mainsequencesoftware.com