edac2502bf281cb0e4a73f2fd933a7dd04e4dea9
[GitHub/Stricted/SpeedportHybridControl.git] / SpeedportHybridControl / PageModel / DslPageModel.cs
1 using SpeedportHybridControl.Data;
2 using SpeedportHybridControl.Implementations;
3 using SpeedportHybridControl.Model;
4 using System;
5 using System.IO;
6 using System.Threading;
7 using System.Timers;
8 using System.Windows;
9
10 namespace SpeedportHybridControl.PageModel {
11 class DslPageModel : SuperViewModel {
12 private DelegateCommand _reloadCommand;
13 private DelegateCommand _autoReloadCommand;
14 private bool _autoReload;
15 private bool _log;
16 private bool _logEnabled;
17 private System.Timers.Timer _timer;
18
19 private Connection _Connection;
20 private Line _Line;
21 private string _datetime;
22
23 private string _lastCRC;
24 private string _lastHEC;
25 private string _lastFEC;
26
27
28 public Connection Connection {
29 get { return _Connection; }
30 set { SetProperty(ref _Connection, value); }
31 }
32
33 public Line Line {
34 get { return _Line; }
35 set { SetProperty(ref _Line, value); }
36 }
37
38 public string datetime {
39 get { return _datetime; }
40 set { SetProperty(ref _datetime, value); }
41 }
42
43 public string lastCRC {
44 get { return _lastCRC; }
45 set { SetProperty(ref _lastCRC, value); }
46 }
47
48 public string lastFEC {
49 get { return _lastFEC; }
50 set { SetProperty(ref _lastFEC, value); }
51 }
52
53 public string lastHEC {
54 get { return _lastHEC; }
55 set { SetProperty(ref _lastHEC, value); }
56 }
57
58
59 public DelegateCommand ReloadCommand {
60 get { return _reloadCommand; }
61 set { SetProperty(ref _reloadCommand, value); }
62 }
63
64 public DelegateCommand AutoReloadCommand {
65 get { return _autoReloadCommand; }
66 set { SetProperty(ref _autoReloadCommand, value); }
67 }
68 public bool AutoReload {
69 get { return _autoReload; }
70 set { SetProperty(ref _autoReload, value); }
71 }
72
73 public bool Log {
74 get { return _log; }
75 set { SetProperty(ref _log, value); }
76 }
77
78 public bool LogEnabled {
79 get { return _logEnabled; }
80 set { SetProperty(ref _logEnabled, value); }
81 }
82
83 private void OnReloadCommandExecute () {
84 new Thread(() => { SpeedportHybrid.initDSL(); }).Start();
85 }
86
87 private void OnAutoReloadCommandExecute () {
88 if (AutoReload.Equals(true)) {
89 StartTimer();
90 LogEnabled = true;
91 }
92 else {
93 StopTimer();
94 }
95 }
96
97 public void StopTimer () {
98 if (Object.Equals(_timer, null).Equals(false)) {
99 _timer.Stop();
100 }
101
102 if (AutoReload.Equals(true)) {
103 AutoReload = false;
104 }
105
106 if (Log.Equals(true)) {
107 Log = false;
108 }
109
110 LogEnabled = false;
111 }
112
113 private void StartTimer () {
114 _timer = new System.Timers.Timer {
115 Interval = 1000, // every second
116 };
117
118 _timer.Elapsed += timer_Elapsed;
119 _timer.Start();
120 }
121
122 private void timer_Elapsed (object sender, ElapsedEventArgs e) {
123 SpeedportHybrid.initDSL();
124
125 Application.Current.Dispatcher.BeginInvoke(new Action(() => {
126 if (Log.Equals(true)) {
127 //log
128 string prepare = "State: {0}, Actual Data Rate: up: {1} down: {2}, Attainable Data Rate: up: {3} down: {4}, SNR Margin up: {5} down: {6}, CRC error count: up: {7} down: {8}, HEC error count: up: {9} down: {10}, FEC error count: up: {11} down: {12}";
129 log(string.Format(prepare, Connection.state, Line.uactual, Line.dactual, Line.uattainable, Line.dattainable, Line.uSNR, Line.dSNR, Line.uCRC, Line.dCRC, Line.uHEC, Line.dHEC, Line.uFEC, Line.dFEC));
130 log(string.Format("CRC/min up: {0} down: {1}, HEC/min up: {2} down: {3}, FEC/min up: {4} down: {5}", Line.uCRCsec, Line.dCRCsec, Line.uHECsec, Line.dHECsec, Line.uFECsec, Line.dFECsec));
131 }
132 }));
133 }
134
135 private void log (string value) {
136 DateTime time = DateTime.Now;
137
138 if (Directory.Exists("log/").Equals(false))
139 Directory.CreateDirectory("log/");
140
141 if (Directory.Exists("log/dsl/").Equals(false))
142 Directory.CreateDirectory("log/dsl/");
143
144 StreamWriter file = new StreamWriter(string.Concat("log/dsl/", time.ToString("dd.MM.yyyy"), ".txt"), true);
145 file.WriteLine(string.Concat("[", time.ToString("dd.MM.yyyy HH:mm:ss"), "]: ", value));
146 file.Close();
147 }
148
149 public DslPageModel () {
150 ReloadCommand = new DelegateCommand(new Action(OnReloadCommandExecute));
151 AutoReloadCommand = new DelegateCommand(new Action(OnAutoReloadCommandExecute));
152 }
153 }
154 }