public Object ConnectData(int topic, ref Array Strings, ref bool GetNewValues) { lock (this) { string table, column, symbol; int index = 0; RTDData data = null; GetNewValues = true; try { string plant = Strings.GetValue(0).ToString().Trim(); if (plantName.Equals(plant)) { table = Strings.GetValue(1).ToString().Trim(); column = Strings.GetValue(2).ToString().Trim(); symbol = Strings.GetValue(3).ToString().Trim(); if (Strings.Length > 4) { index = Int32.Parse(Strings.GetValue(4).ToString().Trim()); } Trace.WriteLineIf(traceSwitch.TraceInfo, "ConnectData " + topic + "," + table + ":" + column + ":" + symbol); string key = table + column + symbol; data = (RTDData)cache[key]; if (data == null) { data = new RTDData(table, column, symbol); cache.Add(key, data); } if (!data.containsTopic(topic)) { data.addTopic(topic, index); } timer.Stop(); timer.Start(); return(data.getVal(topic)); } else { Trace.WriteLineIf(traceSwitch.TraceWarning, "Plant name " + plant + " is not recognised"); } } catch (Exception e) { Trace.WriteLineIf(traceSwitch.TraceInfo, "Exception: " + e.ToString()); } return(null); } }
public void DisconnectData(int topic) { Trace.WriteLineIf(traceSwitch.TraceInfo, "Disconnecting topic: " + topic); lock (this) { foreach (DictionaryEntry de in cache) { RTDData data = (RTDData)de.Value; if (data.containsTopic(topic)) { data.removeTopic(topic); } if (data.getTopics().Length == 0) { cache.Remove(de.Key); } } } timer.Stop(); timer.Start(); }
public Array RefreshData(ref int TopicCount) { Trace.WriteLineIf(traceSwitch.TraceInfo, "RefreshData"); lock (this) { int numItems = 0; foreach (DictionaryEntry de in cache) { RTDData item = (RTDData)de.Value; if (item.Updated) { numItems += item.getTopics().Length; } } object[,] ret = new object[2, numItems]; int i = 0; try { foreach (DictionaryEntry de in cache) { RTDData item = (RTDData)de.Value; if (item.Updated) { int [] topics = item.getTopics(); for (int j = 0; j < topics.Length; j++) { ret[0, i] = topics[j]; ret[1, i] = item.getVal(topics[j]); i++; } } } } catch (Exception e) { Debug.WriteLine(e.ToString()); } TopicCount = i; Trace.Flush(); return(ret); } }
// This function reads messages from the ticker plant void upd() { bool exit = false; Trace.WriteLineIf(traceSwitch.TraceVerbose, "Reader thread started..."); while (!exit) { try { bool notify = false; // read message from ticker plant object r = conn.k(); lock (this) { // check that msg is of expected format if (r is object []) { object [] objs = (object [])r; if ((objs.Length == 3) && (objs[0] is string) && (objs[1] is string) && ((objs[2] is c.Flip) || (objs[2] is c.Dict))) { if (objs[2] is c.Dict) { objs[2] = c.td(objs[2]); } // we only accept upd fn call if ("upd".Equals((string)objs[0])) { // get the table being updated string table = (string)objs[1]; c.Flip flip = (c.Flip)objs[2]; string [] columns = flip.x; object [] data = flip.y; // go through columns looking for sym column for (int i = 0; i < columns.Length; i++) { if ("sym".Equals(columns[i])) { string [] symbols = (string [])data[i]; for (int j = 0; j < columns.Length; j++) { if (i != j) // skip "sym" column { string column = columns[j]; for (int k = 0; k < symbols.Length; k++) { string symbol = symbols[k]; string key = table + column + symbol; // see if we are monitoring for this table/sym/column RTDData curItem = (RTDData)cache[key]; if (curItem != null) { // get the new value for this sym Array a = (Array)data[j]; Object o = a.GetValue(k); bool isNull = false; // if we are filling then check for nulls if (fill) { if (o is double) { if (Double.IsNaN((double)o)) { isNull = true; } } else if (o is float) { if (Single.IsNaN((float)o)) { isNull = true; } } else if (o is int) { if (Int32.MinValue == (int)o) { isNull = true; } } else if (o is short) { if (Int16.MinValue == (short)o) { isNull = true; } } else if (o is long) { if (Int64.MinValue == (long)o) { isNull = true; } } else if (o is string) { if (((string)o).Length == 0) { isNull = true; } } } if (!isNull) { // store the new value curItem.setVal(o); // flag that we need to notify excel later notify = true; } } } } } } } } } else { Trace.WriteLineIf(traceSwitch.TraceWarning, "Received unrecognised function call from plant: " + objs[0]); } } if (notify) { Trace.WriteLineIf(traceSwitch.TraceVerbose, "Update received"); if (xlRTDUpdate != null) { // tell excel to come and get the data xlRTDUpdate.UpdateNotify(); } } Trace.Flush(); } } catch (Exception e) { Trace.WriteLineIf(traceSwitch.TraceError, "Exception whilst receiving update: " + e.ToString()); lock (this) { if (conn != null) { try { conn.Close(); } catch (Exception) { } conn = null; exit = true; if (xlRTDUpdate != null) { xlRTDUpdate.Disconnect(); } } } } } Trace.WriteLineIf(traceSwitch.TraceVerbose, "Connection closed, exiting reader thread"); }
private void subscribe() { // connect to ticker plant connect(); // buid a list of tables and symbols that we want to subscribe to lock (this) { foreach (DictionaryEntry de in cache) { RTDData item = (RTDData)de.Value; ArrayList symbols = (ArrayList)tables[item.Table]; if (symbols == null) { symbols = new ArrayList(); } if (!symbols.Contains(item.Symbol)) { symbols.Add(item.Symbol); } } foreach (DictionaryEntry de in tables) { String table = "`" + (String)de.Key; ArrayList symbols = (ArrayList)de.Value; string [] ss = (string[])symbols.ToArray(typeof(String)); Array.Sort(ss); string s = ""; if (ss.Length > 0) { s += "`$("; for (int i = 0; i < ss.Length; i++) { if (i > 0) { s += ";"; } s += "\"" + ss[i] + "\""; } s += ")"; } else { s = "()"; } string substring = ".u.sub[" + table + ";" + s + "]"; // only send a new request if different from the last request if (!substring.Equals(lastSubs[table])) { Trace.WriteLineIf(traceSwitch.TraceInfo, "subscribing " + substring); conn.ks(substring); lastSubs[table] = substring; } } } }
public Object ConnectData(int topic, ref Array Strings, ref bool GetNewValues) { lock( this) { string table, column, symbol; int index=0; RTDData data= null; GetNewValues = true; try { string plant= Strings.GetValue(0).ToString().Trim(); if( plantName.Equals( plant)) { table = Strings.GetValue(1).ToString().Trim(); column = Strings.GetValue(2).ToString().Trim(); symbol = Strings.GetValue(3).ToString().Trim(); if( Strings.Length > 4) { index= Int32.Parse(Strings.GetValue(4).ToString().Trim()); } Trace.WriteLineIf( traceSwitch.TraceInfo, "ConnectData " + topic + "," + table + ":" + column + ":" + symbol); string key= table+column+symbol; data= (RTDData) cache[key]; if( data == null) { data= new RTDData(table,column,symbol); cache.Add(key, data); } if( !data.containsTopic( topic)) { data.addTopic( topic, index); } timer.Stop(); timer.Start(); return (data.getVal(topic)); } else { Trace.WriteLineIf( traceSwitch.TraceWarning, "Plant name " + plant + " is not recognised"); } } catch (Exception e) { Trace.WriteLineIf( traceSwitch.TraceInfo, "Exception: "+e.ToString()); } return null; } }