using System; using System.Drawing; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace NetComExampleStreams { /// /// Summary description for Form1. /// public class NetComExampleStreamsForm : System.Windows.Forms.Form { #region UI Members internal System.Windows.Forms.Label lblServerName; internal System.Windows.Forms.GroupBox grpStreamProps; internal System.Windows.Forms.Button btnCloseStream; internal System.Windows.Forms.Button btnOpenStream; internal System.Windows.Forms.Button btnObjectRefresh; internal System.Windows.Forms.Label lblObjectCountNum; internal System.Windows.Forms.Label lblObjectCount; internal System.Windows.Forms.Label lblObjectTypeString; internal System.Windows.Forms.Label lblObjectType; internal System.Windows.Forms.ComboBox cmbCheetahObjects; internal System.Windows.Forms.Label lblCheetahObjects; internal System.Windows.Forms.Label lblServerNameComment; internal System.Windows.Forms.TextBox txtServerName; internal System.Windows.Forms.Button btnDisconnect; internal System.Windows.Forms.Button btnConnect; internal System.Windows.Forms.GroupBox grpRecordLog; internal System.Windows.Forms.ListBox lbRecordLog; #endregion #region Private Members //netcom client private MNetCom.MNetComClient mNetComClient; //callback delegates private MNetCom.MNC_SECallback mNetcomSECallback; private MNetCom.MNC_STCallback mNetcomSTCallback; private MNetCom.MNC_TTCallback mNetcomTTCallback; private MNetCom.MNC_CSCCallback mNetcomCSCCallback; private MNetCom.MNC_EVCallback mNetcomEVCallback; private MNetCom.MNC_VTCallback mNetcomVTCallback; //Needed for proper UI updating in a multi-threaded application private delegate void AsyncRecordLogUpdate(String updateText); //cheetah object list holders private List mCheetahObjectList; private List mCheetahTypesList; #endregion /// /// Required designer variable. /// private System.ComponentModel.Container components = null; #region Constructor public NetComExampleStreamsForm() { //initialize class vars this.mNetComClient = new MNetCom.MNetComClient(); // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // //extra UI setup this.btnDisconnect.Enabled = false; this.grpRecordLog.Enabled = false; this.grpStreamProps.Enabled = false; //Set the logfile name if (! ( this.mNetComClient.SetLogFileName(Application.StartupPath.ToString() + "\\NetComExampleStreamsLogfile.txt") ) ) { MessageBox.Show(this, "Call to set the logfile name failed", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } //register the callback functions this.mNetcomSECallback = new MNetCom.MNC_SECallback(NetComCallbackSE); this.mNetComClient.SetCallbackFunctionSE(this.mNetcomSECallback, this); this.mNetcomSTCallback = new MNetCom.MNC_STCallback(NetComCallbackST); this.mNetComClient.SetCallbackFunctionST(this.mNetcomSTCallback, this); this.mNetcomTTCallback = new MNetCom.MNC_TTCallback( NetComCallbackTT); this.mNetComClient.SetCallbackFunctionTT(this.mNetcomTTCallback, this); this.mNetcomCSCCallback = new MNetCom.MNC_CSCCallback( NetComCallbackCSC); this.mNetComClient.SetCallbackFunctionCSC(this.mNetcomCSCCallback, this); this.mNetcomEVCallback = new MNetCom.MNC_EVCallback( NetComCallbackEV); this.mNetComClient.SetCallbackFunctionEV(this.mNetcomEVCallback, this); this.mNetcomVTCallback = new MNetCom.MNC_VTCallback( NetComCallbackVT); this.mNetComClient.SetCallbackFunctionVT(this.mNetcomVTCallback, this); } #endregion /// /// Clean up any resources being used. /// protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.lblServerName = new System.Windows.Forms.Label(); this.grpStreamProps = new System.Windows.Forms.GroupBox(); this.btnCloseStream = new System.Windows.Forms.Button(); this.btnOpenStream = new System.Windows.Forms.Button(); this.btnObjectRefresh = new System.Windows.Forms.Button(); this.lblObjectCountNum = new System.Windows.Forms.Label(); this.lblObjectCount = new System.Windows.Forms.Label(); this.lblObjectTypeString = new System.Windows.Forms.Label(); this.lblObjectType = new System.Windows.Forms.Label(); this.cmbCheetahObjects = new System.Windows.Forms.ComboBox(); this.lblCheetahObjects = new System.Windows.Forms.Label(); this.lblServerNameComment = new System.Windows.Forms.Label(); this.txtServerName = new System.Windows.Forms.TextBox(); this.btnDisconnect = new System.Windows.Forms.Button(); this.btnConnect = new System.Windows.Forms.Button(); this.grpRecordLog = new System.Windows.Forms.GroupBox(); this.lbRecordLog = new System.Windows.Forms.ListBox(); this.grpStreamProps.SuspendLayout(); this.grpRecordLog.SuspendLayout(); this.SuspendLayout(); // // lblServerName // this.lblServerName.Location = new System.Drawing.Point(16, 8); this.lblServerName.Name = "lblServerName"; this.lblServerName.Size = new System.Drawing.Size(72, 16); this.lblServerName.TabIndex = 24; this.lblServerName.Text = "Server Name"; // // grpStreamProps // this.grpStreamProps.Controls.Add(this.btnCloseStream); this.grpStreamProps.Controls.Add(this.btnOpenStream); this.grpStreamProps.Controls.Add(this.btnObjectRefresh); this.grpStreamProps.Controls.Add(this.lblObjectCountNum); this.grpStreamProps.Controls.Add(this.lblObjectCount); this.grpStreamProps.Controls.Add(this.lblObjectTypeString); this.grpStreamProps.Controls.Add(this.lblObjectType); this.grpStreamProps.Controls.Add(this.cmbCheetahObjects); this.grpStreamProps.Controls.Add(this.lblCheetahObjects); this.grpStreamProps.Location = new System.Drawing.Point(16, 64); this.grpStreamProps.Name = "grpStreamProps"; this.grpStreamProps.Size = new System.Drawing.Size(520, 88); this.grpStreamProps.TabIndex = 29; this.grpStreamProps.TabStop = false; this.grpStreamProps.Text = "Stream Properties"; // // btnCloseStream // this.btnCloseStream.Location = new System.Drawing.Point(192, 56); this.btnCloseStream.Name = "btnCloseStream"; this.btnCloseStream.Size = new System.Drawing.Size(88, 23); this.btnCloseStream.TabIndex = 8; this.btnCloseStream.Text = "Close Stream"; this.btnCloseStream.Click += new System.EventHandler(this.btnCloseStream_Click); // // btnOpenStream // this.btnOpenStream.Location = new System.Drawing.Point(104, 56); this.btnOpenStream.Name = "btnOpenStream"; this.btnOpenStream.Size = new System.Drawing.Size(80, 23); this.btnOpenStream.TabIndex = 7; this.btnOpenStream.Text = "Open Stream"; this.btnOpenStream.Click += new System.EventHandler(this.btnOpenStream_Click); // // btnObjectRefresh // this.btnObjectRefresh.Location = new System.Drawing.Point(16, 56); this.btnObjectRefresh.Name = "btnObjectRefresh"; this.btnObjectRefresh.Size = new System.Drawing.Size(80, 23); this.btnObjectRefresh.TabIndex = 6; this.btnObjectRefresh.Text = "Refresh List"; this.btnObjectRefresh.Click += new System.EventHandler(this.btnObjectRefresh_Click); // // lblObjectCountNum // this.lblObjectCountNum.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblObjectCountNum.Location = new System.Drawing.Point(472, 29); this.lblObjectCountNum.Name = "lblObjectCountNum"; this.lblObjectCountNum.Size = new System.Drawing.Size(32, 23); this.lblObjectCountNum.TabIndex = 5; // // lblObjectCount // this.lblObjectCount.Location = new System.Drawing.Point(400, 32); this.lblObjectCount.Name = "lblObjectCount"; this.lblObjectCount.Size = new System.Drawing.Size(72, 16); this.lblObjectCount.TabIndex = 4; this.lblObjectCount.Text = "Object Count"; // // lblObjectTypeString // this.lblObjectTypeString.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.lblObjectTypeString.Location = new System.Drawing.Point(296, 29); this.lblObjectTypeString.Name = "lblObjectTypeString"; this.lblObjectTypeString.Size = new System.Drawing.Size(88, 23); this.lblObjectTypeString.TabIndex = 3; // // lblObjectType // this.lblObjectType.Location = new System.Drawing.Point(224, 32); this.lblObjectType.Name = "lblObjectType"; this.lblObjectType.Size = new System.Drawing.Size(72, 16); this.lblObjectType.TabIndex = 2; this.lblObjectType.Text = "Object Type"; // // cmbCheetahObjects // this.cmbCheetahObjects.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cmbCheetahObjects.Location = new System.Drawing.Point(104, 30); this.cmbCheetahObjects.Name = "cmbCheetahObjects"; this.cmbCheetahObjects.Size = new System.Drawing.Size(112, 21); this.cmbCheetahObjects.TabIndex = 1; this.cmbCheetahObjects.SelectedIndexChanged += new System.EventHandler(this.cmbCheetahObjects_SelectedIndexChanged); // // lblCheetahObjects // this.lblCheetahObjects.Location = new System.Drawing.Point(16, 32); this.lblCheetahObjects.Name = "lblCheetahObjects"; this.lblCheetahObjects.Size = new System.Drawing.Size(88, 16); this.lblCheetahObjects.TabIndex = 0; this.lblCheetahObjects.Text = "Cheetah Objects"; // // lblServerNameComment // this.lblServerNameComment.Location = new System.Drawing.Point(136, 32); this.lblServerNameComment.Name = "lblServerNameComment"; this.lblServerNameComment.Size = new System.Drawing.Size(208, 16); this.lblServerNameComment.TabIndex = 28; this.lblServerNameComment.Text = "(User may enter pc name or IP address)"; // // txtServerName // this.txtServerName.Location = new System.Drawing.Point(96, 8); this.txtServerName.Name = "txtServerName"; this.txtServerName.Size = new System.Drawing.Size(280, 20); this.txtServerName.TabIndex = 27; this.txtServerName.Text = ""; // // btnDisconnect // this.btnDisconnect.Location = new System.Drawing.Point(464, 8); this.btnDisconnect.Name = "btnDisconnect"; this.btnDisconnect.TabIndex = 26; this.btnDisconnect.Text = "Disconnect"; this.btnDisconnect.Click += new System.EventHandler(this.btnDisconnect_Click); // // btnConnect // this.btnConnect.Location = new System.Drawing.Point(384, 8); this.btnConnect.Name = "btnConnect"; this.btnConnect.TabIndex = 25; this.btnConnect.Text = "Connect"; this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click); // // grpRecordLog // this.grpRecordLog.Controls.Add(this.lbRecordLog); this.grpRecordLog.Location = new System.Drawing.Point(16, 168); this.grpRecordLog.Name = "grpRecordLog"; this.grpRecordLog.Size = new System.Drawing.Size(520, 240); this.grpRecordLog.TabIndex = 30; this.grpRecordLog.TabStop = false; this.grpRecordLog.Text = "Record Log"; // // lbRecordLog // this.lbRecordLog.Location = new System.Drawing.Point(8, 16); this.lbRecordLog.Name = "lbRecordLog"; this.lbRecordLog.Size = new System.Drawing.Size(504, 212); this.lbRecordLog.TabIndex = 0; // // NetComExampleStreamsForm // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(552, 422); this.Controls.Add(this.lblServerName); this.Controls.Add(this.grpStreamProps); this.Controls.Add(this.lblServerNameComment); this.Controls.Add(this.txtServerName); this.Controls.Add(this.btnDisconnect); this.Controls.Add(this.btnConnect); this.Controls.Add(this.grpRecordLog); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Name = "NetComExampleStreamsForm"; this.Text = "NetCom Example - Streams"; this.grpStreamProps.ResumeLayout(false); this.grpRecordLog.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.Run(new NetComExampleStreamsForm()); } #region Async UI Update //******************************************************************************************************************** private void RecordLogUpdate(String updateText) { this.lbRecordLog.Items.Add(updateText); this.lbRecordLog.SelectedItem = updateText; } #endregion #region UI Event Handlers //************************************************************** //************************************************************** private void btnConnect_Click(object sender, System.EventArgs e) { if (!( this.mNetComClient.AreWeConnected())) { if (this.mNetComClient.ConnectToServer(this.txtServerName.Text)) { this.mNetComClient.SetApplicationName("NetCom Streams Example"); //change ui to show connected status this.txtServerName.Enabled = false; this.btnConnect.Enabled = false; this.btnDisconnect.Enabled = true; this.grpRecordLog.Enabled = true; this.grpStreamProps.Enabled = true; //reset the array lists this.mCheetahObjectList = new List(); this.mCheetahTypesList = new List(); if (this.mNetComClient.GetDASObjectsAndTypes(ref this.mCheetahObjectList, ref this.mCheetahTypesList) ) { //load all object names into the combo box foreach (string objectName in this.mCheetahObjectList) { this.cmbCheetahObjects.Items.Add(objectName); } //select the first object in the combo box, and update the ui if( this.mCheetahObjectList.Count > 0) { this.cmbCheetahObjects.SelectedIndex = 0; this.lblObjectTypeString.Text = (string)this.mCheetahTypesList[0]; this.lblObjectCountNum.Text = this.mCheetahObjectList.Count.ToString(); } else { MessageBox.Show(this, "Retrieval of Cheetah objects and types failed. List must be refreshed.", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } else { MessageBox.Show(this, "Connection to server failed", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } } } //************************************************************** //************************************************************** private void btnDisconnect_Click(object sender, System.EventArgs e) { if (this.mNetComClient.AreWeConnected()) { if ( this.mNetComClient.DisconnectFromServer() ) { //change ui to show disconnected status this.txtServerName.Enabled = true; this.btnConnect.Enabled = true; this.btnDisconnect.Enabled = false; this.cmbCheetahObjects.Items.Clear(); this.lbRecordLog.Items.Clear(); this.lblObjectCountNum.Text = ""; this.lblObjectTypeString.Text = ""; this.grpRecordLog.Enabled = false; this.grpStreamProps.Enabled = false; } else { MessageBox.Show(this, "Disconnection from server failed", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } } //************************************************************** //************************************************************** private void btnObjectRefresh_Click(object sender, System.EventArgs e) { //reset the cheetah lists this.cmbCheetahObjects.Items.Clear(); this.mCheetahObjectList = new List(); this.mCheetahTypesList = new List(); if (this.mNetComClient.GetDASObjectsAndTypes(ref this.mCheetahObjectList, ref this.mCheetahTypesList) ) { //load all object names into the combo box foreach (string objectName in this.mCheetahObjectList) { this.cmbCheetahObjects.Items.Add(objectName); } //select the first object in the combo box, and update the ui if( this.mCheetahObjectList.Count > 0) { this.cmbCheetahObjects.SelectedIndex = 0; this.lblObjectTypeString.Text = (string)this.mCheetahTypesList[0]; this.lblObjectCountNum.Text = this.mCheetahObjectList.Count.ToString(); } else { MessageBox.Show(this, "Retrieval of Cheetah objects and types failed. List must be refreshed.", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } } //************************************************************** //************************************************************** private void btnOpenStream_Click(object sender, System.EventArgs e) { int curIndex = this.cmbCheetahObjects.SelectedIndex; //ensure something is selected to open if ( curIndex == -1) { MessageBox.Show(this, "No object selected for stream open", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } //get the strings from the object list string curCheetahObject = (string)this.mCheetahObjectList[curIndex]; if (! (mNetComClient.OpenStream(curCheetahObject)) ) { MessageBox.Show(this, "Could not open stream for ObjectName: " + curCheetahObject, "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } //************************************************************** //************************************************************** private void btnCloseStream_Click(object sender, System.EventArgs e) { int curIndex = this.cmbCheetahObjects.SelectedIndex; //ensure something is selected to close if ( curIndex == -1) { MessageBox.Show(this, "No object selected for stream close", "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return; } //get the strings from the object list string curCheetahObject = (string)this.mCheetahObjectList[curIndex]; if (! (mNetComClient.CloseStream(curCheetahObject)) ) { MessageBox.Show(this, "Could not close stream for ObjectName: " + curCheetahObject, "NetCom Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); } } //******************************************************************************************************************** //******************************************************************************************************************** private void cmbCheetahObjects_SelectedIndexChanged(object sender, System.EventArgs e) { int curIndex = this.cmbCheetahObjects.SelectedIndex; if (curIndex == -1) { return; } else { this.lblObjectTypeString.Text = (string)this.mCheetahTypesList[curIndex]; } } #endregion #region NetCom Callback Functions //******************************************************************************************************************** //******************************************************************************************************************** void NetComCallbackSE(Object sender, MNetCom.MSERec records, int numRecords, string objectName) { //since we passed the this pointer to the callback object, we can assume thet sender //is of type NetComExampleStreamsForm. NetComExampleStreamsForm thisClass = (NetComExampleStreamsForm)sender; //add the object name to the textbox string msgString; msgString = "Received SE Record For: " + objectName + " at Cheetah TS " + records.qwTimeStamp.ToString(); // Update UI on the UI thread using delegate. AsyncRecordLogUpdate RecordLogUpdateDelegate = new AsyncRecordLogUpdate(RecordLogUpdate); this.lbRecordLog.BeginInvoke(RecordLogUpdateDelegate, new Object[] {msgString}); } //******************************************************************************************************************** //******************************************************************************************************************** void NetComCallbackST(Object sender, MNetCom.MSTRec records, int numRecords, string objectName) { //since we passed the this pointer to the callback object, we can assume thet sender //is of type NetComExampleStreamsForm. NetComExampleStreamsForm thisClass = (NetComExampleStreamsForm) sender; //add the object name to the textbox string msgString; msgString = "Received ST Record For: " + objectName + " at Cheetah TS " + records.qwTimeStamp.ToString(); // Update UI on the UI thread using delegate. AsyncRecordLogUpdate RecordLogUpdateDelegate = new AsyncRecordLogUpdate(RecordLogUpdate); this.lbRecordLog.BeginInvoke(RecordLogUpdateDelegate, new Object[] {msgString}); } //******************************************************************************************************************** //******************************************************************************************************************** void NetComCallbackTT(Object sender, MNetCom.MTTRec records, int numRecords, string objectName) { //since we passed the this pointer to the callback object, we can assume thet sender //is of type NetComExampleStreamsForm. NetComExampleStreamsForm thisClass = (NetComExampleStreamsForm) sender; //add the object name to the textbox string msgString; msgString = "Received TT Record For: " + objectName + " at Cheetah TS " + records.qwTimeStamp.ToString(); // Update UI on the UI thread using delegate. AsyncRecordLogUpdate RecordLogUpdateDelegate = new AsyncRecordLogUpdate(RecordLogUpdate); this.lbRecordLog.BeginInvoke(RecordLogUpdateDelegate, new Object[] {msgString}); } //******************************************************************************************************************** //******************************************************************************************************************** void NetComCallbackCSC(Object sender, MNetCom.MCRRec records, int numRecords, string objectName) { //since we passed the this pointer to the callback object, we can assume thet sender //is of type NetComExampleStreamsForm. NetComExampleStreamsForm thisClass = (NetComExampleStreamsForm) sender; //add the object name to the textbox string msgString; msgString = "Received CSC Record For: " + objectName + " at Cheetah TS " + records.qwTimeStamp.ToString(); // Update UI on the UI thread using delegate. AsyncRecordLogUpdate RecordLogUpdateDelegate = new AsyncRecordLogUpdate(RecordLogUpdate); this.lbRecordLog.BeginInvoke(RecordLogUpdateDelegate, new Object[] {msgString}); } //******************************************************************************************************************** //******************************************************************************************************************** void NetComCallbackEV(Object sender, MNetCom.MEventRec records, int numRecords, string objectName) { //since we passed the this pointer to the callback object, we can assume thet sender //is of type NetComExampleStreamsForm. NetComExampleStreamsForm thisClass = (NetComExampleStreamsForm) sender; //add the object name to the textbox string msgString; msgString = "Received Event Record For: " + objectName + " at Cheetah TS " + records.qwTimeStamp.ToString() + " with string: " + records.EventString; // Update UI on the UI thread using delegate. AsyncRecordLogUpdate RecordLogUpdateDelegate = new AsyncRecordLogUpdate(RecordLogUpdate); this.lbRecordLog.BeginInvoke(RecordLogUpdateDelegate, new Object[] {msgString}); } //******************************************************************************************************************** //******************************************************************************************************************** void NetComCallbackVT(Object sender, MNetCom.MVideoRec records, int numRecords, string objectName) { //since we passed the this pointer to the callback object, we can assume thet sender //is of type NetComExampleStreamsForm. NetComExampleStreamsForm thisClass = (NetComExampleStreamsForm) sender; //add the object name to the textbox string msgString; msgString = "Received Video Record For: " + objectName + " at Cheetah TS " + records.qwTimeStamp.ToString(); // Update UI on the UI thread using delegate. AsyncRecordLogUpdate RecordLogUpdateDelegate = new AsyncRecordLogUpdate(RecordLogUpdate); this.lbRecordLog.BeginInvoke(RecordLogUpdateDelegate, new Object[] {msgString}); } #endregion } }