Quick Search:

View

Revision:

Diff

Diff from 296 to:

Annotations

Annotate by Age | Author | Mixed | None
/browse/campaignrwm/trunk/CampaignrProject/MainForm.cs

Annotated File View

thassan
107
1 //
thassan
154
2 // Copyright (c) CENS @ UCLA  All rights reserved.
thassan
107
3 //
nmp
218
4 // Written by Taimur Hassan, Nicolai Munk Petersen
thassan
154
5 // Project to port Campaignr to WM 6
6
thassan
107
7 using System;
8 using System.Drawing;
9 using System.Collections;
10 using System.Windows.Forms;
thassan
154
11 using System.Threading;
thassan
107
12 using System.Data;
thassan
208
13 using System.Web;
thassan
143
14 using System.Text;
thassan
178
15 using System.IO;
thassan
165
16 using System.Xml;
nmp
200
17 using System.Xml.Schema;
thassan
143
18 using System.Collections.Generic;
nmp
234
19
20 using Ucla.Cens.Campaignr.Library;
thassan
165
21 using Ucla.Cens.Campaignr.Location;
thassan
177
22 using Ucla.Cens.Campaignr.Messaging;
nmp
234
23 using Ucla.Cens.Campaignr.Library.Upload;
24 using Ucla.Cens.Campaignr.Library.Sensors;
25 using Ucla.Cens.Campaignr.Library.Upload.Modules;
26
thassan
184
27 using System.Runtime.InteropServices;
nmp
216
28 using System.Reflection;
thassan
184
29
thassan
165
30 using SimpleDOMParserCSharp;
thassan
107
31
thassan
143
32 namespace Ucla.Cens.Campaignr.MobApp
thassan
107
33 {
34     /// <summary>
thassan
193
35     /// Main GUI
thassan
107
36     /// </summary>
thassan
143
37     /// 
thassan
107
38     public class MainForm : System.Windows.Forms.Form
39     {
nmp
296
40         private enum OperationModes { UploadCollectUploadCollect };
41
thassan
143
42         private MenuItem campaignMenuItem;
43         private MainMenu mainMenu1;
thassan
107
44         private MenuItem exitMenuItem;
nmp
216
45  
thassan
107
46         private MenuItem menuCollect;
47         private MenuItem menuCollectUpload;
48         private MenuItem menuUpload;
49
nmp
296
50         // Server url to send data 
nmp
218
51         private string _uploadServer = "";
nmp
213
52
nmp
296
53         // Timer delegate that points to the calling menu handler
54         //private Delegate _delegateMenus; // NMP: Waste
55
56         // Timer delegate for Status Textbox
thassan
193
57         private Delegate _delegateStatusUpdate;
nmp
296
58         // Timer delegate for Feedback Textbox
thassan
262
59         private Delegate _delegateFeedbackUpdate;
nmp
296
60         // Timer delegate for Upload Textbox
thassan
193
61         private Delegate _delegateUploadUpdate;
nmp
213
62                            
nmp
296
63         // The main sensor manager that takes care of data collection and upload to server
thassan
154
64         private SensorDataManager _sensorDataManager;
thassan
262
65
66         private FeedbackManager _feedbackManager;
nmp
296
67         // Points to procedure to thread 
thassan
193
68         private TimerCallback _timerDelegate;
nmp
296
69         // Variable for upload monitoring               
thassan
193
70         private System.Threading.Timer _uploadMonitorTimer;              
nmp
296
71         
72         // Transmit and sensors monitor frequency (ms)
73         private const int uploadMonitorInterval = 10000;
74
75         private const int uploadInterval = 10000;
76         
77         // Lock object used for controlling access to multithreaded function resources
thassan
193
78         private object _lock = new object();
thassan
154
79
nmp
218
80         private bool _manualCapture = false;
81
nmp
213
82         private UploadManager _uploadManager;
83
thassan
165
84         private string _campaignname = "";
85         private Boolean _uploadOnStart = false;
nmp
296
86         private Boolean _startOnLoad = false;
nmp
187
87         private Boolean _debugModeOn = false;
thassan
190
88         private Boolean _killUploadMonitor = false;
thassan
178
89         private MenuItem menuConfig;
nmp
296
90         
nmp
185
91         private Label label1;                           
nmp
187
92         private Label status;
93         private Label upload;
nmp
218
94         private Panel loadingPanel;
95         private Label label2;
96         private ProgressBar progressBar1;
thassan
132
97
nmp
266
98         private bool _consoleMode = false;
99         private GraphicalInterface _graphicalInterface = null;
nmp
281
100         
thassan
143
101         #region Public Delegates
thassan
132
102
thassan
193
103         //! Delegate used to call MainForm functions from worker thread
thassan
154
104         public delegate void DelegateMenus(object senderEventArgs args);
thassan
193
105         //! Delegate used to call MainForm functions from worker thread
nmp
183
106         public delegate void DelegateStatusUpdate(Label ctrlObject val);
thassan
193
107         //! Delegate used to call MainForm functions from worker thread
thassan
262
108         public delegate void DelegateFeedbackUpdate(Label ctrlObject val);
109         //! Delegate used to call MainForm functions from worker thread
nmp
183
110         public delegate void DelegateUploadUpdate(Label ctrlObject val);
thassan
132
111
thassan
143
112         #endregion
thassan
132
113
thassan
193
114         /// <constructor>
115         /// MainForm Constructor
116         /// </constructor>
thassan
107
117         public MainForm()
118         {
nmp
265
119             // First, make sure the user has accept the Terms and Conditions
120             string agreement = ConfigurationManager.AppSettings["UserAgreementStatus"];
nmp
281
121             
122             //StringBuilder name = new StringBuilder();
123             //string name = GetCameraDeviceName();
124             //int retval = GetFirstCameraDriver(name);
125             //string s = Marshal.PtrToStringBSTR(name);
126             //IntPtr camera = (IntPtr)0;
127             //InitializeComponent();
nmp
265
128
129             if (agreement == null || agreement != "Accepted")
130             {
131                 // Show Terms and Conditions dialog
132                 UserAgreement ua = new UserAgreement();
133                 if (ua.ShowDialog() == DialogResult.Yes)
134                 {
135                     ConfigurationManager.AppSettings["UserAgreementStatus"] = "Accepted";
136                     ConfigurationManager.Save();
nmp
273
137
138                     ua.Close();
nmp
265
139                 }
140                 else
141                 {
142                     // User did not agre
143                     ConfigurationManager.AppSettings["UserAgreementStatus"] = "Declined";
144                     ConfigurationManager.Save();
nmp
273
145
146                     ua.Close();
147
nmp
265
148                     this.Close();
149                     return;
150                 }
151             }
152             
thassan
107
153             //
154             // Required for Windows Form Designer support
155             //
thassan
154
156             _delegateStatusUpdate = new DelegateStatusUpdate(StatusUpdate);
thassan
262
157             _delegateFeedbackUpdate = new DelegateFeedbackUpdate(FeedbackUpdate);
thassan
178
158             _delegateUploadUpdate = new DelegateUploadUpdate(UploadUpdate);
thassan
165
159
nmp
234
160             _sensorDataManager = new SensorDataManager();
thassan
262
161             _feedbackManager = new FeedbackManager();
nmp
293
162             //_sensorDataManager.Log = true; // log events 
thassan
190
163
nmp
250
164             // Set the folder where campaignr will save the file (deprecated)
thassan
190
165             //_sensorDataManager.SaveFileFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
nmp
250
166             
thassan
166
167             InitializeComponent();
thassan
178
168
nmp
250
169             // Must be called after 
170             if (menuCollectUpload.Checked)
171                 menuCollectUpload_Click(nullnull);
172             else if (menuCollect.Checked)
173                 menuCollect_Click(nullnull);
174             else if (menuUpload.Checked)
175                 menuUpload_Click(nullnull);
176
thassan
184
177             loadConfiguration(nullnull);
thassan
107
178         }
thassan
193
179
thassan
107
180         /// <summary>
181         /// Clean up any resources being used.
182         /// </summary>
thassan
143
183         protected override void Dispose(bool disposing)
thassan
107
184         {
thassan
143
185             base.Dispose(disposing);
thassan
107
186         }
thassan
143
187
thassan
107
188         #region Windows Form Designer generated code
189         /// <summary>
190         /// Required method for Designer support - do not modify
191         /// the contents of this method with the code editor.
192         /// </summary>
193         private void InitializeComponent()
194         {
195             this.mainMenu1 = new System.Windows.Forms.MainMenu();
196             this.campaignMenuItem = new System.Windows.Forms.MenuItem();
197             this.menuCollect = new System.Windows.Forms.MenuItem();
198             this.menuCollectUpload = new System.Windows.Forms.MenuItem();
199             this.menuUpload = new System.Windows.Forms.MenuItem();
thassan
178
200             this.menuConfig = new System.Windows.Forms.MenuItem();
thassan
107
201             this.exitMenuItem = new System.Windows.Forms.MenuItem();
nmp
185
202             this.label1 = new System.Windows.Forms.Label();
nmp
187
203             this.status = new System.Windows.Forms.Label();
204             this.upload = new System.Windows.Forms.Label();
nmp
218
205             this.loadingPanel = new System.Windows.Forms.Panel();
nmp
234
206             this.label2 = new System.Windows.Forms.Label();
nmp
218
207             this.progressBar1 = new System.Windows.Forms.ProgressBar();
208             this.loadingPanel.SuspendLayout();
thassan
107
209             this.SuspendLayout();
210             // 
211             // mainMenu1
212             // 
213             this.mainMenu1.MenuItems.Add(this.campaignMenuItem);
214             this.mainMenu1.MenuItems.Add(this.exitMenuItem);
215             // 
216             // campaignMenuItem
217             // 
nmp
218
218             this.campaignMenuItem.Enabled = false;
thassan
107
219             this.campaignMenuItem.MenuItems.Add(this.menuCollect);
220             this.campaignMenuItem.MenuItems.Add(this.menuCollectUpload);
221             this.campaignMenuItem.MenuItems.Add(this.menuUpload);
thassan
178
222             this.campaignMenuItem.MenuItems.Add(this.menuConfig);
nmp
183
223             this.campaignMenuItem.Text = "Options";
thassan
107
224             // 
225             // menuCollect
226             // 
thassan
190
227             this.menuCollect.Text = "Collect / Save";
thassan
107
228             this.menuCollect.Click += new System.EventHandler(this.menuCollect_Click);
229             // 
230             // menuCollectUpload
231             // 
thassan
190
232             this.menuCollectUpload.Text = "Collect / Upload";
thassan
132
233             this.menuCollectUpload.Click += new System.EventHandler(this.menuCollectUpload_Click);
thassan
107
234             // 
235             // menuUpload
236             // 
thassan
190
237             this.menuUpload.Text = "Upload Saved";
thassan
143
238             this.menuUpload.Click += new System.EventHandler(this.menuUpload_Click);
thassan
107
239             // 
thassan
178
240             // menuConfig
241             // 
thassan
191
242             this.menuConfig.Text = "Change Configuration";
thassan
184
243             this.menuConfig.Click += new System.EventHandler(this.loadConfiguration);
thassan
178
244             // 
thassan
107
245             // exitMenuItem
246             // 
nmp
218
247             this.exitMenuItem.Enabled = false;
nmp
204
248             this.exitMenuItem.Text = "Exit";
thassan
107
249             this.exitMenuItem.Click += new System.EventHandler(this.exitMenuItem_Click);
250             // 
nmp
187
251             // label1
252             // 
253             this.label1.Location = new System.Drawing.Point(0153);
254             this.label1.Name = "label1";
255             this.label1.Size = new System.Drawing.Size(32010);
256             // 
thassan
107
257             // status
258             // 
nmp
187
259             this.status.Font = new System.Drawing.Font("Segoe Condensed"8FSystem.Drawing.FontStyle.Regular);
260             this.status.Location = new System.Drawing.Point(42);
thassan
107
261             this.status.Name = "status";
nmp
187
262             this.status.Size = new System.Drawing.Size(313158);
thassan
177
263             // 
264             // upload
265             // 
nmp
187
266             this.upload.Font = new System.Drawing.Font("Segoe Condensed"8FSystem.Drawing.FontStyle.Regular);
267             this.upload.Location = new System.Drawing.Point(4167);
thassan
177
268             this.upload.Name = "upload";
nmp
187
269             this.upload.Size = new System.Drawing.Size(31320);
270             this.upload.TextAlign = System.Drawing.ContentAlignment.TopCenter;
nmp
185
271             // 
nmp
218
272             // loadingPanel
273             // 
nmp
273
274             this.loadingPanel.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
275                         | System.Windows.Forms.AnchorStyles.Left)
276                         | System.Windows.Forms.AnchorStyles.Right)));
277             this.loadingPanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
nmp
218
278             this.loadingPanel.Controls.Add(this.label2);
279             this.loadingPanel.Controls.Add(this.progressBar1);
nmp
273
280             this.loadingPanel.Location = new System.Drawing.Point(2543);
nmp
218
281             this.loadingPanel.Name = "loadingPanel";
nmp
273
282             this.loadingPanel.Size = new System.Drawing.Size(26991);
nmp
218
283             // 
284             // label2
285             // 
nmp
273
286             this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
287                         | System.Windows.Forms.AnchorStyles.Right)));
nmp
218
288             this.label2.Location = new System.Drawing.Point(324);
289             this.label2.Name = "label2";
nmp
273
290             this.label2.Size = new System.Drawing.Size(26330);
nmp
218
291             this.label2.Text = "Loading please wait...";
292             this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
293             // 
nmp
234
294             // progressBar1
295             // 
nmp
273
296             this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
297                         | System.Windows.Forms.AnchorStyles.Right)));
nmp
234
298             this.progressBar1.Location = new System.Drawing.Point(357);
299             this.progressBar1.Name = "progressBar1";
nmp
273
300             this.progressBar1.Size = new System.Drawing.Size(26319);
nmp
234
301             // 
thassan
107
302             // MainForm
303             // 
304             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
305             this.ClientSize = new System.Drawing.Size(320186);
nmp
218
306             this.Controls.Add(this.loadingPanel);
thassan
177
307             this.Controls.Add(this.upload);
nmp
187
308             this.Controls.Add(this.status);
nmp
185
309             this.Controls.Add(this.label1);
thassan
107
310             this.Menu = this.mainMenu1;
311             this.Name = "MainForm";
312             this.Text = "Campaignr";
thassan
203
313             this.Load += new System.EventHandler(this.MainForm_Load);
thassan
107
314             this.Closed += new System.EventHandler(this.MainForm_Closed);
nmp
218
315             this.loadingPanel.ResumeLayout(false);
thassan
107
316             this.ResumeLayout(false);
317
318         }
319         #endregion
320
321         /// <summary>
322         /// The main entry point for the application.
323         /// </summary>
thassan
262
324
thassan
143
325         static void Main()
thassan
107
326         {
nmp
273
327             ThreadPool.SetMaxThreads(1010);
328             
nmp
265
329             try
330             {
331                 Application.Run(new MainForm());
332             }
nmp
273
333             catch (ObjectDisposedException ex)
nmp
265
334             {
335                 // TODO: Fix ObjectDisposedException exception
nmp
273
336                 string trace = ex.StackTrace;
nmp
265
337             }
thassan
107
338         }
339
thassan
193
340         /// <summary>
341         /// Timer function that displays status and error messages to screen
342         /// </summary>
thassan
178
343         private void monitorUpload(Object o)
344         {
345             lock (_lock)
346             {
347                 Message msg;
nmp
296
348                 //bool error = false;
thassan
190
349                 try
350                 {
thassan
262
351                     while (!_killUploadMonitor && (msg = DatabaseManager.MessageQueue.GetItem()) != null)
thassan
178
352                     {
thassan
190
353                         // fetch all messages one at a time, once fetch, they are removed from queue
nmp
293
354                         if (msg.MType == MessageType.Error)
thassan
190
355                         {
356                             // critical system error
nmp
296
357                            // error = true;   // stop the current operation if there are error messages
nmp
187
358
thassan
190
359                             if (_debugModeOn)
360                                 Invoke(_delegateStatusUpdatenew Object[] { statusmsg.ToString() });
361                         }
nmp
293
362                         else if (msg.MType == MessageType.Warning)
thassan
190
363                         {
364                             if (_debugModeOn)
365                                 Invoke(_delegateStatusUpdatenew Object[] { statusmsg.ToString() });
366                         }
nmp
293
367                         else if (msg.MType == MessageType.Status)
thassan
193
368                         {
nmp
296
369                             Invoke(_delegateStatusUpdatenew Object[] { statusmsg.ToString() }); 
thassan
193
370                         }
nmp
293
371                         else if (msg.MType == MessageType.Information)
nmp
188
372                         {
thassan
190
373                             if (_debugModeOn)
nmp
188
374                                 Invoke(_delegateStatusUpdatenew Object[] { statusmsg.ToString() });
375                         }
nmp
293
376                         else if (msg.MType == MessageType.UploadSuccess)
thassan
190
377                         {
378                             if (msg.Text.Length == 0)
nmp
296
379                                 Invoke(_delegateUploadUpdatenew Object[] { upload"" });                         
thassan
193
380                             else
nmp
296
381                                 Invoke(_delegateUploadUpdatenew Object[] { uploadmsg.ToString() });             
thassan
190
382                         }
nmp
293
383                         else if (msg.MType == MessageType.Feedback)
thassan
262
384                         {
385                             if (msg.Text.Length > 0)
nmp
296
386                                 // Display feedback in textbox
387                                 Invoke(_delegateFeedbackUpdatenew Object[] { statusmsg.ToString() });
thassan
262
388                         }
nmp
293
389                         else if (msg.MType == MessageType.TaskComplete)
thassan
190
390                         {
nmp
296
391                             Invoke(_delegateUploadUpdatenew Object[] { statusmsg.ToString() });
thassan
190
392                         }
thassan
178
393                     }
thassan
190
394
nmp
296
395                     //if (error)
396                     //    Invoke(_delegateMenus, new Object[] { null, null });
397                 } catch {     
398                     // if there is an error at this point, it is most likely because the application is exiting and freeing resources, so don't bother showing it on screen
thassan
190
399                 }
thassan
177
400             }
thassan
154
401         }
402
thassan
193
403         /// <summary>
404         /// Updates the Status textbox
405         /// </summary>       
nmp
183
406         private void StatusUpdate(Label ctrlObject val)
thassan
154
407         {
nmp
183
408             if (ctrl.Text.Length + val.ToString().Length < 5000)
thassan
178
409             {
nmp
187
410                 ctrl.Text = val.ToString() + "\r\n" + ctrl.Text;
thassan
178
411             }
thassan
177
412             else
413             {
nmp
183
414                 ctrl.Text = val.ToString() + "\r\n" + ctrl.Text.Substring(01000);
thassan
177
415             }
nmp
266
416
417             if (this._graphicalInterface != null)
418             {
419                 _graphicalInterface.OutputText = val.ToString().Substring(10);
420             }
thassan
262
421         }
nmp
253
422
thassan
262
423         /// <summary>
424         /// Updates any feedback control
425         /// </summary>       
426         private void FeedbackUpdate(Label ctrlObject val)
427         {
428             if (ctrl.Text.Length + val.ToString().Length < 5000)
nmp
253
429             {
thassan
262
430                 ctrl.Text = val.ToString() + "\r\n" + ctrl.Text;
431             }
432             else
433             {
434                 ctrl.Text = val.ToString() + "\r\n" + ctrl.Text.Substring(01000);
nmp
253
435             }
thassan
154
436         }
437
thassan
193
438         /// <summary>
439         /// Updates the Upload textbox
440         /// </summary>
nmp
183
441         private void UploadUpdate(Label ctrlObject val)
thassan
178
442         {
nmp
266
443             ctrl.Text = val.ToString();
444
445             if (this._graphicalInterface != null)
446             {
447                 _graphicalInterface.OutputText = val.ToString().Substring(10);
448             }
thassan
178
449         }
450
thassan
262
451         //! what do do when the exit menu item is selected
452         private void exitMenuItem_Click(object senderEventArgs e)
thassan
107
453         {
nmp
265
454             if (_sensorDataManager != null)
455             {
nmp
293
456                 DatabaseManager.MessageQueue.PostItem(new Message("Closing Campaignr"MessageType.Status));
nmp
265
457                 _sensorDataManager.Log = false// make sure we turn off the log
458             }
459
460             try
461             {
462                 Close();
463             }
nmp
273
464             catch (Exception ex)
nmp
265
465             {
466                 // TODO: Fix Null Pointer exception
nmp
273
467                 string exs2 = ex.Message;
nmp
265
468             }
thassan
107
469         }
thassan
262
470
471         //! Called when MainForm loads
472         private void MainForm_Load(object senderSystem.EventArgs e)
473         {
474             status.Width = Screen.PrimaryScreen.WorkingArea.Width;
475             status.Height = Screen.PrimaryScreen.WorkingArea.Height;
476         }
477
nmp
296
478         // Close sensors and any transmit monitors
thassan
262
479         private void MainForm_Closed(object senderSystem.EventArgs e)
480         {
nmp
296
481             closeSensors();
thassan
262
482         }
483
484         //! Start upload monitor thread
485         private void startMonitor()
486         {
487             status.Text = "";
488             _killUploadMonitor = false;
489             AutoResetEvent autoEvent = new AutoResetEvent(false);
490             _timerDelegate = new TimerCallback(monitorUpload);
nmp
296
491             _uploadMonitorTimer = new System.Threading.Timer(_timerDelegateautoEvent0uploadMonitorInterval);
492         }
493
494         private void uploadData() { }
495
496         private void collectData() { }
497
498         private void closeSensors(){
499             if (_uploadMonitorTimer != null)
500             {
501                 _killUploadMonitor = true;
502                 _uploadMonitorTimer.Dispose();
503             }
504             _sensorDataManager.CloseSensors();
505         }
506
507         private OperationModes _operationMode;
508
509         // Set mode of operation (upload, collect or collect_upload)
510         // Modes are mutually exclusive
511         private OperationModes Mode
512         {
513             set
514             {
515                 _operationMode = value;
516
517                 // Interrupt sensors and monitor while changing mode
518                 closeSensors();
519
520                 bool activityOn = false;
521
522                 switch (_operationMode)
523                 {
524                     case OperationModes.Upload:
525                         menuCollect.Checked = false;
526                         menuCollectUpload.Checked = false;
527
528                         activityOn = menuUpload.Checked = !menuUpload.Checked;
529
530                         if(activityOn)
531                             _sensorDataManager.UploadOnly();
532
533                         break;
534                     case OperationModes.Collect:
535                         menuUpload.Checked = false;
536                         menuCollectUpload.Checked = false;
thassan
262
537
nmp
296
538                         activityOn = menuCollect.Checked = !menuCollect.Checked;
539
540                         if(activityOn)
541                             _sensorDataManager.Collect(false);
542
543                         break;
544                     case OperationModes.CollectUpload:
545                         menuUpload.Checked = false;
546                         menuCollect.Checked = false;
547
548                         activityOn = menuCollectUpload.Checked = !menuCollectUpload.Checked;
549
550                         if (activityOn && _manualCapture == false)
551                             _sensorDataManager.Collect(true);
552
553                         break;
554                 }
555
556                 if (activityOn)
557                     startMonitor();
558             }
thassan
262
559         }
560
nmp
296
561         // Collect data and store locally
thassan
262
562         private void menuCollect_Click(object senderEventArgs e)
thassan
132
563         {
nmp
296
564             this.Mode = OperationModes.Collect;
565             /*if (menuUpload.Checked)
thassan
190
566                 menuUpload_Click(null, null);
567             else if (menuCollectUpload.Checked)
568                 menuCollectUpload_Click(null, null);
thassan
132
569
thassan
190
570             if (!menuCollect.Checked)
thassan
132
571             {
thassan
154
572                 _delegateMenus = new DelegateMenus(menuCollect_Click);
nmp
296
573                 
thassan
190
574                 _sensorDataManager.Collect(false);
575
thassan
154
576                 startMonitor();
thassan
132
577             }
thassan
143
578             else
thassan
132
579             {
nmp
296
580                 closeSensors();
thassan
132
581             }
582
nmp
296
583             menuCollect.Checked = !menuCollect.Checked;*/
thassan
143
584         }
thassan
132
585
thassan
262
586         //! Collect data and upload to server
nmp
253
587         public void menuCollectUpload_Click(object senderEventArgs e)
thassan
132
588         {
nmp
296
589             this.Mode = OperationModes.CollectUpload;
590             /*if (menuUpload.Checked)
thassan
190
591                 menuUpload_Click(null, null);
592             else if (menuCollect.Checked)
593                 menuCollect_Click(null, null);
thassan
132
594
thassan
143
595             if (!menuCollectUpload.Checked)
thassan
132
596             {
thassan
154
597                 _delegateMenus = new DelegateMenus(menuCollectUpload_Click);
nmp
292
598
599                 if (_manualCapture == false)
nmp
218
600                     _sensorDataManager.Collect(true);
601
thassan
154
602                 startMonitor();
thassan
132
603             }
thassan
143
604             else
thassan
132
605             {
nmp
296
606                 closeSensors();
607             }
608
609             menuCollectUpload.Checked = !menuCollectUpload.Checked;*/
610         }
611
612
613         // Upload to server
614         private void menuUpload_Click(object senderEventArgs e)
615         {
616             this.Mode = OperationModes.Upload;
617             /*if (menuCollectUpload.Checked)
618                 menuCollectUpload_Click(null, null);
619             else if (menuCollect.Checked)
620                 menuCollect_Click(null, null);
621
622             if (!menuUpload.Checked)
623             {
624                 _delegateMenus = new DelegateMenus(menuUpload_Click);
625                 _sensorDataManager.UploadOnly();
626
627                 startMonitor();
628             }
629             else
630             {
631                 closeSensors();
thassan
132
632             }
633
nmp
296
634             menuUpload.Checked = !menuUpload.Checked;*/
thassan
143
635         }
thassan
132
636
nmp
296
637         // Reads the configuration xml file for campaignr
thassan
178
638         private void readXMLConfig(string xmlcfgfilename)
thassan
165
639         {
640             try
641             {
thassan
178
642                 XmlTextReader rdr = new XmlTextReader(xmlcfgfilename);
thassan
165
643                 SimpleDOMParser sdp = new SimpleDOMParser();
644                 SimpleElement se = sdp.parse(rdr);
thassan
178
645
nmp
292
646                 bool flushDatabase = false;
647
nmp
234
648                 if(_sensorDataManager!=null)
649                     _sensorDataManager.ClearSensors();
thassan
190
650
thassan
165
651                 if (se.TagName.ToLower().Equals("campaign"))
652                 {
653                     _campaignname = se.Attributes["name"];
nmp
183
654
nmp
296
655                     // TODO: Flush the database if requested
nmp
292
656                     if (se.Attribute("flushDatabase") != null && se.Attribute("flushDatabase") == "true")
657                         flushDatabase = true;
658
nmp
183
659                     this.Text = "Campaignr - "_campaignname;
660
nmp
296
661                     // Whether or not to start uploading data on start
thassan
165
662                     _uploadOnStart = System.Convert.ToBoolean(se.Attributes["uploadOnStart"]);
nmp
296
663
nmp
187
664                     _debugModeOn = System.Convert.ToBoolean(se.Attributes["debug"]);
thassan
165
665
nmp
189
666                     if (!_debugModeOn)
nmp
293
667                         _sensorDataManager.Log = false;
668                     else
669                         _sensorDataManager.Log = true;
nmp
189
670
thassan
165
671                     SimpleElements elements = se.ChildElements;
nmp
218
672
nmp
296
673                     SimpleElement automatic = elements.Item("automatic");
nmp
218
674
675                     if (automatic==null)
676                     {
677                         automatic = elements.Item("manual");
678                         _manualCapture = true;
679                     }
680
681                     if (automatic==null)
682                     {
thassan
262
683                         DatabaseManager.MessageQueue.PostItem(new Message("Configuration file missing automatic or manual section"MessageType.Error));
nmp
218
684                         return;
685                     }
nmp
296
686
687                     // Whether or not to start collecting data on start
688                     _startOnLoad = System.Convert.ToBoolean(se.Attributes["startOnLoad"]) && !_manualCapture;
689
nmp
218
690                     progressBar1.Value += 10;
691                     int step = (int)Math.Floor(20 / automatic.ChildElements.Count);
thassan
165
692
693                     foreach (SimpleElement child in automatic.ChildElements)
694                     {
nmp
218
695                         progressBar1.Value += step;
696
thassan
165
697                         string name = child.TagName;
nmp
281
698                         if (name.ToLower().Equals("sensor")) // activate the appropriate sensors by parsing this element
thassan
165
699                         {
nmp
281
700                             string type = child.Attributes["type"];
thassan
165
701
702                             if (type != null)
703                             {
nmp
185
704                                 try
705                                 {
nmp
228
706                                     // Get name
707                                     string sensorName = child.Attributes["name"];
708                                     if (sensorName == null)
thassan
233
709                                         //throw new ArgumentException("Missing name attribute for " + type + " sensor");
710                                         sensorName = type.Trim();  // make it compatible with Symbian and older xmls
nmp
228
711                                     else
712                                         sensorName = sensorName.Trim();
713
nmp
185
714                                     if (type.ToLower().Equals("imei"))
nmp
273
715                                     {
716                                         string hashed = child.Attributes["hash"];
717
718                                         if(hashed!=null && hashed=="true")
719                                             _sensorDataManager.assignSensors(new DeviceId("campaignrwm"), sensorName);
720                                         else
721                                             _sensorDataManager.assignSensors(new Imei(), sensorName);
722                                     }
nmp
207
723                                     else if (type.ToLower().Equals("cellid"))
nmp
228
724                                         _sensorDataManager.assignSensors(new CellTower(), sensorName);
nmp
185
725                                     else if (type.ToLower().Equals("timestamp"))
nmp
228
726                                         _sensorDataManager.assignSensors(new Timestamp(), sensorName);
nmp
273
727                                     else if (type.ToLower().Equals("bt_stumble"))
728                                     {
thassan
233
729                                         if (child.ChildElements.Count == 0)
thassan
262
730                                             _sensorDataManager.assignSensors(new BTStumbler(true), sensorName);
thassan
233
731                                         else
732                                         {
733                                             foreach (SimpleElement btchild in child.ChildElements)
734                                             {
735                                                 if (btchild.TagName == "address_only")
thassan
262
736                                                     _sensorDataManager.assignSensors(new BTStumbler(false), sensorName);
thassan
233
737                                             }
738                                         }
739
740                                     }
nmp
185
741                                     else if (type.ToLower().Equals("location"))
nmp
231
742                                     {
743 </