Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Helper function to sort ParticleDevices by online state and than alphabetically
        /// </summary>
        /// <param name="device1">First device to compare</param>
        /// <param name="device2">Second device to compare</param>
        /// <returns>Comparison result</returns>
        internal static int SortCompare(ParticleDevice device1, ParticleDevice device2)
        {
            int result = device2.Connected.CompareTo(device1.Connected);

            if (result == 0)
            {
                result = device1.Name.CompareTo(device2.Name);
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets a device
        /// </summary>
        /// <param name="deviceId">Device ID</param>
        /// <returns>Returns a ParticleDevice</returns>
        public async Task <ParticleDevice> GetDeviceAsync(string deviceId)
        {
            try
            {
                ParticleDevice device = new ParticleDevice(deviceId, this);
                await device.RefreshAsync();

                return(device);
            }
            catch
            {
                return(null);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a new long running task to listen for events on a specific users device
 /// </summary>
 /// <param name="eventHandler">ParticleEventHandler to call when new event arrives</param>
 /// <param name="device">ParticleDevice to limit event stream to</param>
 /// <param name="eventNamePrefix">Prefix to monitor on event stream</param>
 /// <returns>Returns GUID reference to long running event task</returns>
 public async Task <Guid> SubscribeToDeviceEventsWithPrefixAsync(ParticleEventHandler eventHandler, ParticleDevice device, string eventNamePrefix = "")
 {
     return(await SubscribeToDeviceEventsWithPrefixAsync(eventHandler, device.Id, eventNamePrefix));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Flash a compiled firmware to a device
 /// A return of true only means it was sent to the device, not that flash is successful
 /// </summary>
 /// <param name="particeDevice">ParticleDevice</param>
 /// <param name="firmwareStream">Stream of compiled binary</param>
 /// <param name="filename">Filename of compiled binary</param>
 /// <returns>Returns true if binary is sent to device</returns>
 public async Task <bool> DeviceFlashBinaryAsync(ParticleDevice particeDevice, Stream firmwareStream, string filename)
 {
     return(await DeviceFlashBinaryAsync(particeDevice.Id, firmwareStream, filename));
 }