/// <summary> /// Gets a list of all users devices /// </summary> /// <returns>Returns a List of ParticleDevices</returns> public async Task <List <ParticleDevice> > GetDevicesAsync() { try { var jsonSerializer = new JsonSerializer(); jsonSerializer.DateTimeZoneHandling = DateTimeZoneHandling.Local; var responseContent = await GetDataAsync($"{ParticleApiVersion}/{ParticleApiPathDevices}"); var result = JToken.Parse(responseContent); List <ParticleDevice> devices = new List <ParticleDevice>(); foreach (JObject device in (JArray)result) { ParticleDeviceResponse deviceState = device.ToObject <ParticleDeviceResponse>(jsonSerializer); ParticleDevice particleDevice = new ParticleDevice(deviceState, this); devices.Add(particleDevice); } foreach (ParticleDevice device in devices) { if (device.Connected) { await device.RefreshAsync(); } } return(new List <ParticleDevice>(devices)); } catch { return(null); } }
/// <summary> /// Refreshes a devices properties with current information /// </summary> /// <returns>Returns true if the device is updated</returns> public async Task <bool> RefreshAsync() { try { var jsonSerializerSettings = new JsonSerializerSettings() { DateTimeZoneHandling = DateTimeZoneHandling.Local }; var responseContent = await particleCloud.GetDataAsync($"{ParticleCloud.ParticleApiVersion}/{ParticleCloud.ParticleApiPathDevices}/{Id}"); ParticleDeviceResponse deviceState = JsonConvert.DeserializeObject <ParticleDeviceResponse>(responseContent, jsonSerializerSettings); SetDeviceState(deviceState); if (Cellular) { await UpdateMonthlyUssageAsync(); } return(true); } catch { return(false); } }
/// <summary> /// Create a new instance of a ParticleDevice from a ParticleDeviceResponse /// </summary> /// <param name="deviceState">ParticleDeviceResponse from Particle Cloud</param> /// <param name="particleCloud">Authorized ParticleCloud</param> public ParticleDevice(ParticleDeviceResponse deviceState, ParticleCloud particleCloud) { this.deviceState = deviceState; this.particleCloud = particleCloud; UpdateState(); }
/// <summary> /// Create a new instance of a ParticleDevice with only an ID /// </summary> /// <param name="id">Device ID</param> /// <param name="particleCloud">Authorized ParticleCloud</param> public ParticleDevice(string deviceId, ParticleCloud particleCloud) { this.deviceState = new ParticleDeviceResponse(); this.deviceState.Id = deviceId; this.particleCloud = particleCloud; UpdateState(); }
/// <summary> /// Update this device with new values from a ParticleDeviceResponse /// </summary> /// <param name="deviceState">Updated ParticleDeviceResponse</param> private void SetDeviceState(ParticleDeviceResponse deviceState) { bool updateState = false; ParticleDeviceResponse oldDeviceState = this.deviceState; this.deviceState = deviceState; var properties = deviceState.GetType().GetRuntimeProperties(); foreach (PropertyInfo property in properties) { bool updateStateField = property.Name == "Connected" || property.Name == "Functions"; var valueA = property.GetValue(deviceState); var valueB = property.GetValue(oldDeviceState); if (valueA == null || valueB == null) { if (valueA != valueB) { OnPropertyChanged(property.Name); if (updateStateField) { updateState = true; } } } else if (!valueA.Equals(valueB)) { OnPropertyChanged(property.Name); if (updateStateField) { updateState = true; } } } if (updateState) { UpdateState(); } }