Procházet zdrojové kódy

Added additional tests: don't try to connect to bike when comport is not filled in correctly; clean everything up

Bart Reedijk před 10 roky
rodič
revize
8c5cf0503f

+ 9 - 8
Proftaak Remote Healthcare/FietsClientV2/DataHandler.cs

@@ -42,8 +42,8 @@ namespace FietsClientV2
         public delegate void DataDelegate(string[] data);
         public static event DataDelegate IncomingDataEvent;
 
-        public delegate void DebugDelegate(string debugData);
-        public static event DebugDelegate IncomingDebugLineEvent;
+        public delegate void ErrorDelegate(string error);
+        public static event ErrorDelegate IncomingErrorEvent;
 
         public DataHandler()
         {
@@ -56,10 +56,10 @@ namespace FietsClientV2
             if (handler != null) handler(data);
         }
 
-        public static void OnIncomingDebugLineEvent(string debugData)
+        public static void OnIncomingErrorEvent(string error)
         {
-            DebugDelegate handler = IncomingDebugLineEvent;
-            if (handler != null) handler(debugData);
+            ErrorDelegate handler = IncomingErrorEvent;
+            if (handler != null) handler(error);
         }
 
         public void initComm(string portname)
@@ -82,7 +82,7 @@ namespace FietsClientV2
             }
             catch (Exception)
             {
-                OnIncomingDebugLineEvent("ERROR: Exception throwed");
+                OnIncomingErrorEvent("WrongComPort");
                 try { ComPort.Close(); } catch (Exception) { } // probeer om de ComPort wel te sluiten.
                 state = State.notConnected;
             }
@@ -144,6 +144,7 @@ namespace FietsClientV2
         {
             if (ComPort == null || !ComPort.IsOpen)
             {
+                OnIncomingErrorEvent("NotConnectedToBike");
                 state = State.notConnected;
                 return false;
             }
@@ -160,11 +161,11 @@ namespace FietsClientV2
                 case State.command:
                     return true;
                 case State.notConnected:
-                    MessageBox.Show("ERROR: not connected to bike.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                    OnIncomingErrorEvent("NotConnectedToBike");
                     Console.WriteLine("ERROR: not connected to bike.");
                     return false;
                 default:
-                    MessageBox.Show("ERROR: unknown error.", "ËRROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                    OnIncomingErrorEvent("NotConnectedToBike");
                     Console.WriteLine("ERROR: unknown error.");
                     return false;
             }

+ 17 - 0
Proftaak Remote Healthcare/FietsClientV2/PatientForm.cs

@@ -19,6 +19,23 @@ namespace FietsClientV2
             InitializeComponent();
             patienModel = PatientModel.patientModel;
             patienModel.patientform = this;
+            DataHandler.IncomingErrorEvent += HandleError; //initialize event
+        }
+
+        private void HandleError(string error)
+        {
+            switch (error)
+            {
+                case "WrongComPort":
+                    toolStripComboBox1.Text = "";
+                    MessageBox.Show("ERROR: Comport not initialized... trying to close the comport", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                    break;
+                case "NotConnectedToBike":
+                    MessageBox.Show("ERROR: Not connected to bike.", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
+                    break;
+                default:
+                    break;
+            }
         }
 
         private void Form1_Load(object sender, EventArgs e)