Exemple #1
0
        public override string GetVersion()
        {
            string j = null;

            try
            {
                if (mockup_shelly == null)
                {
                    string url = host + "/shelly";
                    j = client.DownloadString(url);
                }
                else
                {
                    j = mockup_shelly;
                }

                dynamic jsonResult = JsonConvert.DeserializeObject(j);
                string  key        = "fw";
                string  value      = jsonResult[key];

                return(value);
            }
            catch (Exception ex)
            {
                ex.ToExceptionless().FirstCarUserID().Submit();
                Logfile.ExceptionWriter(ex, j);
            }

            return("");
        }
Exemple #2
0
        private static void Main(string[] args)
        {
            try
            {
                InitDebugLogging();

                InitStage1();

                InitCheckDocker();

                InitStage2();

                InitConnectToDB();

                InitWebserver();

                InitOpenTopoDataService();

                InitStaticMapService();

                UpdateTeslalogger.StopComfortingMessagesThread();

                MQTTClient.StartMQTTClient();

                InitTLStats();

                UpdateDbInBackground();

                WebHelper.getTLSVersion();

                Logfile.Log("Init finished, now enter main loop");

                GetAllCars();

                InitNearbySuCService();
            }
            catch (Exception ex)
            {
                Logfile.Log(ex.Message);
                Logfile.ExceptionWriter(ex, "main loop");
                Logfile.Log("Teslalogger Stopped!");
                Tools.ExternalLog("Teslalogger Stopped! " + ex.ToString());
            }
            finally
            {
                if (!UpdateTeslalogger.DownloadUpdateAndInstallStarted)
                {
                    try
                    {
                        Logfile.Log("Startup doesn't sucessfully run DownloadUpdateAndInstall() - retry now!");
                        UpdateTeslalogger.DownloadUpdateAndInstall();
                    }
                    catch (Exception ex)
                    {
                        Logfile.Log(ex.Message);
                        Logfile.ExceptionWriter(ex, "Emergency DownloadUpdateAndInstall()");
                    }
                }
            }
        }
Exemple #3
0
        private void GetCurrentJson(HttpListenerRequest request, HttpListenerResponse response)
        {
            System.Diagnostics.Debug.WriteLine(request.Url.LocalPath);

            Match m = Regex.Match(request.Url.LocalPath, @"/currentjson/([0-9]+)");

            if (m.Success && m.Groups.Count == 2 && m.Groups[1].Captures.Count == 1)
            {
                int.TryParse(m.Groups[1].Captures[0].ToString(), out int CarID);
                try
                {
                    if (CurrentJSON.jsonStringHolder.TryGetValue(CarID, out string json))
                    {
                        WriteString(response, json);
                    }
                    else
                    {
                        response.StatusCode = (int)HttpStatusCode.NotFound;
                        WriteString(response, @"URL Not Found!");
                    }
                }
                catch (Exception ex)
                {
                    WriteString(response, ex.ToString());
                    Logfile.ExceptionWriter(ex, request.Url.LocalPath);
                }
            }
        }
Exemple #4
0
        public override string GetVersion()
        {
            string j = null;

            try
            {
                if (mockup_shelly == null)
                {
                    string url = host + "/shelly";
                    j = client.DownloadString(url);
                }
                else
                {
                    j = mockup_shelly;
                }

                dynamic jsonResult = new JavaScriptSerializer().DeserializeObject(j);
                string  key        = "fw";
                string  value      = jsonResult[key];

                return(value);
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, j);
            }

            return("");
        }
Exemple #5
0
        public GeocodeCache()
        {
            DataColumn lat = dt.Columns.Add("lat", typeof(double));
            DataColumn lng = dt.Columns.Add("lng", typeof(double));

            dt.Columns.Add("Value");

            dt.PrimaryKey = new DataColumn[] { lat, lng };

            try
            {
                if (System.IO.File.Exists(FileManager.GetFilePath(TLFilename.GeocodeCache)))
                {
#pragma warning disable CA3075 // Unsichere DTD-Verarbeitung in XML
                    _ = dt.ReadXml(FileManager.GetFilePath(TLFilename.GeocodeCache));
#pragma warning restore CA3075 // Unsichere DTD-Verarbeitung in XML
                    Logfile.Log("GeocodeCache Items: " + dt.Rows.Count);
                }
                else
                {
                    Logfile.Log(FileManager.GetFilePath(TLFilename.GeocodeCache) + " Not found!");
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, "");
            }
        }
Exemple #6
0
        public static bool IsShareData()
        {
            try
            {
                if (IsDocker())
                {
                    return(File.Exists("/tmp/sharedata.txt"));
                }

                string filepath = Path.Combine(FileManager.GetExecutingPath(), "sharedata.txt");
                if (File.Exists(filepath))
                {
                    return(true);
                }

                filepath = Path.Combine(FileManager.GetExecutingPath(), "sharedata.txt.txt");
                if (File.Exists(filepath))
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, "IsShareData");
            }

            return(false);
        }
Exemple #7
0
        public override double?GetUtilityMeterReading_kWh()
        {
            string j = null;

            try
            {
                j = GetCurrentData();

                if (string.IsNullOrEmpty(j))
                {
                    return(null);
                }

                dynamic jsonResult = JsonConvert.DeserializeObject(j);

                string value = jsonResult["evubezugWh"];

                double v = Double.Parse(value, Tools.ciEnUS);
                v = v / 1000;

                return(v);
            }
            catch (Exception ex)
            {
                ex.ToExceptionless().FirstCarUserID().Submit();
                Logfile.ExceptionWriter(ex, j);
            }

            return(null);
        }
Exemple #8
0
        public override double?GetVehicleMeterReading_kWh()
        {
            string j = null;

            try
            {
                j = GetCurrentDataLifetime();
                j = j.Replace("nan,", "null,");

                dynamic jsonResult = new JavaScriptSerializer().DeserializeObject(j);
                string  key        = "energy_wh";
                string  value      = jsonResult[key].ToString();

                double v = Double.Parse(value, Tools.ciEnUS);
                v = v / 1000.0;

                return(v);
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, j);
            }

            return(null);
        }
        public override double?GetVehicleMeterReading_kWh()
        {
            string j = null;

            try
            {
                j = GetCurrentData();

                dynamic jsonResult = JsonConvert.DeserializeObject(j);
                string  key        = "eto";
                string  value      = jsonResult[key];

                double v = Double.Parse(value, Tools.ciEnUS);
                v = v / (double)10.0;

                return(v);
            }
            catch (Exception ex)
            {
                ex.ToExceptionless().FirstCarUserID().Submit();
                Logfile.ExceptionWriter(ex, j);
            }

            return(null);
        }
Exemple #10
0
        private static void InitStage2()
        {
            KeepOnlineMinAfterUsage = Tools.GetSettingsInt("KeepOnlineMinAfterUsage", 1);
            SuspendAPIMinutes       = Tools.GetSettingsInt("SuspendAPIMinutes", 30);

            Logfile.Log("Current Culture: " + Thread.CurrentThread.CurrentCulture.ToString());
            Logfile.Log("Mono Runtime: " + Tools.GetMonoRuntimeVersion());
            Logfile.Log("Grafana Version: " + Tools.GetGrafanaVersion());
            Logfile.Log("OS Version: " + Tools.GetOsVersion());
            Logfile.Log("Update Settings: " + Tools.GetOnlineUpdateSettings().ToString());

            Logfile.Log("DBConnectionstring: " + DBHelper.GetDBConnectionstring(true));

            Logfile.Log("KeepOnlineMinAfterUsage: " + KeepOnlineMinAfterUsage);
            Logfile.Log("SuspendAPIMinutes: " + SuspendAPIMinutes);
            Logfile.Log("SleepPositions: " + ApplicationSettings.Default.SleepPosition);
            Logfile.Log("UseScanMyTesla: " + Tools.UseScanMyTesla());
            Logfile.Log("StreamingPos: " + Tools.StreamingPos());
            try
            {
                Logfile.Log($"Free disk space: {Tools.FreeDiskSpaceMB()}mb");
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, ex.ToString());
            }
        }
Exemple #11
0
        public GeocodeCache()
        {
            DataColumn lat = dt.Columns.Add("lat", typeof(double));
            DataColumn lng = dt.Columns.Add("lng", typeof(double));

            dt.Columns.Add("Value");

            dt.PrimaryKey = new DataColumn[] { lat, lng };

            try
            {
                if (System.IO.File.Exists(FileManager.GetFilePath(TLFilename.GeocodeCache)))
                {
                    dt.ReadXml(FileManager.GetFilePath(TLFilename.GeocodeCache));
                    Logfile.Log("GeocodeCache Items: " + dt.Rows.Count);
                }
                else
                {
                    Logfile.Log(FileManager.GetFilePath(TLFilename.GeocodeCache) + " Not found!");
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, "");
            }
        }
Exemple #12
0
        static Dictionary <string, string> GetLanguageDictionary(string language)
        {
            System.Collections.Generic.Dictionary <string, string> ht = new Dictionary <string, string>();

            string filename = Path.Combine(FileManager.GetExecutingPath(), "language-" + language + ".txt");
            string content  = null;

            if (System.IO.File.Exists(filename))
            {
                try
                {
                    String[] lines = File.ReadAllLines(filename);
                    foreach (string line in lines)
                    {
                        content = line;

                        if (line.Length == 0)
                        {
                            continue;
                        }
                        if (line.StartsWith("#"))
                        {
                            continue;
                        }
                        if (!line.Contains("="))
                        {
                            continue;
                        }

                        int    pos   = line.IndexOf("=");
                        string key   = line.Substring(0, pos).Trim();
                        string value = line.Substring(pos + 1);

                        // Logfile.Log("Key insert: " + key);

                        if (ht.ContainsKey(key))
                        {
                            Logfile.Log($"Error Key '{key}' already in Dictionary!!!");
                            continue;
                        }

                        if (value.Trim().Length > 0)
                        {
                            ht.Add(key, value);
                        }
                        else
                        {
                            ht.Add(key, key + " xxx");
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logfile.Log(ex.Message);
                    Logfile.ExceptionWriter(ex, content);
                }
            }

            return(ht);
        }
Exemple #13
0
        public override bool?IsCharging()
        {
            string j = null;

            try
            {
                j = GetCurrentData();

                dynamic jsonResult = JsonConvert.DeserializeObject(j);
                if (jsonResult == null)
                {
                    return(null);
                }

                string key   = "ladungaktivLP" + LP;
                string value = jsonResult[key];

                return(value == "1");
            }
            catch (Exception ex)
            {
                ex.ToExceptionless().FirstCarUserID().Submit();
                Logfile.ExceptionWriter(ex, j);
            }

            return(null);
        }
Exemple #14
0
        private static void ReadGeofenceFile(List <Address> list, string filename)
        {
            filename = filename.Replace(@"Debug\", "");

            if (System.IO.File.Exists(filename))
            {
                Logfile.Log("Read Geofence File: " + filename);
                string line;
                using (System.IO.StreamReader file = new System.IO.StreamReader(filename))
                {
                    while ((line = file.ReadLine()) != null)
                    {
                        try
                        {
                            if (String.IsNullOrEmpty(line))
                            {
                                continue;
                            }

                            int radius = 50;

                            var args = line.Split(',');

                            if (args.Length > 3)
                            {
                                int.TryParse(args[3], out radius);
                            }

                            list.Add(new Address(args[0].Trim(),
                                                 Double.Parse(args[1].Trim(), Tools.ciEnUS.NumberFormat),
                                                 Double.Parse(args[2].Trim(), Tools.ciEnUS.NumberFormat),
                                                 radius));

                            if (!filename.Contains("geofence.csv"))
                            {
                                Logfile.Log("Address inserted: " + args[0]);
                            }
                        }
                        catch (Exception ex)
                        {
                            Logfile.ExceptionWriter(ex, line);
                        }
                    }
                }
            }
            else
            {
                Logfile.Log("FileNotFound: " + filename);
            }
        }
Exemple #15
0
 public static int ExecuteSQLQuery(string sql)
 {
     try
     {
         using (MySqlConnection con = new MySqlConnection(DBConnectionstring))
         {
             con.Open();
             MySqlCommand cmd = new MySqlCommand(sql, con);
             return(cmd.ExecuteNonQuery());
         }
     }
     catch (Exception ex)
     {
         Logfile.ExceptionWriter(ex, sql);
         throw;
     }
 }
Exemple #16
0
        public static string GetGrafanaVersion()
        {
            try
            {
                string filename = "/usr/share/grafana/VERSION";

                if (File.Exists(filename))
                {
                    return(File.ReadAllText(filename));
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, "GetGrafanaVersion");
            }

            return("?");
        }
Exemple #17
0
 internal static void SetCarVersion(string car_version)
 {
     try
     {
         using (MySqlConnection con = new MySqlConnection(DBConnectionstring))
         {
             con.Open();
             MySqlCommand cmd = new MySqlCommand("insert car_version (StartDate, version) values (@StartDate, @version)", con);
             cmd.Parameters.AddWithValue("@StartDate", DateTime.Now);
             cmd.Parameters.AddWithValue("@version", car_version);
             cmd.ExecuteNonQuery();
         }
     }
     catch (Exception ex)
     {
         Logfile.ExceptionWriter(ex, car_version);
     }
 }
Exemple #18
0
        public static bool IsDocker()
        {
            try
            {
                string filename = "/tmp/teslalogger-DOCKER";

                if (File.Exists(filename))
                {
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, "IsDocker");
            }

            return(false);
        }
Exemple #19
0
        public static void CheckForInterruptedCharging(bool logging)
        {
            try
            {
                using (MySqlConnection con = new MySqlConnection(DBConnectionstring))
                {
                    con.Open();
                    MySqlCommand cmd = new MySqlCommand(@"SELECT chargingstate.id as chargingstate_id , StartDate, EndDate, charging.charge_energy_added as start_charge_energy_added,
                      charging_End.charge_energy_added,
                      charging.ideal_battery_range_km AS SOC,
                      charging_End.ideal_battery_range_km AS EndSOC,
                      charging_End.battery_level as End_battery_level,
                      pos.odometer
                        FROM charging inner JOIN chargingstate ON charging.id = chargingstate.StartChargingID INNER JOIN
                         pos ON chargingstate.pos = pos.id
                         LEFT OUTER JOIN
                         charging AS charging_End ON chargingstate.EndChargingID = charging_End.id
                    where TIMESTAMPDIFF(MINUTE, chargingstate.StartDate, chargingstate.EndDate) > 3 and chargingstate.EndChargingID - chargingstate.StartChargingID > 4
                    and charging.charge_energy_added > 1
                    order by StartDate desc", con);

                    double old_odometer = 0;

                    MySqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        double odometer         = (double)dr["odometer"];
                        int    chargingstate_id = (int)dr["chargingstate_id"];

                        if (old_odometer != odometer)
                        {
                            CombineChargingifNecessary(chargingstate_id, odometer, logging);
                            old_odometer = odometer;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, "");
                throw;
            }
        }
Exemple #20
0
        private static void DeleteChargingstate(int chargingstate_id)
        {
            try
            {
                Logfile.Log("Delete Chargingstate " + chargingstate_id.ToString());

                using (MySqlConnection con = new MySqlConnection(DBConnectionstring))
                {
                    con.Open();
                    MySqlCommand cmd = new MySqlCommand(@"delete from chargingstate where id = @id", con);
                    cmd.Parameters.AddWithValue("@id", chargingstate_id);

                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, chargingstate_id.ToString());
                Logfile.Log(ex.ToString());
            }
        }
Exemple #21
0
        internal static double getLatestOdometer()
        {
            try
            {
                using (MySqlConnection con = new MySqlConnection(DBConnectionstring))
                {
                    con.Open();
                    MySqlCommand    cmd = new MySqlCommand("SELECT EndKm FROM trip order by StartDate desc Limit 1", con);
                    MySqlDataReader dr  = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        return((double)dr[0]);
                    }
                }
            } catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, "getLatestOdometer");
            }

            return(0);
        }
Exemple #22
0
        public override string GetVersion()
        {
            string j = null;

            try
            {
                j = GetCurrentData();

                dynamic jsonResult = new JavaScriptSerializer().DeserializeObject(j);
                string  key        = "fwv";
                string  value      = jsonResult[key];

                return(value);
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, j);
            }

            return("");
        }
        public override bool?IsCharging()
        {
            string j = null;

            try
            {
                j = GetCurrentDataVitals();

                dynamic jsonResult = new JavaScriptSerializer().DeserializeObject(j);
                string  key        = "session_s";
                string  value      = jsonResult[key];

                return(value == "1");
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, j);
            }

            return(null);
        }
Exemple #24
0
        public static bool UpdateIncompleteTrips()
        {
            try
            {
                using (MySqlConnection con = new MySqlConnection(DBHelper.DBConnectionstring))
                {
                    con.Open();
                    MySqlCommand    cmd = new MySqlCommand(@"SELECT pos_start.id as StartPos, pos_end.id as EndPos
                     FROM drivestate 
                     JOIN pos pos_start ON drivestate . StartPos = pos_start. id
                     JOIN pos pos_end ON  drivestate . EndPos = pos_end. id 
                     WHERE
                     (pos_end. odometer - pos_start. odometer ) > 0.1 and 
                     (( pos_start. ideal_battery_range_km is null) or ( pos_end. ideal_battery_range_km is null))", con);
                    MySqlDataReader dr  = cmd.ExecuteReader();

                    while (dr.Read())
                    {
                        try
                        {
                            int StartPos = Convert.ToInt32(dr[0]);
                            int EndPos   = Convert.ToInt32(dr[1]);

                            DBHelper.UpdateDriveStatistics(StartPos, EndPos, false);
                        }
                        catch (Exception ex)
                        {
                            Logfile.Log(ex.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, "UpdateIncompleteTrips");
            }

            return(false);
        }
Exemple #25
0
        public override bool?IsCharging()
        {
            string j = null;

            try
            {
                j = GetCurrentDataVitals();
                j = j.Replace("nan,", "null,");

                dynamic jsonResult = new JavaScriptSerializer().DeserializeObject(j);

                bool vehicle_connected = jsonResult["vehicle_connected"];

                return(vehicle_connected);
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, j);
            }

            return(null);
        }
        public override string GetVersion()
        {
            string j = null;

            try
            {
                j = GetCurrentData();

                dynamic jsonResult = JsonConvert.DeserializeObject(j);
                string  key        = "fwv";
                string  value      = jsonResult[key];

                return(value);
            }
            catch (Exception ex)
            {
                ex.ToExceptionless().FirstCarUserID().Submit();
                Logfile.ExceptionWriter(ex, j);
            }

            return("");
        }
Exemple #27
0
        public override double?GetVehicleMeterReading_kWh()
        {
            string j = null;

            try
            {
                j = GetCurrentData();

                dynamic jsonResult = new JavaScriptSerializer().DeserializeObject(j);
                decimal value1     = jsonResult["emeters"][0]["total"];
                decimal value2     = jsonResult["emeters"][1]["total"];
                decimal value3     = jsonResult["emeters"][2]["total"];

                return((double?)(value1 + value2 + value3) / 1000.0);
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, j);
            }

            return(null);
        }
Exemple #28
0
        private static void Main(string[] args)
        {
            try
            {
                InitDebugLogging();

                InitStage1();

                InitCheckDocker();

                InitStage2();

                InitConnectToDB();

                InitWebserver();

                InitOpenTopoDataService();

                UpdateTeslalogger.StopComfortingMessagesThread();

                MQTTClient.StartMQTTClient();

                InitTLStats();

                UpdateDbInBackground();

                Logfile.Log("Init finished, now enter main loop");

                GetAllCars();

                InitNearbySuCService();
            }
            catch (Exception ex)
            {
                Logfile.Log(ex.Message);
                Logfile.ExceptionWriter(ex, "main loop");
                Logfile.Log("Teslalogger Stopped!");
            }
        }
Exemple #29
0
        private static void UpdateChargingstate(int chargingstate_id, DateTime StartDate, int StartChargingID)
        {
            try
            {
                Logfile.Log($"Update Chargingstate {chargingstate_id} with new StartDate: {StartDate} /  StartChargingID: {StartChargingID}");

                using (MySqlConnection con = new MySqlConnection(DBConnectionstring))
                {
                    con.Open();
                    MySqlCommand cmd = new MySqlCommand(@"update chargingstate set StartDate=@StartDate, StartChargingID=@StartChargingID where id = @id", con);
                    cmd.Parameters.AddWithValue("@id", chargingstate_id);
                    cmd.Parameters.AddWithValue("@StartDate", StartDate);
                    cmd.Parameters.AddWithValue("@StartChargingID", StartChargingID);

                    cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, chargingstate_id.ToString());
                Logfile.Log(ex.ToString());
            }
        }
Exemple #30
0
        internal static string GetLastCarVersion()
        {
            try
            {
                using (MySqlConnection con = new MySqlConnection(DBConnectionstring))
                {
                    con.Open();
                    MySqlCommand    cmd = new MySqlCommand("select version from car_version order by id desc limit 1", con);
                    MySqlDataReader dr  = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        return(dr[0].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                Logfile.ExceptionWriter(ex, "GetLastCarVersion");
                Logfile.Log(ex.ToString());
            }

            return("");
        }