Пример #1
0
 public void ClimateItem_MeanValueResultBelowZeroWithBothBelowZeroIsFormatedCorrectly()
 {
     string left        = "-10.5";
     string right       = "-12.3";
     string resultValue = ClimateItem.Meanvalue(left, right);
     //Assert.Equal("-11.4", resultValue);
 }
Пример #2
0
        public ClimateItem CurrentReading(AppKeyConfig AppConfig)
        {
            ClimateItem item = null;

            // Get the access and refresh tokens if they exist, if they don't exist we cannot proceceed and return null
            NibeAuth auth = getNibeAuthJson();

            if (auth == null)
            {
                return(null);
            }

            // Access and refresh token exist,  try to access using the access token
            item = GetReadingWithAccessCode(auth.access_token, AppConfig);

            // If we get null back, it didn't work and we try to refresh with the refresh token
            if (item == null)
            {
                // If it didn't work to get a new access token we bail out and return null
                auth = Refresh(AppConfig);


                if (auth == null)
                {
                    return(null);
                }

                // It worked to get a new access token, try agin to get data using the new access token
                item = GetReadingWithAccessCode(auth.access_token, AppConfig);
            }

            // If get an item back we return it, it we get null back we can't still access data and return null
            return(item);
        }
Пример #3
0
        public void NibeUnit_CurrentReading_Is_NotSuccessfulDueToMissingAuthFile()
        {
            string nibeauth  = null;
            bool   fileExist = false;

            if (File.Exists("data/nibeauth.json"))
            {
                nibeauth  = File.ReadAllText("data/nibeauth.json");
                fileExist = true;
            }

            File.Delete("data/nibeauth.json");

            Assert.False(File.Exists("data/nibeauth.json"));


            AppKeyConfig configs = new AppKeyConfig();

            ClimateItem item = nibeUnit.CurrentReading(configs);

            Assert.Null(item);
            if (fileExist)
            {
                File.WriteAllText("data/nibeauth.json", nibeauth);
            }
        }
Пример #4
0
        public void NibeUnit_CurrentReading_Is_SuccessfulWithRefresh()
        {
            Assert.True(File.Exists("data/nibeauth.json"));

            string  fileContentExp = "{\"access_token\":\"AAEAAI3RKfqMqpUT6gPct8J4ysi2MvAITg9iJ - fydXOLDEP1DZ_9DbX3hh2Nw3DOydyc_IvBzNJW - k9uon0I4cxNuhhJJ5Z5OQEAUuB6er10aWLS7QnHAxvyAJFgsRUA9BBvmmA21MdBexw4J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4qTONFYRtL1Z5X8rJ_jLzLYFy3I4EykDqw\",\"scope\":\"READSYSTEM\"}";
            ApiStub apiNibeStub    = new ApiStub();

            apiNibeStub.Post(
                "/oauth/token",
                (request, args) =>
            {
                return(fileContentExp);
            }
                );

            apiNibeStub.Start();

            AppKeyConfig configs = new AppKeyConfig();

            configs.NibeHost = apiNibeStub.Address;

            ClimateItem item = nibeUnit.CurrentReading(configs);

            Assert.Null(item);
        }
Пример #5
0
        public void ClimateController_GetById_WithNibe_As_Source_Calls_CurrentReading_ThatReturnsNull()
        {
            ClimateItem item = null;

            // nibeMock.Setup<ClimateItem>(x => x.CurrentReading(It.IsAny<AppKeyConfig>())).Returns(item);
            AppKeyConfig ac = new AppKeyConfig()
            {
                UserName            = Configuration["NETATMO_USERNAME"],
                Password            = Configuration["NETATMO_PASSWORD"],
                NetatmoClientId     = Configuration["NETATMO_CLIENTID"],
                NetatmoClientSecret = Configuration["NETATMO_CLIENTSECRET"],
                NibeClientId        = Configuration["NIBE_CLIENTID"],
                NibeClientSecret    = Configuration["NIBE_CLIENTSECRET"],
                NibeRedirectURI     = Configuration["NIBE_REDIRECTURL"],
                NibeHost            = Configuration["NIBE_HOST"],
                NetatmoHost         = Configuration["NETATMO_HOST"],
                BuildVersion        = Configuration["BUILD_VERSION"]
            };

            nibeMock.Setup <ClimateItem>(x => x.CurrentReading(ac)).Returns(item);

            IActionResult res = _climateController.GetBySource("Nibe");

            nibeMock.Verify(x => x.CurrentReading(It.IsAny <AppKeyConfig>()), Times.AtLeastOnce());

            Assert.IsType <RedirectResult>(res);
        }
Пример #6
0
 public ClimateItemTest()
 {
     _climateItem              = new ClimateItem();
     _climateItem.TimeStamp    = timeStamp;
     _climateItem.OutdoorValue = "4.5";
     _climateItem.IndoorValue  = "22.3";
 }
Пример #7
0
        public void ClimateItem_MeanValueWithDecimalsIsFormatedCorrectly()
        {
            string left        = "10.5";
            string right       = "12.3";
            string resultValue = ClimateItem.Meanvalue(left, right);

            Assert.Equal("11.4", resultValue);
        }
Пример #8
0
        public void ClimateItem_MeanValueResultIsZero()
        {
            string left        = "-6.5";
            string right       = "6.5";
            string resultValue = ClimateItem.Meanvalue(left, right);

            Assert.Equal("0.0", resultValue);
        }
Пример #9
0
        public void ClimateItem_MeanValueRightIsNull()
        {
            string left        = "22.1";
            string right       = null;
            string resultValue = ClimateItem.Meanvalue(left, right);

            Assert.Equal("22.1", resultValue);
        }
Пример #10
0
        public void ClimateItem_MeanValueLeftAndRightAreNull()
        {
            string left  = null;
            string right = null;
            // Throws Exception
            Exception ex = Assert.Throws <ArgumentNullException>(() => ClimateItem.Meanvalue(left, right));

            Assert.True(ex.Message.StartsWith("Value cannot be null"));
        }
Пример #11
0
        public void ClimateController_GetById_With_Netatmo_As_Source_Calls_CurrentReading_WithConfigs()
        {
            ClimateItem item = new ClimateItem();

            netatmoMock.Setup <ClimateItem>(x => x.CurrentReading(It.IsAny <AppKeyConfig>())).Returns(item);


            IActionResult res = _climateController.GetBySource("Netatmo");

            netatmoMock.Verify(x => x.CurrentReading(It.IsAny <AppKeyConfig>()), Times.AtLeastOnce());
        }
Пример #12
0
        public void ClimateController_GetById_With_Netatmo_As_Source_Calls_CurrentReading_IsNullReturnNocontentResult()
        {
            ClimateItem item = null;

            netatmoMock.Setup <ClimateItem>(x => x.CurrentReading(It.IsAny <AppKeyConfig>())).Returns(item);


            IActionResult res = _climateController.GetBySource("Netatmo");

            Assert.IsType <Microsoft.AspNetCore.Mvc.NoContentResult>(res);
            netatmoMock.Verify(x => x.CurrentReading(It.IsAny <AppKeyConfig>()), Times.AtLeastOnce());
        }
Пример #13
0
        public void ClimateItem_MeanOutdoorValueTest()
        {
            ClimateItem left = new ClimateItem()
            {
                IndoorValue = "-10.0", OutdoorValue = "20.0"
            };
            ClimateItem right = new ClimateItem()
            {
                IndoorValue = "-12.0", OutdoorValue = "22.0"
            };
            string resultValue = ClimateItem.MeanOutdoorValue(left, right);

            Assert.Equal("21.0", resultValue);
        }
Пример #14
0
        public void ClimateItem_MeanValueTest()
        {
            ClimateItem left = new ClimateItem()
            {
                IndoorValue = "18.5", OutdoorValue = "-20.0"
            };
            ClimateItem right = new ClimateItem()
            {
                IndoorValue = "19.3", OutdoorValue = "22.0"
            };
            ClimateItem resultValue = ClimateItem.ClimateMeanValues(left, right);

            Assert.Equal("18.9", resultValue.IndoorValue);
            Assert.Equal("1.0", resultValue.OutdoorValue);
        }
Пример #15
0
        public void NetatmoUnitCurrentReading()
        {
            string responseText = "{ \"access_token\":\"544cf4071c7759831d94cdf9|fcb30814afbffd0e39381e74fe38a59a\",\"refresh_token\":\"544cf4071c7759831d94cdf9|2b2c270c1208e8f67d3bd3891e395e1a\",\"scope\":[\"read_station\"],\"expires_in\":10800,\"expire_in\":10800}";
            //string deviceResponse = File.ReadAllText(fileDir + "netatmodeviceresponse.json");
            string  deviceResponse = netatmoData;
            ApiStub apiNetatmoStub = new ApiStub();

            apiNetatmoStub.Post(
                "/oauth2/token",
                (request, args) =>
            {
                return(responseText);
            }
                );
            apiNetatmoStub.Get(
                "/api/devicelist",
                (request, response) =>
            {
                return(deviceResponse);
            }
                );
            string responseReading = "{\"body\":[{\"beg_time\":1481372100,\"value\":[[0.7]]}],\"status\":\"ok\",\"time_exec\":0.034345865249634,\"time_server\":1481371970}";


            apiNetatmoStub.Get(
                "/api/getmeasure",
                (request, args) =>
            {
                return(responseReading);
            }
                );

            apiNetatmoStub.Start();

            AppKeyConfig configs = new AppKeyConfig();

            configs.NetatmoHost = apiNetatmoStub.Address;

            ClimateItem actualReading = netatmoUnit.CurrentReading(configs);

            Assert.NotNull(actualReading);
            Assert.Equal("0.7", actualReading.IndoorValue);
            Assert.Equal("0.7", actualReading.OutdoorValue);
            Assert.NotNull(actualReading.TimeStamp);

            Assert.True(DateTime.Now >= actualReading.TimeStamp);
        }
Пример #16
0
        public void NetatmoUnitCurrentReadingDataCouldNoBeRead()
        {
            string responseText = "{ \"access_token\":\"544cf4071c7759831d94cdf9|fcb30814afbffd0e39381e74fe38a59a\",\"refresh_token\":\"544cf4071c7759831d94cdf9|2b2c270c1208e8f67d3bd3891e395e1a\",\"scope\":[\"read_station\"],\"expires_in\":10800,\"expire_in\":10800}";
            //string deviceResponse = File.ReadAllText(fileDir + "netatmodeviceresponse.json");
            string  deviceResponse = netatmoData;
            ApiStub apiNetatmoStub = new ApiStub();

            apiNetatmoStub.Post(
                "/oauth2/token",
                (request, args) =>
            {
                return(responseText);
            }
                );
            apiNetatmoStub.Get(
                "/api/devicelist",
                (request, response) =>
            {
                return(deviceResponse);
            }
                );
            string responseReading = null;


            apiNetatmoStub.Get(
                "/api/getmeasure",
                (request, args) =>
            {
                return(responseReading);
            }
                );

            apiNetatmoStub.Start();

            AppKeyConfig configs = new AppKeyConfig();

            configs.NetatmoHost = apiNetatmoStub.Address;

            ClimateItem actualReading = netatmoUnit.CurrentReading(configs);

            Assert.Null(actualReading);
        }
Пример #17
0
        public ClimateItem CurrentReading(AppKeyConfig AppConfigs)
        {
            login(AppConfigs);
            setDeviceAndModuleID(AppConfigs);
            ClimateItem reading = new ClimateItem();

            try
            {
                reading.IndoorValue  = this.inDoorTemperature(AppConfigs);
                reading.OutdoorValue = this.outDoorTemperature(AppConfigs);
                reading.TimeStamp    = DateTime.Now;
                return(reading);
            }

            // Somehow we could not get the data.
            catch
            {
                return(null);
            }
        }
Пример #18
0
        public void ClimateController_GetById_WithReading_As_Source_Calls_CurrentReading_WithConfigs()
        {
            ClimateItem nibeitem = new ClimateItem()
            {
                IndoorValue = "20", OutdoorValue = "10"
            };

            nibeMock.Setup <ClimateItem>(x => x.CurrentReading(It.IsAny <AppKeyConfig>())).Returns(nibeitem);

            ClimateItem netatmoitem = new ClimateItem()
            {
                IndoorValue = "20", OutdoorValue = "10"
            };

            netatmoMock.Setup <ClimateItem>(x => x.CurrentReading(It.IsAny <AppKeyConfig>())).Returns(netatmoitem);

            IActionResult res = _climateController.GetBySource("Reading");

            nibeMock.Verify(x => x.CurrentReading(It.IsAny <AppKeyConfig>()), Times.AtLeastOnce());
            netatmoMock.Verify(x => x.CurrentReading(It.IsAny <AppKeyConfig>()), Times.AtLeastOnce());
        }
Пример #19
0
        public void NibeUnit_GetReadingWithAccessCode_Is_NotSuccessful()
        {
            //string reading = "[{\"parameterId\":40004,\"name\":\"outdoor_temperature\",\"title\":\"outdoor temp.\",\"designation\":\"BT1\",\"unit\":\"°C\",\"displayValue\":\"2.7°C\",\"rawValue\":27},{\"parameterId\":40033,\"name\":\"indoor_temperature\",\"title\":\"room temperature\",\"designation\":\"BT50\",\"unit\":\"°C\",\"displayValue\":\"22.7°C\",\"rawValue\":227}]";
            ApiStub apiNibeStub = new ApiStub();

            apiNibeStub.Request(HttpMethod.Post).
            IfRoute("/oauth/token").
            Response((request, args) =>
            {
                return("{ Error, Not Authorized}");
            }
                     ).StatusCode(StatusCodes.Status401Unauthorized);
            apiNibeStub.Start();

            AppKeyConfig configs = new AppKeyConfig();

            configs.NibeHost = apiNibeStub.Address;

            ClimateItem item = nibeUnit.GetReadingWithAccessCode("12345", configs);

            Assert.Null(item);
        }
Пример #20
0
        public void NibeUnit_CurrentReading_Is_SuccessfulWithAccessCode()
        {
            string  reading     = "[{\"parameterId\":40004,\"name\":\"outdoor_temperature\",\"title\":\"outdoor temp.\",\"designation\":\"BT1\",\"unit\":\"°C\",\"displayValue\":\"2.7°C\",\"rawValue\":27},{\"parameterId\":40033,\"name\":\"indoor_temperature\",\"title\":\"room temperature\",\"designation\":\"BT50\",\"unit\":\"°C\",\"displayValue\":\"22.7°C\",\"rawValue\":227}]";
            ApiStub apiNibeStub = new ApiStub();

            apiNibeStub.Get(
                "/api/v1/systems/27401/parameters",
                (request, args) =>
            {
                return(reading);
            }
                );
            apiNibeStub.Start();

            AppKeyConfig configs = new AppKeyConfig();

            configs.NibeHost = apiNibeStub.Address;

            ClimateItem item = nibeUnit.CurrentReading(configs);

            Assert.NotNull(item);
            Assert.Equal(item.IndoorValue, "22.7");
            Assert.Equal(item.OutdoorValue, "2.7");
        }
 public ClimateItemTest()
 {
     _climateItem      = new ClimateItem();
     _climateItem.Name = "Namn";
     _climateItem.Key  = "Nyckel";
 }
        public IActionResult GetBySource(string source)
        {
            _logger.LogInformation("The source is {Source}", source);

            ClimateItem item = null;

            if (source.Equals("Nibe"))
            {
                // Read data from Nibe, if reading works we get data, if not we get null and try to do a login
                item = nibe.CurrentReading(AppConfigs);

                if (item == null)
                {
                    _logger.LogInformation("Logging in to Nibe");
                    var uri = new UriBuilder(AppConfigs.NibeHost)
                    {
                        Path  = $"/oauth/authorize",
                        Query = "response_type=code&client_id=" + AppConfigs.NibeClientId + "&scope=READSYSTEM&redirect_uri=" + AppConfigs.NibeRedirectURI + "&state=12345"
                    }.Uri;
                    _logger.LogInformation("Connecting to {uri}", uri);

                    return(Redirect(uri.AbsoluteUri));
                }
            }

            if (source.Equals("Netatmo"))
            {
                item = netatmo.CurrentReading(AppConfigs);
            }

            if (source.Equals("Reading"))
            {
                ClimateItem netatmoItem = netatmo.CurrentReading(AppConfigs);

                ClimateItem nibeItem = null;
                nibeItem = nibe.CurrentReading(AppConfigs);
                if (nibeItem == null)
                {
                    var uri = new UriBuilder(AppConfigs.NibeHost)
                    {
                        Path  = $"/oauth/authorize",
                        Query = "response_type=code&client_id=" + AppConfigs.NibeClientId + "&scope=READSYSTEM&redirect_uri=" + AppConfigs.NibeRedirectURI + "&state=12345"
                    }.Uri;

                    _logger.LogInformation("redirecting to {redirect}", uri.AbsoluteUri);
                    return(Redirect(uri.AbsoluteUri));
                }
                item = ClimateItem.ClimateMeanValues(netatmoItem, nibeItem);
            }
            if (source.Equals("Ping"))
            {
                item = new ClimateItem()
                {
                    IndoorValue = "20.5", OutdoorValue = "11.1", TimeStamp = DateTime.Now
                };
                _logger.LogInformation("Ping returning {item}", item.ToString());
            }
            if (source.Equals("Version"))
            {
                item = new ClimateItem()
                {
                    IndoorValue = AppConfigs.BuildVersion, OutdoorValue = AppConfigs.BuildVersion, TimeStamp = DateTime.Now
                };
                _logger.LogInformation("Version returning {item}", AppConfigs.BuildVersion);
            }

            if (item == null)
            {
                return(new Microsoft.AspNetCore.Mvc.NoContentResult());
            }
            else
            {
                return(Json(item));
            }
        }