浏览代码

Merge branch 'testrapport-bugfixing' into New-GUI

Bart Reedijk 10 年之前
父节点
当前提交
20b0c61d46

+ 25 - 37
Proftaak Remote Healthcare/FietsClientV2/DataHandler.cs

@@ -4,6 +4,7 @@ using System.IO.Ports;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows.Forms;
 
 namespace FietsClientV2
 {
@@ -41,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()
         {
@@ -55,29 +56,35 @@ 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)
         {
             if (ComPort != null)
+            {
                 ComPort.Close();
+                state = State.notConnected;
+            }
 
             this.portname = portname;
             try
             {
                 ComPort = new SerialPort(this.portname, this.baudrate);
                 ComPort.Open();
+                state = State.connected;
                 ComPort.WriteLine(RESET);
+                state = State.reset;
                 ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived);
             }
             catch (Exception)
             {
-                OnIncomingDebugLineEvent("ERROR: Exception throwed");
+                OnIncomingErrorEvent("WrongComPort");
                 try { ComPort.Close(); } catch (Exception) { } // probeer om de ComPort wel te sluiten.
+                state = State.notConnected;
             }
 
 
@@ -133,24 +140,33 @@ namespace FietsClientV2
             OnIncomingDataEvent(bufferIn);
         }
 
-        private bool checkBikeState()
+        public bool checkBikeState(bool commandMode)
         {
+            if (ComPort == null || !ComPort.IsOpen)
+            {
+                OnIncomingErrorEvent("NotConnectedToBike");
+                state = State.notConnected;
+                return false;
+            }
             switch (state)
             {
                 case State.reset:
-                    setCommandMode();
+                    if (commandMode) setCommandMode();
                     if (returnData != ReturnData.ERROR)
                         return true;
                     return false;
                 case State.connected:
-                    setCommandMode();
+                    if (commandMode) setCommandMode();
                     return true;
                 case State.command:
                     return true;
                 case State.notConnected:
+                    OnIncomingErrorEvent("NotConnectedToBike");
                     Console.WriteLine("ERROR: not connected to bike.");
                     return false;
                 default:
+                    OnIncomingErrorEvent("NotConnectedToBike");
+                    Console.WriteLine("ERROR: unknown error.");
                     return false;
             }
         }
@@ -159,33 +175,5 @@ namespace FietsClientV2
         {
             sendData(COMMAND);
         }
-
-        public void setTime()
-        {
-            if (!checkBikeState())
-                return;
-            sendData(CMD_TIME);
-        }
-
-        public void setDistance()
-        {
-            if (!checkBikeState())
-                return;
-            sendData(CMD_DISTANCE);
-        }
-
-        public void setPower()
-        {
-            if (!checkBikeState())
-                return;
-            sendData(CMD_POWER);
-        }
-
-        public void setEnergy()
-        {
-            if (!checkBikeState())
-                return;
-            sendData(CMD_ENERGY);
-        }
     }
 }

+ 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)

+ 22 - 1
Proftaak Remote Healthcare/FietsClientV2/PatientModel.cs

@@ -20,6 +20,8 @@ namespace FietsClientV2
         private DataHandler dataHandler;
         private Thread workerThread;
 
+        private String powerLog;
+
         private PatientModel()
         {
             dataHandler = new DataHandler();
@@ -42,7 +44,21 @@ namespace FietsClientV2
             while (true)
             {
                 Thread.Sleep(1000);
-                dataHandler.sendData(DataHandler.STATUS);
+
+                if(patientform.actualBox.Text != powerLog)
+                {
+                    setPower(powerLog);
+                }
+
+                try
+                {
+                    dataHandler.sendData(DataHandler.STATUS);
+                }
+                catch (Exception e)
+                {
+                    dataHandler.closeComm();
+                }
+                
             }
         }
         //event handler
@@ -106,24 +122,29 @@ namespace FietsClientV2
         //change bike values
         public void setTimeMode(string time)
         {
+            if (!dataHandler.checkBikeState(false)) return;
             dataHandler.sendData("CM");
             dataHandler.sendData("PT " + time);
         }
 
         public void setPower(string power)
         {
+	        powerLog = power;
+            if (!dataHandler.checkBikeState(false)) return;
             dataHandler.sendData("CM");
             dataHandler.sendData("PW " + power);
         }
 
         public void setDistanceMode(string distance)
         {
+            if (!dataHandler.checkBikeState(false)) return;
             dataHandler.sendData("CM");
             dataHandler.sendData("PD " + distance);
         }
 
         public void reset()
         {
+            if (!dataHandler.checkBikeState(false)) return;
             dataHandler.sendData("RS");
         }
     }