Пример #1
0
        internal static void DisplaySelectedTab(Vector2 displayViewerPosition)
        {
            switch (_selectedTab)
            {
            case Tab.Realism:
                TabRealism.Display(displayViewerPosition);
                break;

            case Tab.Config:
                TabConfig.Display(displayViewerPosition);
                break;

            case Tab.Highlight:
                TabHighlight.Display(displayViewerPosition);
                break;

            case Tab.Sounds:
                TabSounds.Display(displayViewerPosition);
                break;

            case Tab.ToolTips:
                TabToolTips.Display(displayViewerPosition);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Пример #2
0
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private static DyGraphOptions MakeGraphOptions(TabConfig tab)
        {
            return(new DyGraphOptions()
            {
                labels = new List <string>()
                {
                    "Date"
                }.Concat(tab.Items.Select(it => it.GetLabel())).ToArray(),
                series = MakeSeriesOptions(tab.Items),
                axes = MakeAxisOptions(tab.PlotConfig),
                drawAxesAtZero = true,
                includeZero = true,
                connectSeparatedPoints = true,
                ylabel = tab.PlotConfig.LeftAxisName,
                y2label = tab.PlotConfig.RightAxisName,
                visibility = tab.Items.Select(v => v.Checked).ToArray()
            });
        }
Пример #3
0
        public override async Task <ReqResult> OnUiRequestAsync(string command, DataValue parameters)
        {
            switch (command)
            {
            case "Init": {
                var para = parameters.Object <InitParams>();
                var(windowLeft, windowRight) = GetTimeWindow(para.TimeRange, new List <VTTQ[]>());

                var res = new LoadHistoryResult();
                res.Tabs = configuration.Tabs.Select(t => new TabRes()
                    {
                        Name          = t.Name,
                        MaxDataPoints = t.PlotConfig.MaxDataPoints,
                        Variables     = t.Items.Select(it => it.Variable).ToArray(),
                        Options       = MakeGraphOptions(t),
                        Configuration = t
                    }).ToArray();

                res.WindowLeft  = windowLeft.JavaTicks;
                res.WindowRight = windowRight.JavaTicks;

                tabStates = GetInitialTabStates(configuration);

                ObjectRef[]  objects = configuration.Tabs.SelectMany(t => t.Items.Select(it => it.Variable.Object)).Distinct().ToArray();
                ObjectInfo[] infos;
                try {
                    infos = await Connection.GetObjectsByID(objects);
                }
                catch (Exception) {
                    infos = new ObjectInfo[objects.Length];
                    for (int i = 0; i < objects.Length; ++i)
                    {
                        ObjectRef obj = objects[i];
                        try {
                            infos[i] = await Connection.GetObjectByID(obj);
                        }
                        catch (Exception) {
                            infos[i] = new ObjectInfo(obj, "???", "???");
                        }
                    }
                }

                foreach (ObjectInfo info in infos)
                {
                    var numericVariables = info.Variables.Where(IsNumeric).Select(v => v.Name).ToArray();
                    res.ObjectMap[info.ID.ToEncodedString()] = new ObjInfo()
                    {
                        Name      = info.Name,
                        Variables = numericVariables
                    };
                }

                var mods = await Connection.GetModules();

                res.Modules = mods.Select(m => new ModuleInfo()
                    {
                        ID   = m.ID,
                        Name = m.Name
                    }).ToArray();

                await EnableEvents(configuration);

                return(ReqResult.OK(res));
            }

            case "LoadTabData": {
                var para = parameters.Object <LoadHistoryParams>();

                TabState tabState = tabStates.First(ts => ts.TabName == para.TabName);
                tabState.LastTimeRange = para.TimeRange;

                Timestamp tStart = para.TimeRange.GetStart();
                Timestamp tEnd   = para.TimeRange.GetEnd();

                var listHistories = new List <VTTQ[]>();

                foreach (var variable in para.Variables)
                {
                    try {
                        VTTQ[] data = await Connection.HistorianReadRaw(variable, tStart, tEnd, para.MaxDataPoints, BoundingMethod.CompressToN);

                        listHistories.Add(data);
                    }
                    catch (Exception) {
                        listHistories.Add(new VTTQ[0]);
                    }
                }

                tabState.IsLoaded = true;

                var(windowLeft, windowRight) = GetTimeWindow(para.TimeRange, listHistories);

                var res = MemoryManager.GetMemoryStream("LoadHistory");
                try {
                    using (var writer = new StreamWriter(res, Encoding.ASCII, 8 * 1024, leaveOpen: true)) {
                        writer.Write("{ \"WindowLeft\": " + windowLeft.JavaTicks);
                        writer.Write(", \"WindowRight\": " + windowRight.JavaTicks);
                        writer.Write(", \"Data\": ");
                        WriteUnifiedData(writer, listHistories);
                        writer.Write('}');
                    }
                    res.Seek(0, SeekOrigin.Begin);
                }
                catch (Exception) {
                    res.Dispose();
                    throw;
                }
                return(new ReqResult(200, res));
            }

            case "SaveItems": {
                var para = parameters.Object <SaveItemsParams>();

                TabState tabState = tabStates.First(ts => ts.TabName == para.TabName);

                VariableRef[] newVariables = para.Items.Select(it => it.Variable).ToArray();
                bool          reloadData   = !Arrays.Equals(newVariables, tabState.Variables);

                tabState.Variables = newVariables;

                TabConfig tabConfig = configuration.Tabs.FirstOrDefault(t => t.Name == para.TabName);
                tabConfig.Items = para.Items;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                if (reloadData)
                {
                    await EnableEvents(configuration);
                }

                return(ReqResult.OK(new {
                        Options = MakeGraphOptions(tabConfig),
                        Variables = newVariables,
                        Configuration = tabConfig,
                        ReloadData = reloadData
                    }));
            }

            case "SavePlot": {
                var para = parameters.Object <SavePlotParams>();

                TabConfig tabConfig = configuration.Tabs.First(t => t.Name == para.TabName);

                bool reloadData = tabConfig.PlotConfig.MaxDataPoints != para.Plot.MaxDataPoints;

                tabConfig.PlotConfig = para.Plot;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(new {
                        Options = MakeGraphOptions(tabConfig),
                        Configuration = tabConfig,
                        ReloadData = reloadData
                    }));
            }

            case "DeleteTab": {
                var para = parameters.Object <DeleteTabParams>();

                configuration.Tabs = configuration.Tabs.Where(t => t.Name != para.TabName).ToArray();

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(""));
            }

            case "RenameTab": {
                var para = parameters.Object <RenameTabParams>();

                if (para.NewName != para.TabName && configuration.Tabs.Any(t => t.Name == para.NewName))
                {
                    throw new Exception("Tab name already exists!");
                }

                TabConfig tabConfig = configuration.Tabs.First(t => t.Name == para.TabName);
                tabConfig.Name = para.NewName;

                TabState tabState = tabStates.First(ts => ts.TabName == para.TabName);
                tabState.TabName = para.NewName;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(new {
                        Configuration = tabConfig
                    }));
            }

            case "AddTab": {
                var para = parameters.Object <AddTabParams>();
                var(windowLeft, windowRight) = GetTimeWindow(para.TimeRange, new List <VTTQ[]>());

                if (configuration.Tabs.Any(t => t.Name == para.NewName))
                {
                    throw new Exception("Tab name already exists!");
                }

                TabConfig tabConfig = new TabConfig();
                tabConfig.Name = para.NewName;

                var tabs = configuration.Tabs.ToList();
                tabs.Add(tabConfig);
                configuration.Tabs = tabs.ToArray();

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                var tabState = new TabState()
                {
                    TabName       = para.NewName,
                    LastTimeRange = para.TimeRange
                };
                tabStates.Add(tabState);

                return(ReqResult.OK(new {
                        NewTab = new TabRes()
                        {
                            Name = tabConfig.Name,
                            MaxDataPoints = tabConfig.PlotConfig.MaxDataPoints,
                            Options = MakeGraphOptions(tabConfig),
                            Configuration = tabConfig
                        },
                        WindowLeft = windowLeft.JavaTicks,
                        WindowRight = windowRight.JavaTicks
                    }));
            }

            case "MoveLeft": {
                var para = parameters.Object <MoveTabParams>();

                int i = configuration.Tabs.ToList().FindIndex(t => t.Name == para.TabName);
                if (i <= 0)
                {
                    throw new Exception("Can't move left");
                }

                var tmp = configuration.Tabs[i];
                configuration.Tabs[i]     = configuration.Tabs[i - 1];
                configuration.Tabs[i - 1] = tmp;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(""));
            }

            case "MoveRight": {
                var para = parameters.Object <MoveTabParams>();

                int i = configuration.Tabs.ToList().FindIndex(t => t.Name == para.TabName);
                if (i >= configuration.Tabs.Length - 1)
                {
                    throw new Exception("Can't move right");
                }

                var tmp = configuration.Tabs[i];
                configuration.Tabs[i]     = configuration.Tabs[i + 1];
                configuration.Tabs[i + 1] = tmp;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(""));
            }

            case "ReadModuleObjects": {
                var pars = parameters.Object <ReadModuleObjectsParams>();

                ObjectInfo[] objects;

                try {
                    objects = await Connection.GetAllObjects(pars.ModuleID);
                }
                catch (Exception) {
                    objects = new ObjectInfo[0];
                }

                return(ReqResult.OK(new {
                        Items = objects.Where(o => o.Variables.Any(IsNumeric)).Select(o => new Obj()
                        {
                            Type = o.ClassName,
                            ID = o.ID.ToEncodedString(),
                            Name = o.Name,
                            Variables = o.Variables.Where(IsNumeric).Select(v => v.Name).ToArray()
                        })
                    }));
            }

            default:
                return(ReqResult.Bad("Unknown command: " + command));
            }
        }
Пример #4
0
        public override async Task <ReqResult> OnUiRequestAsync(string command, DataValue parameters)
        {
            switch (command)
            {
            case "Init": {
                var para = parameters.Object <InitParams>() ?? throw new Exception("InitParams is null");
                var(windowLeft, windowRight) = GetTimeWindow(para.TimeRange, new List <VTTQs>());

                var res = new LoadHistoryResult();
                res.Tabs = configuration.Tabs.Select(t => new TabRes()
                    {
                        Name          = t.Name,
                        MaxDataPoints = t.PlotConfig.MaxDataPoints,
                        Variables     = t.Items.Select(it => it.Variable).ToArray(),
                        Options       = MakeGraphOptions(t),
                        Configuration = t
                    }).ToArray();

                res.WindowLeft  = windowLeft.JavaTicks;
                res.WindowRight = windowRight.JavaTicks;

                tabStates = GetInitialTabStates(configuration);

                ObjectRef[] objects = configuration.Tabs.SelectMany(t => t.Items.Select(it => it.Variable.Object)).Distinct().ToArray();
                ObjectInfos infos;
                try {
                    infos = await Connection.GetObjectsByID(objects);
                }
                catch (Exception) {
                    infos = new ObjectInfos(objects.Length);
                    for (int i = 0; i < objects.Length; ++i)
                    {
                        ObjectRef obj = objects[i];
                        try {
                            infos.Add(await Connection.GetObjectByID(obj));
                        }
                        catch (Exception) {
                            infos.Add(new ObjectInfo(obj, "???", "???"));
                        }
                    }
                }

                foreach (ObjectInfo info in infos)
                {
                    var numericVariables = info.Variables.Where(IsNumericOrBool).Select(v => v.Name).ToArray();
                    res.ObjectMap[info.ID.ToEncodedString()] = new ObjInfo()
                    {
                        Name      = info.Name,
                        Variables = numericVariables
                    };
                }

                res.Modules = modules;

                await EnableEvents(configuration);

                return(ReqResult.OK(res));
            }

            case "LoadTabData": {
                var para = parameters.Object <LoadHistoryParams>() ?? throw new Exception("LoadHistoryParams is null");

                TabState tabState = tabStates.FirstOrDefault(ts => ts.TabName == para.TabName);
                if (tabState == null)
                {
                    throw new Exception($"Failed to load history data: tab '{para.TabName}' not found");
                }

                tabState.LastTimeRange = para.TimeRange;

                Timestamp tStart = para.TimeRange.GetStart();
                Timestamp tEnd   = para.TimeRange.GetEnd();

                TabConfig     tabConfig = configuration.Tabs.First(t => t.Name == para.TabName);
                QualityFilter filter    = tabConfig.PlotConfig.FilterByQuality;

                var listHistories = new List <VTTQs>();

                foreach (var variable in para.Variables)
                {
                    try {
                        VTTQs data = await Connection.HistorianReadRaw(variable, tStart, tEnd, para.MaxDataPoints, BoundingMethod.CompressToN, filter);

                        listHistories.Add(data);
                    }
                    catch (Exception) {
                        listHistories.Add(new VTTQs());
                    }
                }

                tabState.IsLoaded = true;

                var(windowLeft, windowRight) = GetTimeWindow(para.TimeRange, listHistories);

                var res = MemoryManager.GetMemoryStream("LoadHistory");
                try {
                    using (var writer = new StreamWriter(res, Encoding.ASCII, 8 * 1024, leaveOpen: true)) {
                        writer.Write("{ \"WindowLeft\": " + windowLeft.JavaTicks);
                        writer.Write(", \"WindowRight\": " + windowRight.JavaTicks);
                        writer.Write(", \"Data\": ");
                        WriteUnifiedData(new JsonDataRecordArrayWriter(writer), listHistories);
                        writer.Write('}');
                    }
                    res.Seek(0, SeekOrigin.Begin);
                }
                catch (Exception) {
                    res.Dispose();
                    throw;
                }
                return(new ReqResult(200, res));
            }

            case "DownloadFile": {
                var           para      = parameters.Object <DownloadDataFileParams>() ?? throw new Exception("DownloadDataFileParams is null");
                TabConfig     tabConfig = configuration.Tabs.First(t => t.Name == para.TabName);
                QualityFilter filter    = tabConfig.PlotConfig.FilterByQuality;

                Timestamp tStart = para.TimeRange.GetStart();
                Timestamp tEnd   = para.TimeRange.GetEnd();

                var listHistories = new List <VTTQs>();

                foreach (var variable in para.Variables)
                {
                    try {
                        const int ChunckSize = 5000;
                        VTTQs     data       = await Connection.HistorianReadRaw(variable, tStart, tEnd, ChunckSize, BoundingMethod.TakeFirstN, filter);

                        if (data.Count < ChunckSize)
                        {
                            listHistories.Add(data);
                        }
                        else
                        {
                            var buffer = new VTTQs(data);
                            do
                            {
                                Timestamp t = data[data.Count - 1].T.AddMillis(1);
                                data = await Connection.HistorianReadRaw(variable, t, tEnd, ChunckSize, BoundingMethod.TakeFirstN, filter);

                                buffer.AddRange(data);
                            }while (data.Count == ChunckSize);

                            listHistories.Add(buffer);
                        }
                    }
                    catch (Exception) {
                        listHistories.Add(new VTTQs());
                    }
                }

                var columns = new List <string>();
                columns.Add("Time");
                columns.AddRange(para.VariableNames);

                var res = MemoryManager.GetMemoryStream("DownloadFile");
                try {
                    string contentType;
                    switch (para.FileType)
                    {
                    case FileType.CSV:

                        contentType = "text/csv";
                        using (var writer = new StreamWriter(res, Encoding.UTF8, 8 * 1024, leaveOpen: true)) {
                            WriteUnifiedData(new CsvDataRecordArrayWriter(writer, columns, configuration.DataExport.CSV), listHistories);
                        }
                        break;

                    case FileType.Spreadsheet:

                        contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                        using (var excel = new ExcelPackage(res)) {
                            ExcelWorksheet sheet = excel.Workbook.Worksheets.Add("Data Export");
                            WriteUnifiedData(new ExcelDataRecordArrayWriter(sheet, columns, configuration.DataExport.Spreadsheet), listHistories);
                            excel.Save();
                        }
                        break;

                    default: throw new Exception($"Unknown file type: {para.FileType}");
                    }

                    res.Seek(0, SeekOrigin.Begin);
                    return(new ReqResult(200, res, contentType));
                }
                catch (Exception) {
                    res.Dispose();
                    throw;
                }
            }

            case "SaveItems": {
                var para = parameters.Object <SaveItemsParams>() ?? throw new Exception("SaveItemsParams is null");

                TabState tabState = tabStates.First(ts => ts.TabName == para.TabName);

                VariableRef[] newVariables = para.Items.Select(it => it.Variable).ToArray();
                bool          reloadData   = !Arrays.Equals(newVariables, tabState.Variables);

                tabState.Variables = newVariables;

                TabConfig tabConfig = configuration.Tabs.FirstOrDefault(t => t.Name == para.TabName);
                tabConfig.Items = para.Items;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                if (reloadData)
                {
                    await EnableEvents(configuration);
                }

                return(ReqResult.OK(new {
                        Options = MakeGraphOptions(tabConfig),
                        Variables = newVariables,
                        Configuration = tabConfig,
                        ReloadData = reloadData
                    }));
            }

            case "SavePlot": {
                var para = parameters.Object <SavePlotParams>() ?? throw new Exception("SavePlotParams is null");

                TabConfig tabConfig = configuration.Tabs.First(t => t.Name == para.TabName);

                bool reloadData =
                    tabConfig.PlotConfig.MaxDataPoints != para.Plot.MaxDataPoints ||
                    tabConfig.PlotConfig.FilterByQuality != para.Plot.FilterByQuality ||
                    tabConfig.PlotConfig.LeftAxisLimitY != para.Plot.LeftAxisLimitY ||
                    tabConfig.PlotConfig.RightAxisLimitY != para.Plot.RightAxisLimitY;

                tabConfig.PlotConfig = para.Plot;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(new {
                        Options = MakeGraphOptions(tabConfig),
                        Configuration = tabConfig,
                        ReloadData = reloadData
                    }));
            }

            case "DeleteTab": {
                var para = parameters.Object <DeleteTabParams>() ?? throw new Exception("DeleteTabParams is null");

                configuration.Tabs = configuration.Tabs.Where(t => t.Name != para.TabName).ToArray();

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(""));
            }

            case "RenameTab": {
                var para = parameters.Object <RenameTabParams>() ?? throw new Exception("RenameTabParams is null");

                if (para.NewName != para.TabName && configuration.Tabs.Any(t => t.Name == para.NewName))
                {
                    throw new Exception("Tab name already exists!");
                }

                TabConfig tabConfig = configuration.Tabs.First(t => t.Name == para.TabName);
                tabConfig.Name = para.NewName;

                TabState tabState = tabStates.First(ts => ts.TabName == para.TabName);
                tabState.TabName = para.NewName;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(new {
                        Configuration = tabConfig
                    }));
            }

            case "AddTab": {
                var para = parameters.Object <AddTabParams>() ?? throw new Exception("AddTabParams is null");
                var(windowLeft, windowRight) = GetTimeWindow(para.TimeRange, new List <VTTQs>());

                if (configuration.Tabs.Any(t => t.Name == para.NewName))
                {
                    throw new Exception("Tab name already exists!");
                }

                TabConfig tabConfig = new TabConfig();
                tabConfig.Name = para.NewName;

                var tabs = configuration.Tabs.ToList();
                tabs.Add(tabConfig);
                configuration.Tabs = tabs.ToArray();

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                var tabState = new TabState()
                {
                    TabName       = para.NewName,
                    LastTimeRange = para.TimeRange
                };
                tabStates.Add(tabState);

                return(ReqResult.OK(new {
                        NewTab = new TabRes()
                        {
                            Name = tabConfig.Name,
                            MaxDataPoints = tabConfig.PlotConfig.MaxDataPoints,
                            Options = MakeGraphOptions(tabConfig),
                            Configuration = tabConfig
                        },
                        WindowLeft = windowLeft.JavaTicks,
                        WindowRight = windowRight.JavaTicks
                    }));
            }

            case "MoveLeft": {
                var para = parameters.Object <MoveTabParams>() ?? throw new Exception("MoveTabParams is null");

                int i = configuration.Tabs.ToList().FindIndex(t => t.Name == para.TabName);
                if (i <= 0)
                {
                    throw new Exception("Can't move left");
                }

                var tmp = configuration.Tabs[i];
                configuration.Tabs[i]     = configuration.Tabs[i - 1];
                configuration.Tabs[i - 1] = tmp;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(""));
            }

            case "MoveRight": {
                var para = parameters.Object <MoveTabParams>() ?? throw new Exception("MoveTabParams is null");

                int i = configuration.Tabs.ToList().FindIndex(t => t.Name == para.TabName);
                if (i >= configuration.Tabs.Length - 1)
                {
                    throw new Exception("Can't move right");
                }

                var tmp = configuration.Tabs[i];
                configuration.Tabs[i]     = configuration.Tabs[i + 1];
                configuration.Tabs[i + 1] = tmp;

                DataValue newConfig = ConfigToIndentedDataValue(configuration);

                await Context.SaveViewConfiguration(newConfig);

                return(ReqResult.OK(""));
            }

            case "ReadModuleObjects": {
                var pars = parameters.Object <ReadModuleObjectsParams>() ?? throw new Exception("ReadModuleObjectsParams is null");

                ObjectInfos objects;

                try {
                    objects = await Connection.GetAllObjects(pars.ModuleID);
                }
                catch (Exception) {
                    objects = new ObjectInfos();
                }

                return(ReqResult.OK(new {
                        Items = objects.Where(o => o.Variables.Any(IsNumericOrBool)).Select(o => new Obj()
                        {
                            Type = o.ClassName,
                            ID = o.ID.ToEncodedString(),
                            Name = o.Name,
                            Variables = o.Variables.Where(IsNumericOrBool).Select(v => v.Name).ToArray()
                        }).ToArray()
                    }));
            }

            default:
                return(ReqResult.Bad("Unknown command: " + command));
            }
        }