public static void LoadConfiguration()
        {
            SiteInformation retConf;

            try
            {
                _deviceId = Environment.GetEnvironmentVariable("IOTHUB_DEVICE_CONN_STRING");
                _deviceId = _deviceId.Substring(_deviceId.IndexOf("DeviceId=", StringComparison.CurrentCultureIgnoreCase) + 9);
                _deviceId = _deviceId.Substring(0, _deviceId.IndexOf(';'));
                var stream = DownloadAsync(FileSitePath).GetAwaiter().GetResult();
                retConf = JsonConvert.DeserializeObject <SiteInformation>(Encoding.Default.GetString(stream.ToArray()));
            }
            catch (Exception)
            {
                try
                {
                    StreamReader file     = new StreamReader(Path.Combine(LocalPath, SitePath));
                    var          jsonSite = file.ReadToEnd();
                    file.Close();
                    retConf = JsonConvert.DeserializeObject <SiteInformation>(jsonSite);
                }
                catch (Exception)
                {
                    retConf = new SiteInformation();
                    //default settings
                    retConf.Sprinklers = new List <Sprinkler>();
                    for (int i = 0; i < 3; i++)
                    {
                        Sprinkler spr = new Sprinkler();
                        spr.Name                     = $"Sprinkler {i}";
                        spr.Number                   = i;
                        spr.TypicalProgram           = new TypicalProgram();
                        spr.TypicalProgram.StartTime = new TimeSpan(0, i, 0);
                        spr.TypicalProgram.Duration  = new TimeSpan(0, 20, 0);
                        retConf.Sprinklers.Add(spr);
                    }
                    retConf.Settings                = new Settings();
                    retConf.Settings.Name           = "My super Sprinkler";
                    retConf.Settings.ApiKey         = "123";
                    retConf.Settings.City           = "Antony";
                    retConf.Settings.Language       = Language.French;
                    retConf.Settings.Latitude       = (float)1.2;
                    retConf.Settings.Longitude      = (float)2.4;
                    retConf.Settings.Unit           = Unit.SI;
                    retConf.Settings.TimeZoneInfoId = TimeZoneInfo.Local.Id;
                    retConf.FuzzySprinklers         = new List <FuzzySprinkler>();
                    for (int i = 0; i < 3; i++)
                    {
                        FuzzySprinkler fz = new FuzzySprinkler();
                        fz.TempMin       = i * 10;
                        fz.TempMax       = (i + 1) * 10;
                        fz.SprinklingMax = (float)(100.0 * i / 3);
                        fz.RainMax       = (i + 1) * 2;
                        retConf.FuzzySprinklers.Add(fz);
                    }
                    retConf.SprinklerPrograms = new List <SprinklerProgram>();
                }
            }

            // Set the timer for automated forecast
            if (string.IsNullOrEmpty(retConf.Settings.TimeZoneInfoId))
            {
                retConf.Settings.TimeZoneInfoId = TimeZoneInfo.Local.Id;
            }
            DateTime dt = DateTime.Now;

            try
            {
                dt = dt.Add(TimeZoneInfo.FindSystemTimeZoneById(retConf.Settings.TimeZoneInfoId).GetUtcOffset(dt));
            }
            catch (Exception)
            {
                retConf.Settings.TimeZoneInfoId = TimeZoneInfo.Local.Id;
                dt = dt.Add(TimeZoneInfo.FindSystemTimeZoneById(retConf.Settings.TimeZoneInfoId).GetUtcOffset(dt));
            }
            TimeSpan dueDate;
            TimeSpan period = new TimeSpan(24, 0, 0);

            if (dt.TimeOfDay > retConf.Settings.TimeToCheck)
            {
                dueDate = new TimeSpan(24, 0, 0) - dt.TimeOfDay + retConf.Settings.TimeToCheck;
            }
            else
            {
                dueDate = retConf.Settings.TimeToCheck - dt.TimeOfDay;
            }

            _timerForecastCallBack = new Timer(TimerForecastCheck, null, dueDate, period);
            _timerProgramCallBack  = new Timer(TimerProgramCheck, null, new TimeSpan(0, 1, 0), new TimeSpan(0, 1, 0));
        }
Exemplo n.º 2
0
 public AzureIoT(ISiteInformation siteInformation)
 {
     _site = (SiteInformation)siteInformation;
     InitIoTHub();
 }