//Matlab /// <summary> /// Checks to see if Matlab is still open and asks to Reload the logged data and reopens matlab. /// </summary> /// <param name="showDialog"> Whether to show a dialog with yes or no.</param> public void checkMatlab(bool showDialog) { if (showDialog) { bool hasMatlab = MatlabAccess.isMatlabOpen(); if (!hasMatlab && !checkedMatlab) { DialogResult result1 = MessageBox.Show(" Matlab has been closed. Do you want to reopen Matlab and load with Logged inputs? To start with an empty workspace, click No.", "Matlab has Closed", MessageBoxButtons.YesNoCancel); if (result1 == DialogResult.Yes) { MatlabAccess.MatlabStartup(); LogSystem.SaveLog("temp.txt"); LogSystem.Clear(); importLog("temp.txt"); checkedMatlab = false; } else if (result1 == DialogResult.No) { MatlabAccess.MatlabStartup(); } else { checkedMatlab = true; } } } else { MatlabAccess.MatlabStartup(); LogSystem.SaveLog("temp.txt"); LogSystem.Clear(); importLog("temp.txt"); checkedMatlab = false; } }
// Import to AF /// <summary> /// Gets value from Matlab and writes it to AF. /// </summary> /// <remarks> Will not write, if the Attribute is read-only. A Matlab Variable Name must be input.</remarks> /// <param name="path"> The path to the Element to search with.</param> /// <param name="workspaceVariableName">The variable name in Matlab being used.</param> /// <param name="AFName">The attribute name in AF being written to.</param> public void ImportToAF(string path, string workspaceVariableName, string AFName) { object val = null; double dbVal; //LOGIC: A variable name must be entered. try { MatlabAccess.GetWorkspaceData(workspaceVariableName, "base", out val); } catch { mainForm.Status("Couldn't find the variable in the Matlab Workspace"); } List <string> searchPaths = new List <string>() { path }; AFKeyedResults <string, AFElement> results = AFElement.FindElementsByPath(searchPaths, null); AFElement Element = results[path]; AFAttribute Attribute = Element.Attributes[AFName]; double.TryParse(val.ToString(), out dbVal); try { AFAccess.writeToAF(Element, Attribute, dbVal); } catch { mainForm.Status("Cannot Write to this Attribute"); } }
/// <summary> /// Accesses the AFServer to get data. /// </summary> /// <param name="server_database"> The string of the server and database 'server'-'database'</param> /// <param name="attName"> Name of the Attribute.</param> /// <param name="MatlabName"> Variable name to be used in Matlab workspace.</param> /// <param name="searchPath"> The path to the Element.</param> /// <param name="start">The time at which data will start being imported from.</param> /// <param name="end">The time when data will stop being imported.</param> /// <param name="edit"> true: Adds the log input into the LogSystem (generally true)</param> /// <returns></returns> public void getAFData(string server_database, string attName, string MatlabName, string searchPath, string start, string end, bool edit) { MatlabName = MatlabAccess.modifyMatlabName(MatlabName); if (MatlabName == string.Empty) { return; } try { AFAccess.getAFData(server_database, attName, MatlabName, searchPath, start, end, edit); mainForm.Status("Data sent for " + attName); return; } catch { checkMatlab(true); mainForm.Status("ERROR: Data not sent for " + attName); return; } }
// Export Data /// <summary> /// Get the Data from PIServer using AFSDK. /// </summary> /// <param name="point"> The name of the PIPoint.</param> /// <param name="server"> The PIServer</param> /// <param name="MatlabName"> The name of the variable to be used for Matlab.</param> /// <param name="start"> The time to start taking data from.</param> /// <param name="end">The time to stop taking data from.</param> /// <param name="edit"> true: Adds the log to the LogSystem (generally true)</param> /// <returns></returns> public void getPIData(string point, string server, string MatlabName, string start, string end, bool edit) { string name = MatlabAccess.modifyMatlabName(MatlabName); if (name == string.Empty) { return; } try { AFAccess.getPIData(name, server, point, start, end, edit); mainForm.Status("Data sent for " + point); return; } catch { checkMatlab(true); mainForm.Status("ERROR: Data not sent for " + point); return; } }
/// <summary> /// Resulting Action of editting the List View. /// 1) Edit the List Item /// 2) Re-log the item /// 3) Edit the Matlab workspace. /// </summary> /// <param name="name"> The new variable name. (or the same)</param> /// <param name="start"> The new start time. (or the same)</param> /// <param name="end"> The new end time. (or the same)</param> /// <param name="previous"></param> public void EditLog(string name, string start, string end, LogInput previous) { MatlabAccess.removeMatlabVariable(previous.getKeyVariableName()); if (previous.getKeyVariableName() != name) { name = MatlabAccess.modifyMatlabName(name); } if (name == string.Empty) { name = previous.getKeyVariableName(); } string attName = previous.getAttribute(); string path = previous.getPath(); string elem = previous.getElement(); //EDIT LIST VIEW if (AFAccess.isAbsoluteTimeString(start, end, previous)) { lv_LogDialog.SelectedItems[0].SubItems[3].Text = start + " = " + end; lv_LogDialog.SelectedItems[0].Text = name; } else { AFTimeRange range = new AFTimeRange(start, end); lv_LogDialog.SelectedItems[0].SubItems[3].Text = range.ToString(); lv_LogDialog.SelectedItems[0].Text = name; } //EDIT ACTUAL LOG LogSystem.removeLogInput(previous.getKeyVariableName(), previous.getServerDatabase()); //Workspace Edit - remove variable, getNewData if (path == "PI.Point") { string[] info = Regex.Split(previous.getServerDatabase(), "'"); control.getPIData(attName, info[1], name, start, end, false); } else { control.getAFData(previous.getServerDatabase(), attName, name, path, start, end, false); } }
//TODO add time reliance to getPIData public static void getPIData(string MatlabName, string server, string point, string start, string end) { LogInput logInput; //LOG DATA AFTimeRange aftr; //TIMERANGE try { aftr = checkAFTimeRange(start, end); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } PISDK.PISDKClass _pisdk = new PISDK.PISDKClass(); PISDK.PIPoint pt = _pisdk.Servers[server].PIPoints[point]; // Add the correct SERVER NAME & PI Points PISDK.PIValues values = pt.Data.RecordedValues(start, end, PISDK.BoundaryTypeConstants.btAuto, "", PISDK.FilteredViewConstants.fvRemoveFiltered, null); //Get Values logInput = new LogInput(MatlabName, "PI.Point", point, "PI.Point", aftr); isRelativeTimeString(start, end, logInput); double[] timeStamp = new double[values.Count]; int i = 0; foreach (PISDK.PIValue val in values) { DateTime UTCTime = val.TimeStamp.LocalDate; timeStamp[i] = UTCTime.ToOADate() + 693960; i++; } logInput.setAttributeGetValueFormat(0); logInput.setTimespaceFormat("ts0"); string server_database = "'" + server + "'-'PI.Point'"; logInput.setServerDatabase(server_database); LogSystem.addLogInput(server_database, logInput, true); MatlabAccess.sendDataToMatlab(MatlabName, PIValuesToArray(values)); //Export MatlabAccess.sendDataToMatlab(MatlabName + "Time", timeStamp); }
/// <summary> /// Starts up the Desktop Matlab or attaches to COM-Enabled Matlab. /// to Com-Enable Matlab use [ enableservice('AutomationServer',true);] /// </summary> public MainControl() { // Matlab Startup and Connection to Desktop MatlabAccess.MatlabStartup(); checkedMatlab = false; }
/// <summary> /// Common calls of getting Data from AFServer /// </summary> /// <param name="server_database">String representing the server and database</param> /// <param name="AttributeName"> name of the attribute</param> /// <param name="MatlabName">variable name for the Matlab Workspace</param> /// <param name="start">Start time of data collection.</param> /// <param name="end">End time of the data collection.</param> /// <param name="afobject"> AF object - AFAttribute, AFEventFrame, or PIPoint</param> /// <param name="addToListView"> Whether to add to the Listview (generally true)</param> public static void getData(string server_database, string AttributeName, string MatlabName, string start, string end, Object afobject, bool addToListView) { MatlabName = MatlabAccess.modifyMatlabName(MatlabName); LogInput logInput = null; AFValues Values = new AFValues(); AFAttribute attribute; object[] vals; double[] dbVals; double[] timestamps = null; int[] statuses; int baddata; //TIME RANGE AFTimeRange aftr; try { aftr = checkAFTimeRange(start, end); } catch (Exception ex) { MessageBox.Show(ex.Message); throw ex; } /// Get Object Type type = afobject.GetType(); string typestring = type.ToString(); // LogInput logInput = new LogInput(MatlabName, Element.Name, attribute.Name, path, aftr); switch (type.ToString()) { case "OSIsoft.AF.Asset.AFAttribute": attribute = (AFAttribute)afobject; logInput = new LogInput(MatlabName, attribute.Element.Name, attribute.Name, attribute.Element.GetPath(), aftr); if (attribute.PIPoint == null) // Constant Value { aftr = new AFTimeRange("*", "*"); logInput.setAFTimeRange(aftr); Values = attribute.GetValues(aftr, dataPref, null); } else // PI Point - Time Matters!! { /* Summary: Attribute.GetValues - Important Call! * Parameter Int32 => DataPref * = 0 : All Values returned * < 0 : Evenly spaced values, including start and end * > 0 : # of intervals, for each interval 5 points are given (first, last, high, low, and exceptional) */ Values = attribute.GetValues(aftr, dataPref, null); // FULLY UNDERSTAND THIS !!! Important Call!!!! } break; case "OSIsoft.AF.EventFrame.AFEventFrame": AFEventFrame frame = (AFEventFrame)afobject; logInput = new LogInput(MatlabName, frame.Name, frame.Attributes[AttributeName].Name, frame.GetPath(), aftr); attribute = frame.Attributes[AttributeName]; logInput.setAFTimeRange(aftr); AFValue val = attribute.GetValue(aftr); Values = new AFValues() { val }; break; case "OSIsoft.AF.PI.PIPoint": PIPoint piPoint = (PIPoint)afobject; string path = piPoint.GetPath(); logInput = new LogInput(MatlabName, "PI.Point", piPoint.Name, "PI.Point", aftr); Values = piPoint.RecordedValues(aftr, AFBoundaryType.Interpolated, "", true, Int32.MaxValue); break; } //Determine if AFTIME Absolute String isAbsoluteTimeString(start, end, logInput); logInput.setAttributeGetValueFormat(dataPref); logInput.setTimespaceFormat(dataFormat); ConvertAFValues.GetValuesArray(Values, false, out vals, out timestamps, out statuses, out baddata); try { MatlabAccess.sendDataToMatlab(MatlabName, AFValuesToArray(vals)); if (Timestamp) { MatlabAccess.sendDataToMatlab(MatlabName + "Time", timestamps); } } catch { logInput.setServerDatabase(server_database); LogSystem.addLogInput(server_database, logInput, addToListView); throw new NullReferenceException(); } logInput.setServerDatabase(server_database); LogSystem.addLogInput(server_database, logInput, addToListView); }