/// <summary>
        /// Flash a known app to a device
        /// A return of true only means it was sent to the device, not that flash is successful
        /// </summary>
        /// <param name="app">Known app name by Particle Cloud</param>
        /// <param name="monitorStatus">Whether or not to monitor status</param>
        /// <returns>Returns true if known app is sent to device</returns>
        public async Task <bool> FlashKnownAppAsync(string app, bool monitorStatus = false)
        {
            if (string.IsNullOrWhiteSpace(app))
            {
                throw new ArgumentNullException(nameof(app));
            }

            var data = new Dictionary <string, string>
            {
                { "app", app }
            };

            try
            {
                IsFlashing = true;
                State      = ParticleDeviceState.Flashing;
                if (monitorStatus)
                {
                    MonitorForOnlineEvent();
                }
                var responseContent = await particleCloud.PutDataAsync($"{ParticleCloud.ParticleApiVersion}/{ParticleCloud.ParticleApiPathDevices}/{Id}", data);

                return(true);
            }
            catch
            {
                IsFlashing = false;
                State      = ParticleDeviceState.Unknown;
                return(false);
            }
        }
Ejemplo n.º 2
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="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> FlashBinaryAsync(Stream firmwareStream, string filename)
        {
            if (firmwareStream == null)
            {
                throw new ArgumentNullException(nameof(firmwareStream));
            }

            State      = ParticleDeviceState.Flashing;
            isFlashing = true;

            using (IHttpContent file = new HttpStreamContent(firmwareStream.AsInputStream()))
            {
                file.Headers.ContentType = HttpMediaTypeHeaderValue.Parse("application/octet-stream");

                var content = new HttpMultipartFormDataContent();
                content.Add(new HttpStringContent("binary"), "file_type");
                content.Add(file, "file", filename);

                try
                {
                    onlineEventListenerID = await SubscribeToDeviceEventsWithPrefixAsync(CheckForOnlineEvent);

                    var responseContent = await particleCloud.PutDataAsync($"{ParticleCloud.ParticleApiVersion}/{ParticleCloud.ParticleApiPathDevices}/{Id}", content);

                    isFlashing = false;
                    return(true);
                }
                catch
                {
                    isFlashing = false;
                    State      = ParticleDeviceState.Unknown;
                    return(false);
                }
            }
        }