Softata: An ASP.NET Core API Interface - 1
An ASP.NET Core wrapper to SoftataLib provides a Swagger interface enabling simple and direct testing of individual API calls.
Try the Softata Swagger on Azure:
Link: softatawebapi on azurewebsites.net Nb Link here may change.
Will separately need the Pico up and running to action API but can c=view it here without.
Nb: Is minus MetaInfo as below … working on it.
The SoftataWebAPI app launches as a browser app with a Swagger UI. This enables the individual launching of a SoftataLib method call. Where parameters are required they can be entered into the method’s UI before the method is called. The returned result is then displayed.
Swagger … is a suite of open-source tools for API developers. It is based on the OpenAPI Specification, which is a standard for describing RESTful APIs. Swagger can help developers design, build, document, and test APIs faster and more easily. It can also generate interactive API documentation, client SDKs, server stub code, and more.
Getting started
- Boot the Arduino (Pico W) device
- Wait until it has booted, when the in built LED displays the slow blink (4s on/4s off).
- Make sure that the Arduino IDE Serial Monitor is open to the relevant port.
- Connect to the device via Swagger as follows:
- Open the Softata [Post] Start method and select [Try it out] (top right).
- Enter the Pico’s IPAddress as per the Arduino Serial Monitor, and check the port.
- Click on the [Execute] button
= Alternatively use two calls:
- Connect
- SensMessage with Begin parameter.
Example calls
1. Getting list of Device Types from Swagger
Making a call to the Arduino device to get a list of supported device types.
The returned value is a json string:
[
"DEVICES",
"sensor",
"actuator",
"communication",
"display",
"serial"
]
2. The sequence of calls for Getting Device Types list:
/// <summary>
/// Get a list of Device Types
/// </summary>
/// <returns>List of device types</returns>
// GET: api/<SoftataController>
[Route("GetDevices")]
[HttpGet]
public IEnumerable<string> Get()
{
string value = SoftataLib.SendMessageCmd("Devices");
return value.Split(',',':').ToList();
}
… the ASP.NET Core Controller method that calls SendMessageCmd() in SoftataLib with the parameter “Devices”:
public static string SendMessageCmd(string cmd)
{
List<byte> sendmsg = new List<byte> { 1, (byte)cmd[0] };
byte[] sendBytes = sendmsg.ToArray<byte>();
int sent = Client.Send(sendBytes, 0, sendBytes.Length, SocketFlags.None);
//Wait for response
while (Client.Available == 0) ;
byte[] data = new byte[100];
int recvd = Client.Receive(data);
string result = Encoding.ASCII.GetString(data).Substring(0, recvd).Trim();
return result;
}
… that makes a call to the Arduino service that is interpreted there (nb only the first letter of “Devices” ‘D’ is used) as follows:
case (byte)'D': //Get Device Types
{
String devicesCSV = Grove::GetListofDevices();
Serial.println(devicesCSV);
client.print(devicesCSV);
}
… which calls Grove::GetListofDevices() in the Grove class:
static String GetListofDevices()
{
String list ="DEVICES:";
int numDevices = (int) DEVICETYPE_NONE;
for (int n=0;n<numDevices;n++)
{
list.concat(device_name[n]);
if (n != (numDevices-1))
list.concat(',');
}
return list;
}
device_name[]
is an array of string names of device types supported by Softata.
Nb: The Grove
GetListofDevices()
method is static within the class and so whilst the service requires connection, no instances of the Grove class need to be instantiated, that is no Display,Sensor or Actuator SetUp() needs to have run.
Topic | Subtopic | |
This Category Links | ||
Category: | Softata Index: | Softata |
Next: > | Softata | An ASP.NET Core API Interface - 2 |
< Prev: | Softata - Adding a new display | 7. Epilog |