cleanup
[GitHub/Stricted/SpeedportHybridControl.git] / SpeedportHybridControl / PageModel / DslPageModel.cs
CommitLineData
6c89a163
S
1using SpeedportHybridControl.Data;
2using SpeedportHybridControl.Implementations;
3using SpeedportHybridControl.Model;
4using System;
5using System.IO;
6using System.Threading;
7using System.Timers;
8using System.Windows;
9
10namespace 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
3574608a
S
27 private int lastdFEC;
28 private int lastuFEC;
29 private int lastdHEC;
30 private int lastuHEC;
31 private int lastdCRC;
32 private int lastuCRC;
33 private DateTime lastReload;
34
6c89a163
S
35 public Connection Connection {
36 get { return _Connection; }
37 set { SetProperty(ref _Connection, value); }
38 }
39
40 public Line Line {
41 get { return _Line; }
42 set { SetProperty(ref _Line, value); }
43 }
44
45 public string datetime {
46 get { return _datetime; }
47 set { SetProperty(ref _datetime, value); }
48 }
49
50 public string lastCRC {
51 get { return _lastCRC; }
52 set { SetProperty(ref _lastCRC, value); }
53 }
54
55 public string lastFEC {
56 get { return _lastFEC; }
57 set { SetProperty(ref _lastFEC, value); }
58 }
59
60 public string lastHEC {
61 get { return _lastHEC; }
62 set { SetProperty(ref _lastHEC, value); }
63 }
64
65
66 public DelegateCommand ReloadCommand {
67 get { return _reloadCommand; }
68 set { SetProperty(ref _reloadCommand, value); }
69 }
70
71 public DelegateCommand AutoReloadCommand {
72 get { return _autoReloadCommand; }
73 set { SetProperty(ref _autoReloadCommand, value); }
74 }
913a0c17 75
6c89a163
S
76 public bool AutoReload {
77 get { return _autoReload; }
78 set { SetProperty(ref _autoReload, value); }
79 }
80
81 public bool Log {
82 get { return _log; }
83 set { SetProperty(ref _log, value); }
84 }
85
86 public bool LogEnabled {
87 get { return _logEnabled; }
88 set { SetProperty(ref _logEnabled, value); }
89 }
90
91 private void OnReloadCommandExecute () {
3574608a
S
92 new Thread(() => {
93 SpeedportHybrid.initDSL();
94
95 if (lastReload.Equals(DateTime.MinValue).Equals(false)) {
96 DateTime now = DateTime.Now;
97 double difference = Math.Ceiling(Math.Ceiling((DateTime.Now - lastReload).TotalSeconds) / 60);
98
99 double diffdCRC = Math.Ceiling((Line.dCRC - lastdCRC) / difference);
100 double diffuCRC = Math.Ceiling((Line.uCRC - lastuCRC) / difference);
101 lastCRC = string.Format("CRC/min (last {0} min) Upstream: {1} Downstream: {2}", difference, diffuCRC, diffdCRC);
102
103 double diffdHEC = Math.Ceiling((Line.dHEC - lastdHEC) / difference);
104 double diffuHEC = Math.Ceiling((Line.uHEC - lastuHEC) / difference);
105 lastHEC = string.Format("HEC/min (last {0} min) Upstream: {1} Downstream: {2}", difference, diffuHEC, diffdHEC);
106
107 double diffdFEC = Math.Ceiling((Line.dFEC - lastdFEC) / difference);
108 double diffuFEC = Math.Ceiling((Line.uFEC - lastuFEC) / difference);
109 lastFEC = string.Format("FEC/min (last {0} min) Upstream: {1} Downstream: {2}", difference, diffuFEC, diffdFEC);
110 }
111
112 lastReload = DateTime.Now;
113 lastdCRC = Line.dCRC;
114 lastuCRC = Line.uCRC;
115
116 lastdHEC = Line.dHEC;
117 lastuHEC = Line.uHEC;
118
119 lastdFEC = Line.dFEC;
120 lastuFEC = Line.uFEC;
121 }).Start();
6c89a163
S
122 }
123
124 private void OnAutoReloadCommandExecute () {
125 if (AutoReload.Equals(true)) {
126 StartTimer();
127 LogEnabled = true;
128 }
129 else {
130 StopTimer();
131 }
132 }
133
134 public void StopTimer () {
135 if (Object.Equals(_timer, null).Equals(false)) {
136 _timer.Stop();
137 }
138
139 if (AutoReload.Equals(true)) {
140 AutoReload = false;
141 }
142
143 if (Log.Equals(true)) {
144 Log = false;
145 }
146
147 LogEnabled = false;
148 }
149
150 private void StartTimer () {
151 _timer = new System.Timers.Timer {
152 Interval = 1000, // every second
153 };
154
155 _timer.Elapsed += timer_Elapsed;
156 _timer.Start();
157 }
158
159 private void timer_Elapsed (object sender, ElapsedEventArgs e) {
160 SpeedportHybrid.initDSL();
161
162 Application.Current.Dispatcher.BeginInvoke(new Action(() => {
163 if (Log.Equals(true)) {
164 //log
165 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}";
166 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));
167 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));
168 }
169 }));
170 }
171
172 private void log (string value) {
173 DateTime time = DateTime.Now;
174
175 if (Directory.Exists("log/").Equals(false))
176 Directory.CreateDirectory("log/");
177
178 if (Directory.Exists("log/dsl/").Equals(false))
179 Directory.CreateDirectory("log/dsl/");
180
181 StreamWriter file = new StreamWriter(string.Concat("log/dsl/", time.ToString("dd.MM.yyyy"), ".txt"), true);
182 file.WriteLine(string.Concat("[", time.ToString("dd.MM.yyyy HH:mm:ss"), "]: ", value));
183 file.Close();
184 }
185
186 public DslPageModel () {
187 ReloadCommand = new DelegateCommand(new Action(OnReloadCommandExecute));
188 AutoReloadCommand = new DelegateCommand(new Action(OnAutoReloadCommandExecute));
189 }
190 }
191}