aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Scripting/Tango.Scripting.Test/MainWindowVM.cs
blob: f57de886f6f12be97260a596df7c4e867bc925bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.Core.Commands;
using Tango.Scripting.Basic;
using Tango.SharedUI;

namespace Tango.Scripting.Test
{
    public class TestContext : IContext
    {

    }

    public class MainWindowVM : ViewModel
    {

        public RelayCommand AddScriptCommand { get; set; }
        public RelayCommand RunCommand { get; set; }

        private Project<TestContext> _project;
        public Project<TestContext> Project
        {
            get { return _project; }
            set { _project = value; RaisePropertyChangedAuto(); }
        }

        public MainWindowVM()
        {
            Project = Project<TestContext>.New("untitled");
            Project.Scripts.Add(Script.New("main.csx", Encoding.Default.GetString(Properties.Resources.template), true));
            AddScriptCommand = new RelayCommand(AddScriptFile);
            RunCommand = new RelayCommand(RunProject);
        }

        private void AddScriptFile()
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "CSharp Script|*.csx";
            if (dlg.ShowDialog().Value)
            {
                AddScript(dlg.FileName);
            }
        }

        private void AddScript(String file)
        {
            Project.Scripts.Add(Script.New(file));
        }

        private async void RunProject()
        {
            var session = await Project.Run(null);

            session.StateChanged += (x, e) => 
            {
                if (e.State == ProjectSessionState.Completed)
                {
                    MessageBox.Show(e.ReturnValue.ToString());
                }
            };
        }
    }
}
ectedLogChanged(); } } private ObservableCollection<DateTime> _dates; public ObservableCollection<DateTime> Dates { get { return _dates; } set { _dates = value; RaisePropertyChangedAuto(); } } private DateTime _selectedDate; public DateTime SelectedDate { get { return _selectedDate; } set { _selectedDate = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); } } private DateTime _minDate; public DateTime MinDate { get { return _minDate; } set { _minDate = value; RaisePropertyChangedAuto(); } } private DateTime _maxDate; public DateTime MaxDate { get { return _maxDate; } set { _maxDate = value; RaisePropertyChangedAuto(); } } private bool _isRealTime; public bool IsRealTime { get { return _isRealTime; } set { _isRealTime = value; RaisePropertyChangedAuto(); OnSelectedDateChanged(); } } private bool _realTimePaused; public bool RealTimePaused { get { return _realTimePaused; } set { _realTimePaused = value; RaisePropertyChangedAuto(); if (!_realTimePaused) { _realTimeLogs.InsertRange(0, _pausedLogs); _pausedLogs.Clear(); } } } public RelayCommand NavigateToHomeCommand { get; set; } public RelayCommand ToggleRealTimePaused { get; set; } public RelayCommand ClearRealTimeLogsCommand { get; set; } public EmbeddedLogsViewVM(LoggingNavigationManager navigation, IStudioApplicationManager application, INotificationProvider notification) { _notification = notification; NavigateToHomeCommand = new RelayCommand(() => navigation.NavigateTo(LoggingNavigationView.HomeView)); _parser = new EmbeddedLogFileParser(); _logFiles = _parser.GetLogFiles(); Dates = new ObservableCollection<DateTime>(_logFiles.Select(x => x.DateTime).OrderBy(x => x)); _realTimeLogs = new ControlledObservableCollection<LogItemBase>(); _pausedLogs = new List<LogItemBase>(); IsRealTime = true; RealTimePaused = true; SelectedDate = Dates.LastOrDefault(); if (Dates.Count > 0) { MinDate = Dates.Min(); MaxDate = Dates.Max(); } MachineOperator.EmbeddedLogManager.NewLog += EmbeddedLogManager_NewLog; ToggleRealTimePaused = new RelayCommand(() => RealTimePaused = !RealTimePaused); ClearRealTimeLogsCommand = new RelayCommand(() => { _realTimeLogs.Clear(); }); } private void EmbeddedLogManager_NewLog(object sender, LogItemBase log) { if (!RealTimePaused) { InvokeUI(() => { _realTimeLogs.Insert(0, log); }); } else { _pausedLogs.Add(log); } } private async void OnSelectedDateChanged() { if (IsRealTime) { Logs = _realTimeLogs; } else { List<LogItemBase> logs = new List<LogItemBase>(); using (_notification.PushTaskItem("Loading embedded device logs...")) { await Task.Factory.StartNew(() => { foreach (var logFile in _logFiles.Where(x => x.DateTime.Date == SelectedDate.Date)) { logs.AddRange(_parser.Parse(logFile)); } }); } Logs = new ControlledObservableCollection<LogItemBase>(logs); } } private void OnSelectedLogChanged() { if (SelectedLog != null && !_dialog_shown) { _dialog_shown = true; _notification.ShowModalDialog<LogDetailsViewVM, EmbeddedLogDetailsView>(new LogDetailsViewVM(SelectedLog), (x) => { }, () => { _dialog_shown = false; }); } } } }