I just managed to build C# programs for Ubisense Client.
It enables Client to get location event(including tag location coordinates, tag nume), and spatial event(tag enters or exits a zone) using API. When one of the event occurs, a massage including the event information will send out over a socket connection.
The code just combined 3 part codes
1. get location event using API (got from website http://eval.ubisense.net/howto/LocationEvents_article/LocationEvents.html)
2. get spatial event using API (got from website http://eval.ubisense.net/howto/SpatialRelationMonitor_article/SpatialRelationMonitor.html)
3. socket connection code
Now, the program works well. However, as I don't really know of C# programming, the code may not very efficient.
The code has been attached, and Please give me some suggestion that I can make the code more efficient!!!!
- Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using Ubisense;
using Monitor = Ubisense.USpatial.Monitor;
using Naming = Ubisense.UName.Naming;
using Ubisense.ULocation;
using Ubisense.UBase;
using CellData = Ubisense.ULocation.CellData;
namespace Ubisense_Data_Retriever
{
public partial class Form1 : Form
{
static Naming.Schema naming_schema = new Naming.Schema(false);
static Monitor.Schema monitorSchema = new Monitor.Schema(false);//to query the current relationshiops between objects
static Naming.Schema namingSchema = new Naming.Schema(false); //to query the names of the tags
static Ubisense.USpatial.Ownership.Schema ownershipSchema = new Ubisense.USpatial.Ownership.Schema(false);
private Ubisense.ULocation.CellData.Schema cellData;
static MultiCell location = new MultiCell(); // to load all possible cells into celldata schema if we have many cells in the system
private DateTime currentTime = new DateTime();
private bool start = false;
TcpClient client;
NetworkStream stream;
public Form1()
{
InitializeComponent();
this.timer1.Interval = 500;
this.timer1.Enabled = false;
client = new TcpClient("127.0.0.1", 13000); //initial the connnection ports
stream = client.GetStream();
Byte[] data = System.Text.Encoding.ASCII.GetBytes("Connection initiated\n");
stream.Write(data, 0, data.Length);
}
private void Form1_Load(object sender, EventArgs e)
{
/*
To load all the tags when the form is loaded
*/
namingSchema.ConnectAsClient(); // connect to the server
foreach (KeyValuePair<string, Ubisense.ULocation.Cell> cells in location.GetAvailableCells())
{
location.LoadCell(cells.Value, true); //get all the possible cells in the system and add them into Multicell
}
cellData = location.Schema;
this.getTags();
Ubisense.UCell.Config.Schema cell_schema = new Ubisense.UCell.Config.Schema(false);
cell_schema.ConnectAsClient();
// Create an example event client for a USpatial::Cell
Ubisense.USpatial.Monitor.Schema monitor_schema = new Ubisense.USpatial.Monitor.Schema(true);
// Iterate over every row in the UCell.Config database
using (Ubisense.UCell.Config.ReadTransaction xact = cell_schema.ReadTransaction())
{
foreach (Ubisense.UCell.Config.Names.RowType row in Ubisense.UCell.Config.Names.name_(xact))
{
// spatial_cell will only get a non-nil result
// for cells of type USpatial::Cell.
Ubisense.USpatial.Cell spatial_cell = new Ubisense.USpatial.Cell();
spatial_cell.Narrow(row.cell_);
if (!spatial_cell.Nil())
{
// Simply connect to the first USpatial::Cell found
monitor_schema.ConnectAsClient(spatial_cell.Id.ToString());
break;
}
}
}
// Disconnect from the cell_schema service and dispose of it as it's
// no longer needed.
cell_schema.Disconnect();
if (cell_schema != null)
{
cell_schema.Dispose();
}
//location handler
Ubisense.ULocation.CellData.Location.AddUpdateHandler(location.Schema, CellData_Update);
// Add the insert/delete handlers for the monitor_schema.
// These will be called when rows are inserted/deleted into/from
//spacial event handler
Ubisense.USpatial.Monitor.Contains.AddInsertHandler(monitor_schema, OnInsert);
Ubisense.USpatial.Monitor.Contains.AddDeleteHandler(monitor_schema, OnDelete);
// Connect to the naming_schema as a cache client
naming_schema.ConnectAsClient();
}
private void getTags()
{
using (Naming.ReadTransaction names = namingSchema.ReadTransaction()) // ReadTransaction() function call will return an arry with each member containing an object and a name
{
foreach (Naming.ObjectName.RowType row in Naming.ObjectName.name_(names)) // get the name out
{
this.tag_listView.Items.Add(row.name_.ToString());
}
}
}
//collect tags location (x,y,z), and tag name
private void CellData_Update(CellData.Location.RowType old_row, CellData.Location.RowType new_row)
{
currentTime = DateTime.Now;
if (this.InvokeRequired)
this.BeginInvoke(new MethodInvoker(delegate() { CellData_Update(old_row, new_row); }));
else if (this.start == true)
{
string name = this.object_name(new_row.object_);
if (name != "no name")
{
String szData = name + " " + new_row.position_.P.X.ToString() + " " + new_row.position_.P.Y.ToString() + " " + new_row.position_.P.Z.ToString() + "\n";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(szData); // encoding the info into bytes
stream.Write(byData, 0, byData.Length);
}
}
}
// Insert handler for the Ubisense.USpatial.Monitor.Contains relation
private void OnInsert(Ubisense.USpatial.Interaction _i)
{
// Use the naming_schema to get the names of the objects involved
using (Ubisense.UName.Naming.ReadTransaction xact = naming_schema.ReadTransaction())
{
foreach (Ubisense.UName.Naming.ObjectName.RowType object_name_row in Ubisense.UName.Naming.ObjectName.object_name_(xact, _i.object_))
{
foreach (Ubisense.UName.Naming.ObjectName.RowType subject_name_row in Ubisense.UName.Naming.ObjectName.object_name_(xact, _i.subject_))
{
String szData = object_name_row.name_ + " enters " + subject_name_row.name_ + "\n";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(szData); // encoding the info into bytes
stream.Write(byData, 0, byData.Length);
}
}
}
}
// Delete handler for the Ubisense.USpatial.Monitor.Contains relation
private void OnDelete(Ubisense.USpatial.Interaction _i)
{
// Use the naming_schema to get the names of the objects involved
using (Ubisense.UName.Naming.ReadTransaction xact = naming_schema.ReadTransaction())
{
foreach (Ubisense.UName.Naming.ObjectName.RowType object_name_row in Ubisense.UName.Naming.ObjectName.object_name_(xact, _i.object_))
{
foreach (Ubisense.UName.Naming.ObjectName.RowType subject_name_row in Ubisense.UName.Naming.ObjectName.object_name_(xact, _i.subject_))
{
String szData = object_name_row.name_ + " exits " + subject_name_row.name_ + "\n";
byte[] byData = System.Text.Encoding.ASCII.GetBytes(szData); // encoding the info into bytes
stream.Write(byData, 0, byData.Length);
}
}
}
}
//get object name
private string object_name(UObject obj)
{
using (Naming.ReadTransaction names = namingSchema.ReadTransaction())
{
foreach (Naming.ObjectName.RowType row in Naming.ObjectName.name_(names))
{
if (row.object_.Id == obj.Id)
return row.name_;
}
}
return "no name";
}
//define a button to control start or stop the data sending
private void start_button_Click(object sender, EventArgs e)
{
if (this.start_button.Text == "Start dump data")
{
//client = new TcpClient("127.0.0.1", 13000);
//stream = client.GetStream();
start = true;
this.start_button.Text = "Stop dump data";
byte[] data = System.Text.Encoding.ASCII.GetBytes("Hello again\n");
stream.Write(data, 0, data.Length);
}
else
{
//stream.Close();
start = false;
this.start_button.Text = "Start dump data";
}
}
}
}
