Пример #1
0
        public static void Start(WebHelper wh)
        {
            try
            {
                if (!DBHelper.ColumnExists("pos", "battery_level"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN battery_level DOUBLE NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN battery_level DOUBLE NULL");
                }

                if (!DBHelper.ColumnExists("drivestate", "outside_temp_avg"))
                {
                    Logfile.Log("ALTER TABLE drivestate ADD COLUMN outside_temp_avg DOUBLE NULL, ADD COLUMN speed_max INT NULL, ADD COLUMN power_max INT NULL, ADD COLUMN power_min INT NULL, ADD COLUMN power_avg DOUBLE NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE drivestate ADD COLUMN outside_temp_avg DOUBLE NULL, ADD COLUMN speed_max INT NULL, ADD COLUMN power_max INT NULL, ADD COLUMN power_min INT NULL, ADD COLUMN power_avg DOUBLE NULL");

                    DBHelper.UpdateAllDrivestateData();
                }

                if (!DBHelper.ColumnExists("charging", "charger_pilot_current"))
                {
                    Logfile.Log("ALTER TABLE charging ADD COLUMN charger_pilot_current INT NULL, ADD COLUMN charge_current_request INT NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE charging ADD COLUMN charger_pilot_current INT NULL, ADD COLUMN charge_current_request INT NULL");
                }

                if (!DBHelper.TableExists("car_version"))
                {
                    Logfile.Log("CREATE TABLE car_version (id int NOT NULL AUTO_INCREMENT, StartDate datetime NOT NULL, version varchar(50), PRIMARY KEY(id))");
                    DBHelper.ExecuteSQLQuery("CREATE TABLE car_version (id int NOT NULL AUTO_INCREMENT, StartDate datetime NOT NULL, version varchar(50), PRIMARY KEY(id))");
                }


                if (!DBHelper.ColumnExists("trip", "outside_temp_avg"))
                {
                    UpdateDBView(wh);
                }

                if (System.IO.File.Exists("cmd_updated.txt"))
                {
                    Logfile.Log("Update skipped!");
                    return;
                }

                System.IO.File.AppendAllText("cmd_updated.txt", DateTime.Now.ToLongTimeString());
                Logfile.Log("Start update");

                if (Tools.IsMono())
                {
                    chmod("cmd_updated.txt", 666);
                    chmod("/etc/teslalogger/nohup.out", 666);
                    chmod("MQTTClient.exe.config", 666);

                    if (!exec_mono("git", "--version", false).Contains("git version"))
                    {
                        exec_mono("apt-get", "-y install git");
                        exec_mono("git", "--version");
                    }

                    exec_mono("rm", "-rf /etc/teslalogger/git/*");

                    exec_mono("rm", "-rf /etc/teslalogger/git");
                    exec_mono("mkdir", "/etc/teslalogger/git");
                    exec_mono("git", "clone https://github.com/bassmaster187/TeslaLogger /etc/teslalogger/git/");

                    Tools.CopyFilesRecursively(new System.IO.DirectoryInfo("/etc/teslalogger/git/TeslaLogger/GrafanaPlugins"), new System.IO.DirectoryInfo("/var/lib/grafana/plugins"));
                    Tools.CopyFilesRecursively(new System.IO.DirectoryInfo("/etc/teslalogger/git/TeslaLogger/www"), new System.IO.DirectoryInfo("/var/www/html"));
                    Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/bin/geofence.csv", "/etc/teslalogger/geofence.csv");
                    Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/GrafanaConfig/sample.yaml", "/etc/grafana/provisioning/dashboards/sample.yaml");

                    if (!System.IO.Directory.Exists("/var/lib/grafana/dashboards"))
                    {
                        System.IO.Directory.CreateDirectory("/var/lib/grafana/dashboards");
                    }

                    Tools.CopyFilesRecursively(new System.IO.DirectoryInfo("/etc/teslalogger/git/TeslaLogger/bin"), new System.IO.DirectoryInfo("/etc/teslalogger"));

                    try
                    {
                        if (!File.Exists("/etc/teslalogger/MQTTClient.exe.config"))
                        {
                            Logfile.Log("Copy empty MQTTClient.exe.config file");
                            Tools.CopyFile("/etc/teslalogger/git/MQTTClient/App.config", "/etc/teslalogger/MQTTClient.exe.config");
                        }
                    }
                    catch (Exception ex)
                    {
                        Logfile.Log(ex.ToString());
                    }
                }

                Logfile.Log("End update");

                Logfile.Log("Rebooting");

                exec_mono("reboot", "");
            }
            catch (Exception ex)
            {
                Logfile.Log("Error in update: " + ex.ToString());
            }
        }
Пример #2
0
        public static void Start()
        {
            try
            {
                if (!DBHelper.ColumnExists("pos", "battery_level"))
                {
                    Tools.Log("ALTER TABLE pos ADD COLUMN battery_level DOUBLE NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN battery_level DOUBLE NULL");
                }

                if (System.IO.File.Exists("cmd_updated.txt"))
                {
                    Tools.Log("Update skipped!");
                    return;
                }

                System.IO.File.AppendAllText("cmd_updated.txt", DateTime.Now.ToLongTimeString());
                Tools.Log("Start update");

                if (Tools.IsMono())
                {
                    chmod("cmd_updated.txt", 666);
                    chmod("/etc/teslalogger/nohup.out", 666);
                    chmod("MQTTClient.exe.config", 666);

                    if (!exec_mono("git", "--version", false).Contains("git version"))
                    {
                        exec_mono("apt-get", "-y install git");
                        exec_mono("git", "--version");
                    }

                    exec_mono("rm", "-rf /etc/teslalogger/git/*");

                    exec_mono("rm", "-rf /etc/teslalogger/git");
                    exec_mono("mkdir", "/etc/teslalogger/git");
                    exec_mono("git", "clone https://github.com/bassmaster187/TeslaLogger /etc/teslalogger/git/");

                    Tools.CopyFilesRecursively(new System.IO.DirectoryInfo("/etc/teslalogger/git/TeslaLogger/www"), new System.IO.DirectoryInfo("/var/www/html"));
                    Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/bin/geofence.csv", "/etc/teslalogger/geofence.csv");
                    Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/GrafanaConfig/sample.yaml", "/etc/grafana/provisioning/dashboards/sample.yaml");

                    if (!System.IO.Directory.Exists("/var/lib/grafana/dashboards"))
                    {
                        System.IO.Directory.CreateDirectory("/var/lib/grafana/dashboards");
                    }

                    Tools.CopyFilesRecursively(new System.IO.DirectoryInfo("/etc/teslalogger/git/TeslaLogger/Grafana"), new System.IO.DirectoryInfo("/var/lib/grafana/dashboards"));

                    Tools.CopyFilesRecursively(new System.IO.DirectoryInfo("/etc/teslalogger/git/TeslaLogger/bin"), new System.IO.DirectoryInfo("/etc/teslalogger"));

                    try
                    {
                        if (!File.Exists("/etc/teslalogger/MQTTClient.exe.config"))
                        {
                            Tools.Log("Copy empty MQTTClient.exe.config file");
                            Tools.CopyFile("/etc/teslalogger/git/MQTTClient/App.config", "/etc/teslalogger/MQTTClient.exe.config");
                        }
                    }
                    catch (Exception ex)
                    {
                        Tools.Log(ex.ToString());
                    }
                }

                Tools.Log("End update");

                Tools.Log("Rebooting");

                exec_mono("reboot", "");
            }
            catch (Exception ex)
            {
                Tools.Log("Error in update: " + ex.ToString());
            }
        }
Пример #3
0
        public static void Start(WebHelper wh)
        {
            try
            {
                if (!DBHelper.ColumnExists("pos", "battery_level"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN battery_level DOUBLE NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN battery_level DOUBLE NULL");
                }

                if (!DBHelper.ColumnExists("drivestate", "outside_temp_avg"))
                {
                    Logfile.Log("ALTER TABLE drivestate ADD COLUMN outside_temp_avg DOUBLE NULL, ADD COLUMN speed_max INT NULL, ADD COLUMN power_max INT NULL, ADD COLUMN power_min INT NULL, ADD COLUMN power_avg DOUBLE NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE drivestate ADD COLUMN outside_temp_avg DOUBLE NULL, ADD COLUMN speed_max INT NULL, ADD COLUMN power_max INT NULL, ADD COLUMN power_min INT NULL, ADD COLUMN power_avg DOUBLE NULL");

                    DBHelper.UpdateAllDrivestateData();
                }

                if (!DBHelper.ColumnExists("charging", "charger_pilot_current"))
                {
                    Logfile.Log("ALTER TABLE charging ADD COLUMN charger_pilot_current INT NULL, ADD COLUMN charge_current_request INT NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE charging ADD COLUMN charger_pilot_current INT NULL, ADD COLUMN charge_current_request INT NULL");
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.TableExists("car_version"))
                {
                    Logfile.Log("CREATE TABLE car_version (id int NOT NULL AUTO_INCREMENT, StartDate datetime NOT NULL, version varchar(50), PRIMARY KEY(id))");
                    DBHelper.ExecuteSQLQuery("CREATE TABLE car_version (id int NOT NULL AUTO_INCREMENT, StartDate datetime NOT NULL, version varchar(50), PRIMARY KEY(id))");
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.TableExists("can"))
                {
                    Logfile.Log("CREATE TABLE `can` (`datum` datetime NOT NULL, `id` mediumint NOT NULL, `val` double DEFAULT NULL, PRIMARY KEY(`datum`,`id`) ) ENGINE = InnoDB DEFAULT CHARSET = latin1;");
                    DBHelper.ExecuteSQLQuery("CREATE TABLE `can` (`datum` datetime NOT NULL, `id` mediumint NOT NULL, `val` double DEFAULT NULL, PRIMARY KEY(`datum`,`id`) ) ENGINE = InnoDB DEFAULT CHARSET = latin1;");
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("pos", "inside_temp"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN inside_temp DOUBLE NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN inside_temp DOUBLE NULL", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("pos", "battery_heater"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN battery_heater TINYINT(1) NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN battery_heater TINYINT(1) NULL", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("pos", "is_preconditioning"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN is_preconditioning TINYINT(1) NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN is_preconditioning TINYINT(1) NULL", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("pos", "sentry_mode"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN sentry_mode TINYINT(1) NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN sentry_mode TINYINT(1) NULL", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "conn_charge_cable"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN conn_charge_cable varchar(50)");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN conn_charge_cable varchar(50)", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "fast_charger_brand"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN fast_charger_brand varchar(50)");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN fast_charger_brand varchar(50)", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "fast_charger_type"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN fast_charger_type varchar(50)");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN fast_charger_type varchar(50)", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "fast_charger_present"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN fast_charger_present TINYINT(1)");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN fast_charger_present TINYINT(1)", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("charging", "battery_heater"))
                {
                    Logfile.Log("ALTER TABLE charging ADD COLUMN battery_heater TINYINT(1) NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE charging ADD COLUMN battery_heater TINYINT(1) NULL", 600);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "max_charger_power"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN max_charger_power int NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN max_charger_power int NULL", 600);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("trip", "outside_temp_avg"))
                {
                    UpdateDBView(wh);
                }

                System.Threading.Timer t = new System.Threading.Timer(FileChecker, null, 10000, 5000);

                chmod("/var/www/html/admin/wallpapers", 777);

                UpdatePHPini();

                try
                {
                    // create empty weather.ini file
                    string filepath = System.IO.Path.Combine(FileManager.GetExecutingPath(), "weather.ini");
                    if (!File.Exists(filepath))
                    {
                        System.IO.File.WriteAllText(filepath, "city = \"Berlin, de\"\r\nappid = \"12345678901234567890123456789012\"");
                    }

                    UpdateTeslalogger.chmod(filepath, 666, false);
                }
                catch (Exception)
                { }


                if (System.IO.File.Exists("cmd_updated.txt"))
                {
                    Logfile.Log("Update skipped!");
                    return;
                }

                System.IO.File.AppendAllText("cmd_updated.txt", DateTime.Now.ToLongTimeString());
                Logfile.Log("Start update");

                if (Tools.IsMono())
                {
                    chmod("VERSION", 666);
                    chmod("settings.json", 666);
                    chmod("cmd_updated.txt", 666);
                    chmod("MQTTClient.exe.config", 666);

                    if (!exec_mono("git", "--version", false).Contains("git version"))
                    {
                        exec_mono("apt-get", "-y install git");
                        exec_mono("git", "--version");
                    }

                    exec_mono("rm", "-rf /etc/teslalogger/git/*");

                    exec_mono("rm", "-rf /etc/teslalogger/git");
                    exec_mono("mkdir", "/etc/teslalogger/git");
                    exec_mono("git", "clone https://github.com/bassmaster187/TeslaLogger /etc/teslalogger/git/");

                    Tools.CopyFilesRecursively(new System.IO.DirectoryInfo("/etc/teslalogger/git/TeslaLogger/GrafanaPlugins"), new System.IO.DirectoryInfo("/var/lib/grafana/plugins"));
                    Tools.CopyFilesRecursively(new System.IO.DirectoryInfo("/etc/teslalogger/git/TeslaLogger/www"), new System.IO.DirectoryInfo("/var/www/html"));
                    Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/bin/geofence.csv", "/etc/teslalogger/geofence.csv");
                    Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/GrafanaConfig/sample.yaml", "/etc/grafana/provisioning/dashboards/sample.yaml");

                    if (!System.IO.Directory.Exists("/var/lib/grafana/dashboards"))
                    {
                        System.IO.Directory.CreateDirectory("/var/lib/grafana/dashboards");
                    }

                    Tools.CopyFilesRecursively(new System.IO.DirectoryInfo("/etc/teslalogger/git/TeslaLogger/bin"), new System.IO.DirectoryInfo("/etc/teslalogger"));

                    try
                    {
                        if (!File.Exists("/etc/teslalogger/MQTTClient.exe.config"))
                        {
                            Logfile.Log("Copy empty MQTTClient.exe.config file");
                            Tools.CopyFile("/etc/teslalogger/git/MQTTClient/App.config", "/etc/teslalogger/MQTTClient.exe.config");
                        }
                    }
                    catch (Exception ex)
                    {
                        Logfile.Log(ex.ToString());
                    }
                }

                Logfile.Log("End update");

                Logfile.Log("Rebooting");

                exec_mono("reboot", "");
            }
            catch (Exception ex)
            {
                Logfile.Log("Error in update: " + ex.ToString());
            }
        }
Пример #4
0
        public static void Start(WebHelper wh)
        {
            try
            {
                shareDataOnStartup = Tools.IsShareData();

                if (!DBHelper.ColumnExists("pos", "battery_level"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN battery_level DOUBLE NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN battery_level DOUBLE NULL");
                }

                if (!DBHelper.ColumnExists("drivestate", "outside_temp_avg"))
                {
                    Logfile.Log("ALTER TABLE drivestate ADD COLUMN outside_temp_avg DOUBLE NULL, ADD COLUMN speed_max INT NULL, ADD COLUMN power_max INT NULL, ADD COLUMN power_min INT NULL, ADD COLUMN power_avg DOUBLE NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE drivestate ADD COLUMN outside_temp_avg DOUBLE NULL, ADD COLUMN speed_max INT NULL, ADD COLUMN power_max INT NULL, ADD COLUMN power_min INT NULL, ADD COLUMN power_avg DOUBLE NULL");

                    DBHelper.UpdateAllDrivestateData();
                }

                if (!DBHelper.ColumnExists("charging", "charger_pilot_current"))
                {
                    Logfile.Log("ALTER TABLE charging ADD COLUMN charger_pilot_current INT NULL, ADD COLUMN charge_current_request INT NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE charging ADD COLUMN charger_pilot_current INT NULL, ADD COLUMN charge_current_request INT NULL");
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.TableExists("car_version"))
                {
                    Logfile.Log("CREATE TABLE car_version (id int NOT NULL AUTO_INCREMENT, StartDate datetime NOT NULL, version varchar(50), PRIMARY KEY(id))");
                    DBHelper.ExecuteSQLQuery("CREATE TABLE car_version (id int NOT NULL AUTO_INCREMENT, StartDate datetime NOT NULL, version varchar(50), PRIMARY KEY(id))");
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.TableExists("can"))
                {
                    Logfile.Log("CREATE TABLE `can` (`datum` datetime NOT NULL, `id` mediumint NOT NULL, `val` double DEFAULT NULL, PRIMARY KEY(`datum`,`id`) ) ENGINE = InnoDB DEFAULT CHARSET = latin1;");
                    DBHelper.ExecuteSQLQuery("CREATE TABLE `can` (`datum` datetime NOT NULL, `id` mediumint NOT NULL, `val` double DEFAULT NULL, PRIMARY KEY(`datum`,`id`) ) ENGINE = InnoDB DEFAULT CHARSET = latin1;");
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("pos", "inside_temp"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN inside_temp DOUBLE NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN inside_temp DOUBLE NULL", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("pos", "battery_heater"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN battery_heater TINYINT(1) NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN battery_heater TINYINT(1) NULL", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("pos", "is_preconditioning"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN is_preconditioning TINYINT(1) NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN is_preconditioning TINYINT(1) NULL", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("pos", "sentry_mode"))
                {
                    Logfile.Log("ALTER TABLE pos ADD COLUMN sentry_mode TINYINT(1) NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE pos ADD COLUMN sentry_mode TINYINT(1) NULL", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "conn_charge_cable"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN conn_charge_cable varchar(50)");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN conn_charge_cable varchar(50)", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "fast_charger_brand"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN fast_charger_brand varchar(50)");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN fast_charger_brand varchar(50)", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "fast_charger_type"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN fast_charger_type varchar(50)");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN fast_charger_type varchar(50)", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "fast_charger_present"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN fast_charger_present TINYINT(1)");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN fast_charger_present TINYINT(1)", 300);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("charging", "battery_heater"))
                {
                    Logfile.Log("ALTER TABLE charging ADD COLUMN battery_heater TINYINT(1) NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE charging ADD COLUMN battery_heater TINYINT(1) NULL", 600);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("chargingstate", "max_charger_power"))
                {
                    Logfile.Log("ALTER TABLE chargingstate ADD COLUMN max_charger_power int NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE chargingstate ADD COLUMN max_charger_power int NULL", 600);
                    Logfile.Log("ALTER TABLE OK");
                }

                if (!DBHelper.ColumnExists("trip", "outside_temp_avg"))
                {
                    UpdateDBView(wh);
                }

                if (!DBHelper.TableExists("mothership"))
                {
                    Logfile.Log("CREATE TABLE mothership (id int NOT NULL AUTO_INCREMENT, ts datetime NOT NULL, commandid int NOT NULL, duration DOUBLE NULL, PRIMARY KEY(id))");
                    DBHelper.ExecuteSQLQuery("CREATE TABLE mothership (id int NOT NULL AUTO_INCREMENT, ts datetime NOT NULL, commandid int NOT NULL, duration DOUBLE NULL, PRIMARY KEY(id))");
                    Logfile.Log("CREATE TABLE OK");
                }
                if (!DBHelper.TableExists("mothershipcommands"))
                {
                    Logfile.Log("CREATE TABLE mothershipcommands (id int NOT NULL AUTO_INCREMENT, command varchar(50) NOT NULL, PRIMARY KEY(id))");
                    DBHelper.ExecuteSQLQuery("CREATE TABLE mothershipcommands (id int NOT NULL AUTO_INCREMENT, command varchar(50) NOT NULL, PRIMARY KEY(id))");
                    Logfile.Log("CREATE TABLE OK");
                }
                if (!DBHelper.ColumnExists("mothership", "httpcode"))
                {
                    Logfile.Log("ALTER TABLE mothership ADD COLUMN httpcode int NULL");
                    DBHelper.ExecuteSQLQuery("ALTER TABLE mothership ADD COLUMN httpcode int NULL", 600);
                    Logfile.Log("ALTER TABLE OK");
                }
                if (!DBHelper.TableExists("httpcodes"))
                {
                    Logfile.Log("CREATE TABLE httpcodes (id int NOT NULL, text varchar(50) NOT NULL, PRIMARY KEY(id))");
                    DBHelper.ExecuteSQLQuery("CREATE TABLE httpcodes (id int NOT NULL, text varchar(50) NOT NULL, PRIMARY KEY(id))");
                    Logfile.Log("CREATE TABLE OK");
                }

                if (!DBHelper.IndexExists("can_ix", "can"))
                {
                    Logfile.Log("alter table can add index can_ix (id,datum)");
                    DBHelper.ExecuteSQLQuery("alter table can add index can_ix (id,datum)", 600);
                    Logfile.Log("ALTER TABLE OK");
                }


                DBHelper.EnableMothership();

                CheckDBCharset();

                DBHelper.UpdateHTTPStatusCodes();

                timer = new System.Threading.Timer(FileChecker, wh, 10000, 5000);

                Chmod("/var/www/html/admin/wallpapers", 777);

                UpdatePHPini();

                try
                {
                    // create empty weather.ini file
                    string filepath = Path.Combine(FileManager.GetExecutingPath(), "weather.ini");
                    if (!File.Exists(filepath))
                    {
                        File.WriteAllText(filepath, "city = \"Berlin, de\"\r\nappid = \"12345678901234567890123456789012\"");
                    }

                    Chmod(filepath, 666, false);
                }
                catch (Exception)
                { }


                if (File.Exists("cmd_updated.txt"))
                {
                    Logfile.Log("Update skipped!");
                    return;
                }

                File.AppendAllText("cmd_updated.txt", DateTime.Now.ToLongTimeString());
                Logfile.Log("Start update");

                if (Tools.IsMono())
                {
                    Chmod("VERSION", 666);
                    Chmod("settings.json", 666);
                    Chmod("cmd_updated.txt", 666);
                    Chmod("MQTTClient.exe.config", 666);

                    if (!Exec_mono("git", "--version", false).Contains("git version"))
                    {
                        Exec_mono("apt-get", "-y install git");
                        Exec_mono("git", "--version");
                    }

                    Exec_mono("rm", "-rf /etc/teslalogger/git/*");

                    Exec_mono("rm", "-rf /etc/teslalogger/git");
                    Exec_mono("mkdir", "/etc/teslalogger/git");
                    Exec_mono("mozroots", "--import --sync --machine");
                    for (int x = 1; x < 10; x++)
                    {
                        Logfile.Log("git clone: try " + x);
                        Exec_mono("git", "clone --progress https://github.com/bassmaster187/TeslaLogger /etc/teslalogger/git/", true, true);

                        if (Directory.Exists("/etc/teslalogger/git/TeslaLogger/GrafanaPlugins"))
                        {
                            Logfile.Log("git clone success!");
                            break;
                        }
                        Logfile.Log("Git failed. Retry in 30 sec!");
                        System.Threading.Thread.Sleep(30000);
                    }

                    Tools.CopyFilesRecursively(new DirectoryInfo("/etc/teslalogger/git/TeslaLogger/GrafanaPlugins"), new DirectoryInfo("/var/lib/grafana/plugins"));
                    Tools.CopyFilesRecursively(new DirectoryInfo("/etc/teslalogger/git/TeslaLogger/www"), new DirectoryInfo("/var/www/html"));
                    Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/bin/geofence.csv", "/etc/teslalogger/geofence.csv");
                    Tools.CopyFile("/etc/teslalogger/git/TeslaLogger/GrafanaConfig/sample.yaml", "/etc/grafana/provisioning/dashboards/sample.yaml");

                    if (!Directory.Exists("/var/lib/grafana/dashboards"))
                    {
                        Directory.CreateDirectory("/var/lib/grafana/dashboards");
                    }

                    Tools.CopyFilesRecursively(new DirectoryInfo("/etc/teslalogger/git/TeslaLogger/bin"), new DirectoryInfo("/etc/teslalogger"));

                    try
                    {
                        if (!File.Exists("/etc/teslalogger/MQTTClient.exe.config"))
                        {
                            Logfile.Log("Copy empty MQTTClient.exe.config file");
                            Tools.CopyFile("/etc/teslalogger/git/MQTTClient/App.config", "/etc/teslalogger/MQTTClient.exe.config");
                        }
                    }
                    catch (Exception ex)
                    {
                        Logfile.Log(ex.ToString());
                    }
                }

                Logfile.Log("End update");

                Logfile.Log("Rebooting");

                Exec_mono("reboot", "");
            }
            catch (Exception ex)
            {
                Logfile.Log("Error in update: " + ex.ToString());
            }
        }