Пример #1
0
        private static void RandomizeMotionValue(object state)
        {
            bool       motionDetected = Convert.ToBoolean(Random.Next(0, 2));
            SensorInfo sensorInfo     = SensorInfosByDataType[MotionDataType];

            sensorInfo.UpdateCurrentValue(motionDetected);
        }
Пример #2
0
        private static Task <MethodResponse> SetAmbientLight(MethodRequest methodRequest, object userContext)
        {
            var data = Encoding.UTF8.GetString(methodRequest.Data);

            // Check the payload is a double value
            if (double.TryParse(data, out double newAmbientLight))
            {
                SensorInfo sensorInfo = SensorInfosByDataType[LightDataType];
                sensorInfo.UpdateCurrentValue(newAmbientLight);
                Console.WriteLine("Current Ambient Lighting set to {0} ({0:P})", newAmbientLight);
                Console.ResetColor();

                // Acknowlege the direct method call with a 200 success message
                string result = "{\"result\":\"Executed direct method: " + methodRequest.Name + "\"}";
                return(Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(result), 200)));
            }
            else
            {
                // Acknowlege the direct method call with a 400 error message
                string result = "{\"result\":\"Invalid parameter\"}";
                return(Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(result), 400)));
            }
        }
Пример #3
0
        private static Task <MethodResponse> SetDesiredTemperature(MethodRequest methodRequest, object userContext)
        {
            var data = Encoding.UTF8.GetString(methodRequest.Data);

            // Check the payload is a single integer value
            if (int.TryParse(data, out int newDesiredTemperature))
            {
                SensorInfo sensorInfo = SensorInfosByDataType[TemperatureDataType];
                sensorInfo.UpdateCurrentValue(newDesiredTemperature);
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Current Temperature set to {0}°", data);
                Console.ResetColor();

                // Acknowlege the direct method call with a 200 success message
                string result = "{\"result\":\"Executed direct method: " + methodRequest.Name + "\"}";
                return(Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(result), 200)));
            }
            else
            {
                // Acknowlege the direct method call with a 400 error message
                string result = "{\"result\":\"Invalid parameter\"}";
                return(Task.FromResult(new MethodResponse(Encoding.UTF8.GetBytes(result), 400)));
            }
        }