private static void WriteTagValues(PHDHistorian oPhd, List <Tags> taglist, Socket writer) { using (BlockingCollection <string> queue = new BlockingCollection <string>()) { Task consumer = new Task(() => { foreach (string value in queue.GetConsumingEnumerable()) // will block waiting for completion adding { WriteLine(writer, value); } }); Task producer = new Task(() => { taglist.ForEach((tags) => { System.Collections.ArrayList datas = oPhd.FetchStructData(tags); foreach (PHDataStruct pd in datas) { queue.Add(serializer.Serialize(pd)); // [TagName, Confidence, Value, Timestamp, Units] } }); queue.CompleteAdding(); }); producer.Start(); consumer.Start(); producer.Wait(); consumer.Wait(); } }
public static void Read(string tag, Tag MyTag, PHDHistorian phd, Double[] timestamps, Double[] values, short[] confidences, string pathOutput) { Console.WriteLine($"{DateTime.Now} - Start PHD query, tag: {tag}"); phd.FetchData(MyTag, ref timestamps, ref values, ref confidences); var pathOutput1 = pathOutput + tag + ".txt"; var writer = File.CreateText(pathOutput1); Console.WriteLine($"{DateTime.Now} - End PHD query, tag: {tag}"); if (timestamps != null) { int count = timestamps.GetUpperBound(0); Console.WriteLine($"{DateTime.Now} - Start writing to file"); Console.WriteLine($"{DateTime.Now} - Retrieved {count} data points."); using (writer) { writer.WriteLine($"No; timestamp; value; confidence"); for (int i = 0; i < count; ++i) { writer.WriteLine($"{i + 1}) {DateTime.FromOADate(timestamps[i])}, {values[i]}, {confidences[i]}"); } } Console.WriteLine($"{DateTime.Now} - End writing to file"); Console.WriteLine(); if (count > 2000000) { Console.WriteLine("Wait 1min due to big data package"); Thread.Sleep(15000); Console.WriteLine(); } } }
private static void Browse(Dictionary <string, string> args, Socket writer, PHDHistorian oPhd) { try { TagFilter filter = new TagFilter(); filter.Tagname = "*"; string tagmodel = "onlyname"; // tag info model, [onlyname, verbose]default is onlyname, uint count = 0; // maximum number of tags to be returned if (args.ContainsKey("tagname") && !string.IsNullOrEmpty(args["tagname"])) { filter.Tagname = args["tagname"]; } if (args.ContainsKey("datatype") && !string.IsNullOrEmpty(args["datatype"])) { filter.DataType = args["datatype"].ToCharArray()[0]; } if (args.ContainsKey("tagmodel") && !string.IsNullOrEmpty(args["tagmodel"])) { tagmodel = args["tagmodel"]; } if (args.ContainsKey("count") && !string.IsNullOrEmpty(args["count"])) { count = uint.Parse(args["count"]); } DataSet ds = oPhd.BrowsingTags(count, filter); DataTable table = ds.Tables[0]; WriteLine(writer, "200, Tags total number: " + table.Rows.Count);// return the number of browsed tags foreach (DataRow row in table.Rows) { if (tagmodel.Equals("verbose")) { // [Name, Tagno, Description, Units, SourceCollector, DataType, DataSize, SourceTagname, SourceUnits, ParentTagname, ParentTagno] WriteLine(writer, serializer.Serialize(TagModel(row))); } else { WriteLine(writer, (string)row["Name"]); } } WriteLine(writer, "###END###"); } catch (Exception e) { System.Console.Out.WriteLine("\nBrowse tags from {0}:{1} failed, \n{2}\n", oPhd.DefaultServer.HostName, oPhd.DefaultServer.Port, e.Message); WriteLine(writer, "200, false, " + e.Message); } }
private static void Ping(Dictionary <string, string> args, Socket writer, PHDHistorian oPhd) { try { DataSet ds = oPhd.GetLinks(); System.Console.Out.WriteLine("ping {0}:{1} successful", oPhd.DefaultServer.HostName, oPhd.DefaultServer.Port); WriteLine(writer, "200, true"); } catch (Exception e) { System.Console.Out.WriteLine("\nping {0}:{1} failed, \n{2}\n", oPhd.DefaultServer.HostName, oPhd.DefaultServer.Port, e.Message); WriteLine(writer, "200, false"); } }
public PHDR() { var configurationSection = GlobalAttributes.Configuration.GetSection("RTDBInfo").GetSection("PHD"); string tempIp = GlobalAttributes.Configuration.GetSection("RTDBInfo").GetSection("TempIP").Value; IConfigurationSection phdInfo = configurationSection.GetSection(tempIp); PHDServer _phdServer = new PHDServer { HostName = tempIp, UserName = phdInfo.GetSection("UserName").Value, Password = phdInfo.GetSection("Password").Value, APIVersion = SERVERVERSION.RAPI200 }; _session = new PHDHistorian { DefaultServer = _phdServer, Sampletype = SAMPLETYPE.Snapshot }; }
public PHD() { var configurationSection = GlobalAttributes.Configuration.GetSection("RTDBInfo").GetSection("PHD"); string tempIp = GlobalAttributes.Configuration.GetSection("RTDBInfo").GetSection("TempIP").Value; IConfigurationSection phdInfo = configurationSection.GetSection(tempIp); var _phdServer = new PHDServer(tempIp, SERVERVERSION.API200) { UserName = phdInfo.GetSection("UserName").Value, Password = phdInfo.GetSection("Password").Value, Port = Convert.ToInt32(phdInfo.GetSection("Port").Value), }; _log.Info($"UserName:{_phdServer.UserName}\r\nPassword:{_phdServer.Password}\r\nPort:{_phdServer.Port}"); _session = new PHDHistorian { DefaultServer = _phdServer }; }
private static List <Tags> TagsFromBrowse(PHDHistorian oPhd, Dictionary <string, string> args) { TagFilter filter = new TagFilter(); filter.Tagname = "*"; uint count = 0; if (args.ContainsKey("tagname") && !string.IsNullOrEmpty(args["tagname"])) { filter.Tagname = args["tagname"];// tag name pattern, such as "AC-*", default is "*" } if (args.ContainsKey("datatype") && !string.IsNullOrEmpty(args["datatype"])) { filter.DataType = args["datatype"].ToCharArray()[0];// data type, such as 'F', 'C', ... } if (args.ContainsKey("count") && !string.IsNullOrEmpty(args["count"])) { count = uint.Parse(args["count"]);// maxinum number to browse } List <Tags> taglist = new List <Tags>(); DataSet ds = oPhd.BrowsingTags(count, filter); DataTable table = ds.Tables[0]; Tags _tags = new Tags(); foreach (DataRow row in table.Rows) { _tags.Add(new Tag((string)row["Name"])); if (_tags.Count > TAGS_MAX_COUNT) { taglist.Add(_tags); _tags = new Tags(); } } if (_tags.Count > 0) { taglist.Add(_tags); } return(taglist); }
static void Main(string[] args) { { PHDHistorian phd = new PHDHistorian(); phd.DefaultServer = new PHDServer("162.20.0.26", SERVERVERSION.RAPI200); phd.Sampletype = SAMPLETYPE.Raw; phd.ReductionType = REDUCTIONTYPE.None; Double[] timestamps = null; Double[] values = null; short[] confidences = null; Console.WriteLine("Path of tag list:"); var path = Console.ReadLine(); Console.WriteLine("Path of output:"); var pathOutput = Console.ReadLine(); var tagList = new List <string>(); using (var reader = new StreamReader(path)) { while (!reader.EndOfStream) { var lines = reader.ReadLine(); tagList.Add(lines); } } Console.WriteLine("Start time (ralative time):"); phd.StartTime = Console.ReadLine(); Console.WriteLine("End time (ralative time):"); phd.EndTime = Console.ReadLine(); Console.WriteLine("Enter log file name:"); var logWrite = File.CreateText(Console.ReadLine()); int milliseconds = 60000; using (logWrite) { foreach (string tag in tagList) { Tag MyTag = new Tag(tag); try { PHDRead.Read(tag, MyTag, phd, timestamps, values, confidences, pathOutput); phd.Dispose(); } catch (Exception ex) { Console.WriteLine(ex.Message); logWrite.WriteLine($"{DateTime.Now} - {tag} - {ex.Message}"); if (!ex.Message.ToString().Contains("10012") && !ex.Message.ToString().Contains("10006") && !ex.Message.ToString().Contains("10068") && !ex.Message.ToString().Contains("10060")) { while (true) { phd.Dispose(); Thread.Sleep(milliseconds); try { PHDRead.Read(tag, MyTag, phd, timestamps, values, confidences, pathOutput); break; } catch (Exception ex1) { Console.WriteLine($"{DateTime.Now}: {ex1.Message}"); logWrite.WriteLine($"{DateTime.Now} - {tag} - {ex1.Message}"); } } } } } } } }
private static void Fetch(Dictionary <string, string> args, StreamReader reader, Socket writer, PHDHistorian oPhd) { oPhd.UTCDateTime = true; if (args.ContainsKey("starttime") && !string.IsNullOrEmpty(args["starttime"])) { oPhd.StartTime = args["starttime"]; } if (args.ContainsKey("endtime") && !string.IsNullOrEmpty(args["endtime"])) { oPhd.EndTime = args["endtime"]; } if (args.ContainsKey("sampletype") && !string.IsNullOrEmpty(args["sampletype"])) { oPhd.Sampletype = SampleType(args["sampletype"]); // [average, snapshot, resampled, interpolatedraw, raw ]: default is raw } if (args.ContainsKey("reductiontype") && !string.IsNullOrEmpty(args["reductiontype"])) { oPhd.ReductionType = ReductionType(args["reductiontype"]);// [ average, max, min, first, last, none ]: default is none } if (args.ContainsKey("offset") && !string.IsNullOrEmpty(args["offset"])) { oPhd.ReductionOffset = ReductionOffset(args["offset"]); // [ after, around, before ]: default is before } if (args.ContainsKey("samplefrequency") && !string.IsNullOrEmpty(args["samplefrequency"])) { oPhd.SampleFrequency = uint.Parse(args["samplefrequency"]); } if (args.ContainsKey("reductionfrequency") && !string.IsNullOrEmpty(args["reductionfrequency"])) { oPhd.ReductionFrequency = uint.Parse(args["reductionfrequency"]); } try { string tagsource = ""; if (args.ContainsKey("tagsource") && !string.IsNullOrEmpty(args["tagsource"])) { tagsource = args["tagsource"]; } List <Tags> taglist = new List <Tags>(); if (tagsource.Equals("browse")) { taglist = TagsFromBrowse(oPhd, args); } else if (tagsource.Equals("list")) { string list = args["list"]; // must taglist.Add(TagsFromList(list)); } else //if (tagsource.Equals("lines")) { while (true) { string line = reader.ReadLine(); if (line.Contains("###END###")) { break; } taglist.Add(TagsFromList(line)); } } WriteTagValues(oPhd, taglist, writer); } catch (Exception e) { System.Console.Out.WriteLine("\nFetch tag values from {0}:{1} failed, \n{2}\n", oPhd.DefaultServer.HostName, oPhd.DefaultServer.Port, e.Message); WriteLine(writer, "200, false, " + e.Message); } }
public static void HandleRequest(TcpClient tcpclient, StreamReader reader, Dictionary <string, string> args) { string hostname; string requesttype; try { hostname = args["hostname"]; requesttype = args["type"]; // [browse, fetch, ping]: default is ping } catch (Exception e) { System.Console.Out.WriteLine("hostname argument not available: {0}", e.Message); WriteLine(tcpclient.Client, "400, Bad Request, hostname argument not available"); return; } using (PHDHistorian oPhd = new PHDHistorian()) { using (PHDServer server = new PHDServer(hostname, SERVERVERSION.RAPI200)) { if (args.ContainsKey("port") && !string.IsNullOrEmpty(args["port"])) { server.Port = int.Parse(args["port"]); } //SafeGet(args, "username", server.UserName); if (args.ContainsKey("username") && !string.IsNullOrEmpty(args["username"])) { server.UserName = args["username"]; } if (args.ContainsKey("password") && !string.IsNullOrEmpty(args["password"])) { server.Password = args["password"]; } if (args.ContainsKey("windowsuser") && !string.IsNullOrEmpty(args["windowsuser"])) { server.WindowsUsername = args["windowsuser"]; } if (args.ContainsKey("windowspwd") && !string.IsNullOrEmpty(args["windowspwd"])) { server.WindowsPassword = args["windowspwd"]; } oPhd.DefaultServer = server; if (requesttype.Equals("browse")) { Console.Out.WriteLine("start to browse ..."); Browse(args, tcpclient.Client, oPhd); } else if (requesttype.Equals("fetch")) { Console.Out.WriteLine("start to fetch ..."); Fetch(args, reader, tcpclient.Client, oPhd); } else { Console.Out.WriteLine("start to ping ..."); Ping(args, tcpclient.Client, oPhd); } } } }
static void Main() { PHDHistorian PHD = new PHDHistorian(); PHDServer DefaultServer = new PHDServer("192.168.0.4"); DefaultServer.APIVersion = SERVERVERSION.RAPI200; //DefaultServer.UserName = "******"; //DefaultServer.Password = "******"; //PutData DefaultServer.WindowsUsername = "******"; DefaultServer.WindowsPassword = "******"; PHD.DefaultServer = DefaultServer; PHD.DefaultServer.Port = 3150; string flag = "Read"; if (flag == "Read") { Double[] timestamps = null; Double[] values = null; short[] confidences = null; Tag MyTag = new Tag("RTOR.TI1237.DACA.PV"); //PHD.StartTime = "NOW-1H"; //PHD.EndTime = "NOW"; PHD.StartTime = "6/12/2020 9:00"; PHD.EndTime = "6/12/2020 10:00"; //PHD.Sampletype = SAMPLETYPE.Raw; PHD.Sampletype = SAMPLETYPE.Average; //PHD.SampleFrequency = 3600; //PHD.ReductionType = REDUCTIONTYPE.None; PHD.ReductionType = REDUCTIONTYPE.Average; PHD.ReductionFrequency = 300; PHD.FetchData(MyTag, ref timestamps, ref values, ref confidences); int count = timestamps.GetUpperBound(0); Console.WriteLine("Retrieved {0} data points", count); Console.WriteLine(); for (int i = 0; i < count; ++i) { Console.WriteLine("{0},{1},{2},{3}", i + 1, DateTime.FromOADate(timestamps[i]), values[i], confidences[i]); } //Console.WriteLine("{0},{1},{2},{3}", // count + 1, // DateTime.FromOADate(timestamps[count]), // values[count], // confidences[count]); } else if (flag == "Write") { Tag MyTag = new Tag("RTOS.TEST.PV"); float floatValue = 3; PHD.PutData(MyTag, floatValue); } PHD.Dispose(); }