blob: cf71ed270cf2caaac6e1746e0104de3728aec29a (
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using Tango.Core;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.FSE.Common.FileSystem;
using Tango.Logging;
using Tango.PPC.Shared.Logs;
using Tango.SharedUI.Components;
namespace Tango.FSE.Common.Logging
{
/// <summary>
/// Represents a remote log file model.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <seealso cref="Tango.Core.ExtendedObject" />
public class RemoteLogFileModel<T> : ExtendedObject where T : LogItemBase
{
private ILogFileParser<T> _parser;
/// <summary>
/// Occurs when the remote log file has downloaded successfully.
/// </summary>
public event EventHandler DownloadCompleted;
[TangoInject]
private ILoggingProvider LoggingProvider { get; set; }
/// <summary>
/// Gets or sets the remote log file.
/// </summary>
public RemoteLogFile RemoteLogFile { get; set; }
/// <summary>
/// Gets the duration of the remote log file.
/// </summary>
public TimeSpan Duration
{
get { return RemoteLogFile.DateModified - RemoteLogFile.DateCreated; }
}
private RemoteLogFileStatus _status;
/// <summary>
/// Gets or sets the remote log file status.
/// </summary>
public RemoteLogFileStatus Status
{
get { return _status; }
set { _status = value; RaisePropertyChangedAuto(); }
}
private FileSystemHandler _handler;
/// <summary>
/// Gets or sets the remote log file download handler.
/// </summary>
public FileSystemHandler Handler
{
get { return _handler; }
set { _handler = value; RaisePropertyChangedAuto(); }
}
/// <summary>
/// Gets or sets the temporary file (where the actual log file is stored locally).
/// </summary>
public String TemporaryFile { get; set; }
private ObservableCollection<LogItemBase> _logItems;
/// <summary>
/// Gets or sets the log items.
/// </summary>
public ObservableCollection<LogItemBase> LogItems
{
get { return _logItems; }
set { _logItems = value; RaisePropertyChangedAuto(); }
}
/// <summary>
/// Gets or sets the download command.
/// </summary>
public RelayCommand DownloadCommand { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="RemoteLogFileModel{T}"/> class.
/// </summary>
/// <param name="parser">The parser.</param>
public RemoteLogFileModel(ILogFileParser<T> parser)
{
TangoIOC.Default.Inject(this);
_parser = parser;
LogItems = new ObservableCollection<LogItemBase>();
DownloadCommand = new RelayCommand(DownloadLogFile);
}
/// <summary>
/// Downloads the log file.
/// </summary>
public async void DownloadLogFile()
{
if (Status == RemoteLogFileStatus.None || Status == RemoteLogFileStatus.Failed)
{
Status = RemoteLogFileStatus.Downloading;
String tempLogFile = TemporaryManager.CreateImaginaryFile(".log");
Handler = await LoggingProvider.DownloadLogFile(RemoteLogFile, tempLogFile);
Handler.StatusChanged += (x, status) =>
{
if (status == FileSystemHandlerStatus.Completed)
{
TemporaryFile = tempLogFile;
LogItems = new ObservableCollection<LogItemBase>(_parser.Parse(tempLogFile, RemoteLogFile.DateCreated.ToLocalTime()));
Status = RemoteLogFileStatus.Downloaded;
DownloadCompleted?.Invoke(this, new EventArgs());
}
else if (status == FileSystemHandlerStatus.Failed)
{
Status = RemoteLogFileStatus.Failed;
}
};
}
}
}
}
|