Ejemplo n.º 1
0
        private static void CalculateChargeDuration(int journeyId)
        {
            try
            {
                using (MySqlConnection con = new MySqlConnection(DBHelper.DBConnectionstring))
                {
                    con.Open();
                    double charge_duration_minutes = 0;
                    using (MySqlCommand cmd = new MySqlCommand(@"
SELECT
    chargingstate.EndDate,
    chargingstate.StartDate
FROM
    chargingstate
WHERE
    chargingstate.Pos >= (SELECT StartPosID FROM journeys WHERE ID = @journeyID)
    AND chargingstate.Pos < (SELECT EndPosID FROM journeys WHERE ID = @journeyID)
    AND chargingstate.carID = (SELECT CarID FROM journeys WHERE ID = @journeyID)
", con))
                    {
                        cmd.Parameters.AddWithValue("@journeyID", journeyId);
                        Tools.DebugLog(cmd);
                        MySqlDataReader dr = SQLTracer.TraceDR(cmd);
                        while (dr.Read())
                        {
                            DateTime d1 = (DateTime)dr[0];
                            DateTime d2 = (DateTime)dr[1];
                            TimeSpan ts = d1 - d2;
                            charge_duration_minutes += ts.TotalMinutes;
                        }
                        dr.Close();
                    }
                    using (MySqlCommand cmd = new MySqlCommand(@"
UPDATE
    journeys
SET
    charge_duration_minutes = @charge_duration_minutes
WHERE
    Id = @journeyID", con))
                    {
                        cmd.Parameters.AddWithValue("@journeyID", journeyId);
                        cmd.Parameters.AddWithValue("@charge_duration_minutes", (int)charge_duration_minutes);
                        Tools.DebugLog(cmd);
                        SQLTracer.TraceNQ(cmd);
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToExceptionless().FirstCarUserID().Submit();
                Logfile.Log(ex.ToString());
            }
        }
Ejemplo n.º 2
0
        internal static void JourneysDelete(HttpListenerRequest request, HttpListenerResponse response)
        {
            // in: CarID, StartPosID, EndPosId
            // out: CarID, StartPosID, EndPosId
            // action: render really delete HTML
            response.AddHeader("Content-Type", "text/html; charset=utf-8");
            StringBuilder sb = new StringBuilder();
            int journeyID = Convert.ToInt32(GetUrlParameterValue(request, "id"), Tools.ciEnUS);
            try
            {
                using (MySqlConnection con = new MySqlConnection(DBHelper.DBConnectionstring))
                {
                    con.Open();
                    using (MySqlCommand cmd = new MySqlCommand(@"
SELECT
    journeys.name,
    cars.display_name,
    tripStart.Start_address,
    tripEnd.End_address
FROM
    journeys,
    cars,
    trip tripStart,
    trip tripEnd
WHERE
    journeys.CarID = cars.Id
    AND journeys.StartPosID = tripStart.StartPosID
    AND journeys.EndPosID = tripEnd.EndPosID
    AND journeys.ID = @journeyID", con))
                    {
                        cmd.Parameters.AddWithValue("@journeyID", journeyID);
                        Tools.DebugLog(cmd);
                        MySqlDataReader dr = SQLTracer.TraceDR(cmd);
                        if (dr.Read())
                        {
                            sb.Append($@"
<tr><td>{WebUtility.HtmlEncode(TEXT_LABEL_REALLY_DELETE)}&nbsp;{WebUtility.HtmlEncode(dr[0].ToString())}&nbsp;({WebUtility.HtmlEncode(dr[1].ToString())})&nbsp;-&nbsp;{WebUtility.HtmlEncode(dr[2].ToString())}{WebUtility.HtmlEncode("-->")}{WebUtility.HtmlEncode(dr[3].ToString())}?</td>
<td><form action=""{EndPoints["JourneysDeleteDelete"]}""><input type=""hidden"" name=""id"" value=""{journeyID}""><button type=""submit"">{WebUtility.HtmlEncode(TEXT_BUTTON_DELETE_DELETE)}</button></form></td>
");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToExceptionless().FirstCarUserID().Submit();
                Logfile.Log(ex.ToString());
                sb.Append(ex.ToString());
            }
            WriteString(response, html1 + sb.ToString() + html2);
        }
Ejemplo n.º 3
0
        internal static void JourneysCreateStart(HttpListenerRequest request, HttpListenerResponse response)
        {
            string json = "";
            string data = WebServer.GetDataFromRequestInputStream(request);
            dynamic r = JsonConvert.DeserializeObject(data);

            int CarID = r["carid"];
            Tools.DebugLog($"JourneysCreateStart CarID:{CarID}");

            var o = new List<object>();
            o.Add(new KeyValuePair<string, string>("", "Please Select"));

            try
            {
                using (MySqlConnection con = new MySqlConnection(DBHelper.DBConnectionstring))
                {
                    con.Open();
                    using (MySqlCommand cmd = new MySqlCommand(@"
SELECT
    StartPosID,
    StartDate, 
    Start_address
FROM
    trip
WHERE
    CarID = @CarID
ORDER BY
    StartDate", con))
                    {
                        cmd.Parameters.AddWithValue("@CarID", CarID);
                        Tools.DebugLog(cmd);
                        MySqlDataReader dr = SQLTracer.TraceDR(cmd);
                        while (dr.Read() && dr[0] != DBNull.Value)
                        {
                            o.Add(new KeyValuePair<string, string>(dr[0].ToString(), dr[1].ToString() + " - " + dr[2].ToString()));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.ToExceptionless().FirstCarUserID().Submit();
                Logfile.Log(ex.ToString());
            }

            json = JsonConvert.SerializeObject(o);

            WriteString(response, json);
        }
Ejemplo n.º 4
0
        internal string Dump()
        {
            StringBuilder sb = new StringBuilder();

            _ = sb.Append($"TeslaLogger process statistics{Environment.NewLine}");
            try
            {
                Process proc = Process.GetCurrentProcess();
                _ = sb.Append($"WorkingSet64:        {proc.WorkingSet64,12}{Environment.NewLine}");
                _ = sb.Append($"PeakWorkingSet64:    {proc.PeakWorkingSet64,12}{Environment.NewLine}");
                _ = sb.Append($"PrivateMemorySize64: {proc.PrivateMemorySize64,12}{Environment.NewLine}");
                _ = sb.Append($"VirtualMemorySize64: {proc.VirtualMemorySize64,12}{Environment.NewLine}");
                _ = sb.Append($"StartTime: {proc.StartTime}{Environment.NewLine}");
                _ = sb.Append($"Database sizes: DB {DBHelper.Database}{Environment.NewLine}");
                using (MySqlConnection con = new MySqlConnection(DBHelper.DBConnectionstring))
                {
                    con.Open();
                    using (MySqlCommand cmd = new MySqlCommand(@"
SELECT
  TABLE_NAME AS `Table`,
  ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size (MB)`
FROM
  information_schema.TABLES
WHERE
  TABLE_SCHEMA = @dbschema
  and ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) > 0.9
ORDER BY
  (DATA_LENGTH + INDEX_LENGTH)
DESC", con))
                    {
                        cmd.Parameters.AddWithValue("@dbschema", DBHelper.Database);
                        MySqlDataReader dr = SQLTracer.TraceDR(cmd);
                        while (dr.Read())
                        {
                            _ = sb.Append($"  table {dr[0]} has {dr[1]}mb{Environment.NewLine}");
                        }
                    }
                }
            }
            catch (Exception) { }
            return(sb.ToString());
        }
Ejemplo n.º 5
0
 private static bool GetSuperchargerByName(string suc, out int sucID)
 {
     using (MySqlConnection con = new MySqlConnection(DBHelper.DBConnectionstring))
     {
         con.Open();
         // find internal ID of supercharger by name
         using (MySqlCommand cmd = new MySqlCommand("SELECT id from superchargers where name = @name", con))
         {
             cmd.Parameters.AddWithValue("@name", suc);
             MySqlDataReader dr = SQLTracer.TraceDR(cmd);
             if (dr.Read() && dr[0] != DBNull.Value)
             {
                 if (int.TryParse(dr[0].ToString(), out sucID))
                 {
                     return(true);
                 }
             }
         }
     }
     sucID = int.MinValue;
     return(false);
 }