Пример #1
0
        private const string LED_CODE = "LED"; // LED equipment code

        #endregion Fields

        #region Methods

        private static void HandleNotifications(IClientService service, Device device, CancellationToken token)
        {
            try
            {
                var timestamp = DateTime.UtcNow;
                while (true)
                {
                    // poll notification from the server
                    var notifications = service.PollNotifications(device.Id.Value, timestamp, token);
                    if (notifications == null)
                        continue;

                    // display information about received notification
                    foreach (var notification in notifications.Where(n =>
                        n.Name == "equipment" && n.GetParameter("equipment") == LED_CODE))
                    {
                        var message = "Device sent LED state change notification, new state: {0}";
                        Console.WriteLine(string.Format(message, notification.GetParameter("state")));
                    }

                    // update last received notification timestamp
                    timestamp = notifications.Max(n => n.Timestamp.Value);
                }
            }
            catch (OperationCanceledException)
            {
                return;
            }
        }
        /// <summary>
        /// Updates device on behalf of device.
        /// </summary>
        /// <param name="device">The <see cref="Device"/> object.</param>
        public async Task UpdateDevice(Device device)
        {
            if (device == null)
                throw new ArgumentNullException("device");
            if (string.IsNullOrEmpty(device.Id))
                throw new ArgumentException("Device ID is null or empty", "device.Id");
            //if (string.IsNullOrEmpty(device.Key))
            //    throw new ArgumentException("Device key is null or empty", "device.Key");

            //if (device.Network != null)
            //{
            //    if (string.IsNullOrEmpty(device.Network.Name))
            //        throw new ArgumentException("Device network name is null or empty!", "device.Network.Name");
            //}
            //if (device.DeviceClass != null)
            //{
            //    if (string.IsNullOrEmpty(device.DeviceClass.Name))
            //        throw new ArgumentException("Device class name is null or empty!", "device.DeviceClass.Name");
            //    if (string.IsNullOrEmpty(device.DeviceClass.Version))
            //        throw new ArgumentException("Device class version is null or empty!", "device.DeviceClass.Version");
            //}

            await _restClient.Put(string.Format("device/{0}", device.Id), device);
        }
Пример #3
0
 async Task LoadDevice()
 {
     LoadingItems++;
     try
     {
         device = await ClientService.Current.GetDeviceAsync(deviceId);
         DefaultViewModel["Device"] = device;
     }
     catch (Exception ex)
     {
         new MessageDialog(ex.Message, "Error").ShowAsync();
     }
     LoadingItems--;
 }