First Version Ever that runs a little bit
This commit is contained in:
parent
6513a92c77
commit
78e559985f
@ -50,7 +50,7 @@ void doJob(String task, String value) {
|
||||
co = 0x00539F; //Rwth Color
|
||||
}
|
||||
job = value.toInt();
|
||||
Rb.fillRectangle(1, 2, 6, 7, 0x000000);
|
||||
Rb.fillRectangle(1, 2, 7, 6, 0x000000);
|
||||
Rb.drawChar(b, 1, 1, co);
|
||||
} else if(task == "online") {
|
||||
Rb.setPixelXY(0,7,value.toInt()==0?0xFF0000:0x00FF00);
|
||||
|
@ -42,7 +42,7 @@ void loadFromDisplay() {
|
||||
print_disp("getStoreData=1");
|
||||
parse(disp.readStringUntil('\n'));
|
||||
}
|
||||
if(keepAlive()) {
|
||||
if(storeCount > 0 && keepAlive()) {
|
||||
doJob("getStore","1");
|
||||
}
|
||||
}
|
||||
|
@ -10,518 +10,460 @@ using System.Data.SQLite;
|
||||
using MySql;
|
||||
using MySql.Data.Common;
|
||||
using MySql.Data.MySqlClient;
|
||||
using TimeKeeper.Exceptions;
|
||||
|
||||
namespace BF2Statistics.Database
|
||||
{
|
||||
public class DatabaseDriver : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Current DB Engine
|
||||
/// </summary>
|
||||
public DatabaseEngine DatabaseEngine { get; protected set; }
|
||||
namespace TimeKeeper.Database {
|
||||
public class DatabaseDriver : IDisposable {
|
||||
/// <summary>
|
||||
/// Current DB Engine
|
||||
/// </summary>
|
||||
public DatabaseEngine DatabaseEngine { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// The database connection
|
||||
/// </summary>
|
||||
public DbConnection Connection { get; protected set; }
|
||||
/// <summary>
|
||||
/// The database connection
|
||||
/// </summary>
|
||||
public DbConnection Connection { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the Database connection is open
|
||||
/// </summary>
|
||||
public bool IsConnected
|
||||
{
|
||||
get { return (Connection.State == ConnectionState.Open); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current conenction state of the database
|
||||
/// </summary>
|
||||
public ConnectionState State
|
||||
{
|
||||
get { return Connection.State; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of queries ran by this instance
|
||||
/// </summary>
|
||||
public int NumQueries = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Random, yes... But its used here when building queries dynamically
|
||||
/// </summary>
|
||||
protected static char Comma = ',';
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the disposed method was called
|
||||
/// </summary>
|
||||
protected bool IsDisposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="Engine">The string name, for the GetDatabaseEngine() method</param>
|
||||
/// <param name="Host">The Database server IP Address</param>
|
||||
/// <param name="Port">The Database server Port Number</param>
|
||||
/// <param name="DatabaseName">The name of the database</param>
|
||||
/// <param name="User">A username, with database privliages</param>
|
||||
/// <param name="Pass">The password to the User</param>
|
||||
public DatabaseDriver(string Engine, string Host, int Port, string DatabaseName, string User, string Pass)
|
||||
{
|
||||
// Set class variables, and create a new connection builder
|
||||
this.DatabaseEngine = GetDatabaseEngine(Engine);
|
||||
DbConnectionStringBuilder Builder;
|
||||
|
||||
// Establish the connection
|
||||
if (this.DatabaseEngine == DatabaseEngine.Sqlite)
|
||||
{
|
||||
// Create the connection
|
||||
Builder = new SQLiteConnectionStringBuilder();
|
||||
Builder.Add("Data Source", Path.Combine(Program.RootPath, DatabaseName + ".sqlite3"));
|
||||
Connection = new SQLiteConnection(Builder.ConnectionString);
|
||||
}
|
||||
else if (this.DatabaseEngine == DatabaseEngine.Mysql)
|
||||
{
|
||||
// Create the connection
|
||||
Builder = new MySqlConnectionStringBuilder();
|
||||
Builder.Add("Server", Host);
|
||||
Builder.Add("Port", Port);
|
||||
Builder.Add("User ID", User);
|
||||
Builder.Add("Password", Pass);
|
||||
Builder.Add("Database", DatabaseName);
|
||||
Builder.Add("Convert Zero Datetime", "true");
|
||||
Connection = new MySqlConnection(Builder.ConnectionString);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Invalid Database type.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destructor
|
||||
/// </summary>
|
||||
~DatabaseDriver()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the DB connection
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if(Connection != null && !IsDisposed)
|
||||
{
|
||||
try
|
||||
{
|
||||
Connection.Close();
|
||||
Connection.Dispose();
|
||||
}
|
||||
catch (ObjectDisposedException) { }
|
||||
|
||||
IsDisposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens the database connection
|
||||
/// </summary>
|
||||
public void Connect()
|
||||
{
|
||||
if (Connection.State != ConnectionState.Open)
|
||||
{
|
||||
try
|
||||
{
|
||||
Connection.Open();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new DbConnectException("Unable to etablish database connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the connection to the database
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
try {
|
||||
if (Connection.State != ConnectionState.Closed)
|
||||
Connection.Close();
|
||||
}
|
||||
catch (ObjectDisposedException) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command to be executed on the database
|
||||
/// </summary>
|
||||
/// <param name="QueryString"></param>
|
||||
public DbCommand CreateCommand(string QueryString)
|
||||
{
|
||||
if (DatabaseEngine == Database.DatabaseEngine.Sqlite)
|
||||
return new SQLiteCommand(QueryString, Connection as SQLiteConnection);
|
||||
else
|
||||
return new MySqlCommand(QueryString, Connection as MySqlConnection);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a DbParameter using the current Database engine's Parameter object
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public DbParameter CreateParam()
|
||||
{
|
||||
if (DatabaseEngine == Database.DatabaseEngine.Sqlite)
|
||||
return (new SQLiteParameter() as DbParameter);
|
||||
else
|
||||
return (new MySqlParameter() as DbParameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries the database, and returns a result set
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL Statement to run on the database</param>
|
||||
/// <returns></returns>
|
||||
public List<Dictionary<string, object>> Query(string Sql)
|
||||
{
|
||||
return this.Query(Sql, new List<DbParameter>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries the database, and returns a result set
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL Statement to run on the database</param>
|
||||
/// <param name="Items">Additional parameters are parameter values for the query.
|
||||
/// The first parameter replaces @P0, second @P1 etc etc.
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
public List<Dictionary<string, object>> Query(string Sql, params object[] Items)
|
||||
{
|
||||
List<DbParameter> Params = new List<DbParameter>(Items.Length);
|
||||
for (int i = 0; i < Items.Length; i++)
|
||||
{
|
||||
DbParameter Param = this.CreateParam();
|
||||
Param.ParameterName = "@P" + i;
|
||||
Param.Value = Items[i];
|
||||
Params.Add(Param);
|
||||
}
|
||||
|
||||
return this.Query(Sql, Params);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries the database, and returns a result set
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL Statement to run on the database</param>
|
||||
/// <param name="Params">A list of sql params to add to the command</param>
|
||||
/// <returns></returns>
|
||||
public List<Dictionary<string, object>> Query(string Sql, List<DbParameter> Params)
|
||||
{
|
||||
// Create our Rows result
|
||||
List<Dictionary<string, object>> Rows = new List<Dictionary<string, object>>();
|
||||
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Create the SQL Command
|
||||
using (DbCommand Command = this.CreateCommand(Sql))
|
||||
{
|
||||
// Add params
|
||||
foreach (DbParameter Param in Params)
|
||||
Command.Parameters.Add(Param);
|
||||
|
||||
// Execute the query
|
||||
using (DbDataReader Reader = Command.ExecuteReader())
|
||||
{
|
||||
// If we have rows, add them to the list
|
||||
if (Reader.HasRows)
|
||||
{
|
||||
// Add each row to the rows list
|
||||
while (Reader.Read())
|
||||
{
|
||||
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
|
||||
for (int i = 0; i < Reader.FieldCount; ++i)
|
||||
Row.Add(Reader.GetName(i), Reader.GetValue(i));
|
||||
|
||||
Rows.Add(Row);
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
Reader.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// Return Rows
|
||||
return Rows;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries the database, and returns 1 row at a time until all rows are returned
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL Statement to run on the database</param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<Dictionary<string, object>> QueryReader(string Sql)
|
||||
{
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Create the SQL Command, and execute the reader
|
||||
using (DbCommand Command = this.CreateCommand(Sql))
|
||||
using (DbDataReader Reader = Command.ExecuteReader())
|
||||
{
|
||||
// If we have rows, add them to the list
|
||||
if (Reader.HasRows)
|
||||
{
|
||||
// Add each row to the rows list
|
||||
while (Reader.Read())
|
||||
{
|
||||
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
|
||||
for (int i = 0; i < Reader.FieldCount; ++i)
|
||||
Row.Add(Reader.GetName(i), Reader.GetValue(i));
|
||||
|
||||
yield return Row;
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
Reader.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a command, and returns 1 row at a time until all rows are returned
|
||||
/// </summary>
|
||||
/// <param name="Command">The database command to execute the reader on</param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<Dictionary<string, object>> QueryReader(DbCommand Command)
|
||||
{
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Execute the query
|
||||
using (Command)
|
||||
using (DbDataReader Reader = Command.ExecuteReader())
|
||||
{
|
||||
// If we have rows, add them to the list
|
||||
if (Reader.HasRows)
|
||||
{
|
||||
// Add each row to the rows list
|
||||
while (Reader.Read())
|
||||
{
|
||||
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
|
||||
for (int i = 0; i < Reader.FieldCount; ++i)
|
||||
Row.Add(Reader.GetName(i), Reader.GetValue(i));
|
||||
|
||||
yield return Row;
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
Reader.Close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Executes a command, and returns the resulting rows
|
||||
/// </summary>
|
||||
/// <param name="Command">The database command to execute the reader on</param>
|
||||
/// <returns></returns>
|
||||
public List<Dictionary<string, object>> ExecuteReader(DbCommand Command)
|
||||
{
|
||||
// Execute the query
|
||||
List<Dictionary<string, object>> Rows = new List<Dictionary<string, object>>();
|
||||
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
using (Command)
|
||||
using (DbDataReader Reader = Command.ExecuteReader())
|
||||
{
|
||||
// If we have rows, add them to the list
|
||||
if (Reader.HasRows)
|
||||
{
|
||||
// Add each row to the rows list
|
||||
while (Reader.Read())
|
||||
{
|
||||
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
|
||||
for (int i = 0; i < Reader.FieldCount; ++i)
|
||||
Row.Add(Reader.GetName(i), Reader.GetValue(i));
|
||||
|
||||
Rows.Add(Row);
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
Reader.Close();
|
||||
}
|
||||
|
||||
// Return Rows
|
||||
return Rows;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a statement on the database (Update, Delete, Insert)
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executes</param>
|
||||
/// <returns>Returns the number of rows affected by the statement</returns>
|
||||
public int Execute(string Sql)
|
||||
{
|
||||
// Create the SQL Command
|
||||
using (DbCommand Command = this.CreateCommand(Sql))
|
||||
return Command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a statement on the database (Update, Delete, Insert)
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <param name="Params">A list of Sqlparameters</param>
|
||||
/// <returns>Returns the number of rows affected by the statement</returns>
|
||||
public int Execute(string Sql, List<DbParameter> Params)
|
||||
{
|
||||
// Create the SQL Command
|
||||
using (DbCommand Command = this.CreateCommand(Sql))
|
||||
{
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Add params
|
||||
foreach (DbParameter Param in Params)
|
||||
Command.Parameters.Add(Param);
|
||||
|
||||
// Execute command, and dispose of the command
|
||||
return Command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a statement on the database (Update, Delete, Insert)
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <param name="Items">Additional parameters are parameter values for the query.
|
||||
/// The first parameter replaces @P0, second @P1 etc etc.
|
||||
/// </param>
|
||||
/// <returns>Returns the number of rows affected by the statement</returns>
|
||||
public int Execute(string Sql, params object[] Items)
|
||||
{
|
||||
// Create the SQL Command
|
||||
using (DbCommand Command = this.CreateCommand(Sql))
|
||||
{
|
||||
// Add params
|
||||
for (int i = 0; i < Items.Length; i++)
|
||||
{
|
||||
DbParameter Param = this.CreateParam();
|
||||
Param.ParameterName = "@P" + i;
|
||||
Param.Value = Items[i];
|
||||
Command.Parameters.Add(Param);
|
||||
}
|
||||
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Execute command, and dispose of the command
|
||||
return Command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the query, and returns the first column of the first row in the result
|
||||
/// set returned by the query. Additional columns or rows are ignored.
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <returns></returns>
|
||||
public object ExecuteScalar(string Sql)
|
||||
{
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Create the SQL Command
|
||||
using (DbCommand Command = this.CreateCommand(Sql))
|
||||
return Command.ExecuteScalar();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the query, and returns the first column of the first row in the result
|
||||
/// set returned by the query. Additional columns or rows are ignored.
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <param name="Params">A list of Sqlparameters</param>
|
||||
/// <returns></returns>
|
||||
public object ExecuteScalar(string Sql, List<DbParameter> Params)
|
||||
{
|
||||
// Create the SQL Command
|
||||
using (DbCommand Command = this.CreateCommand(Sql))
|
||||
{
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Add params
|
||||
foreach (DbParameter Param in Params)
|
||||
Command.Parameters.Add(Param);
|
||||
|
||||
// Execute command, and dispose of the command
|
||||
return Command.ExecuteScalar();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the query, and returns the first column of the first row in the result
|
||||
/// set returned by the query. Additional columns or rows are ignored.
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <param name="Items"></param>
|
||||
/// <returns></returns>
|
||||
public object ExecuteScalar(string Sql, params object[] Items)
|
||||
{
|
||||
// Create the SQL Command
|
||||
using (DbCommand Command = this.CreateCommand(Sql))
|
||||
{
|
||||
// Add params
|
||||
for (int i = 0; i < Items.Length; i++)
|
||||
{
|
||||
DbParameter Param = this.CreateParam();
|
||||
Param.ParameterName = "@P" + i;
|
||||
Param.Value = Items[i];
|
||||
Command.Parameters.Add(Param);
|
||||
}
|
||||
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Execute command, and dispose of the command
|
||||
return Command.ExecuteScalar();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Begins a new database transaction
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public DbTransaction BeginTransaction()
|
||||
{
|
||||
return Connection.BeginTransaction();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Begins a new database transaction
|
||||
/// </summary>
|
||||
/// <param name="Level"></param>
|
||||
/// <returns></returns>
|
||||
public DbTransaction BeginTransaction(IsolationLevel Level)
|
||||
{
|
||||
return Connection.BeginTransaction(Level);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a database string name to a DatabaseEngine type.
|
||||
/// </summary>
|
||||
/// <param name="Name"></param>
|
||||
/// <returns></returns>
|
||||
public static DatabaseEngine GetDatabaseEngine(string Name)
|
||||
{
|
||||
return ((DatabaseEngine)Enum.Parse(typeof(DatabaseEngine), Name, true));
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns whether the Database connection is open
|
||||
/// </summary>
|
||||
public bool IsConnected {
|
||||
get { return (Connection.State == ConnectionState.Open); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current conenction state of the database
|
||||
/// </summary>
|
||||
public ConnectionState State {
|
||||
get { return Connection.State; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of queries ran by this instance
|
||||
/// </summary>
|
||||
public int NumQueries = 0;
|
||||
|
||||
/// <summary>
|
||||
/// Random, yes... But its used here when building queries dynamically
|
||||
/// </summary>
|
||||
protected static char Comma = ',';
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the disposed method was called
|
||||
/// </summary>
|
||||
protected bool IsDisposed = false;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="Engine">The string name, for the GetDatabaseEngine() method</param>
|
||||
/// <param name="Host">The Database server IP Address</param>
|
||||
/// <param name="Port">The Database server Port Number</param>
|
||||
/// <param name="DatabaseName">The name of the database</param>
|
||||
/// <param name="User">A username, with database privliages</param>
|
||||
/// <param name="Pass">The password to the User</param>
|
||||
public DatabaseDriver(string Engine, string Host, int Port, string DatabaseName, string User, string Pass, DatabaseEngine[] allowedEngines) {
|
||||
// Set class variables, and create a new connection builder
|
||||
this.DatabaseEngine = GetDatabaseEngine(Engine);
|
||||
DbConnectionStringBuilder Builder;
|
||||
|
||||
// Establish the connection
|
||||
if(this.DatabaseEngine == DatabaseEngine.Sqlite && allowedEngines.Contains(DatabaseEngine.Sqlite)) {
|
||||
// Create the connection
|
||||
Builder = new SQLiteConnectionStringBuilder();
|
||||
Builder.Add("Data Source", Path.Combine(StaticConfig.RootPath, DatabaseName + ".sqlite3"));
|
||||
Connection = new SQLiteConnection(Builder.ConnectionString);
|
||||
} else if(this.DatabaseEngine == DatabaseEngine.Mysql && allowedEngines.Contains(DatabaseEngine.Mysql)) {
|
||||
// Create the connection
|
||||
Builder = new MySqlConnectionStringBuilder();
|
||||
Builder.Add("Server", Host);
|
||||
Builder.Add("Port", Port);
|
||||
Builder.Add("User ID", User);
|
||||
Builder.Add("Password", Pass);
|
||||
Builder.Add("Database", DatabaseName);
|
||||
Builder.Add("Convert Zero Datetime", "true");
|
||||
Connection = new MySqlConnection(Builder.ConnectionString);
|
||||
} else {
|
||||
throw new Exception("Invalid Database type.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destructor
|
||||
/// </summary>
|
||||
~DatabaseDriver() {
|
||||
Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the DB connection
|
||||
/// </summary>
|
||||
public void Dispose() {
|
||||
if(Connection != null && !IsDisposed) {
|
||||
try {
|
||||
Connection.Close();
|
||||
Connection.Dispose();
|
||||
} catch(ObjectDisposedException) { }
|
||||
|
||||
IsDisposed = true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens the database connection
|
||||
/// </summary>
|
||||
public void Connect() {
|
||||
if(Connection.State != ConnectionState.Open) {
|
||||
try {
|
||||
Connection.Open();
|
||||
} catch(Exception e) {
|
||||
throw new DbConnectException("Unable to etablish database connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Closes the connection to the database
|
||||
/// </summary>
|
||||
public void Close() {
|
||||
try {
|
||||
if(Connection.State != ConnectionState.Closed)
|
||||
Connection.Close();
|
||||
} catch(ObjectDisposedException) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new command to be executed on the database
|
||||
/// </summary>
|
||||
/// <param name="QueryString"></param>
|
||||
public DbCommand CreateCommand(string QueryString) {
|
||||
if(DatabaseEngine == Database.DatabaseEngine.Sqlite)
|
||||
return new SQLiteCommand(QueryString, Connection as SQLiteConnection);
|
||||
else
|
||||
return new MySqlCommand(QueryString, Connection as MySqlConnection);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a DbParameter using the current Database engine's Parameter object
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public DbParameter CreateParam() {
|
||||
if(DatabaseEngine == Database.DatabaseEngine.Sqlite)
|
||||
return (new SQLiteParameter() as DbParameter);
|
||||
else
|
||||
return (new MySqlParameter() as DbParameter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries the database, and returns a result set
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL Statement to run on the database</param>
|
||||
/// <returns></returns>
|
||||
public List<Dictionary<string, object>> Query(string Sql) {
|
||||
return this.Query(Sql, new List<DbParameter>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries the database, and returns a result set
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL Statement to run on the database</param>
|
||||
/// <param name="Items">Additional parameters are parameter values for the query.
|
||||
/// The first parameter replaces @P0, second @P1 etc etc.
|
||||
/// </param>
|
||||
/// <returns></returns>
|
||||
public List<Dictionary<string, object>> Query(string Sql, params object[] Items) {
|
||||
List<DbParameter> Params = new List<DbParameter>(Items.Length);
|
||||
for(int i = 0; i < Items.Length; i++) {
|
||||
DbParameter Param = this.CreateParam();
|
||||
Param.ParameterName = "@P" + i;
|
||||
Param.Value = Items[i];
|
||||
Params.Add(Param);
|
||||
}
|
||||
|
||||
return this.Query(Sql, Params);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries the database, and returns a result set
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL Statement to run on the database</param>
|
||||
/// <param name="Params">A list of sql params to add to the command</param>
|
||||
/// <returns></returns>
|
||||
public List<Dictionary<string, object>> Query(string Sql, List<DbParameter> Params) {
|
||||
// Create our Rows result
|
||||
List<Dictionary<string, object>> Rows = new List<Dictionary<string, object>>();
|
||||
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Create the SQL Command
|
||||
using(DbCommand Command = this.CreateCommand(Sql)) {
|
||||
// Add params
|
||||
foreach(DbParameter Param in Params)
|
||||
Command.Parameters.Add(Param);
|
||||
|
||||
// Execute the query
|
||||
using(DbDataReader Reader = Command.ExecuteReader()) {
|
||||
// If we have rows, add them to the list
|
||||
if(Reader.HasRows) {
|
||||
// Add each row to the rows list
|
||||
while(Reader.Read()) {
|
||||
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
|
||||
for(int i = 0; i < Reader.FieldCount; ++i)
|
||||
Row.Add(Reader.GetName(i), Reader.GetValue(i));
|
||||
|
||||
Rows.Add(Row);
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
Reader.Close();
|
||||
}
|
||||
}
|
||||
|
||||
// Return Rows
|
||||
return Rows;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queries the database, and returns 1 row at a time until all rows are returned
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL Statement to run on the database</param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<Dictionary<string, object>> QueryReader(string Sql) {
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Create the SQL Command, and execute the reader
|
||||
using(DbCommand Command = this.CreateCommand(Sql))
|
||||
using(DbDataReader Reader = Command.ExecuteReader()) {
|
||||
// If we have rows, add them to the list
|
||||
if(Reader.HasRows) {
|
||||
// Add each row to the rows list
|
||||
while(Reader.Read()) {
|
||||
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
|
||||
for(int i = 0; i < Reader.FieldCount; ++i)
|
||||
Row.Add(Reader.GetName(i), Reader.GetValue(i));
|
||||
|
||||
yield return Row;
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
Reader.Close();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a command, and returns 1 row at a time until all rows are returned
|
||||
/// </summary>
|
||||
/// <param name="Command">The database command to execute the reader on</param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<Dictionary<string, object>> QueryReader(DbCommand Command) {
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Execute the query
|
||||
using(Command)
|
||||
using(DbDataReader Reader = Command.ExecuteReader()) {
|
||||
// If we have rows, add them to the list
|
||||
if(Reader.HasRows) {
|
||||
// Add each row to the rows list
|
||||
while(Reader.Read()) {
|
||||
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
|
||||
for(int i = 0; i < Reader.FieldCount; ++i)
|
||||
Row.Add(Reader.GetName(i), Reader.GetValue(i));
|
||||
|
||||
yield return Row;
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
Reader.Close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Executes a command, and returns the resulting rows
|
||||
/// </summary>
|
||||
/// <param name="Command">The database command to execute the reader on</param>
|
||||
/// <returns></returns>
|
||||
public List<Dictionary<string, object>> ExecuteReader(DbCommand Command) {
|
||||
// Execute the query
|
||||
List<Dictionary<string, object>> Rows = new List<Dictionary<string, object>>();
|
||||
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
using(Command)
|
||||
using(DbDataReader Reader = Command.ExecuteReader()) {
|
||||
// If we have rows, add them to the list
|
||||
if(Reader.HasRows) {
|
||||
// Add each row to the rows list
|
||||
while(Reader.Read()) {
|
||||
Dictionary<string, object> Row = new Dictionary<string, object>(Reader.FieldCount);
|
||||
for(int i = 0; i < Reader.FieldCount; ++i)
|
||||
Row.Add(Reader.GetName(i), Reader.GetValue(i));
|
||||
|
||||
Rows.Add(Row);
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
Reader.Close();
|
||||
}
|
||||
|
||||
// Return Rows
|
||||
return Rows;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a statement on the database (Update, Delete, Insert)
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executes</param>
|
||||
/// <returns>Returns the number of rows affected by the statement</returns>
|
||||
public int Execute(string Sql) {
|
||||
// Create the SQL Command
|
||||
using(DbCommand Command = this.CreateCommand(Sql))
|
||||
return Command.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a statement on the database (Update, Delete, Insert)
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <param name="Params">A list of Sqlparameters</param>
|
||||
/// <returns>Returns the number of rows affected by the statement</returns>
|
||||
public int Execute(string Sql, List<DbParameter> Params) {
|
||||
// Create the SQL Command
|
||||
using(DbCommand Command = this.CreateCommand(Sql)) {
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Add params
|
||||
foreach(DbParameter Param in Params)
|
||||
Command.Parameters.Add(Param);
|
||||
|
||||
// Execute command, and dispose of the command
|
||||
return Command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes a statement on the database (Update, Delete, Insert)
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <param name="Items">Additional parameters are parameter values for the query.
|
||||
/// The first parameter replaces @P0, second @P1 etc etc.
|
||||
/// </param>
|
||||
/// <returns>Returns the number of rows affected by the statement</returns>
|
||||
public int Execute(string Sql, params object[] Items) {
|
||||
// Create the SQL Command
|
||||
using(DbCommand Command = this.CreateCommand(Sql)) {
|
||||
// Add params
|
||||
for(int i = 0; i < Items.Length; i++) {
|
||||
DbParameter Param = this.CreateParam();
|
||||
Param.ParameterName = "@P" + i;
|
||||
Param.Value = Items[i];
|
||||
Command.Parameters.Add(Param);
|
||||
}
|
||||
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Execute command, and dispose of the command
|
||||
return Command.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the query, and returns the first column of the first row in the result
|
||||
/// set returned by the query. Additional columns or rows are ignored.
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <returns></returns>
|
||||
public object ExecuteScalar(string Sql) {
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Create the SQL Command
|
||||
using(DbCommand Command = this.CreateCommand(Sql))
|
||||
return Command.ExecuteScalar();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the query, and returns the first column of the first row in the result
|
||||
/// set returned by the query. Additional columns or rows are ignored.
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <param name="Params">A list of Sqlparameters</param>
|
||||
/// <returns></returns>
|
||||
public object ExecuteScalar(string Sql, List<DbParameter> Params) {
|
||||
// Create the SQL Command
|
||||
using(DbCommand Command = this.CreateCommand(Sql)) {
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Add params
|
||||
foreach(DbParameter Param in Params)
|
||||
Command.Parameters.Add(Param);
|
||||
|
||||
// Execute command, and dispose of the command
|
||||
return Command.ExecuteScalar();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the query, and returns the first column of the first row in the result
|
||||
/// set returned by the query. Additional columns or rows are ignored.
|
||||
/// </summary>
|
||||
/// <param name="Sql">The SQL statement to be executed</param>
|
||||
/// <param name="Items"></param>
|
||||
/// <returns></returns>
|
||||
public object ExecuteScalar(string Sql, params object[] Items) {
|
||||
// Create the SQL Command
|
||||
using(DbCommand Command = this.CreateCommand(Sql)) {
|
||||
// Add params
|
||||
for(int i = 0; i < Items.Length; i++) {
|
||||
DbParameter Param = this.CreateParam();
|
||||
Param.ParameterName = "@P" + i;
|
||||
Param.Value = Items[i];
|
||||
Command.Parameters.Add(Param);
|
||||
}
|
||||
|
||||
// Increase Query Count
|
||||
NumQueries++;
|
||||
|
||||
// Execute command, and dispose of the command
|
||||
return Command.ExecuteScalar();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Begins a new database transaction
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public DbTransaction BeginTransaction() {
|
||||
return Connection.BeginTransaction();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Begins a new database transaction
|
||||
/// </summary>
|
||||
/// <param name="Level"></param>
|
||||
/// <returns></returns>
|
||||
public DbTransaction BeginTransaction(IsolationLevel Level) {
|
||||
return Connection.BeginTransaction(Level);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a database string name to a DatabaseEngine type.
|
||||
/// </summary>
|
||||
/// <param name="Name"></param>
|
||||
/// <returns></returns>
|
||||
public static DatabaseEngine GetDatabaseEngine(string Name) {
|
||||
return ((DatabaseEngine)Enum.Parse(typeof(DatabaseEngine), Name, true));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BF2Statistics.Database
|
||||
namespace TimeKeeper.Database
|
||||
{
|
||||
public enum DatabaseEngine
|
||||
{
|
||||
|
@ -1,298 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace BF2Statistics.Database
|
||||
{
|
||||
/// <summary>
|
||||
/// A class to provide common tasks against the Gamespy Login Database
|
||||
/// </summary>
|
||||
public class GamespyDatabase : DatabaseDriver, IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
public GamespyDatabase() :
|
||||
base(
|
||||
MainForm.Config.GamespyDBEngine,
|
||||
MainForm.Config.GamespyDBHost,
|
||||
MainForm.Config.GamespyDBPort,
|
||||
MainForm.Config.GamespyDBName,
|
||||
MainForm.Config.GamespyDBUser,
|
||||
MainForm.Config.GamespyDBPass
|
||||
)
|
||||
{
|
||||
// Try and Reconnect
|
||||
try
|
||||
{
|
||||
Connect();
|
||||
|
||||
// Try to get the database version
|
||||
try
|
||||
{
|
||||
if (base.Query("SELECT dbver FROM _version LIMIT 1").Count == 0)
|
||||
throw new Exception();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// If an exception is thrown, table doesnt exist... fresh install
|
||||
if (DatabaseEngine == DatabaseEngine.Sqlite)
|
||||
base.Execute(Utils.GetResourceAsString("BF2Statistics.SQL.SQLite.Gamespy.sql"));
|
||||
else
|
||||
base.Execute(Utils.GetResourceAsString("BF2Statistics.SQL.MySQL.Gamespy.sql"));
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
if (Connection != null)
|
||||
Connection.Dispose();
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destructor
|
||||
/// </summary>
|
||||
~GamespyDatabase()
|
||||
{
|
||||
if (!IsDisposed)
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches an account from the gamespy database
|
||||
/// </summary>
|
||||
/// <param name="Nick">The user's Nick</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> GetUser(string Nick)
|
||||
{
|
||||
// Fetch the user
|
||||
var Rows = base.Query("SELECT * FROM accounts WHERE name=@P0", Nick);
|
||||
return (Rows.Count == 0) ? null : Rows[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches an account from the gamespy database
|
||||
/// </summary>
|
||||
/// <param name="Pid">The account player ID</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> GetUser(int Pid)
|
||||
{
|
||||
// Fetch the user
|
||||
var Rows = base.Query("SELECT * FROM accounts WHERE id=@P0", Pid);
|
||||
return (Rows.Count == 0) ? null : Rows[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches an account from the gamespy database
|
||||
/// </summary>
|
||||
/// <param name="Email">Account email</param>
|
||||
/// <param name="Password">Account Password</param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, object> GetUser(string Email, string Password)
|
||||
{
|
||||
var Rows = base.Query("SELECT * FROM accounts WHERE email=@P0 AND password=@P1", Email, Password);
|
||||
return (Rows.Count == 0) ? null : Rows[0];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of player names that are similar to the passed parameter
|
||||
/// </summary>
|
||||
/// <param name="Nick"></param>
|
||||
/// <returns></returns>
|
||||
public List<string> GetUsersLike(string Nick)
|
||||
{
|
||||
// Generate our return list
|
||||
List<string> List = new List<string>();
|
||||
var Rows = base.Query("SELECT name FROM accounts WHERE name LIKE @P0", "%" + Nick + "%");
|
||||
foreach (Dictionary<string, object> Account in Rows)
|
||||
List.Add(Account["name"].ToString());
|
||||
|
||||
return List;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns wether an account exists from the provided Nick
|
||||
/// </summary>
|
||||
/// <param name="Nick"></param>
|
||||
/// <returns></returns>
|
||||
public bool UserExists(string Nick)
|
||||
{
|
||||
// Fetch the user
|
||||
return (base.Query("SELECT id FROM accounts WHERE name=@P0", Nick).Count != 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns wether an account exists from the provided Account Id
|
||||
/// </summary>
|
||||
/// <param name="PID"></param>
|
||||
/// <returns></returns>
|
||||
public bool UserExists(int PID)
|
||||
{
|
||||
// Fetch the user
|
||||
return (base.Query("SELECT name FROM accounts WHERE id=@P0", PID).Count != 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Gamespy Account
|
||||
/// </summary>
|
||||
/// <remarks>Used by the login server when a create account request is made</remarks>
|
||||
/// <param name="Nick">The Account Name</param>
|
||||
/// <param name="Pass">The Account Password</param>
|
||||
/// <param name="Email">The Account Email Address</param>
|
||||
/// <param name="Country">The Country Code for this Account</param>
|
||||
/// <returns>A bool indicating whether the account was created sucessfully</returns>
|
||||
public bool CreateUser(string Nick, string Pass, string Email, string Country)
|
||||
{
|
||||
int Pid = 0;
|
||||
|
||||
// Attempt to connect to stats database, and get a PID from there
|
||||
/*try
|
||||
{
|
||||
// try see if the player ID exists in the stats database
|
||||
using (StatsDatabase Db = new StatsDatabase())
|
||||
{
|
||||
// NOTE: online account names in the stats DB start with a single space!
|
||||
var Row = Db.Query("SELECT id FROM player WHERE upper(name) = upper(@P0)", " " + Nick);
|
||||
Pid = (Row.Count == 0) ? GenerateAccountId() : Int32.Parse(Row[0]["id"].ToString());
|
||||
}
|
||||
}
|
||||
catch
|
||||
{*/
|
||||
Pid = GenerateAccountId();
|
||||
//}
|
||||
|
||||
// Create the user in the database
|
||||
int Rows = base.Execute("INSERT INTO accounts(id, name, password, email, country) VALUES(@P0, @P1, @P2, @P3, @P4)",
|
||||
Pid, Nick, Pass, Email, Country
|
||||
);
|
||||
|
||||
return (Rows != 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a new Account Id
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private int GenerateAccountId()
|
||||
{
|
||||
var Row = base.Query("SELECT COALESCE(MAX(id), 500000000) AS max FROM accounts");
|
||||
int max = Int32.Parse(Row[0]["max"].ToString()) + 1;
|
||||
return (max < 500000000) ? 500000000 : max;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Gamespy Account
|
||||
/// </summary>
|
||||
/// <remarks>Only used in the Gamespy Account Creation Form</remarks>
|
||||
/// <param name="Pid">The Profile Id to assign this account</param>
|
||||
/// <param name="Nick">The Account Name</param>
|
||||
/// <param name="Pass">The Account Password</param>
|
||||
/// <param name="Email">The Account Email Address</param>
|
||||
/// <param name="Country">The Country Code for this Account</param>
|
||||
/// <returns>A bool indicating whether the account was created sucessfully</returns>
|
||||
public bool CreateUser(int Pid, string Nick, string Pass, string Email, string Country)
|
||||
{
|
||||
// Make sure the user doesnt exist!
|
||||
if (UserExists(Pid))
|
||||
throw new Exception("Account ID is already taken!");
|
||||
else if(UserExists(Nick))
|
||||
throw new Exception("Account username is already taken!");
|
||||
|
||||
// Create the user in the database
|
||||
int Rows = base.Execute("INSERT INTO accounts(id, name, password, email, country) VALUES(@P0, @P1, @P2, @P3, @P4)",
|
||||
Pid, Nick, Pass, Email, Country
|
||||
);
|
||||
|
||||
return (Rows != 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates an Accounts Country Code
|
||||
/// </summary>
|
||||
/// <param name="Nick"></param>
|
||||
/// <param name="Country"></param>
|
||||
public void UpdateUser(string Nick, string Country)
|
||||
{
|
||||
base.Execute("UPDATE accounts SET country=@P0 WHERE name=@P1", Nick, Country);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates an Account's information by ID
|
||||
/// </summary>
|
||||
/// <param name="Id">The Current Account ID</param>
|
||||
/// <param name="NewPid">New Account ID</param>
|
||||
/// <param name="NewNick">New Account Name</param>
|
||||
/// <param name="NewPassword">New Account Password</param>
|
||||
/// <param name="NewEmail">New Account Email Address</param>
|
||||
public void UpdateUser(int Id, int NewPid, string NewNick, string NewPassword, string NewEmail)
|
||||
{
|
||||
base.Execute("UPDATE accounts SET id=@P0, name=@P1, password=@P2, email=@P3 WHERE id=@P4",
|
||||
NewPid, NewNick, NewPassword, NewEmail, Id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a Gamespy Account
|
||||
/// </summary>
|
||||
/// <param name="Nick"></param>
|
||||
/// <returns></returns>
|
||||
public int DeleteUser(string Nick)
|
||||
{
|
||||
return base.Execute("DELETE FROM accounts WHERE name=@P0", Nick);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a Gamespy Account
|
||||
/// </summary>
|
||||
/// <param name="Nick"></param>
|
||||
/// <returns></returns>
|
||||
public int DeleteUser(int Pid)
|
||||
{
|
||||
return base.Execute("DELETE FROM accounts WHERE id=@P0", Pid);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches a Gamespy Account id from an account name
|
||||
/// </summary>
|
||||
/// <param name="Nick"></param>
|
||||
/// <returns></returns>
|
||||
public int GetPID(string Nick)
|
||||
{
|
||||
var Rows = base.Query("SELECT id FROM accounts WHERE name=@P0", Nick);
|
||||
return (Rows.Count == 0) ? 0 : Int32.Parse(Rows[0]["id"].ToString());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the Account (Player) Id for an account by Name
|
||||
/// </summary>
|
||||
/// <param name="Nick">The account Nick we are setting the new Pid for</param>
|
||||
/// <param name="Pid">The new Pid</param>
|
||||
/// <returns></returns>
|
||||
public int SetPID(string Nick, int Pid)
|
||||
{
|
||||
// If no user exists, return code -1
|
||||
if (!UserExists(Nick))
|
||||
return -1;
|
||||
|
||||
// If the Pid already exists, return -2
|
||||
if (UserExists(Pid))
|
||||
return -2;
|
||||
|
||||
// If PID is false, the PID is not taken
|
||||
int Success = base.Execute("UPDATE accounts SET id=@P0 WHERE name=@P1", Pid, Nick);
|
||||
return (Success > 0) ? 1 : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of accounts in the database
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetNumAccounts()
|
||||
{
|
||||
var Row = base.Query("SELECT COUNT(id) AS count FROM accounts");
|
||||
return Int32.Parse(Row[0]["count"].ToString());
|
||||
}
|
||||
}
|
||||
}
|
98
TimeKeeper/Database/SQL/MySQL.Install.sql
Normal file
98
TimeKeeper/Database/SQL/MySQL.Install.sql
Normal file
@ -0,0 +1,98 @@
|
||||
-- phpMyAdmin SQL Dump
|
||||
-- version 3.4.11.1deb2+deb7u2
|
||||
-- http://www.phpmyadmin.net
|
||||
--
|
||||
-- Host: localhost
|
||||
-- Erstellungszeit: 26. Jan 2016 um 10:01
|
||||
-- Server Version: 5.6.25
|
||||
-- PHP-Version: 5.4.45-1~dotdeb+7.1
|
||||
|
||||
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
|
||||
SET time_zone = "+00:00";
|
||||
|
||||
--
|
||||
-- Datenbank: `worktime`
|
||||
--
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `location`
|
||||
--
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `location` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(255) NOT NULL,
|
||||
`tag` varchar(10) NOT NULL,
|
||||
`weeklength` float NOT NULL,
|
||||
`days` text NOT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `month`
|
||||
--
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `month` (
|
||||
`year` int(11) NOT NULL,
|
||||
`month` int(11) NOT NULL,
|
||||
`duration` double NOT NULL,
|
||||
`location` int(11) DEFAULT NULL,
|
||||
UNIQUE KEY `year` (`year`,`month`,`location`),
|
||||
KEY `location` (`location`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `work`
|
||||
--
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `work` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`date` date NOT NULL,
|
||||
`start` time NOT NULL,
|
||||
`stop` time NOT NULL,
|
||||
`pause` int(11) DEFAULT NULL,
|
||||
`type` enum('ok','free','we','ill','vac','feiertag') NOT NULL,
|
||||
`notice` varchar(200) DEFAULT NULL,
|
||||
`location` int(11) DEFAULT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `location` (`location`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
-- --------------------------------------------------------
|
||||
|
||||
--
|
||||
-- Tabellenstruktur für Tabelle `_version`
|
||||
--
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `_version` (
|
||||
`dbver` int(4) NOT NULL,
|
||||
`dbdate` datetime NOT NULL,
|
||||
PRIMARY KEY (`dbver`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
--
|
||||
-- Constraints der exportierten Tabellen
|
||||
--
|
||||
|
||||
--
|
||||
-- Constraints der Tabelle `month`
|
||||
--
|
||||
ALTER TABLE `month`
|
||||
ADD CONSTRAINT `month_ibfk_1` FOREIGN KEY (`location`) REFERENCES `location` (`id`) ON DELETE SET NULL ON UPDATE SET NULL;
|
||||
|
||||
--
|
||||
-- Constraints der Tabelle `work`
|
||||
--
|
||||
ALTER TABLE `work`
|
||||
ADD CONSTRAINT `work_ibfk_1` FOREIGN KEY (`location`) REFERENCES `location` (`id`) ON DELETE SET NULL ON UPDATE SET NULL;
|
||||
|
||||
--
|
||||
-- Daten
|
||||
--
|
||||
|
||||
INSERT INTO `_version` (`dbver` ,`dbdate`) VALUES ('1', CURRENT_TIMESTAMP);
|
94
TimeKeeper/Database/TDatabase.cs
Normal file
94
TimeKeeper/Database/TDatabase.cs
Normal file
@ -0,0 +1,94 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TimeKeeper.Models.Types;
|
||||
|
||||
namespace TimeKeeper.Database {
|
||||
public class TDatabase : DatabaseDriver, IDisposable {
|
||||
private static TDatabase instance;
|
||||
|
||||
private TDatabase() : base(StaticConfig.DbConfig.Engine, StaticConfig.DbConfig.Host, StaticConfig.DbConfig.Port, StaticConfig.DbConfig.Db, StaticConfig.DbConfig.User, StaticConfig.DbConfig.Pass, new DatabaseEngine[] { DatabaseEngine.Mysql}) {
|
||||
// Try and Reconnect
|
||||
try {
|
||||
Connect();
|
||||
|
||||
// Try to get the database version
|
||||
try {
|
||||
if(base.Query("SELECT dbver FROM _version LIMIT 1").Count == 0)
|
||||
throw new Exception();
|
||||
} catch {
|
||||
base.Execute(Utils.GetResourceAsString("TimeKeeper.Database.SQL.MySQL.Install.sql"));
|
||||
}
|
||||
} catch(Exception) {
|
||||
if(Connection != null)
|
||||
Connection.Dispose();
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static TDatabase getInstance() {
|
||||
if(instance == null) {
|
||||
instance = new TDatabase();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
~TDatabase() {
|
||||
if(!IsDisposed)
|
||||
base.Dispose();
|
||||
}
|
||||
|
||||
internal void addWorking(WorkMessage m) {
|
||||
List<Dictionary<String, Object>> dayItems = this.getWorkOnDay(m.time);
|
||||
if(dayItems.Count == 0 && m.working == 1) {
|
||||
this.insertStartWork(m.time, m.jobID);
|
||||
} else if(dayItems.Count > 0) {
|
||||
int started = -1;
|
||||
int job = -1;
|
||||
foreach(Dictionary<String, Object> dayItem in dayItems) {
|
||||
if((TimeSpan)dayItem["stop"] == TimeSpan.Zero) {
|
||||
started = (int)dayItem["id"];
|
||||
job = (int)dayItem["location"];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(started != -1 && m.working == 0 && m.jobID == job) {
|
||||
this.updateStopWorking(started, m.time);
|
||||
} else if(started == -1 && m.working == 1) {
|
||||
this.insertStartWork(m.time, m.jobID);
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
} else {
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateStopWorking(Int32 started, DateTime time) {
|
||||
base.Execute("UPDATE `work` SET stop=@P0 WHERE id=@P1",
|
||||
time.ToString("HH:mm:ss"), started.ToString());
|
||||
}
|
||||
|
||||
internal bool insertStartWork(DateTime time, Int32 jobID) {
|
||||
int Rows = base.Execute("INSERT INTO `work` (`date`, `start`, `type`, `location`) VALUES (@P0, @P1, @P2, @P3)",
|
||||
time.ToString("yyyy-MM-dd"), time.ToString("HH:mm:ss"), "ok", jobID.ToString()
|
||||
);
|
||||
return (Rows != 0);
|
||||
}
|
||||
|
||||
internal List<Dictionary<String, Object>> getWorkOnDay(DateTime time) {
|
||||
return base.Query("SELECT * FROM `work` WHERE date=@P0 ORDER BY `start` ASC", time.ToString("yyyy-MM-dd"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the number of accounts in the database
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public int GetNumAccounts() {
|
||||
var Row = base.Query("SELECT COUNT(id) AS count FROM accounts");
|
||||
return Int32.Parse(Row[0]["count"].ToString());
|
||||
}
|
||||
}
|
||||
}
|
11
TimeKeeper/Exceptions/DbConnectException.cs
Normal file
11
TimeKeeper/Exceptions/DbConnectException.cs
Normal file
@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TimeKeeper.Exceptions {
|
||||
class DbConnectException : Exception {
|
||||
public DbConnectException(string Message, Exception Inner) : base(Message, Inner) { }
|
||||
}
|
||||
}
|
17
TimeKeeper/Misc/StaticConfig.cs
Normal file
17
TimeKeeper/Misc/StaticConfig.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using BlubbFish.Utils;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace TimeKeeper {
|
||||
class StaticConfig {
|
||||
public static class DbConfig {
|
||||
private static InIReader i = InIReader.getInstance("settings.ini");
|
||||
public static string Engine { get { return i.getValue("database", "engine"); } }
|
||||
public static string Host { get { return i.getValue("database", "host"); } }
|
||||
public static int Port { get { return int.Parse(i.getValue("database", "port")); } }
|
||||
public static string Db { get { return i.getValue("database", "db"); } }
|
||||
public static string User { get { return i.getValue("database", "user"); } }
|
||||
public static string Pass { get { return i.getValue("database", "pass"); } }
|
||||
}
|
||||
public static readonly string RootPath = Application.StartupPath;
|
||||
}
|
||||
}
|
21
TimeKeeper/Misc/Utils.cs
Normal file
21
TimeKeeper/Misc/Utils.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TimeKeeper {
|
||||
class Utils {
|
||||
public static string GetResourceAsString(string ResName) {
|
||||
string Res = "";
|
||||
using(Stream ResourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ResName)) {
|
||||
using(StreamReader Reader = new StreamReader(ResourceStream)) {
|
||||
Res = Reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
return Res;
|
||||
}
|
||||
}
|
||||
}
|
31
TimeKeeper/Models/MDatabase.cs
Normal file
31
TimeKeeper/Models/MDatabase.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using BlubbFish.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TimeKeeper.Database;
|
||||
using TimeKeeper.Models.Types;
|
||||
|
||||
namespace TimeKeeper.Models {
|
||||
class Database : OwnModel<Database> {
|
||||
private TDatabase database;
|
||||
|
||||
public Queue<WorkMessage> Messages { get; internal set; }
|
||||
|
||||
protected override void init() {
|
||||
this.database = TDatabase.getInstance();
|
||||
}
|
||||
|
||||
private Database() {
|
||||
this.init();
|
||||
}
|
||||
|
||||
internal void checkinMessages() {
|
||||
while(this.Messages.Count > 0) {
|
||||
WorkMessage m = this.Messages.Dequeue();
|
||||
this.database.addWorking(m);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -18,10 +18,12 @@ namespace TimeKeeper.Models {
|
||||
private Thread setTimeThread;
|
||||
private InIReader settingsfile;
|
||||
private FileLogger sLogger;
|
||||
private Stack<WorkMessages> MessagesValue = new Stack<WorkMessages>();
|
||||
private Queue<WorkMessage> MessagesValue = new Queue<WorkMessage>();
|
||||
private Database DatabaseModel;
|
||||
private Boolean initComplete = false;
|
||||
|
||||
private Tray() {
|
||||
this.sLogger = FileLogger.getInstance("serial.log", true);
|
||||
this.sLogger = FileLogger.getInstance("serial.log", false);
|
||||
this.init();
|
||||
}
|
||||
|
||||
@ -35,13 +37,15 @@ namespace TimeKeeper.Models {
|
||||
set { this.OffsetTimeValue = value; this.update(); }
|
||||
}
|
||||
|
||||
public Stack<WorkMessages> Messages {
|
||||
public Queue<WorkMessage> Messages {
|
||||
get { return this.MessagesValue; }
|
||||
}
|
||||
|
||||
public void MessagesPush(WorkMessages m) {
|
||||
this.MessagesValue.Push(m);
|
||||
public void MessagesPush(WorkMessage m) {
|
||||
this.MessagesValue.Enqueue(m);
|
||||
this.update();
|
||||
this.DatabaseModel.Messages = this.Messages;
|
||||
this.DatabaseModel.checkinMessages();
|
||||
}
|
||||
|
||||
override protected void init() {
|
||||
@ -52,6 +56,7 @@ namespace TimeKeeper.Models {
|
||||
this.serialConnectThread.Start();
|
||||
this.setTimeThread = new Thread(timeRunner);
|
||||
this.setTimeThread.Start();
|
||||
this.DatabaseModel = Database.Instance;
|
||||
}
|
||||
|
||||
internal void Dispose() {
|
||||
@ -63,7 +68,9 @@ namespace TimeKeeper.Models {
|
||||
}
|
||||
|
||||
private void timeRunner() {
|
||||
Thread.Sleep(1000 * 10);
|
||||
while(!this.initComplete) {
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
while(true) {
|
||||
DateTime n = DateTime.UtcNow;
|
||||
this.DataSendHandler("time=" + ((int)((n - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds)).ToString());
|
||||
@ -90,21 +97,23 @@ namespace TimeKeeper.Models {
|
||||
private void DataReceivedHandler(Object sender, SerialDataReceivedEventArgs e) {
|
||||
SerialPort sp = (SerialPort)sender;
|
||||
string s = sp.ReadLine().Trim();
|
||||
sLogger.setLine("<-: " + s);
|
||||
sLogger.setLine("<-: " + s, DateTime.Now);
|
||||
this.parseSerial(s);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.Synchronized)]
|
||||
private void DataSendHandler(String v) {
|
||||
sLogger.setLine("->: " + v);
|
||||
sLogger.setLine("->: " + v, DateTime.Now);
|
||||
this.serial.WriteLine(v);
|
||||
}
|
||||
|
||||
private void parseSerial(String s) {
|
||||
if(s == "requestKeep=1") {
|
||||
this.DataSendHandler("keep=1");
|
||||
} else if(s == "Init...." || s == "Init loading!" || s == "Init finished!") {
|
||||
} else if(s == "Init...." || s == "Init loading!") {
|
||||
//Ignore that Stuff
|
||||
} else if(s == "Init finished!") {
|
||||
this.initComplete = true;
|
||||
} else if((s.Length > 4 && s.Substring(0, 4) == "d->:") || (s.Length > 4 && s.Substring(0, 4) == "i<-:")) {
|
||||
//Ignore that Stuff also....
|
||||
} else if(s.Length > 2 && s.Substring(0, 2) == "t=") {
|
||||
@ -134,7 +143,7 @@ namespace TimeKeeper.Models {
|
||||
working = int.Parse(t[1]);
|
||||
}
|
||||
}
|
||||
this.MessagesPush(new WorkMessages(userID, time, jobID, working));
|
||||
this.MessagesPush(new WorkMessage(userID, time, jobID, working));
|
||||
}
|
||||
|
||||
private void setOffset(String v) {
|
||||
|
@ -5,13 +5,13 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TimeKeeper.Models.Types {
|
||||
class WorkMessages {
|
||||
class WorkMessage {
|
||||
public Int32 jobID;
|
||||
public DateTime time;
|
||||
public Int64 userID;
|
||||
public Int32 working;
|
||||
|
||||
public WorkMessages(Int64 userID, DateTime time, Int32 jobID, Int32 working) {
|
||||
public WorkMessage(Int64 userID, DateTime time, Int32 jobID, Int32 working) {
|
||||
this.userID = userID;
|
||||
this.time = time;
|
||||
this.jobID = jobID;
|
@ -13,6 +13,9 @@ namespace TimeKeeper {
|
||||
t.execute();
|
||||
Application.Run();
|
||||
} catch(Exception e) {
|
||||
if(e.InnerException != null) {
|
||||
e = e.InnerException;
|
||||
}
|
||||
MessageBox.Show("Fehler: " + e.Message + "\nStack: " + e.StackTrace, "Exception: " + e.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
@ -13,7 +13,7 @@
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
@ -23,7 +23,7 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<PlatformTarget>AnyCPU</PlatformTarget>
|
||||
<PlatformTarget>x64</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
@ -38,8 +38,15 @@
|
||||
<ApplicationIcon>Resources\Icons\main.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MySql.Data">
|
||||
<HintPath>..\lib\MySql.Data.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.SQLite, Version=1.0.84.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=x86">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\lib\System.Data.SQLite.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
@ -51,9 +58,15 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Controller\CTray.cs" />
|
||||
<Compile Include="Controller\CWindow.cs" />
|
||||
<Compile Include="Database\DatabaseDriver.cs" />
|
||||
<Compile Include="Database\DatabaseEngine.cs" />
|
||||
<Compile Include="Database\TDatabase.cs" />
|
||||
<Compile Include="Exceptions\DbConnectException.cs" />
|
||||
<Compile Include="Misc\Utils.cs" />
|
||||
<Compile Include="Models\MDatabase.cs" />
|
||||
<Compile Include="Models\MTray.cs" />
|
||||
<Compile Include="Models\MWindow.cs" />
|
||||
<Compile Include="Models\Types\WorkMessages.cs" />
|
||||
<Compile Include="Models\Types\WorkMessage.cs" />
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
@ -61,6 +74,7 @@
|
||||
<DesignTime>True</DesignTime>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Misc\StaticConfig.cs" />
|
||||
<Compile Include="View\ViewWindowForm.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
@ -89,10 +103,12 @@
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Include="Database\SQL\MySQL.Install.sql" />
|
||||
<Content Include="Resources\Icons\door_open.png" />
|
||||
<Content Include="Resources\Icons\main_con.ico" />
|
||||
<Content Include="Resources\Icons\main_dis.ico" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
@ -7,12 +7,15 @@ using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using TimeKeeper.Properties;
|
||||
using System.Drawing;
|
||||
using TimeKeeper.Models;
|
||||
|
||||
namespace TimeKeeper.View {
|
||||
class Tray : OwnView {
|
||||
public Models.Tray Model { get; private set; }
|
||||
|
||||
private NotifyIcon trayi;
|
||||
private Icon[] trayIcons = { new Icon(Resources.TrayIconConnected, 40, 40), new Icon(Resources.TrayIconDisconnected, 40, 40) };
|
||||
private Boolean offsetTimeFail;
|
||||
|
||||
public Tray() {
|
||||
this.init();
|
||||
@ -23,6 +26,19 @@ namespace TimeKeeper.View {
|
||||
public override void update() {
|
||||
this.trayi.ContextMenuStrip = this.genMenu();
|
||||
this.trayi.Icon = (this.Model.isConnected) ? this.trayIcons[0] : this.trayIcons[1];
|
||||
if(this.Model.Messages.Count > 0) {
|
||||
Models.Types.WorkMessage m = this.Model.Messages.Peek();
|
||||
string text = (m.working==1)?"Angefangen zu arbeiten ":"Aufgehört zu arbeiten ";
|
||||
text += "am " + m.time.ToShortDateString() + " " + m.time.ToLongTimeString() + " ";
|
||||
text += "in Job " + m.jobID.ToString();
|
||||
this.showBallonTooltip(text, ToolTipIcon.Info);
|
||||
} else if(Math.Abs(this.Model.OffsetTime.TotalSeconds) >= 2) {
|
||||
this.showBallonTooltip("Achtung Abweichung von der Zeit: " + this.Model.OffsetTime.TotalSeconds, ToolTipIcon.Warning);
|
||||
this.offsetTimeFail = true;
|
||||
} else if(Math.Abs(this.Model.OffsetTime.TotalSeconds) < 2 && this.offsetTimeFail) {
|
||||
this.showBallonTooltip("Zeitabweichung ok: " + this.Model.OffsetTime.TotalSeconds, ToolTipIcon.Info);
|
||||
this.offsetTimeFail = false;
|
||||
}
|
||||
}
|
||||
|
||||
override protected void init() {
|
||||
|
@ -1,2 +1,10 @@
|
||||
[general]
|
||||
comport=COM4
|
||||
comport=COM4
|
||||
|
||||
[database]
|
||||
engine=Mysql
|
||||
host=localhost
|
||||
port=3306
|
||||
db=worktime
|
||||
user=root
|
||||
pass=mafia
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user