Skip to content

Setup: Manual Configuration

Trais McAllister edited this page Jan 18, 2023 · 1 revision

Adapter Setup: Manual Configuration

Setting up an adapter manually is similar to managing a Dictionary. All DataItems are managed via their DataItem.Name in the `Adapter.

Initialize the Adapter

In this example, we'll use the TcpAdapter to get started:

var adapter = new TcpAdapter();
adapter.Start();
// ... manage and publish data items
adapter.Stop();

Initialize the DataItems

Next, we'll want to add all of our DataItems manually:

// See Sample, Event, Condition, Message, and TimeSeries data item types
adapter.AddDataItem(new Event("avail")); // EVENT: AVAILABILITY
adapter.AddDataItem(new Event("exec"));  // EVENT: EXECUTION
adapter.AddDataItem(new Sample("xpos")); // SAMPLE: PATH_POSITION
adapter.AddDataItem(new Sample("ypos")); // SAMPLE: PATH_POSITION
adapter.AddDataItem(new Sample("zpos")); // SAMPLE: PATH_POSITION

Update the DataItems

For this example, we'll introduce a loop to randomly update the DataItem values:

var rand = new Random();
string[] executionStates = { "ACTIVE", "READY" };
while (true) {
  adapter["avail"].Value = "AVAILABLE";

  adapter["exec"].Value = executionStates[rand.Next(0, executionStates.Length)];

  adapter["xpos"].Value = rand.NextDouble() * 1000;
  adapter["ypos"].Value = rand.NextDouble() * 1000;
  adapter["zpos"].Value = rand.NextDouble() * 1000;

  System.Threading.Thread.Sleep(1000);
}

Publish the DataItems

Finally, we'll insert the method for publishing the changed values:

var rand = new Random();
string[] executionStates = { "ACTIVE", "READY" };
while (true) {
  // ... update the values (shown above)

  adapter.Send(DataItemSendTypes.Changed);

  System.Threading.Thread.Sleep(1000);
}

Full Example

var adapter = new TcpAdapter();

adapter.AddDataItem(new Event("avail")); // EVENT: AVAILABILITY
adapter.AddDataItem(new Event("exec"));  // EVENT: EXECUTION
adapter.AddDataItem(new Sample("xpos")); // SAMPLE: PATH_POSITION
adapter.AddDataItem(new Sample("ypos")); // SAMPLE: PATH_POSITION
adapter.AddDataItem(new Sample("zpos")); // SAMPLE: PATH_POSITION

adapter.Start();

var rand = new Random();
string[] executionStates = { "ACTIVE", "READY" };
while (true) {
  adapter["avail"].Value = "AVAILABLE";

  adapter["exec"].Value = executionStates[rand.Next(0, executionStates.Length)];

  adapter["xpos"].Value = rand.NextDouble() * 1000;
  adapter["ypos"].Value = rand.NextDouble() * 1000;
  adapter["zpos"].Value = rand.NextDouble() * 1000;

  adapter.Send(DataItemSendTypes.Changed);

  System.Threading.Thread.Sleep(1000);
}

adapter.Stop();