Пример #1
0
 public static void Main(string[] args)
 {
     // Read config from json file
     AppConfig  = Models.Tools.getConfig();
     currentTLM = new Models.tlm();
     carState   = new Models.CarState();
     CreateWebHostBuilder(args).Build().Run();
 }
Пример #2
0
        public static void SaveDataIntoInfluxDB(tlm objTLM)
        {
            char[] Token          = Program.AppConfig.InfluxDBToken.ToCharArray();
            var    influxDBClient = InfluxDBClientFactory.CreateV1(Program.AppConfig.InfluxDBServer,
                                                                   Program.AppConfig.InfluxDBUser, Token, Program.AppConfig.InfluxDBDataBase, null);

            using (var writeApi = influxDBClient.GetWriteApi())
            {
                // Let's calculate the distance between the previous point.
                var distance = Models.PreviousData.getDistance(objTLM.lat, objTLM.lon);

                // Let's calculate Consumption kWh
                // Car Battery degradation
                var percentUtil    = Program.AppConfig.CAR_BATTERY * objTLM.soh / 100;
                var ConsumptionkWh = Models.PreviousData.getDiffSOC(objTLM.soc) * percentUtil / 100;

                // Let's calculate Consumption kWh/100
                // Not Necesary, it's calculated in Grafana
                // var ConsumptionkWh100 = ConsumptionkWh / (distance / 1000) * 100;

                // Let's conver the UTC to datetime
                var timeFromOVMS = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(objTLM.utc * 1000.0);

                //
                // Write by Point
                //
                var point = PointData.Measurement("ovms")
                            .Tag("vehicle", "konaev")
                            .Field("alt", objTLM.alt)
                            .Field("batt_temp", objTLM.batt_temp)
                            .Field("current", objTLM.current)
                            .Field("ext_temp", objTLM.ext_temp)
                            .Field("is_charging", objTLM.is_charging)
                            .Field("lat", objTLM.lat)
                            .Field("lon", objTLM.lon)
                            .Field("power", objTLM.power)
                            .Field("soc", objTLM.soc)
                            .Field("soh", objTLM.soh)
                            .Field("speed", objTLM.speed)
                            .Field("utc", objTLM.utc)
                            .Field("distance", distance)
                            .Field("consumptionkwh", ConsumptionkWh)
                            .Field("voltage", objTLM.voltage)
                            //.Field("Consumptionkwh100", ConsumptionkWh100)
                            .Timestamp(timeFromOVMS, WritePrecision.Ns);

                writeApi.WritePoint(point);
            }
            influxDBClient.Dispose();
        }
Пример #3
0
 public returnTLM(tlm t)
 {
     this.alt         = t.alt;
     this.batt_temp   = t.batt_temp;
     this.car_model   = t.car_model;
     this.current     = t.current;
     this.ext_temp    = t.ext_temp;
     this.is_charging = t.is_charging;
     this.lat         = t.lat;
     this.lon         = t.lon;
     this.power       = t.power;
     this.soc         = Convert.ToInt32(Math.Floor(t.soc)); //Convert.ToInt32(Math.Round(t.soc,0));
     this.soh         = t.soh;
     this.speed       = t.speed;
     this.utc         = t.utc;
     this.voltage     = t.voltage;
 }
Пример #4
0
        public static int SendData2ABRP(tlm objTLM)
        {
            int Counter = 0;

            // Just Continue if GPS coordinates are different from 0
            if (objTLM.lat == 0 && objTLM.lon == 0 && objTLM.alt == 0)
            {
                // There is no GPS data yet
                return(0);
            }

            // Fill utc and car model
            objTLM.car_model = Program.AppConfig.CAR_MODEL;
            // utc is comming from tlm
            //objTLM.utc = Convert.ToInt32(DateTime.UtcNow.Subtract(DateTime.MinValue.AddYears(1969)).TotalMilliseconds / 1000);

            // Reparse TLM
            var    returnTLM          = new returnTLM(objTLM);
            string stringTLMParameter = SerializeReturnTLM(returnTLM);

            // Send data to ABRP (read from config)
            var URL = Program.AppConfig.ABRPUrl;

            var urljson = URL += "?";

            urljson += "api_key=" + Program.AppConfig.ABRP_api_key;
            urljson += "&";
            urljson += "token=" + Program.AppConfig.ABRP_token;
            urljson += "&";
            urljson += "tlm=" + stringTLMParameter;

            try
            {
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri(urljson);
                    //HTTP GET
                    var responseTask = client.GetAsync("");
                    responseTask.Wait();

                    var result = responseTask.Result;
                    var log    = result.Version + " | " + result.ReasonPhrase + " | " + result.RequestMessage;
                    if (result.IsSuccessStatusCode)
                    {
                        // Ha ido bien
                        Counter = Models.Tools.AddCount();
                    }
                    if (Program.AppConfig.DebugMode)
                    {
                        Models.Tools.guardarLog(log);
                        // Guardar en log los datos enviados
                        // Models.Tools.guardarLog(stringTLMParameter);
                    }
                }
                // Guardar los datos en InfluxDB
                Models.Tools.SaveDataIntoInfluxDB(objTLM);
            }
            catch (Exception ex)
            {
                string msg = "Source: " + ex.Source + " | " + ex.Message;
                if (ex.InnerException != null)
                {
                    msg += " | " + ex.InnerException.Message;
                }
                Models.Tools.guardarLog(msg);
            }

            return(Counter);
        }