Ejemplo n.º 1
0
        async static Task Main(string[] args)
        {
            // Mqtt Client Options
            Console.WriteLine("Hello World!");

            var deviceMqttClientOptions = new DeviceClientOptionsBuilder()
                                          .WithClientId("/bay-uwt_015d7f1c-5c56-4a1d-ae8a-18b11cfd5b9f")
                                          .WithCSEId("/bay-uwt_015d7f1c-5c56-4a1d-ae8a-18b11cfd5b9f")
                                          .WithMqttOptions("mqtt.kocdigital.com", 1883, 120, "bay1RI", "WL6#q7Q7")
                                          .Build();

            // Create Enrollment Service to Enroll Device to the System
            IEnrollmentServiceFactory _enrollmentServiceFactory = new EnrollmentServiceFactory();
            var _enrollmentService = _enrollmentServiceFactory.CreateEnrollmentService(deviceMqttClientOptions);

            // Enroll Device to the System
            var deviceEnrollmentResult = await _enrollmentService.EnrollDeviceAsync("DeviceName");

            Console.WriteLine("Device ID: " + deviceEnrollmentResult.DeviceId + " Device Name: " + deviceEnrollmentResult.DeviceName);
        }
Ejemplo n.º 2
0
        async static Task Main(string[] args)
        {
            // Mqtt Client Options
            var deviceMqttClientOptions = new DeviceClientOptionsBuilder()
                                          .WithClientId("CAEb473a19a-cb16-4065-88c9-eb1d02abe80d")
                                          .WithCSEId("/bay-uwt_015d7f1c-5c56-4a1d-ae8a-18b11cfd5b9f")
                                          .WithMqttOptions("mqtt.kocdigital.com", 1883, 120, "bay1RI", "WL6#q7Q7")
                                          .Build();

            IEnrollmentServiceFactory       _enrollmentServiceFactory       = new EnrollmentServiceFactory();
            IDeviceManagementServiceFactory _deviceManagementServiceFactory = new DeviceManagementServiceFactory();
            IDeviceServiceFactory           _deviceServiceFactory           = new DeviceServiceFactory();

            // Create Enrollment Service to Enroll Device
            var enrollmentService = _enrollmentServiceFactory.CreateEnrollmentService(deviceMqttClientOptions);

            // Create Device Management Service to Use Device Management Objects
            var deviceManagementService = _deviceManagementServiceFactory.CreateDeviceManagementService(deviceMqttClientOptions);

            // Create Device Service to Use Device Functionalities
            var deviceService = _deviceServiceFactory.CreateDeviceService(deviceMqttClientOptions);

            //// It creates a device on the platform.
            await enrollmentService.EnrollDeviceAsync("RaspberryDevice");

            // Here related resources and properties are loaded to device management objects
            await deviceManagementService.ConnectToPlatformAsync();

            // Here related resources and properties are loaded to device service objects
            await deviceService.ConnectToPlatformAsync();

            // Sensor Creation
            // Parameters: Sensor Name and isBidirectional
            var sensor1 = await deviceService.CreateSensorAsync("TemperatureSensor", true);

            var sensor2 = await deviceService.CreateSensorAsync("HumidtySensor", true);

            var existedSensors = deviceService.GetSensors();

            // Notification event when any sensor value change request is sent.
            deviceService.UseSensorValueChangeRequestHandler(async e =>
            {
                Console.WriteLine(e.SensorId);
                Console.WriteLine(e.Value);
            });

            if (existedSensors.Count > 0)
            { // It sends the request to platform to save sensor data.
                foreach (var existedSensor in existedSensors)
                {
                    await deviceService.PushSensorDataToPlatformAsync(existedSensor.Id, "OFF", "unit off", "data off");

                    await deviceService.PushSensorDataToPlatformAsync(existedSensor.Id, "ON", "unit on", "data off");
                }
            }

            var battery = new DeviceBatteryCreateData {
                BatteryLevel  = 10,
                BatteryStatus = BatteryStatus.LowBattery,
                Name          = "DeviceBattery"
            };
            var batteryBackup = new DeviceBatteryCreateData {
                BatteryLevel  = 100,
                BatteryStatus = BatteryStatus.Normal,
                Name          = "DeviceSecondBattery"
            };

            var reboot = new DeviceRebootCreateData {
                Name = "DeviceReboot/FactoryResetFunction"
            };

            // It sends a request to save battery data on platform.
            var batteryResult = await deviceManagementService.CreateBatteryAsync(battery);

            var batteryBackupResult = await deviceManagementService.CreateBatteryAsync(batteryBackup);

            //var rebootResult = await deviceManagementService.CreateRebootAsync(reboot);
            var batteries = await deviceManagementService.GetBatteriesAsync();

            //var reboots = await deviceManagementService.GetRebootAsync();

            // Notification event when reboot update request is handled to process.
            deviceManagementService.UseDeviceManagementRebootFunctionHandler(async e => {
                Console.WriteLine(e.IsFactoryReset);
                Console.WriteLine(e.Reboot);
            });


            Console.ReadLine();
        }