Пример #1
0
        protected override async Task <bool> OnLeaveStateProc(DMTaskState state, ITransport transport)
        {
            var report = new TwinCollection();

            switch (state)
            {
            case DMTaskState.DM_IDLE:
                report = null;      // No report for leaving idle state
                break;

            case DMTaskState.CU_PENDING:
                report = null;      // No report for leaving pending state
                break;

            case DMTaskState.CU_DOWNLOADING:
                report.Set(LogPath, _logBuilder.Append($"Downloaded({(int)_watch.Elapsed.TotalSeconds}s)"));
                break;

            case DMTaskState.CU_APPLYING:
                report.Set(LogPath, _logBuilder.Append($"Applied({(int)_watch.Elapsed.TotalSeconds}s)"));
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(state));
            }

            if (report != null)
            {
                await transport.UpdateReportedPropertiesAsync(report);
            }

            return(true);
        }
Пример #2
0
        private void BuildReport(TwinCollection report, string stepName, string status)
        {
            report.Set(FormattableString.Invariant($"{ReportPrefix}.{stepName}.{Status}"), status);
            report.Set(FormattableString.Invariant($"{ReportPrefix}.{stepName}.{LastUpdate}"), DateTime.UtcNow.ToString(CultureInfo.CurrentCulture));
            report.Set(FormattableString.Invariant($"{ReportPrefix}.{stepName}.{Duration}"), (int)_stepWatch.Elapsed.TotalSeconds);

            if (status == Failed)
            {
                BuildReport(report, Failed);
            }
            else
            {
                BuildReport(report, Running);
            }
        }
Пример #3
0
        private void BuildReport(TwinCollection report, string stepName, string status)
        {
            report.Set($"{ReportPrefix}.{stepName}.{Status}", status);
            report.Set($"{ReportPrefix}.{stepName}.{LastUpdate}", DateTime.UtcNow.ToString());
            report.Set($"{ReportPrefix}.{stepName}.{Duration}", (int)_stepWatch.Elapsed.TotalSeconds);

            if (status == Failed)
            {
                BuildReport(report, Failed);
            }
            else
            {
                BuildReport(report, Running);
            }
        }
Пример #4
0
        protected void AddConfigs(TwinCollection patch)
        {
            var telemetryWithInterval = _telemetryController as ITelemetryWithInterval;

            if (telemetryWithInterval != null)
            {
                patch.Set(TelemetryIntervalPropertyName, telemetryWithInterval.TelemetryIntervalInSeconds);
            }

            var telemetryWithTemperatureMeanValue = _telemetryController as ITelemetryWithTemperatureMeanValue;

            if (telemetryWithTemperatureMeanValue != null)
            {
                patch.Set(TemperatureMeanValuePropertyName, telemetryWithTemperatureMeanValue.TemperatureMeanValue);
            }

            patch.Set(StartupTimePropertyName, DateTime.UtcNow.ToString(CultureInfo.CurrentCulture));
        }
Пример #5
0
        protected override async Task <bool> OnEnterStateProc(DMTaskState state, ITransport transport)
        {
            bool   succeed = true;
            var    report  = new TwinCollection();
            string status;

            switch (state)
            {
            case DMTaskState.CU_PENDING:
                report = null;      // No report for entering pending state
                break;

            case DMTaskState.CU_DOWNLOADING:
                _watch  = Stopwatch.StartNew();
                succeed = Version != "downloadFail";
                status  = succeed ? "Downloading" : "Dowload failed";
                report.Set(LogPath, _logBuilder.Append(status, succeed));
                break;

            case DMTaskState.CU_APPLYING:
                _watch  = Stopwatch.StartNew();
                succeed = Version != "applyFail";
                status  = succeed ? "Applying" : "Apply failed";
                report.Set(LogPath, _logBuilder.Append(status, succeed));
                break;

            case DMTaskState.DM_IDLE:
                report.Set(DeviceBase.ConfigurationVersionPropertyName, Version);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(state));
            }

            if (report != null)
            {
                await transport.UpdateReportedPropertiesAsync(report);
            }

            return(succeed);
        }
        protected async Task OnTemperatureMeanValueUpdate(object value)
        {
            // When set the temperature we clean up the firmware update status for demo purpose.
            var clear = new TwinCollection();

            clear.Set(FirmwareUpdate.ReportPrefix, null);
            await Transport.UpdateReportedPropertiesAsync(clear);

            var telemetry = _telemetryController as ITelemetryWithTemperatureMeanValue;

            if (telemetry != null)
            {
                telemetry.TemperatureMeanValue = Convert.ToDouble(value);
            }

            await UpdateReportedTemperatureMeanValue();
        }
        private void setTwinProperties(PropertyViewModel twin, TwinCollection prop, string key)
        {
            if (twin.IsDeleted)
            {
                prop.Set(key, null);
            }
            else
            {
                switch (twin.DataType)
                {
                case Infrastructure.Models.TwinDataType.String:
                    string valueString = twin.Value.Value.ToString();
                    prop.Set(key, valueString);
                    break;

                case Infrastructure.Models.TwinDataType.Number:
                    int   valueInt;
                    float valuefloat;
                    if (int.TryParse(twin.Value.Value.ToString(), out valueInt))
                    {
                        prop.Set(key, valueInt);
                    }
                    else if (float.TryParse(twin.Value.Value.ToString(), out valuefloat))
                    {
                        prop.Set(key, valuefloat);
                    }
                    else
                    {
                        prop.Set(key, twin.Value.Value as string);
                    }
                    break;

                case Infrastructure.Models.TwinDataType.Boolean:
                    bool valueBool;
                    if (bool.TryParse(twin.Value.Value.ToString(), out valueBool))
                    {
                        prop.Set(key, valueBool);
                    }
                    else
                    {
                        prop.Set(key, twin.Value.Value as string);
                    }
                    break;
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Cross synchonize DeviceProperties and ReportedProperties
        /// </summary>
        /// <returns></returns>
        protected void CrossSyncProperties(TwinCollection patch, TwinCollection reported, bool regenerate)
        {
            var devicePropertiesType = DeviceProperties.GetType();
            var reportedPairs        = reported.AsEnumerableFlatten().ToDictionary(pair => pair.Key, pair => pair.Value);

            if (!regenerate)
            {
                // Overwrite regenerated DeviceProperties by current ReportedProperties
                foreach (var pair in reportedPairs)
                {
                    string devicePropertyName = _propertyMapping.SingleOrDefault(p => p.Value == pair.Key).Key;
                    if (string.IsNullOrWhiteSpace(devicePropertyName))
                    {
                        continue;
                    }

                    try
                    {
                        DeviceProperties.SetProperty(devicePropertyName, pair.Value.Value);
                    }
                    catch
                    {
                        // Ignore any failure while overwriting the DeviceProperties
                    }
                }
            }

            // Add missing DeviceProperties to ReportedProperties
            foreach (var property in devicePropertiesType.GetProperties())
            {
                string reportedName;
                if (!_propertyMapping.TryGetValue(property.Name, out reportedName))
                {
                    continue;
                }

                var value = property.GetValue(DeviceProperties);
                if (regenerate || value != null && !reportedPairs.ContainsKey(reportedName))
                {
                    patch.Set(reportedName, value);
                }
            }
        }
Пример #9
0
        protected override async Task <bool> OnEnterStateProc(DMTaskState state, ITransport transport)
        {
            bool succeed = true;
            var  report  = new TwinCollection();

            switch (state)
            {
            case DMTaskState.FU_PENDING:
                var clear = new TwinCollection();
                clear.Set(ReportPrefix, null);
                await transport.UpdateReportedPropertiesAsync(clear);

                _masterWatch = Stopwatch.StartNew();
                _stepWatch   = Stopwatch.StartNew();
                BuildReport(report, Running);
                break;

            case DMTaskState.FU_DOWNLOADING:
                _stepWatch = Stopwatch.StartNew();

                try
                {
                    using (var client = new HttpClient())
                    {
                        FirmwareVersion = (await client.GetStringAsync(Uri)).Trim();
                    }
                }
                catch
                {
                    succeed = false;
                }

                BuildReport(report, "Download", succeed ? Running : Failed);
                break;

            case DMTaskState.FU_APPLYING:
                _stepWatch = Stopwatch.StartNew();
                succeed    = FirmwareVersion != "applyFail";
                BuildReport(report, "Applied", succeed ? Running : Failed);
                break;

            case DMTaskState.FU_REBOOTING:
                _stepWatch = Stopwatch.StartNew();
                succeed    = FirmwareVersion != "rebootFail";
                BuildReport(report, "Reboot", succeed ? Running : Failed);
                break;

            case DMTaskState.DM_IDLE:
                BuildReport(report, Complete);

                report.Set(DeviceBase.StartupTimePropertyName, DateTime.UtcNow.ToString());
                report.Set(DeviceBase.FirmwareVersionPropertyName, FirmwareVersion);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(state));
            }

            if (report != null)
            {
                await transport.UpdateReportedPropertiesAsync(report);
            }

            return(succeed);
        }
Пример #10
0
 private void BuildReport(TwinCollection report, string status)
 {
     report.Set(FormattableString.Invariant($"{ReportPrefix}.{Status}"), status);
     report.Set(FormattableString.Invariant($"{ReportPrefix}.{LastUpdate}"), DateTime.UtcNow.ToString(CultureInfo.CurrentCulture));
     report.Set(FormattableString.Invariant($"{ReportPrefix}.{Duration}"), (int)_masterWatch.Elapsed.TotalSeconds);
 }
Пример #11
0
 private void BuildReport(TwinCollection report, string status)
 {
     report.Set($"{ReportPrefix}.{Status}", status);
     report.Set($"{ReportPrefix}.{LastUpdate}", DateTime.UtcNow.ToString());
     report.Set($"{ReportPrefix}.{Duration}", (int)_masterWatch.Elapsed.TotalSeconds);
 }