Exemplo n.º 1
0
        public void CreateCurrentJSON()
        {
            try
            {
                int    duration    = 0;
                double distance    = 0;
                double trip_kwh    = 0.0;
                double trip_avg_wh = 0.0;

                try
                {
                    if (current_trip_end == DateTime.MinValue)
                    {
                        duration = (int)((TimeSpan)(DateTime.Now - current_trip_start)).TotalSeconds;
                        distance = current_odometer - current_trip_km_start;
                        trip_kwh = (current_trip_start_range - current_ideal_battery_range_km) * Wh_TR;

                        if (distance > 0)
                        {
                            trip_avg_wh = trip_kwh / distance * 1000;
                        }
                    }
                    else
                    {
                        duration = (int)((TimeSpan)(current_trip_end - current_trip_start)).TotalSeconds;
                        distance = current_trip_km_end - current_trip_km_start;
                        trip_kwh = (current_trip_start_range - current_trip_end_range) * Wh_TR;

                        if (distance > 0)
                        {
                            trip_avg_wh = trip_kwh / distance * 1000;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Tools.Log(ex.ToString());
                    duration = 0;
                }
                if (duration < 0)
                {
                    duration = 0;
                }


                var values = new Dictionary <string, object>
                {
                    { "charging", current_charging },
                    { "driving", current_driving },
                    { "online", current_online },
                    { "sleeping", current_sleeping },
                    { "speed", current_speed },
                    { "power", current_power },
                    { "odometer", current_odometer },
                    { "ideal_battery_range_km", current_ideal_battery_range_km },
                    { "outside_temp", current_outside_temp },
                    { "battery_level", current_battery_level },
                    { "charger_voltage", current_charger_voltage },
                    { "charger_phases", current_charger_phases },
                    { "charger_actual_current", current_charger_actual_current },
                    { "charge_energy_added", current_charge_energy_added },
                    { "charger_power", current_charger_power },
                    { "car_version", current_car_version },
                    { "trip_start", current_trip_start.ToString("t", Tools.ciDeDE) },
                    { "trip_max_speed", current_trip_max_speed },
                    { "trip_max_power", current_trip_max_power },
                    { "trip_duration_sec", duration },
                    { "trip_kwh", trip_kwh },
                    { "trip_avg_kwh", trip_avg_wh },
                    { "trip_distance", distance }
                };

                current_json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(values);

                FileManager.WriteCurrentJsonFile(current_json);
            }
            catch (Exception ex)
            {
                Tools.Log(ex.ToString());
                current_json = "";
            }
        }
Exemplo n.º 2
0
        public void CreateCurrentJSON()
        {
            try
            {
                lastJSONwrite = DateTime.UtcNow;

                int    duration    = 0;
                double distance    = 0;
                double trip_kwh    = 0.0;
                double trip_avg_wh = 0.0;

                try
                {
                    if (current_trip_end == DateTime.MinValue)
                    {
                        duration = (int)(DateTime.Now - current_trip_start).TotalSeconds;
                        distance = current_odometer - current_trip_km_start;
                        trip_kwh = (current_trip_start_range - current_ideal_battery_range_km) * Wh_TR;

                        if (distance > 0)
                        {
                            trip_avg_wh = trip_kwh / distance * 1000;
                        }
                    }
                    else
                    {
                        duration = (int)(current_trip_end - current_trip_start).TotalSeconds;
                        distance = current_trip_km_end - current_trip_km_start;
                        trip_kwh = (current_trip_start_range - current_trip_end_range) * Wh_TR;

                        if (distance > 0)
                        {
                            trip_avg_wh = trip_kwh / distance * 1000;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logfile.Log(ex.ToString());
                    duration = 0;
                }
                if (duration < 0)
                {
                    duration = 0;
                }

                Dictionary <string, object> values = new Dictionary <string, object>
                {
                    { "charging", current_charging },
                    { "driving", current_driving },
                    { "online", current_online },
                    { "sleeping", current_sleeping },
                    { "falling_asleep", current_falling_asleep },
                    { "speed", current_speed },
                    { "power", current_power },
                    { "odometer", current_odometer },
                    { "ideal_battery_range_km", current_ideal_battery_range_km },
                    { "outside_temp", current_outside_temp },
                    { "battery_level", current_battery_level },
                    { "charger_voltage", current_charger_voltage },
                    { "charger_phases", current_charger_phases },
                    { "charger_actual_current", current_charger_actual_current },
                    { "charge_energy_added", current_charge_energy_added },
                    { "charger_power", current_charger_power },
                    { "car_version", current_car_version },
                    { "trip_start", current_trip_start.ToString("t", Tools.ciDeDE) },
                    { "trip_start_dt", current_trip_start.ToString("s") },
                    { "trip_max_speed", current_trip_max_speed },
                    { "trip_max_power", current_trip_max_power },
                    { "trip_duration_sec", duration },
                    { "trip_kwh", trip_kwh },
                    { "trip_avg_kwh", trip_avg_wh },
                    { "trip_distance", distance },
                    { "ts", DateTime.Now.ToString("s") },
                    { "latitude", latitude },
                    { "longitude", longitude },
                    { "charge_limit_soc", charge_limit_soc },
                    { "inside_temperature", current_inside_temperature },
                    { "battery_heater", current_battery_heater },
                    { "is_preconditioning", current_is_preconditioning },
                    { "sentry_mode", current_is_sentry_mode },
                    { "country_code", current_country_code },
                    { "state", current_state }
                };

                TimeSpan ts = DateTime.Now - lastScanMyTeslaReceived;
                if (ts.TotalMinutes < 5)
                {
                    values.Add("SMTCellTempAvg", SMTCellTempAvg);
                    values.Add("SMTCellMinV", SMTCellMinV);
                    values.Add("SMTCellAvgV", SMTCellAvgV);
                    values.Add("SMTCellMaxV", SMTCellMaxV);
                    values.Add("SMTBMSmaxCharge", SMTBMSmaxCharge);
                    values.Add("SMTBMSmaxDischarge", SMTBMSmaxDischarge);
                }

                current_json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(values);

                FileManager.WriteCurrentJsonFile(current_json);
            }
            catch (Exception ex)
            {
                Logfile.Log(ex.ToString());
                current_json = "";
            }
        }