[chronojump] Sqlite 1.33. chronopicRegister SQL and list working



commit 6577b29d688c8b007555c96101148217f3f889d8
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Sep 29 01:19:45 2016 +0200

    Sqlite 1.33. chronopicRegister SQL and list working

 src/Makefile.am                 |    1 +
 src/chronopicRegister.cs        |   98 ++++++++++++++++++++++++++++++++-
 src/constants.cs                |    2 +
 src/sqlite/chronopicRegister.cs |  113 +++++++++++++++++++++++++++++++++++++++
 src/sqlite/main.cs              |   25 ++++++++-
 5 files changed, 234 insertions(+), 5 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index b6cd5a2..deccdd3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -88,6 +88,7 @@ SOURCES = \
        stats/graphs/runSimple.cs\
        stats/graphs/runIntervallic.cs\
        stats/graphs/jumpSimpleSubtraction.cs\
+       sqlite/chronopicRegister.cs\
        sqlite/main.cs\
        sqlite/preferences.cs\
        sqlite/session.cs\
diff --git a/src/chronopicRegister.cs b/src/chronopicRegister.cs
index 5dfc173..e45f977 100644
--- a/src/chronopicRegister.cs
+++ b/src/chronopicRegister.cs
@@ -29,28 +29,119 @@ public class ChronopicRegisterPort
        public string Port;
        public bool FTDI;
        public string SerialNumber;
+       public enum Types { UNKNOWN, CONTACTS, ENCODER }
+       public Types Type;
 
-       public ChronopicRegisterPort (string port) {
+       //constructor when port is known (searching FTDI stuff on a serial port)
+       public ChronopicRegisterPort (string port)
+       {
                this.Port = port;
                this.FTDI = false;
                this.SerialNumber = "";
+               this.Type = Types.UNKNOWN;
+       }
+
+       //constructor used on SqliteChronopicRegister.SelectAll()
+       public ChronopicRegisterPort (string serialNumber, Types type)
+       {
+               this.Port = "";
+               this.FTDI = true;
+               this.SerialNumber = serialNumber;
+               this.Type = type;
+       }
+
+       public override string ToString()
+       {
+               return "Port: " + Port + " ; FTDI: " + FTDI.ToString() +
+                       " ; SerialNumber: " + SerialNumber + " ; Type: " + Type.ToString();
+       }
+}
+
+public class ChronopicRegisterPortList
+{
+       public List<ChronopicRegisterPort> L;
+
+       //constructor
+       public ChronopicRegisterPortList ()
+       {
+               this.L = SqliteChronopicRegister.SelectAll(false);
+       }
+
+       public void Print()
+       {
+               LogB.Information("Printing ChronopicRegisterPortList... ");
+               foreach(ChronopicRegisterPort crp in L)
+                       LogB.Information(crp.ToString());
+
+               LogB.Information("... Done!");
+       }
+
+       public bool Exists(ChronopicRegisterPort crp)
+       {
+               foreach(ChronopicRegisterPort c in L)
+                       if(c.SerialNumber == crp.SerialNumber)
+                               return true;
+
+               return false;
+       }
+
+       public void Add (ChronopicRegisterPort crp)
+       {
+               //Add to SQL
+               SqliteChronopicRegister.Insert(false, crp);
+
+               //Add to list
+               L.Add(crp);
        }
 
-       public override string ToString() {
-               return "Port: " + Port + " ; FTDI: " + FTDI.ToString() + " ; SerialNumber: " + SerialNumber;
+       public void Update (ChronopicRegisterPort crp, ChronopicRegisterPort.Types newType)
+       {
+               //Update SQL
+               SqliteChronopicRegister.Update(false, crp, newType);
+
+               //Update list
+               foreach(ChronopicRegisterPort c in L) {
+                       if(c.SerialNumber == crp.SerialNumber) {
+                               c.Type = newType;
+                               break;
+                       }
+               }
        }
+
+       public void Delete (ChronopicRegisterPort crp)
+       {
+               //Delete from SQL
+               SqliteChronopicRegister.Delete(false, crp);
+
+               //Delete from list
+               L.Remove(crp);
+       }
+
 }
 
+
 public class ChronopicRegister 
 {
        public ChronopicRegister () 
        {
+               //1 print the registered ports on SQL
+               ChronopicRegisterPortList crpl = new ChronopicRegisterPortList();
+               crpl.Print();
+
                List<string> ports = getPorts(true);
                foreach(string p in ports) {
                        LogB.Information(string.Format("ChronopicRegister for port: " + p));
                        ChronopicRegisterPort crp = readFTDI(p);
                        LogB.Information(crp.ToString());
+
+                       //2 add to registered list (add also on database)
+                       if(crp.FTDI && ! crpl.Exists(crp))
+                               crpl.Add(crp);
                }
+
+               //1 print the registered ports on SQL
+               crpl = new ChronopicRegisterPortList();
+               crpl.Print();
        }
 
        private List<string> getPorts(bool debug) 
@@ -65,6 +156,7 @@ public class ChronopicRegister
                return l;
        }
 
+       //read all information of one port
        private ChronopicRegisterPort readFTDI(string port) 
        {
                //if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX)
diff --git a/src/constants.cs b/src/constants.cs
index 1afb15c..b394737 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -122,6 +122,8 @@ public class Constants
        public const string PreferencesTable = "preferences";
        public const string CountryTable = "country";
        public const string ConvertTempTable = "convertTemp"; //for conversions
+       public const string ChronopicRegisterTable = "chronopicRegister";
+
        //tests
        public const string JumpTable = "jump";
        public const string JumpRjTable = "jumpRj";
diff --git a/src/sqlite/chronopicRegister.cs b/src/sqlite/chronopicRegister.cs
new file mode 100644
index 0000000..efe6803
--- /dev/null
+++ b/src/sqlite/chronopicRegister.cs
@@ -0,0 +1,113 @@
+/*
+ * 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) 2004-2016   Xavier de Blas <xaviblas gmail com> 
+ */
+
+using System;
+//using System.Data;
+using System.Collections.Generic; //List<T>
+using Mono.Data.Sqlite;
+
+class SqliteChronopicRegister : Sqlite
+{
+       private static string table = Constants.ChronopicRegisterTable;
+
+       public SqliteChronopicRegister() {
+       }
+       
+       ~SqliteChronopicRegister() {}
+       
+       /*
+        * create and initialize tables
+        */
+       
+       
+       protected internal static void createTableChronopicRegister()
+       {
+               dbcmd.CommandText = 
+                       "CREATE TABLE " + table + " ( " +
+                       "serialNumber TEXT, " +
+                       "type TEXT )";
+               dbcmd.ExecuteNonQuery();
+       }
+       
+       public static List<ChronopicRegisterPort> SelectAll (bool dbconOpened)
+       {
+               openIfNeeded(dbconOpened);
+               
+               dbcmd.CommandText = "SELECT * FROM " + table;
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+               
+               SqliteDataReader reader = dbcmd.ExecuteReader();
+       
+               List<ChronopicRegisterPort> l = new List<ChronopicRegisterPort>();
+               while(reader.Read()) {
+                       ChronopicRegisterPort crp = new ChronopicRegisterPort(
+                                       reader[0].ToString(), //serialNumber
+                                       (ChronopicRegisterPort.Types) Enum.Parse(
+                                               typeof(ChronopicRegisterPort.Types), reader[1].ToString()) 
//type
+                                       );
+                       l.Add(crp);
+               }
+               
+               closeIfNeeded(dbconOpened);
+
+               return l;
+       }
+
+       public static void Insert(bool dbconOpened, ChronopicRegisterPort crp)
+       {
+               openIfNeeded(dbconOpened);
+               
+               dbcmd.CommandText = "INSERT INTO " + table + 
+                       " (serialNumber, type) VALUES (\"" + 
+                       crp.SerialNumber + "\", \"" + crp.Type.ToString() + "\")" ;
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+               
+               closeIfNeeded(dbconOpened);
+       }
+       
+       public static void Update(bool dbconOpened, ChronopicRegisterPort crp, ChronopicRegisterPort.Types 
newType)
+       {
+               openIfNeeded(dbconOpened);
+
+               dbcmd.CommandText = "UPDATE " + table +
+                       " SET type = \"" + newType.ToString() + 
+                       "\" WHERE serialNumber = \"" + crp.SerialNumber + "\"" ;
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+               
+               closeIfNeeded(dbconOpened);
+       }
+       
+       public static void Delete(bool dbconOpened, ChronopicRegisterPort crp)
+       {
+               openIfNeeded(dbconOpened);
+
+               dbcmd.CommandText = "Delete FROM " + table +
+                       " WHERE serialNumber = " + crp.SerialNumber;
+               LogB.SQL(dbcmd.CommandText.ToString());
+               dbcmd.ExecuteNonQuery();
+               
+               closeIfNeeded(dbconOpened);
+       }
+
+}
+
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index 762b549..713bd6a 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -78,7 +78,7 @@ class Sqlite
        /*
         * Important, change this if there's any update to database
         */
-       static string lastChronojumpDatabaseVersion = "1.32";
+       static string lastChronojumpDatabaseVersion = "1.33";
 
        public Sqlite() {
        }
@@ -131,6 +131,17 @@ class Sqlite
                IsOpened = false;
        }
 
+       protected static void openIfNeeded(bool dbconOpened)
+       {
+               if(! dbconOpened)
+                       Open();
+       }
+       protected static void closeIfNeeded(bool dbconOpened)
+       {
+               if(! dbconOpened)
+                       Close();
+       }
+
        public static bool Connect()
        {
                /*
@@ -1897,6 +1908,13 @@ class Sqlite
 
                                currentVersion = updateVersion("1.32");
                        }
+                       if(currentVersion == "1.32") {
+                               LogB.SQL("Added chronopicRegister table");
+
+                               SqliteChronopicRegister.createTableChronopicRegister();
+
+                               currentVersion = updateVersion("1.33");
+                       }
 
 
 
@@ -2060,8 +2078,11 @@ class Sqlite
                                
                SqliteExecuteAuto.createTableExecuteAuto();
                SqliteExecuteAuto.addChronojumpProfileAndBilateral();
-               
+
+               SqliteChronopicRegister.createTableChronopicRegister();
+
                //changes [from - to - desc]
+               //1.32 - 1.33 Converted DB to 1.33 Added chronopicRegister table
                //1.31 - 1.32 Converted DB to 1.32 encoderCaptureOptionsWin -> preferences
                //1.30 - 1.31 Converted DB to 1.31 Insert encoderCaptureCheckFullyExtended and ...Value at 
preferences
                //1.29 - 1.30 Converted DB to 1.30 Added SIMULATED session


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