I have monitor node in OPCUA and the data will auto update in Notification_MonitoredItem().
I use ViewState to save the data and timer tick (1s) to push data on web asp. But the ViewState always [null].
How to fix this or have any way to get data and show on web view.
Thanks
protected void Subcribe_Click(object sender, EventArgs e)
{
if (Connect.myMonitoredItem != null && Connect.mySubscription != null)
{
try
{
Connect.myMonitoredItem = Connect.myClientHelperAPI.RemoveMonitoredItem(Connect.mySubscription, Connect.myMonitoredItem);
}
catch
{
}
}
try
{
//use different item names for correct assignment at the notificatino event
Connect.itemCount++;
string monitoredItemName = "myItem" + Connect.itemCount.ToString();
if (Connect.mySubscription == null)
{
Connect.mySubscription = Connect.myClientHelperAPI.Subscribe(1000);
}
Connect.myMonitoredItem = Connect.myClientHelperAPI.AddMonitoredItem(Connect.mySubscription, SubcriptionID.Text, monitoredItemName, 1);
Connect.myClientHelperAPI.ItemChangedNotification += new MonitoredItemNotificationEventHandler(Notification_MonitoredItem);
timer1.Enabled = true;
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "Alert", "mess('error','Opps...'," + ex.ToString() + ")", true);
}
}
private void Notification_MonitoredItem(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
{
MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
if (notification == null)
{
return;
}
ViewState["Item"] = "Item name: " + monitoredItem.DisplayName;
ViewState["Value"] = "Value: " + Utils.Format("{0}", notification.Value.WrappedValue.ToString());
ViewState["Source"] = "Source timestamp: " + notification.Value.SourceTimestamp.ToString();
ViewState["Server"] = "Server timestamp: " + notification.Value.ServerTimestamp.ToString();
}
protected void UpdateTimer_Tick(object sender, EventArgs e)
{
if (ViewState["Item"] != null && ViewState["Value"] != null && ViewState["Source"] != null && ViewState["Server"] != null)
{
subscriptionTextBox.Text = ViewState["Item"].ToString();
subscriptionTextBox.Text += ViewState["Value"].ToString();
subscriptionTextBox.Text += ViewState["Source"].ToString();
subscriptionTextBox.Text += ViewState["Server"].ToString();
}
}