Esempio n. 1
0
    public static double GetCarbonIntensityByCountry(string countryCode)
    {
        if (!_instance.co2_map.ContainsKey(countryCode))
        {
            return(1);
        }
        Co2Data data = _instance.co2_map[countryCode];

        return(data.carbonIntensity);
    }
Esempio n. 2
0
    void parse_co2_table(StreamReader sr)
    {
        string line;

        while ((line = sr.ReadLine()) != null)
        {
            // split key from json
            string[] kv_pair = line.Split('=');

            // find key
            string[] key_split = kv_pair[0].Split('-');
            string   key       = key_split[0];

            // parse json, manually...
            string   trimmed    = kv_pair[1].Substring(1, kv_pair[1].Length - 2);
            string[] data_pairs = trimmed.Split(',');
            Co2Data  data       = new Co2Data();
            foreach (string dp in data_pairs)
            {
                String[] data_pair = dp.Split(':');
                if (String.Equals(data_pair[1].Trim(), "null"))
                {
                    continue;
                }
                switch (data_pair[0].Trim())
                {
                case "\"carbonIntensity\"":
                    // Debug.Log("HEJ: " + data_pair[1].Trim());
                    data.carbonIntensity = Double.Parse(data_pair[1].Trim(), CultureInfo.InvariantCulture);
                    break;

                case "\"fossilFuelPercentage\"":
                    data.fossilFuelPercentage = Double.Parse(data_pair[1].Trim(), CultureInfo.InvariantCulture);
                    break;

                case "\"time\"":
                    data.time = data_pair[1].Trim();
                    break;
                }
            }

            // insert into co2_map
            co2_map.Add(key, data);
        }
    }