public void Test_DatReader() { int tag_count = 0; int val_count = 0; DatReader dr = new DatReader("../../InputData"); foreach (string floatfile_name in dr.GetFloatfiles()) { Console.Write($"{floatfile_name}\n"); foreach (DatTagRecord tag in dr.ReadTagFile(floatfile_name)) { tag_count++; } foreach (DatFloatRecord val in dr.ReadFloatFile(floatfile_name)) { val_count++; } } Assert.AreEqual(tag_count, 32); Assert.AreEqual(val_count, 128); }
public static void MakeFTH(DatReader dr, string floatfile_name, StreamWriter outwriter, List <string> all_tagnames) { Dictionary <int, string> pointnames = new Dictionary <int, string>(); foreach (DatTagRecord tag in dr.ReadTagFile(floatfile_name)) { pointnames[tag.id] = PIAPI.TagToPoint(tag.name); if (!all_tagnames.Contains(tag.name)) { all_tagnames.Add(tag.name); } } int batch_count = 0; string batch = ""; CultureInfo culture = new CultureInfo("en-US"); foreach (DatFloatRecord val in dr.ReadFloatFile(floatfile_name)) { if (val.status != 'U') { batch += $"{pointnames[val.tagid]},{val.datetime.ToString("dd-MMM-yy HH:mm:ss.fff", culture)},{val.val}\n"; batch_count++; } if (batch_count == Globals.BATCH_SIZE) { outwriter.Write(batch); // async? batch_count = 0; batch = ""; } } if (batch_count > 0) { outwriter.Write(batch); } }
public static void MakeSQL(DatReader dr, string floatfile_name, SqlConnection conn, string TagTable, string FloatTable) { Dictionary <int, int> sql_tagids = new Dictionary <int, int>(); string query_select = $"SELECT [TagIndex],[TagType],[TagDataType] FROM [{TagTable}] WHERE TagName = @1"; SqlCommand cmd_select = new SqlCommand(query_select, conn); cmd_select.Parameters.Add("@1", SqlDbType.NVarChar, 255); string query_insert = $"INSERT INTO [{TagTable}] VALUES (@1,@2,@3,@4)"; SqlCommand cmd_insert = new SqlCommand(query_insert, conn); cmd_insert.Parameters.Add("@1", SqlDbType.NVarChar, 255); cmd_insert.Parameters.Add("@2", SqlDbType.SmallInt); cmd_insert.Parameters.Add("@3", SqlDbType.SmallInt); cmd_insert.Parameters.Add("@4", SqlDbType.SmallInt); string tagname_filename = floatfile_name.Replace(" (Float)", " (Tagname)"); foreach (DatTagRecord tag in dr.ReadTagFile(floatfile_name)) { bool tag_exists = false; cmd_select.Parameters["@1"].Value = tag.name; SqlDataReader reader = cmd_select.ExecuteReader(); if (reader.HasRows) { reader.Read(); Int16 ti = reader.GetInt16(0); byte tt = (byte)reader.GetInt16(1); Int16 td = reader.GetInt16(2); if (tt == tag.type && td == tag.dtype) { tag_exists = true; sql_tagids[tag.id] = ti; } } reader.Close(); if (!tag_exists) { Console.WriteLine("creating {0} -> {1} T:{2} DT:{3}", tag.id, tag.name, tag.type, tag.dtype); cmd_insert.Parameters["@1"].Value = tag.name; cmd_insert.Parameters["@2"].Value = tag.id; cmd_insert.Parameters["@3"].Value = tag.type; cmd_insert.Parameters["@4"].Value = tag.dtype; int affected = cmd_insert.ExecuteNonQuery(); sql_tagids[tag.id] = tag.id; } reader.Close(); } string query_tpl = $"INSERT INTO [{FloatTable}] VALUES "; int batch_count = 0; string batch = ""; foreach (DatFloatRecord val in dr.ReadFloatFile(floatfile_name)) { batch_count++; DateTime datetime_sec = DateTime.ParseExact(new string(val.time_sec), "yyyyMMddHH:mm:ss", CultureInfo.InvariantCulture); batch += $"('{datetime_sec.ToString("yyyy-MM-dd HH:mm:ss")}',{val.milli},{sql_tagids[val.tagid]},{val.val},'{val.status}','{val.marker}'),"; if (batch_count == Globals.BATCH_SIZE) { string query = query_tpl + batch.Substring(0, batch.Length - 1); SqlCommand cmd = new SqlCommand(query, conn); int affected = cmd.ExecuteNonQuery(); batch_count = 0; batch = ""; } } if (batch_count > 0) { string query = query_tpl + batch.Substring(0, batch.Length - 1); SqlCommand cmd = new SqlCommand(query, conn); int affected = cmd.ExecuteNonQuery(); batch_count = 0; batch = ""; } }
static void Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Error: argument not found"); Console.WriteLine("Usage: dat2fth ServerName PointPrefix [Path]"); return; } PIAPI.Connect(args[0]); PIAPI.POINT_PREFIX = args[1]; string path = "."; if (args.Length >= 3) { path = args[2]; } // TODO: expose this for easy filtering List <string> allowed_tagnames = new List <string>(); //allowed_tagnames.Add("AI\\FY59_1"); DatReader dr = new DatReader(path); foreach (string floatfile_name in dr.GetFloatfiles()) { Console.WriteLine("converting {0}", Path.GetFileName(floatfile_name)); Dictionary <int, int> pointids = new Dictionary <int, int>(); foreach (DatTagRecord tag in dr.ReadTagFile(floatfile_name)) { if (allowed_tagnames.Count > 0 && !allowed_tagnames.Contains(tag.name)) { continue; } string pointname = PIAPI.TagToPoint(tag.name); pointids[tag.id] = PIAPI.GetPointNumber(pointname); } int batch_count = 0; Int32[] ptids = new Int32[Globals.BATCH_SIZE]; double[] vs = new double[Globals.BATCH_SIZE]; PITIMESTAMP[] ts = new PITIMESTAMP[Globals.BATCH_SIZE]; // can use intermediate storage, more readable but less speed // List<FloatSnapshot> snaps = new List<FloatSnapshot>(); foreach (DatFloatRecord val in dr.ReadFloatFile(floatfile_name)) { if (!pointids.ContainsKey(val.tagid)) { continue; } Int32 ptid = pointids[val.tagid]; ptids[batch_count] = ptid; vs[batch_count] = val.val; ts[batch_count] = new PITIMESTAMP(val.datetime); batch_count++; if (batch_count == Globals.BATCH_SIZE) { PIAPI.PutSnapshots(batch_count, ptids, vs, ts); batch_count = 0; } } if (batch_count > 0) { PIAPI.PutSnapshots(batch_count, ptids, vs, ts); } } }