[chronojump] Capture trigger code. Done! Untested! DB: 1.39



commit 9191ae750930f7a7e4a3f408b0a5f4d068361f63
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Mar 31 18:07:26 2017 +0200

    Capture trigger code. Done! Untested! DB: 1.39

 chronojump.csproj                     |    2 +
 diagrams/sqlite/chronojump_sqlite.dia |  Bin 14608 -> 14969 bytes
 diagrams/sqlite/chronojump_sqlite.png |  Bin 247832 -> 255738 bytes
 src/Makefile.am                       |    2 +
 src/constants.cs                      |    1 +
 src/encoderCapture.cs                 |   28 ++++---
 src/gui/encoder.cs                    |    4 +
 src/sqlite/main.cs                    |   10 ++-
 src/sqlite/trigger.cs                 |  115 +++++++++++++++++++++++++++
 src/trigger.cs                        |  140 +++++++++++++++++++++++++++++++++
 10 files changed, 291 insertions(+), 11 deletions(-)
---
diff --git a/chronojump.csproj b/chronojump.csproj
index 47bc461..c269f24 100644
--- a/chronojump.csproj
+++ b/chronojump.csproj
@@ -1115,6 +1115,8 @@
     <Compile Include="src\sprint.cs" />
     <Compile Include="src\gui\forceSensor.cs" />
     <Compile Include="src\gui\sprint.cs" />
+    <Compile Include="src\sqlite\trigger.cs" />
+    <Compile Include="src\trigger.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="src\" />
diff --git a/diagrams/sqlite/chronojump_sqlite.dia b/diagrams/sqlite/chronojump_sqlite.dia
index b123754..59bc8c2 100644
Binary files a/diagrams/sqlite/chronojump_sqlite.dia and b/diagrams/sqlite/chronojump_sqlite.dia differ
diff --git a/diagrams/sqlite/chronojump_sqlite.png b/diagrams/sqlite/chronojump_sqlite.png
index 6e66947..9c0ac7a 100644
Binary files a/diagrams/sqlite/chronojump_sqlite.png and b/diagrams/sqlite/chronojump_sqlite.png differ
diff --git a/src/Makefile.am b/src/Makefile.am
index 77eb331..45831d5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -118,6 +118,7 @@ SOURCES = \
        sqlite/event.cs\
        sqlite/sport.cs\
        sqlite/speciallity.cs\
+       sqlite/trigger.cs\
        sqlite/usefulObjects.cs\
        sqlite/country.cs\
        sqlite/oldConvert.cs\
@@ -170,6 +171,7 @@ SOURCES = \
        treeViewPulse.cs\
        treeViewReactionTime.cs\
        treeViewMultiChronopic.cs\
+       trigger.cs\
        util.cs\
        utilAll.cs\
        utilCSV.cs\
diff --git a/src/constants.cs b/src/constants.cs
index 45a8bba..8c17ec8 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -141,6 +141,7 @@ public class Constants
        public const string EncoderExerciseTable = "encoderExercise";
        public const string Encoder1RMTable = "encoder1RM";
        public const string ExecuteAutoTable = "executeAuto";
+       public const string TriggerTable = "trigger";
 
        // Dummy variables that exists for translating purposes
        // pragma warning is to avoid warnings of "defined and not used" for these variables.
diff --git a/src/encoderCapture.cs b/src/encoderCapture.cs
index 12b7f51..93ec555 100644
--- a/src/encoderCapture.cs
+++ b/src/encoderCapture.cs
@@ -55,7 +55,9 @@ public abstract class EncoderCapture
 
        private int TRIGGER_ON = 84; //'T' from TRIGGER_ON on encoder firmware
        private int TRIGGER_OFF = 116; //'t' from TRIGGER_OFF on encoder firmware
-       private BoolMsList boolMsList;
+
+       //private BoolMsList boolMsList;
+       private TriggerList triggerList;
        
        /*
         * sum: sum ob byteReaded, it's the vertical position
@@ -190,7 +192,7 @@ public abstract class EncoderCapture
                initSpecific();
 
                //prepare for receiving triggers from encoder
-               boolMsList = new BoolMsList();
+               triggerList = new TriggerList();
                Util.FileDelete(Util.GetEncoderTriggerFileName());
 
                cancel = false;
@@ -232,12 +234,12 @@ public abstract class EncoderCapture
 
                        if(byteReaded == TRIGGER_ON)
                        {
-                               boolMsList.Add(true, i);
+                               triggerList.Add(new Trigger(Trigger.Modes.ENCODER, i, true));
                                continue;
                        }
                        else if(byteReaded == TRIGGER_OFF)
                        {
-                               boolMsList.Add(false, i);
+                               triggerList.Add(new Trigger(Trigger.Modes.ENCODER, i, false));
                                continue;
                        }
 
@@ -262,8 +264,8 @@ public abstract class EncoderCapture
                                        {
                                                LogB.Information("Cleaning on capture");
 
-                                               //remove this time on existing boolMs records
-                                               boolMsList.Substract(consecutiveZeros);
+                                               //remove this time on existing trigger records
+                                               triggerList.Substract(consecutiveZeros);
 
                                                consecutiveZeros = -1;
                                                encoderReadedInertialDisc = new List<int>();
@@ -521,8 +523,6 @@ public abstract class EncoderCapture
 
                saveToFile(outputData1);
 
-               boolMsList.Write();
-
                LogB.Debug("runEncoderCaptureCsharp ended");
 
                return true;
@@ -656,8 +656,8 @@ public abstract class EncoderCapture
 
                if(count > allowedZeroMSAtStart)
                {
-                       l.RemoveRange(0, count-allowedZeroMSAtStart);
-                       boolMsList.Substract(count-allowedZeroMSAtStart);
+                       l.RemoveRange(0, count - allowedZeroMSAtStart);
+                       triggerList.Substract(count - allowedZeroMSAtStart);
                } // else: not enough zeros at start, don't need to trim 
 
                return l; 
@@ -680,6 +680,11 @@ public abstract class EncoderCapture
                writer.Close();
                ((IDisposable)writer).Dispose();
        }
+
+       public void SaveTriggers()
+       {
+               triggerList.SQLInsert();
+       }
        
        //this methods only applies to inertial subclass
        protected virtual void inertialCheckIfInverted() {
@@ -923,6 +928,7 @@ public class EncoderCaptureIMCalc : EncoderCapture
        
 }
 
+/*
 public class BoolMsList
 {
        private List<BoolMs> l;
@@ -967,6 +973,7 @@ public class BoolMsList
                ((IDisposable)writer).Dispose();
        }
 }
+
 public class BoolMs
 {
        private bool b;
@@ -988,3 +995,4 @@ public class BoolMs
                return b.ToString() + ": " + ms.ToString();
        }
 }
+*/
diff --git a/src/gui/encoder.cs b/src/gui/encoder.cs
index c9fd985..c2a4eb0 100644
--- a/src/gui/encoder.cs
+++ b/src/gui/encoder.cs
@@ -5742,6 +5742,10 @@ public partial class ChronoJumpWindow
                                                
encoderCaptureSaveCurvesAllNoneBest(preferences.encoderAutoSaveCurve,
                                                                
Constants.GetEncoderVariablesCapture(preferences.encoderCaptureMainVariable));
 
+                                       //save the triggers now that we have an encoderSignalUniqueID
+                                       if(action == encoderActions.CURVES_AC)
+                                               eCapture.SaveTriggers(); //dbcon is closed
+
                                } else
                                        encoder_pulsebar_capture.Text = "";
                
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index ec866fd..1ec7cba 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -125,7 +125,7 @@ class Sqlite
        /*
         * Important, change this if there's any update to database
         */
-       static string lastChronojumpDatabaseVersion = "1.38";
+       static string lastChronojumpDatabaseVersion = "1.39";
 
        public Sqlite() {
        }
@@ -2143,6 +2143,12 @@ class Sqlite
 
                                currentVersion = updateVersion("1.38");
                        }
+                       if(currentVersion == "1.38")
+                       {
+                               LogB.SQL("Created trigger table");
+                               SqliteTrigger.createTableTrigger();
+                               currentVersion = updateVersion("1.39");
+                       }
 
 
                        // --- add more updates here
@@ -2318,8 +2324,10 @@ class Sqlite
                SqliteExecuteAuto.addChronojumpProfileAndBilateral();
 
                SqliteChronopicRegister.createTableChronopicRegister();
+               SqliteTrigger.createTableTrigger();
 
                //changes [from - to - desc]
+               //1.38 - 1.39 Converted DB to 1.39 Created trigger table 
                //1.37 - 1.38 Converted DB to 1.38 encoderConfiguration always with 12 values. Empty 
encoderConfiguration list_d as '' instead of '-1' or '0'
                //1.36 - 1.37 Converted DB to 1.37 Deleted encoderConfiguration variable. Added 
encoderConfiguration table (1.36)
                //1.35 - 1.36 Converted DB to 1.36 Deleted encoderConfiguration table
diff --git a/src/sqlite/trigger.cs b/src/sqlite/trigger.cs
new file mode 100644
index 0000000..d96e988
--- /dev/null
+++ b/src/sqlite/trigger.cs
@@ -0,0 +1,115 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation; either version 2 of the License, or   
+ *    (at your option) any later version.
+ *    
+ * ChronoJump is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ *    GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2017   Xavier de Blas <xaviblas gmail com> 
+ */
+
+using System;
+//using System.Data;
+using System.Collections.Generic; //List<T>
+using Mono.Data.Sqlite;
+
+class SqliteTrigger : Sqlite
+{
+       private static string table = Constants.TriggerTable;
+
+       public SqliteTrigger() {
+       }
+       
+       ~SqliteTrigger() {}
+       
+       /*
+        * create and initialize tables
+        */
+       
+       
+       protected internal static void createTableTrigger()
+       {
+               dbcmd.CommandText = 
+                       "CREATE TABLE " + table + " ( " +
+                       "uniqueID INTEGER PRIMARY KEY, " +
+                       "mode TEXT, " +         //encoder; gauge
+                       "modeID INT, " +        //on encoder: uniqueID
+                       "ms INT, " +
+                       "inOut INT, " +         //bool
+                       "name TEXT, " +
+                       "color TEXT, " +
+                       "comments TEXT )";
+               dbcmd.ExecuteNonQuery();
+       }
+       
+       public static List<Trigger> Select (bool dbconOpened, Trigger.Modes mode, int modeID)
+       {
+               openIfNeeded(dbconOpened);
+               
+               dbcmd.CommandText = "SELECT * FROM " + table + " WHERE mode = " + mode + " AND modeID = " + 
modeID;
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+               
+               SqliteDataReader reader = dbcmd.ExecuteReader();
+       
+               List<Trigger> l = new List<Trigger>();
+               while(reader.Read()) {
+                       Trigger trigger = new Trigger(
+                                       Convert.ToInt32(reader[0]),             //uniqueID
+                                       (Trigger.Modes) Enum.Parse(
+                                               typeof(Trigger.Modes), reader[1].ToString()), //mode
+                                       Convert.ToInt32(reader[2]),             //modeID
+                                       Convert.ToInt32(reader[3]),             //ms
+                                       Util.StringToBool(reader[4].ToString()),//inOut
+                                       reader[5].ToString(),                   //name
+                                       reader[6].ToString(),                   //color
+                                       reader[7].ToString()                    //comments
+                                       );
+                       l.Add(trigger);
+               }
+
+               reader.Close();
+               closeIfNeeded(dbconOpened);
+
+               return l;
+       }
+
+       public static void InsertList(bool dbconOpened, List<Trigger> l)
+       {
+               openIfNeeded(dbconOpened);
+       
+               foreach(Trigger trigger in l)
+               {
+                       dbcmd.CommandText = "INSERT INTO " + table + 
+                               " (uniqueID, mode, modeID, ms, inOut, name, color, comments) VALUES (" +
+                               trigger.ToSQL() + ")";
+                       LogB.SQL(dbcmd.CommandText.ToString());
+                       dbcmd.ExecuteNonQuery();
+               }
+               
+               closeIfNeeded(dbconOpened);
+       }
+       
+       public static void Delete(bool dbconOpened, Trigger trigger)
+       {
+               openIfNeeded(dbconOpened);
+
+               dbcmd.CommandText = "Delete FROM " + table + " WHERE uniqueID = " + trigger.UniqueID;
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+               
+               closeIfNeeded(dbconOpened);
+       }
+
+}
+
diff --git a/src/trigger.cs b/src/trigger.cs
new file mode 100644
index 0000000..e3671ea
--- /dev/null
+++ b/src/trigger.cs
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2017  Xavier de Blas <xaviblas gmail com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+using System;
+using System.Collections.Generic; //List<T>
+
+public class Trigger
+{
+       public enum Modes { ENCODER }
+
+       private int uniqueID;
+       private Modes mode;
+       private int modeID;
+       private int ms;
+       private bool inOut;
+       private string name;
+       private string color;
+       private string comments;
+
+       //constructor used on capture
+       public Trigger (Modes mode, int ms, bool inOut)
+       {
+               this.uniqueID = -1;     //will be assigned on SQL insertion
+               this.mode = mode;
+               this.modeID = -1;       //will be assigned on SQL insertion
+               this.ms = ms;
+               this.inOut = inOut;
+               this.name = "";
+               this.color = "";
+               this.comments = "";
+       }
+
+       //constructor used on loading from SQL
+       public Trigger (int uniqueID, Modes mode, int modeID, int ms, bool inOut, string name, string color, 
string comments)
+       {
+               this.uniqueID = uniqueID;
+               this.mode = mode;
+               this.modeID = modeID;
+               this.ms = ms;
+               this.inOut = inOut;
+               this.name = name;
+               this.color = color;
+               this.comments = comments;
+       }
+
+       public void Substract(int msToSubstract)
+       {
+               ms -= msToSubstract;
+       }
+
+       public string ToSQL()
+       {
+               return 
+                       uniqueID.ToString() + "," +
+                       "\"" + mode.ToString() + "\"" + "," +
+                       modeID.ToString() + "," +
+                       ms.ToString() + "," +
+                       Util.BoolToInt(inOut).ToString() + "," +
+                       "\"" + name.ToString() + "\"" + "," +
+                       "\"" + color.ToString() + "\"" + "," +
+                       "\"" + comments.ToString() + "\""
+                       ;
+       }
+
+       public int UniqueID {
+               get { return uniqueID; }
+       }
+}
+
+public class TriggerList
+{
+       private List<Trigger> l;
+       public TriggerList()
+       {
+               l = new List<Trigger>();
+       }
+
+       public void Add(Trigger trigger)
+       {
+               l.Add(trigger);
+       }
+
+       public void Substract(int msToSubstract)
+       {
+               foreach(Trigger trigger in l)
+                       trigger.Substract(msToSubstract);
+       }
+
+       //just to debug
+       public void Print()
+       {
+               LogB.Information("Printing trigger list");
+               foreach(Trigger trigger in l)
+                       LogB.Information(trigger.ToString());
+       }
+
+       public void SQLInsert()
+       {
+               //save triggers to file (if any)
+               if(l == null || l.Count == 0)
+                       return;
+
+               LogB.Debug("runEncoderCaptureCsharp SQL inserting triggers");
+               SqliteTrigger.InsertList(false, l);
+       }
+       /*
+       public void Write()
+       {
+               //save triggers to file (if any)
+               if(l == null || l.Count == 0)
+                       return;
+
+               LogB.Debug("runEncoderCaptureCsharp saving triggers");
+               TextWriter writer = File.CreateText(Util.GetEncoderTriggerDateTimeFileName());
+
+               foreach(Trigger trigger in l)
+                       writer.WriteLine(trigger.ToString());
+
+               writer.Flush();
+               writer.Close();
+               ((IDisposable)writer).Dispose();
+       }
+       */
+}
+


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]