Ejemplo n.º 1
0
        public void GetMetadata()
        {
            var cmd = new CommandGetMetadata("Measurement", new string[] { "ID", "SignalID", "TagName" });

            cmd.DebugToConsole();
            cmd = (CommandGetMetadata)(CtpCommand)cmd;
        }
        public void ProcessCommand(CommandGetMetadata command, CtpNetStream encoder)
        {
            var repository = m_repository;

            if (repository.ContainsTable(command.Table))
            {
                var table         = repository[command.Table];
                var columns       = new List <MetadataColumn>();
                var columnIndexes = new List <int>();
                if (command.Columns == null || command.Columns.Count == 0)
                {
                    columns.AddRange(table.Columns);
                    for (int x = 0; x < columns.Count; x++)
                    {
                        columnIndexes.Add(x);
                    }
                }
                else
                {
                    foreach (var column in command.Columns)
                    {
                        var c = table.Columns.FindIndex(x => x.Name == column);
                        if (c < 0)
                        {
                            encoder.Send(new CommandMetadataRequestFailed("Syntax Error", "Could not find the specified column " + column));
                            return;
                        }
                        columnIndexes.Add(c);
                        columns.Add(table.Columns[c]);
                    }
                }

                byte channelID = 1;
                int  rowCount  = 0;

                var         rowEncoder = new MetadataRowEncoder(columns);
                CtpObject[] values     = new CtpObject[columns.Count];
                encoder.Send(new CommandBeginMetadataResponse(channelID, Guid.Empty, repository.RuntimeID, repository.VersionNumber, command.Table, columns));
                foreach (var row in table.Rows)
                {
                    for (int x = 0; x < values.Length; x++)
                    {
                        values[x] = row.Fields[columnIndexes[x]];
                    }
                    rowEncoder.AddRow(values);
                    if (rowEncoder.Size > 30000)
                    {
                        throw new NotImplementedException();
                        //encoder.SendRaw(rowEncoder.ToArray(), channelID);
                        rowEncoder.Clear();
                    }

                    rowCount++;
                }
                if (rowEncoder.Size > 0)
                {
                    throw new NotImplementedException();
                    //encoder.SendRaw(rowEncoder.ToArray(), channelID);
                    rowEncoder.Clear();
                }
                encoder.Send(new CommandEndMetadataResponse(channelID, rowCount));
            }
            else
            {
                encoder.Send(new CommandMetadataRequestFailed("Syntax Error", "Could not find the specified table " + command.Table));
            }
        }
        //[TestMethod]
        //public void TestGetMetadataSimple()
        //{
        //    var db = Load();

        //    Queue<byte[]> packets = new Queue<byte[]>();

        //    var writer = new WireEncoder();
        //    var reader = new WireDecoder();

        //    writer.NewPacket += (bytes, start, length) => packets.Enqueue(Clone(bytes, start, length));

        //    var s = new CommandGetMetadataBasic(null, null, "Measurement", db["Measurement"].Columns.Select(x => x.Name));
        //    //statements.Add(BuildRequest("Vendor", "ID", "Acronym", "Name"));
        //    //var s = BuildRequest("Measurement", db["Measurement"].Columns.Select(x => x.Name).ToArray());
        //    var s2 = s.ToSttpMarkup();
        //    Console.WriteLine(s2.EncodedSize);
        //    Console.WriteLine(s2.CompressedSize);
        //    Console.WriteLine(s2.ToYAML());

        //    writer.SendCustomCommand(s);

        //    while (packets.Count > 0)
        //    {
        //        var data = packets.Dequeue();
        //        reader.FillBuffer(data, 0, data.Length);
        //    }

        //    CommandObjects cmd = reader.NextCommand();
        //    //Console.WriteLine(cmd.ToXMLString());

        //    Assert.AreEqual(cmd.CommandName, "GetMetadataBasic");

        //    db.ProcessCommand(cmd.GetMetadataBasic, writer);

        //    while (packets.Count > 0)
        //    {
        //        var data = packets.Dequeue();
        //        reader.FillBuffer(data, 0, data.Length);
        //    }

        //    cmd = reader.NextCommand();

        //    Stopwatch sw = new Stopwatch();
        //    sw.Restart();
        //    Console.WriteLine(cmd.Markup.EncodedSize);
        //    Console.WriteLine(sw.Elapsed.TotalMilliseconds);
        //    sw.Restart();
        //    Console.WriteLine(cmd.Markup.CompressedSize);
        //    Console.WriteLine(sw.Elapsed.TotalMilliseconds);
        //    sw.Restart();

        //    Assert.AreEqual(cmd.CommandName, "Metadata");

        //    MetadataQueryTable tbl = null;
        //    MetadataSubCommandObjects subCmd;
        //    while ((subCmd = cmd.Metadata.NextCommand()) != null)
        //    {
        //        switch (subCmd.SubCommand)
        //        {
        //            case MetadataSubCommand.DefineResponse:
        //                tbl = new MetadataQueryTable(subCmd.DefineResponse);
        //                break;
        //            case MetadataSubCommand.DefineRow:
        //                tbl.ProcessCommand(subCmd.DefineRow);
        //                break;
        //            case MetadataSubCommand.UndefineRow:
        //                tbl.ProcessCommand(subCmd.UndefineRow);
        //                break;
        //            case MetadataSubCommand.Finished:
        //                break;
        //            default:
        //                throw new ArgumentOutOfRangeException();
        //        }
        //    }

        //    var t = tbl.ToTable();
        //    MakeCSV(t);
        //}

        //[TestMethod]
        //public void TestGetMetadataJoin()
        //{
        //    var db = Load();

        //    Queue<byte[]> packets = new Queue<byte[]>();

        //    var writer = new WireEncoder();
        //    var reader = new WireDecoder();

        //    writer.NewPacket += (bytes, start, length) => packets.Enqueue(Clone(bytes, start, length));

        //    //statements.Add(BuildRequest("Vendor", "ID", "Acronym", "Name"));

        //    var s = BuildRequest("Measurement", db["Measurement"].Columns.Select(x => x.Name).ToArray());
        //    s.JoinedTables.Add(new SttpQueryJoinedTable(0, "DeviceID", "Device", 1));
        //    s.ColumnInputs.Add(new SttpQueryColumn(1, "Name", -1));
        //    s.Outputs.Add(new SttpQueryOutputColumns(-1, "DeviceName"));
        //    s.Literals.Add(new SttpQueryLiteral((SttpValue)327, -2));
        //    s.Procedure.Add(new SttpQueryProcedureStep("EQU", new int[] { 3, -2 }.ToList(), -3));
        //    s.WhereBooleanVariable = -3;
        //    s.ValidateAndRemapAllIndexes(out int j, out int j1);
        //    var s2 = s.ToSttpMarkup();
        //    Console.WriteLine(s2.EncodedSize);
        //    Console.WriteLine(s2.CompressedSize);
        //    Console.WriteLine(s2.ToYAML());

        //    writer.SendCustomCommand(s);

        //    while (packets.Count > 0)
        //    {
        //        var data = packets.Dequeue();
        //        reader.FillBuffer(data, 0, data.Length);
        //    }

        //    CommandObjects cmd = reader.NextCommand();

        //    Console.WriteLine(cmd.ToXMLString());

        //    Assert.AreEqual(cmd.CommandName, "GetMetadataAdvance");

        //    db.ProcessCommand(cmd.GetMetadataAdvance, writer);

        //    while (packets.Count > 0)
        //    {
        //        var data = packets.Dequeue();
        //        reader.FillBuffer(data, 0, data.Length);
        //    }

        //    cmd = reader.NextCommand();

        //    Console.WriteLine(cmd.Markup.ToYAML());

        //    Assert.AreEqual(cmd.CommandName, "Metadata");

        //    MetadataQueryTable tbl = null;
        //    MetadataSubCommandObjects subCmd;
        //    while ((subCmd = cmd.Metadata.NextCommand()) != null)
        //    {
        //        switch (subCmd.SubCommand)
        //        {
        //            case MetadataSubCommand.DefineResponse:
        //                tbl = new MetadataQueryTable(subCmd.DefineResponse);
        //                break;
        //            case MetadataSubCommand.DefineRow:
        //                tbl.ProcessCommand(subCmd.DefineRow);
        //                break;
        //            case MetadataSubCommand.UndefineRow:
        //                tbl.ProcessCommand(subCmd.UndefineRow);
        //                break;
        //            case MetadataSubCommand.Finished:
        //                break;
        //            default:
        //                throw new ArgumentOutOfRangeException();
        //        }
        //    }

        //    var t = tbl.ToTable();
        //    MakeCSV(t);
        //}

        private static CommandGetMetadata BuildRequest(string tableName, params string[] columns)
        {
            var s = new CommandGetMetadata(tableName, columns);

            return(s);
        }