Ejemplo n.º 1
0
        // POST: ChangeAPIKey
        public ActionResult ChangeDeviceAPIKey(string Id)
        {
            using (_context = new ApplicationDbContext())
            {
                //Get the device to change the APIKey
                WeatherDevice deviceToChange = _context.WeatherDevices.FirstOrDefault(wd => wd.Id == Id);

                //If the device doesn't exist
                if (deviceToChange == null)
                {
                    return(HttpNotFound());
                }

                //Get all current weather updates from the database
                List <WeatherUpdate> deviceUpdates = _context.WeatherUpdates.Where(wu => wu.ParentDeviceID == deviceToChange.Id).ToList();

                //Create a new APIKey and replace it with the current device APIKey
                string newAPIKey = GetNewDeviceAPIKey();
                deviceToChange.APIKey = newAPIKey;

                //Save the changes to the database
                _context.SaveChanges();

                //Redirect the user back to edit page
                return(RedirectToAction("Edit", new { Id = deviceToChange.Id }));
            }
        }
Ejemplo n.º 2
0
        //This function will create a new device APIKey
        public string GetNewDeviceAPIKey()
        {
            //Intialise vairables
            string newApiKey    = "";
            bool   duplicateKey = true;

            using (_context = new ApplicationDbContext())
            {
                //While the database has not found a new key for the device
                while (duplicateKey)
                {
                    newApiKey = Guid.NewGuid().ToString();
                    WeatherDevice deviceSearch = _context.WeatherDevices.FirstOrDefault(wd => wd.APIKey == newApiKey);

                    if (deviceSearch != null)
                    {
                        continue;
                    }

                    break;
                }
            }

            //Return the new api key that was created
            return(newApiKey);
        }
Ejemplo n.º 3
0
        private void InitializeHubs()
        {
            _weatherDevice = new WeatherDevice();

            _weatherDevice.AddTelemetry(new TelemetryFormat {
                Name = "temperature", DisplayName = "Temp", Type = "Double"
            },
                                        (double)0);
            _weatherDevice.AddTelemetry(new TelemetryFormat {
                Name = "pressure", DisplayName = "hPa", Type = "Double"
            },
                                        (double)0);

            _weatherDevice.AddTelemetry(new TelemetryFormat {
                Name = "humidity", DisplayName = "g", Type = "Double"
            },
                                        (double)0);



            _weatherDevice.DeviceId  = "[DeviceId]";
            _weatherDevice.DeviceKey = "[DeviceKey]";
            _weatherDevice.HostName  = "[HostName]";

            _weatherDevice.SendTelemetryFreq = 60000;
            _weatherDevice.Connect();


            _weatherDevice.onReceivedMessage += WeatherDevice_onReceivedMessage;
        }
Ejemplo n.º 4
0
        private void InitializeHubs()
        {
            _weatherDevice = new WeatherDevice();

            _weatherDevice.AddTelemetry(new TelemetryFormat {
                Name = "temperature", DisplayName = "Temp", Type = "Double"
            },
                                        (double)0);
            _weatherDevice.AddTelemetry(new TelemetryFormat {
                Name = "pressure", DisplayName = "hPa", Type = "Double"
            },
                                        (double)0);

            _weatherDevice.AddTelemetry(new TelemetryFormat {
                Name = "humidity", DisplayName = "g", Type = "Double"
            },
                                        (double)0);



            _weatherDevice.DeviceId  = "IgniteJhbThings";
            _weatherDevice.DeviceKey = "uJLwieHWZubfji7/TXoybKXbrSxdyFQgdYNl0YwZJA8=";
            _weatherDevice.HostName  = "AzureIotEdgeJhbMsDugHub.azure-devices.net";

            _weatherDevice.SendTelemetryFreq = 60000;
            _weatherDevice.Connect();


            _weatherDevice.onReceivedMessage += WeatherDevice_onReceivedMessage;
        }
Ejemplo n.º 5
0
        // GET: Details
        public ActionResult Details(string Id, WeatherDisplay displayType = 0)
        {
            using (_context = new ApplicationDbContext())
            {
                //Find the weather device in the database
                WeatherDevice weatherDevice = _context.WeatherDevices.First(wd => wd.Id == Id);

                //If the device has not been found
                if (weatherDevice == null)
                {
                    weatherDevice = new WeatherDevice();
                }

                List <WeatherUpdate> weatherUpdates = null;
                DateTime             displayTime    = DateTime.Now;

                //Get the weather updates that correlates with the current Date and Time the user selects
                switch (displayType)
                {
                case WeatherDisplay.OneHour:
                    displayTime    = DateTime.Now.AddHours(-1);
                    weatherUpdates = _context.WeatherUpdates.Where(wu => wu.TimeUpdateSent >= displayTime && wu.ParentDeviceID == Id).ToList();
                    break;

                case WeatherDisplay.SixHours:
                    displayTime    = DateTime.Now.AddHours(-6);
                    weatherUpdates = _context.WeatherUpdates.Where(wu => wu.TimeUpdateSent >= displayTime && wu.ParentDeviceID == Id).ToList();
                    break;

                case WeatherDisplay.TwentyFourHours:
                    displayTime    = DateTime.Now.AddDays(-1);
                    weatherUpdates = _context.WeatherUpdates.Where(wu => wu.TimeUpdateSent >= displayTime && wu.ParentDeviceID == Id).ToList();
                    break;

                case WeatherDisplay.PastSevenDays:
                    displayTime    = DateTime.Now.AddDays(-7);
                    weatherUpdates = _context.WeatherUpdates.Where(wu => wu.TimeUpdateSent >= displayTime && wu.ParentDeviceID == Id).ToList();
                    break;

                case WeatherDisplay.PastThirtyDays:
                    displayTime    = DateTime.Now.AddDays(-30);
                    weatherUpdates = _context.WeatherUpdates.Where(wu => wu.TimeUpdateSent >= displayTime && wu.ParentDeviceID == Id).ToList();
                    break;

                default:
                    break;
                }

                weatherUpdates = weatherUpdates.OrderBy(time => time.TimeUpdateSent.TimeOfDay).ToList();

                DetailsWeatherDevice currentView = new DetailsWeatherDevice
                {
                    CurrentDevice  = weatherDevice,
                    WeatherUpdates = weatherUpdates
                };

                return(View(currentView));
            }
        }
Ejemplo n.º 6
0
        public ActionResult Create(WeatherDevice device)
        {
            using (_context = new ApplicationDbContext())
            {
                //Add weather device to the database and save
                _context.WeatherDevices.Add(device);
                _context.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
        private void InitializeHubs()
        {
            _weatherDevice = new WeatherDevice();

            _weatherDevice.DeviceId  = "[DeviceId]";
            _weatherDevice.DeviceKey = "[DeviceKey]";
            _weatherDevice.HostName  = "[HostName]";

            _weatherDevice.SendTelemetryFreq = 10000;
            _weatherDevice.Connect();
        }
Ejemplo n.º 8
0
        public ActionResult Edit(string Id)
        {
            using (_context = new ApplicationDbContext())
            {
                WeatherDevice weatherDevice = _context.WeatherDevices.First(wd => wd.Id == Id);

                if (weatherDevice == null)
                {
                    return(HttpNotFound());
                }

                return(View(weatherDevice));
            }
        }
Ejemplo n.º 9
0
        public bool WeatherUpdate(WeatherUpdateAPI weatherUpdate)
        {
            using (_context = new ApplicationDbContext())
            {
                //Get device that sent the request with the current APIKey
                if (!ModelState.IsValid)
                {
                    throw new HttpResponseException(HttpStatusCode.BadRequest);
                }

                WeatherDevice currentDevice = _context.WeatherDevices.First(wd => wd.APIKey == weatherUpdate.APIKey);

                //Check if the device is currently null
                if (currentDevice == null)
                {
                    return(false);
                }

                //Create a new weather update with the associated values sent to the server
                WeatherUpdate newWeatherUpdate = new WeatherUpdate
                {
                    ParentDeviceID  = currentDevice.Id,
                    Temperature     = weatherUpdate.Temperature,
                    Humidity        = weatherUpdate.Humidity,
                    HeatIndex       = weatherUpdate.HeatIndex,
                    LPG             = weatherUpdate.LPG,
                    CarbonEmissions = weatherUpdate.CarbonEmissions,
                    Smoke           = weatherUpdate.Smoke
                };
                newWeatherUpdate.CalculateDewPoint();
                currentDevice.LatestWeatherUpdate = DateTime.Now;


                //Add the new weather update to the Database and Save
                _context.WeatherUpdates.Add(newWeatherUpdate);
                _context.SaveChanges();

                return(true);
            }
        }
Ejemplo n.º 10
0
        public ActionResult Edit(WeatherDevice weatherDevice)
        {
            using (_context = new ApplicationDbContext())
            {
                var deviceToEdit = _context.WeatherDevices.First(wd => wd.Id == weatherDevice.Id);

                if (deviceToEdit == null)
                {
                    return(HttpNotFound());
                }

                deviceToEdit.Name                = weatherDevice.Name;
                deviceToEdit.Location            = weatherDevice.Location;
                deviceToEdit.LatestWeatherUpdate = weatherDevice.LatestWeatherUpdate;
                deviceToEdit.NextMaintenanceDate = weatherDevice.NextMaintenanceDate;
                deviceToEdit.ActiveDevice        = weatherDevice.ActiveDevice;
                deviceToEdit.APIKey              = weatherDevice.APIKey;

                _context.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
Ejemplo n.º 11
0
        public ActionResult Create()
        {
            WeatherDevice device = new WeatherDevice();

            return(View(device));
        }
Ejemplo n.º 12
0
        internal async Task <bool> LoadDataAsync()
        {
            if (_lastRefresh.AddMinutes(10).CompareTo(DateTime.Now) > 0)
            {
                return(true);
            }
            var stationData = await _aPICommands.GetStationsData().ConfigureAwait(false);

            if (stationData == null)
            {
                return(false);
            }
            _lastRefresh        = DateTime.Now;
            Mail                = stationData.Body.User.Mail;
            Language            = stationData.Body.User.Administrative.Lang;
            CultureInfo         = new CultureInfo(stationData.Body.User.Administrative.RegLocale);
            RegionInfo          = new RegionInfo(stationData.Body.User.Administrative.Country);
            Unit                = stationData.Body.User.Administrative.Unit.ToUnitSystem();
            WindUnit            = stationData.Body.User.Administrative.Windunit.ToWindUnit();
            PressureUnit        = stationData.Body.User.Administrative.Pressureunit.ToPressureUnit();
            FeelLikeTemperature = stationData.Body.User.Administrative.FeelLikeAlgo.ToFeelLikeAlgo();
            foreach (StationData.Device device in stationData.Body.Devices)
            {
                WeatherDevice weatherDevice = new WeatherDevice(_aPICommands)
                {
                    Place = new Location()
                    {
                        Altitude  = device.Place.Altitude,
                        City      = device.Place.City,
                        Country   = device.Place.Country,
                        Latitude  = device.Place.Location[0],
                        Longitude = device.Place.Location[1],
                        TimeZone  = device.Place.Timezone
                    }
                };
                weatherDevice.Base.Load(device);
                foreach (StationData.WeatherModule module in device.Modules)
                {
                    switch (module.Type.ToModuleType())
                    {
                    case ModuleType.Outdoor:
                        weatherDevice.OutdoorModule.Load(module, weatherDevice.Base.Id);
                        break;

                    case ModuleType.Indoor:
                        IndoorModule indoorModule;
                        if (!weatherDevice.IndoorModule1.Available)
                        {
                            indoorModule = weatherDevice.IndoorModule1;
                        }
                        else
                        if (!weatherDevice.IndoorModule2.Available)
                        {
                            indoorModule = weatherDevice.IndoorModule2;
                        }
                        else
                        {
                            indoorModule = weatherDevice.IndoorModule3;
                        }
                        indoorModule.Load(module, weatherDevice.Base.Id);
                        break;

                    case ModuleType.RainGauge:
                        weatherDevice.RainGauge.Load(module, weatherDevice.Base.Id);
                        break;

                    case ModuleType.Anenometer:
                        weatherDevice.Anemometer.Load(module, weatherDevice.Base.Id);
                        break;
                    }
                }
                Devices.Add(weatherDevice);
            }
            return(true);
        }
Ejemplo n.º 13
0
 private async Task SendStateValueForDevice(WeatherDevice weatherDevice)
 {
     await _statesValuesHost.Send(FormatModuleNameForStateValueKeyRules(weatherDevice.ModuleName), weatherDevice.DashboardData);
 }