2
* Xibo - Digitial Signage - http://www.xibo.org.uk
3
* Copyright (C) 2009 Daniel Garner
5
* This file is part of Xibo.
7
* Xibo is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU Affero General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
12
* Xibo is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU Affero General Public License for more details.
17
* You should have received a copy of the GNU Affero General Public License
18
* along with Xibo. If not, see <http://www.gnu.org/licenses/>.
21
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
25
using System.Windows.Forms;
32
private Collection<Stat> _stats;
33
private xmds.xmds _xmds;
34
private String _lastSubmit;
35
private HardwareKey _hardwareKey;
36
private Boolean _xmdsProcessing;
40
_stats = new Collection<Stat>();
41
_xmds = new xmds.xmds();
43
// Register a listener for the XMDS stats
44
_xmds.SubmitStatsCompleted += new XiboClient.xmds.SubmitStatsCompletedEventHandler(_xmds_SubmitStatsCompleted);
46
// Get the key for this display
47
_hardwareKey = new HardwareKey();
49
_xmdsProcessing = false;
53
/// Record a complete Layout Event
55
/// <param name="fromDT"></param>
56
/// <param name="toDT"></param>
57
/// <param name="scheduleID"></param>
58
/// <param name="layoutID"></param>
59
public void RecordLayout(String fromDT, String toDT, int scheduleID, int layoutID)
61
if (!Properties.Settings.Default.statsEnabled) return;
63
Stat stat = new Stat();
65
stat.type = StatType.Layout;
66
stat.fromDate = fromDT;
68
stat.scheduleID = scheduleID;
69
stat.layoutID = layoutID;
77
/// Record a complete Media Event
79
/// <param name="fromDT"></param>
80
/// <param name="toDT"></param>
81
/// <param name="layoutID"></param>
82
/// <param name="mediaID"></param>
83
public void RecordMedia(String fromDT, String toDT, int layoutID, String mediaID)
85
if (!Properties.Settings.Default.statsEnabled) return;
87
Stat stat = new Stat();
89
stat.type = StatType.Media;
90
stat.fromDate = fromDT;
92
stat.layoutID = layoutID;
93
stat.mediaID = mediaID;
101
/// Record a complete Event
103
/// <param name="fromDT"></param>
104
/// <param name="toDT"></param>
105
/// <param name="tag"></param>
106
public void RecordEvent(String fromDT, String toDT, String tag)
108
if (!Properties.Settings.Default.statsEnabled) return;
110
Stat stat = new Stat();
112
stat.type = StatType.Event;
113
stat.fromDate = fromDT;
125
/// <param name="stat"></param>
126
public void RecordStat(Stat stat)
128
System.Diagnostics.Debug.WriteLine(String.Format("Recording a Stat Record. Current Count = {0}", _stats.Count.ToString()), LogType.Audit.ToString());
132
// At some point we will need to flush the stats
133
if (_stats.Count >= Properties.Settings.Default.StatsFlushCount)
146
System.Diagnostics.Debug.WriteLine(new LogMessage("Flush", String.Format("IN")), LogType.Audit.ToString());
148
// Determine if there is anything to flush
149
if (_stats.Count < 1 || _xmdsProcessing) return;
151
int threshold = ((int)Properties.Settings.Default.collectInterval * 5);
153
// Determine where we want to log.
154
if (Properties.Settings.Default.XmdsLastConnection.AddSeconds(threshold) < DateTime.Now)
163
System.Diagnostics.Debug.WriteLine(new LogMessage("Flush", String.Format("OUT")), LogType.Audit.ToString());
167
/// Send the Stat to a File
169
private void FlushToFile()
171
System.Diagnostics.Debug.WriteLine(new LogMessage("FlushToFile", String.Format("IN")), LogType.Audit.ToString());
173
// There is something to flush - we want to parse the collection adding to the TextWriter each time.
176
// Open the Text Writer
177
StreamWriter tw = new StreamWriter(File.Open(Application.UserAppDataPath + "//" + Properties.Settings.Default.StatsLogFile, FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF8);
181
foreach (Stat stat in _stats)
183
tw.WriteLine(stat.ToString());
188
System.Diagnostics.Trace.WriteLine(new LogMessage("FlushToFile", String.Format("Error writing stats line to file with exception {0}", ex.Message)), LogType.Error.ToString());
199
// Log this exception
200
System.Diagnostics.Trace.WriteLine(new LogMessage("FlushToFile", String.Format("Error writing stats to file with exception {0}", ex.Message)), LogType.Error.ToString());
204
// Always clear the stats. If the file open failed for some reason then we dont want to try again
208
System.Diagnostics.Debug.WriteLine(new LogMessage("FlushToFile", String.Format("OUT")), LogType.Audit.ToString());
212
/// Send the Stats to XMDS
214
private void FlushToXmds()
216
System.Diagnostics.Debug.WriteLine(new LogMessage("FlushToXmds", String.Format("IN")), LogType.Audit.ToString());
222
// Load the Stats collection into a string
225
foreach (Stat stat in _stats)
227
stats += stat.ToString();
232
System.Diagnostics.Trace.WriteLine(new LogMessage("FlushToXmds", String.Format("Error converting stat to a string {0}", ex.Message)), LogType.Error.ToString());
237
// Store the stats as the last sent (so we have a record if it fails)
240
// Clear the stats collection
243
// Submit the string to XMDS
244
_xmdsProcessing = true;
246
_xmds.SubmitStatsAsync(Properties.Settings.Default.Version, Properties.Settings.Default.ServerKey, _hardwareKey.Key, stats);
249
System.Diagnostics.Debug.WriteLine(new LogMessage("FlushToXmds", String.Format("OUT")), LogType.Audit.ToString());
253
/// Capture the XMDS call and see if it went well
255
/// <param name="sender"></param>
256
/// <param name="e"></param>
257
void _xmds_SubmitStatsCompleted(object sender, XiboClient.xmds.SubmitStatsCompletedEventArgs e)
259
System.Diagnostics.Debug.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("IN")), LogType.Audit.ToString());
261
_xmdsProcessing = false;
263
// Test if we succeeded or not
266
// We had an error, log it.
267
System.Diagnostics.Trace.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("Error during Submit to XMDS {0}", e.Error.Message)), LogType.Error.ToString());
269
// Dump the stats to a file instead
270
if (_lastSubmit != "")
274
// Open the Text Writer
275
StreamWriter tw = new StreamWriter(File.Open(Application.UserAppDataPath + "//" + Properties.Settings.Default.StatsLogFile, FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF8);
279
tw.Write(_lastSubmit);
283
// Log this exception
284
System.Diagnostics.Trace.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("Error writing stats to file with exception {0}", ex.Message)), LogType.Error.ToString());
294
// Log this exception
295
System.Diagnostics.Trace.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("Could not open the file with exception {0}", ex.Message)), LogType.Error.ToString());
300
// Clear the last sumbit
303
System.Diagnostics.Debug.WriteLine(new LogMessage("_xmds_SubmitStatsCompleted", String.Format("OUT")), LogType.Audit.ToString());
309
public StatType type;
310
public String fromDate;
311
public String toDate;
313
public int scheduleID;
314
public String mediaID;
317
public override string ToString()
319
// Format the message into the expected XML sub nodes.
320
// Just do this with a string builder rather than an XML builder.
323
theMessage = String.Format("<stat type=\"{0}\" fromdt=\"{1}\" todt=\"{2}\" layoutid=\"{3}\" scheduleid=\"{4}\" mediaid=\"{5}\"></stat>", type, fromDate, toDate, layoutID.ToString(), scheduleID.ToString(), mediaID);
329
public enum StatType { Layout, Media, Event };