Ejemplo n.º 1
0
 private async void txtStatusWeb_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
 {
     if (UtilityWebServer.mInstance.WebServerRunnning)
     {
         try
         {
             await UtilityWebServer.mInstance.StopWebServer();
         }
         catch (Exception ex)
         {
             SanitaLog.e(TAG, ex);
         }
     }
     else
     {
         try
         {
             int num = await UtilityWebServer.mInstance.StartWebServer();
         }
         catch (Exception ex)
         {
             SanitaLog.e(TAG, ex);
         }
     }
 }
Ejemplo n.º 2
0
        public bool CheckConnection(String host, String user, String password, String database, String port)
        {
            try
            {
                GetDatabaseDAO().SetConnectionConfig(host, user, password, database, port);

                if (GetDatabaseType() == DATABASE_TYPE.POSTGRESQL)
                {
                    StringBuilder sql = new StringBuilder();
                    sql.Append(" SELECT NOW() AS TIME_NOW ");
                    DataRow row = GetDatabaseDAO().DoGetDataRow(sql.ToString());
                    if (row != null)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else if (GetDatabaseType() == DATABASE_TYPE.SQLITE)
                {
                    return(File.Exists("Database\\" + database + ".db"));
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                SanitaLog.e(TAG, "Check connection error !", ex);
                return(false);
            }
        }
Ejemplo n.º 3
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            MyVar.mMediboxSetting.DB_SERVER       = local_txtServer.Text;
            MyVar.mMediboxSetting.DB_NAME         = local_txtDatabase.Text;
            MyVar.mMediboxSetting.DB_USERID       = local_txtUser.Text;
            MyVar.mMediboxSetting.DB_USERPASSWORD = local_txtPassword.Text;

            //Save
            try
            {
                MediboxSetting mMediboxSetting = ObjectCopier.Clone(MyVar.mMediboxSetting);

                if (!mMediboxSetting.DB_ENCRIPT)
                {
                    mMediboxSetting.DB_ENCRIPT      = true;
                    mMediboxSetting.DB_SERVER       = CryptorEngine.Encrypt(mMediboxSetting.DB_SERVER, true);
                    mMediboxSetting.DB_USERID       = CryptorEngine.Encrypt(mMediboxSetting.DB_USERID, true);
                    mMediboxSetting.DB_PORT         = CryptorEngine.Encrypt(mMediboxSetting.DB_PORT, true);
                    mMediboxSetting.DB_USERPASSWORD = CryptorEngine.Encrypt(mMediboxSetting.DB_USERPASSWORD, true);
                    mMediboxSetting.DB_NAME         = CryptorEngine.Encrypt(mMediboxSetting.DB_NAME, true);
                }

                File.WriteAllText(MyVar.SettingsFileName, JsonConvert.SerializeObject(mMediboxSetting));
            }
            catch (Exception ex)
            {
                SanitaLog.e(TAG, ex);
            }

            SoftUpdatePresenter.SetConnectionConfig(local_txtServer.Text, local_txtUser.Text, local_txtPassword.Text, local_txtDatabase.Text, MyVar.DEFAULT_PORT);

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }
Ejemplo n.º 4
0
        public Byte[] GetDataRaw(String url)
        {
            try
            {
                return(mWebClient.DownloadData(url));
            }
            catch (Exception ex)
            {
                SanitaLog.e(TAG, ex);
            }

            return(null);
        }
Ejemplo n.º 5
0
        public void StopTask()
        {
            SanitaLogEx.e(TAG, "Stop task...");

            try
            {
                if (m_BackgroundWorker.CurrentThread != null)
                {
                    m_BackgroundWorker.CurrentThread.Suspend();
                    m_BackgroundWorker.StopImmediately();
                }
            }
            catch (Exception ex)
            {
                SanitaLog.e(TAG, ex);
            }
        }
Ejemplo n.º 6
0
        public String GetData(String url)
        {
            try
            {
                var buf = mWebClient.DownloadData(url);
                if (buf != null)
                {
                    return(System.Text.Encoding.UTF8.GetString(buf));
                }
            }
            catch (Exception ex)
            {
                SanitaLog.e(TAG, ex);
            }

            return("");
        }
Ejemplo n.º 7
0
        //Public
        public Task <int> StartWebServer()
        {
            WebServerPort = 7001;
            ErrorMessage  = "";

            return(Task <int> .Factory.StartNew((Func <int>)(() =>
            {
                try
                {
                    if (mWebHost != null && mWebHost.Running)
                    {
                        if (mWebHost.Port == WebServerPort)
                        {
                            return 1;
                        }
                        this.StopWebServer().Wait();
                    }
                    if (this.mWebHost == null)
                    {
                        mWebHost = new WebHost();
                        mWebHost.Port = this.WebServerPort;
                        mWebHost.RootDirectoryPath = this.WebServerDirectoryPath;
                        mWebHost.Start();
                    }
                    this.WebServerRunnning = true;
                    return 0;
                }
                catch (Exception ex)
                {
                    if (mWebHost != null)
                    {
                        mWebHost.Dispose();
                        mWebHost = (WebHost)null;
                    }

                    SanitaLog.e(TAG, "StartWebServer error !", ex);

                    ErrorMessage = ex.ToString();
                    WebServerRunnning = false;
                    return -1;
                }
            })));
        }
Ejemplo n.º 8
0
        private void bwAsync_Worker(object sender, DoWorkEventArgs e)
        {
            SanitaLogEx.e(TAG, "Start get timer...");

            try
            {
                using (IDbConnection connection = SoftUpdatePresenter.GetConnection())
                {
                    //Open connection
                    connection.Open();

                    //Get time
                    DateTime dt = SoftUpdatePresenter.GetCurrentTime(null, null);

                    //Close connection
                    connection.Close();
                }
            }
            catch (Exception ex)
            {
                SanitaLog.e(TAG, ex);
            }
        }
Ejemplo n.º 9
0
        public T GetData <T>(String url)
        {
            try
            {
                var buf = mWebClient.DownloadData(url);
                if (buf != null)
                {
                    string download = System.Text.Encoding.ASCII.GetString(buf);
                    SanitaLog.d(TAG, download);

                    return(JsonConvert.DeserializeObject <T>(download));
                }
                else
                {
                    SanitaLog.d(TAG, "buf=null");
                }
            }
            catch (Exception ex)
            {
                SanitaLog.e(TAG, ex);
            }

            return(default(T));
        }
Ejemplo n.º 10
0
        public override bool SynchDatabase(IDbConnection connection, IDbTransaction trans, ExBackgroundWorker worker, int number_hosobenhan, bool IsCapNhatTrigger)
        {
            synch_worker = worker;

            //Get database name
            String DatabaseName = myBaseDao.GetDatabaseName();
            String file_name    = "Database\\" + DatabaseName + ".db";

            SanitaLogEx.d(TAG, "[SynchDatabase] Database = [" + DatabaseName + "]");

            //If have no database -> create datbase
            if (!File.Exists("Database\\" + DatabaseName + ".db"))
            {
                SanitaLog.e(TAG, "[SynchDatabase] Database chưa tồn tại");
                CreateDatabase(DatabaseName);
            }

            //Get all base table
            IList <ClassTable> listTable = GetListTable(connection, trans, DatabaseName);

            //Synch database
            if (synch_worker != null)
            {
                synch_worker.ReportProgress(0, listFixTable.Count);
            }
            for (int i = 0; i < listFixTable.Count; i++)
            {
                if (synch_worker != null)
                {
                    synch_worker.ReportProgress(i + 1, "Nâng cấp table '" + listFixTable[i].Table + "...");
                }

                //Convert mysql to sqlite
                for (int j = 0; j < listFixTable[i].listColumn.Count; j++)
                {
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("int(10) unsigned NOT NULL auto_increment", "INTEGER");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("int(10) unsigned", "INTEGER");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("int(10)", "INTEGER");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("int(11)", "INTEGER");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("text CHARACTER SET utf8 COLLATE utf8_unicode_ci", "text");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("longblob", "BLOB");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("double", "REAL");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("Datetime DEFAULT '1-1-1'", "TEXT");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("datetime DEFAULT '1-1-1'", "TEXT");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("Datetime", "TEXT");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("datetime", "TEXT");
                    listFixTable[i].listColumn[j].ColumnDefine = listFixTable[i].listColumn[j].ColumnDefine.Replace("timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP", "TEXT");
                }

                //Tim xem co table ton tai chua
                var foundTable = listTable.FirstOrDefault(p => p.Table.Equals(listFixTable[i].Table, StringComparison.CurrentCultureIgnoreCase));
                if (foundTable == null)
                {
                    //Create table
                    SanitaLogEx.d(TAG, "[SynchDatabase] Create table '" + listFixTable[i].Table + "'");
                    if (CreateTable(connection, trans, DatabaseName, listFixTable[i]) <= -100)
                    {
                        SanitaLogEx.e(TAG, "[SynchDatabase] Create table lỗi");
                        if (synch_worker != null)
                        {
                            synch_worker.ReportProgress(-1, "Tạo table '" + listFixTable[i].Table + "' lỗi !");
                        }
                        return(false);
                    }
                }
                else
                {
                    //Alter table
                    SanitaLogEx.e(TAG, "[SynchDatabase] Alter table '" + listFixTable[i].Table + "'");
                    if (AlterTableTable(connection, trans, DatabaseName, listFixTable[i], foundTable) <= -100)
                    {
                        SanitaLogEx.e(TAG, "[SynchDatabase] Alter table lỗi");
                        if (synch_worker != null)
                        {
                            synch_worker.ReportProgress(-1, "Update table '" + listFixTable[i].Table + "' lỗi !");
                        }
                        return(false);
                    }
                }

                if (synch_worker != null)
                {
                    synch_worker.ReportProgress(i + 1, "Nâng cấp table '" + listFixTable[i].Table + "' OK !");
                }
            }

            return(true);
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            SanitaLog.SetLogName("Medibox");
            SanitaLogEx.SetLogName("Medibox");

            //Create temp
            if (!Directory.Exists(Path.Combine(Application.StartupPath, "Data")))
            {
                Directory.CreateDirectory(Path.Combine(Application.StartupPath, "Data"));
            }
            if (!Directory.Exists(Path.Combine(Application.StartupPath, "Log")))
            {
                Directory.CreateDirectory(Path.Combine(Application.StartupPath, "Log"));
            }

            //Get info
            object[] customAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes(false);
            foreach (object attribute in customAttributes)
            {
                if (attribute.GetType() == typeof(AssemblyDescriptionAttribute))
                {
                    MyVar.mAppDescription = ((AssemblyDescriptionAttribute)attribute).Description;
                    SanitaLog.d(TAG, "Description:" + MyVar.mAppDescription);
                }
                if (attribute.GetType() == typeof(AssemblyTitleAttribute))
                {
                    MyVar.mAppName = ((AssemblyTitleAttribute)attribute).Title;
                    SanitaLog.d(TAG, "Name:" + MyVar.mAppName);
                }
                if (attribute.GetType() == typeof(AssemblyFileVersionAttribute))
                {
                    MyVar.mAppVersion = ((AssemblyFileVersionAttribute)attribute).Version;
                    SanitaLog.d(TAG, "Version:" + MyVar.mAppVersion);
                }
            }

            //Init Toast
            ToastNotification.DefaultToastGlowColor = eToastGlowColor.Red;
            ToastNotification.DefaultToastPosition  = eToastPosition.MiddleCenter;
            ToastNotification.ToastFont             = new System.Drawing.Font("Tahoma", 10);

            //Load setting
            try
            {
                MyVar.mMediboxSetting = JsonConvert.DeserializeObject <MediboxSetting>(File.ReadAllText(MyVar.SettingsFileName));
                if (MyVar.mMediboxSetting.DB_ENCRIPT)
                {
                    MyVar.mMediboxSetting.DB_ENCRIPT      = false;
                    MyVar.mMediboxSetting.DB_SERVER       = CryptorEngine.Decrypt(MyVar.mMediboxSetting.DB_SERVER, true);
                    MyVar.mMediboxSetting.DB_USERID       = CryptorEngine.Decrypt(MyVar.mMediboxSetting.DB_USERID, true);
                    MyVar.mMediboxSetting.DB_PORT         = CryptorEngine.Decrypt(MyVar.mMediboxSetting.DB_PORT, true);
                    MyVar.mMediboxSetting.DB_USERPASSWORD = CryptorEngine.Decrypt(MyVar.mMediboxSetting.DB_USERPASSWORD, true);
                    MyVar.mMediboxSetting.DB_NAME         = CryptorEngine.Decrypt(MyVar.mMediboxSetting.DB_NAME, true);
                }
            }
            catch (Exception ex)
            {
                SanitaLog.e(TAG, ex);
            }

            //Start application
            {
                //Check database
                while (true)
                {
                    String Localserver   = MyVar.mMediboxSetting.DB_SERVER;
                    String Localdatabase = MyVar.mMediboxSetting.DB_NAME;
                    String Localuserid   = MyVar.mMediboxSetting.DB_USERID;
                    String Localpassword = MyVar.mMediboxSetting.DB_USERPASSWORD;

                    SystemInfo.DatabaseType = Sanita.Utility.Database.Utility.DatabaseUtility.DATABASE_TYPE.POSTGRESQL;
                    SoftUpdatePresenter.SetConnectionConfig(Localserver, Localuserid, Localpassword, Localdatabase, MyVar.DEFAULT_PORT);

                    //Init database
                    SoftUpdatePresenter.InitDatabase();

                    //Check database OK
                    if (!SoftUpdatePresenter.IsDatabaseOK())
                    {
                        using (FormConfigDatabase form = new FormConfigDatabase())
                        {
                            if (form.ShowDialog() == DialogResult.OK)
                            {
                                continue;
                            }
                            else
                            {
                                SanitaMessageBox.Show("Lỗi kết nối cơ sở dữ liệu !", "Thông Báo");
                                return;
                            }
                        }
                    }

                    break;
                }

                //Start main form
                FormLogin mFormLogin = new FormLogin();
                Application.Run(mFormLogin);
            }
        }
Ejemplo n.º 12
0
        public static void DoUpdateDatabaseSQL()
        {
            SanitaLog.d(TAG, "DoUpdateDatabaseSQL--START");

            try
            {
                IBaseDao baseDAO = MediboxDatabaseUtility.GetDatabaseDAO();

                //Get connection
                using (IDbConnection connection = baseDAO.GetConnection())
                {
                    //Open connection
                    connection.Open();

                    //Begin transtation
                    using (IDbTransaction trans = connection.BeginTransaction())
                    {
                        //Check sql
                        SoftUpdate mSoftUpdate = SoftUpdateDB.mInstance.GetSoftUpdate_Simple(connection, trans);
                        SanitaLog.d(TAG, "Database sql = " + mSoftUpdate.SoftUpdateSQL);
                        SanitaLog.d(TAG, "Software sql = " + MediboxDatabaseUtility.GetDatabaseVersion());

                        if (MediboxDatabaseUtility.GetDatabaseVersion().GetDouble() > mSoftUpdate.SoftUpdateSQL.GetDouble())
                        {
                            SanitaLog.d(TAG, "BEGIN UPDATE SQL");
                            int hosobenhan_partion = 0;

                            if (mLocalDatabase.SynchDatabase(connection, trans, null, hosobenhan_partion, false))
                            {
                                SanitaLog.d(TAG, "UPDATE SUCCESS !");
                                mSoftUpdate.SoftUpdateSQL = MediboxDatabaseUtility.GetDatabaseVersion();
                                if (mSoftUpdate.SoftUpdateID > 0)
                                {
                                    if (SoftUpdateDB.mInstance.UpdateSoftUpdate_SQL(connection, trans, mSoftUpdate) < 0)
                                    {
                                        SanitaLog.d(TAG, "UPDATE SQL VERSION TO DATABASE NG !");

                                        //Rollback
                                        trans.Rollback();
                                        connection.Close();
                                        return;
                                    }
                                }
                                else
                                {
                                    if (SoftUpdateDB.mInstance.InsertSoftUpdate(connection, trans, mSoftUpdate) < 0)
                                    {
                                        SanitaLog.d(TAG, "INSERT SQL VERSION TO DATABASE NG !");

                                        //Rollback
                                        trans.Rollback();
                                        connection.Close();
                                        return;
                                    }
                                }
                            }

                            SanitaLog.d(TAG, "END UPDATE SQL");
                        }

                        //-----------------------------------------------------------------------------
                        //Commit transtation
                        trans.Commit();

                        //Close connection
                        connection.Close();
                    }
                }

                SanitaLog.d(TAG, "DoUpdateDatabaseSQL--END");
            }
            catch (Exception ex)
            {
                SanitaLog.e(TAG, ex);
            }
        }