///////Display tags in certain time frame/////////////////// public List <PointVal> searchPiPoints(string tagName, string startTime, string endTime) { //Create new list List <PointVal> list = new List <PointVal>(); try { //Get point from tagname PIPoint point = PI_Server.PIPoints[tagName]; //Get recorded values for selected time // in order to display only those points in the time frame choosen by user PIValues values = point.Data.RecordedValues(startTime, endTime); //Walk through values list and save each value foreach (PIValue value in values) { //Create new value object PointVal val = new PointVal(); //Save date from values timestamp val.date = value.TimeStamp.LocalDate.ToString(); //Save the value, but first check to see if value is a digital state or an actual value // when a tag is first created it does not have a value and therefore it does not //contain a value. Instead is displays " NO DATA" which means it is in digital state DigitalState state = value.Value as DigitalState; if (state != null) { //Not a value, it's a digital state // it turns it into a string in order to be saved and displayed val.value = (value.Value as DigitalState).Name.ToString(); } else { //Convert value to object, then object to string object obj = value.Value; val.value = obj.ToString(); } //Add new value to list list.Add(val); } } catch (Exception ex) { } return(list);// return the tags recorded on the list }
///////Display tags in certain time frame/////////////////// public List<PointVal> searchPiPoints(string tagName, string startTime, string endTime) { //Create new list List<PointVal> list = new List<PointVal>(); try { //Get point from tagname PIPoint point = PI_Server.PIPoints[tagName]; //Get recorded values for selected time // in order to display only those points in the time frame choosen by user PIValues values = point.Data.RecordedValues(startTime, endTime); //Walk through values list and save each value foreach (PIValue value in values) { //Create new value object PointVal val = new PointVal(); //Save date from values timestamp val.date = value.TimeStamp.LocalDate.ToString(); //Save the value, but first check to see if value is a digital state or an actual value // when a tag is first created it does not have a value and therefore it does not //contain a value. Instead is displays " NO DATA" which means it is in digital state DigitalState state = value.Value as DigitalState; if (state != null) { //Not a value, it's a digital state // it turns it into a string in order to be saved and displayed val.value = (value.Value as DigitalState).Name.ToString(); } else { //Convert value to object, then object to string object obj = value.Value; val.value = obj.ToString(); } //Add new value to list list.Add(val); } } catch (Exception ex) { } return list;// return the tags recorded on the list }
/// <summary> /// Monitor PI points for changes /// </summary> /// <param name="ar"></param> public static void osiReadCallback(IAsyncResult ar) { Console.WriteLine("In osiReadCallBack..."); // Retrieve the state object and the handler socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket handler = state.workSocket; /****Search current VFD State and Frequency PI values*****/ //initailize OSI PI and server OSIPI myPI = new OSIPI(); myPI.connect_Server("ESMARTSERVER-PC"); //Create PI values and Lists for each PointVal savedVfdState = null; PointVal currentVfdState = null; PointVal savedDesiredFlow = null; PointVal currentDesiredFlow = null; List <PointVal> vfdStateList; List <PointVal> desiredFlowList; bool firstRun = false; if (myPI.check_connection()) { //put VFD state, frequency, lock, and desired floe rate in a saved PI points vfdStateList = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString()); savedVfdState = currentVfdState = vfdStateList.ElementAt(0); desiredFlowList = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString()); savedDesiredFlow = currentDesiredFlow = desiredFlowList.ElementAt(0); } while (true) { //get current OSI PI value for VFD, frequency, lock, and desire flow rate if (myPI.check_connection()) { vfdStateList = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString()); currentVfdState = vfdStateList.ElementAt(0); desiredFlowList = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString()); currentDesiredFlow = desiredFlowList.ElementAt(0); } //check for first run to send initail DesiredFlow value if (firstRun == false) { //Send to Rasp PI to initiate function to change flow via frequency savedDesiredFlow = currentDesiredFlow; osiSend(handler, "<,DF=" + savedDesiredFlow.value.ToString() + ",EOF,>"); firstRun = true; } //check if the OSI server value has changed for VfdState if (currentVfdState.value != savedVfdState.value) { //send current value to Rasp PI savedVfdState = currentVfdState; osiSend(handler, "<,S=" + savedVfdState.value.ToString() + ",EOF,>"); } //check if the OSI server value has changed for desired flow rate if (currentDesiredFlow.value != savedDesiredFlow.value) { //send current value to Rasp PI savedDesiredFlow = currentDesiredFlow; osiSend(handler, "<,DF=" + savedDesiredFlow.value.ToString() + ",EOF,>"); } Thread.Sleep(5000); //sleep 5 sec then update } }
/// <summary> /// Listen for changes in the OSI points and send changes to Rasp PI (VFD State, Flow, Lock, and Desired Flow Rate) /// </summary> public static void osiListen() { // Data buffer for incoming data. byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socket. IPAddress ipAddress = IPAddress.Parse("192.168.144.144"); int port = 9700; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen for incoming connections. try { //listener.Connect(localEndPoint); listener.Bind(localEndPoint); listener.Listen(100); while (true) { // Set the event to nonsignaled state. allDone.Reset(); if (!listener.Poll(300, SelectMode.SelectRead)) { Console.WriteLine("Dong Dong Dong"); } // Start an asynchronous socket to listen for connections. Console.WriteLine("Waiting for Rasp PI connection..."); listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); // Wait until a connection is made before continuing. allDone.WaitOne(); Console.WriteLine("Rasp PI connected!!!!"); } } catch (Exception e) { Console.WriteLine(e.ToString()); } /****Search current VFD State and Frequency PI values*****/ //initailize OSI PI and server OSIPI myPI = new OSIPI(); myPI.connect_Server("ESMARTSERVER-PC"); //Create PI values and Lists for each PointVal savedVfdState = null; PointVal currentVfdState = null; PointVal savedFreq = null; PointVal currentFreq = null; PointVal savedLockState = null; PointVal currentLockState = null; PointVal savedDesiredFlow = null; PointVal currentDesiredFlow = null; List <PointVal> vfdStateList; List <PointVal> freqList; List <PointVal> lockStateList; List <PointVal> desiredFlowList; if (myPI.check_connection()) { //put VFD state, frequency, lock, and desired floe rate in a saved PI points vfdStateList = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString()); savedVfdState = currentVfdState = vfdStateList.ElementAt(0); freqList = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString()); savedFreq = currentFreq = freqList.ElementAt(0); lockStateList = myPI.searchPiPoints("SP14VICE_Lock", DateTime.Now.ToString(), DateTime.Now.ToString()); savedLockState = currentLockState = lockStateList.ElementAt(0); desiredFlowList = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString()); savedDesiredFlow = currentDesiredFlow = desiredFlowList.ElementAt(0); } while (true) { //get current OSI PI value for VFD, frequency, lock, and desire flow rate if (myPI.check_connection()) { vfdStateList = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString()); currentVfdState = vfdStateList.ElementAt(0); freqList = myPI.searchPiPoints("SP14VICE_Freq", DateTime.Now.ToString(), DateTime.Now.ToString()); currentFreq = freqList.ElementAt(0); lockStateList = myPI.searchPiPoints("SP14VICE_Lock", DateTime.Now.ToString(), DateTime.Now.ToString()); currentLockState = lockStateList.ElementAt(0); desiredFlowList = myPI.searchPiPoints("SP14VICE_DesiredFlow", DateTime.Now.ToString(), DateTime.Now.ToString()); currentDesiredFlow = desiredFlowList.ElementAt(0); } //check if the OSI server value has changed for VfdState if (currentVfdState.value != savedVfdState.value) { //send current value to Rasp PI savedVfdState = currentVfdState; Send(listener, "<,S=" + savedVfdState.ToString() + ",EOF,>"); // PythonComm.sendTCP("192.168.144.126", 9750, "<,S=" + savedVfdState.ToString() + ",EOF,>"); } //check if the OSI server value has changed for frequency if (currentFreq.value != savedFreq.value) { //send current value to Rasp PI savedFreq = currentFreq; Send(listener, "<,F=" + savedFreq.ToString() + ",EOF,>"); // PythonComm.sendTCP("192.168.144.126", 9750, "<,F=" + savedFreq.ToString() + ",EOF,>"); } //check if the OSI server value has changed for Lock if (currentLockState.value != savedLockState.value) { //send current value to Rasp PI savedLockState = currentLockState; Send(listener, "<,S=" + savedLockState.ToString() + ",EOF,>"); // PythonComm.sendTCP("192.168.144.126", 9750, "<,S=" + savedLockState.ToString() + ",EOF,>"); } //check if the OSI server value has changed for desired flow rate if (currentLockState.value != savedLockState.value) { //Initiate function to change flow via frequency savedDesiredFlow = currentDesiredFlow; Thread changeFreq = new Thread(new ParameterizedThreadStart(freqConversion)); //thread to change frequency changeFreq.Start(savedDesiredFlow.value); //start thread } Thread.Sleep(5000); //sleep 5 sec then update } }
public static void osiListen() { // Data buffer for incoming data. byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socket. IPAddress ipAddress = IPAddress.Parse("192.168.144.144"); int port = 9700; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen for incoming connections. try { //listener.Connect(localEndPoint); listener.Bind(localEndPoint); listener.Listen(100); while (true) { // Set the event to nonsignaled state. allDone.Reset(); if (!listener.Poll(300, SelectMode.SelectRead)) { Console.WriteLine("Dong Dong Dong"); } // Start an asynchronous socket to listen for connections. Console.WriteLine("Waiting for Rasp PI connection..."); listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); // Wait until a connection is made before continuing. allDone.WaitOne(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } /****Search current VFD State PI value*****/ OSIPI myPI = new OSIPI(); myPI.connect_Server("ESMARTSERVER-PC"); PointVal stateVal = null; PointVal currentVal = null; if (myPI.check_connection()) { //put VFD state in a saved PI point List <PointVal> vfdState = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString()); stateVal = vfdState.ElementAt(0); } while (true) { //get current OSI PI value for VFD if (myPI.check_connection()) { List <PointVal> vfdCurrentState = myPI.searchPiPoints("SP14VICE_VfdState", DateTime.Now.ToString(), DateTime.Now.ToString()); currentVal = vfdCurrentState.ElementAt(0); } //check if the OSI server value has changed if (currentVal != stateVal) { //send current value to Rasp PI stateVal = currentVal; Send(listener, "<,S=" + stateVal.ToString() + ",EOF,>"); } Thread.Sleep(5000); } }