Пример #1
0
        public static async Task <string> UpdateSecure(
            [OrchestrationTrigger] DurableOrchestrationContext context)
        {
            _targetDeviceId = context.GetInput <string[]>();

            if (Client == null)
            {
                Client = await GetSecureDocumentClient();
            }

            var deviceId        = _targetDeviceId[0];
            var updateVersionId = _targetDeviceId[1];
            var specType        = _targetDeviceId[2];



            // Get the DeviceComponent Document
            var doc = await context.CallActivityAsync <dynamic>("GetDoc", deviceId);


            _documentId = doc.Id;


            var binder = new Binder();

            binder.BindingData.Add("documentId", _documentId);
            binder.BindingData.Add("attachmentId", updateVersionId);

            // Get the Attachment
            string attachment = await context.CallActivityAsync <dynamic>(nameof(GetAttachment), binder);


            var result = await context.CallActivityAsync <CloudToDeviceMethodResult>(nameof(StartFirmwareUpdate), attachment);

            if (result.Status != 0)
            {
                return(result.Status.ToString());
            }


            var status = await context.CallActivityAsync <string>(nameof(QueryTwinFwUpdateReportedSecure), DateTime.Now);

            var instant = new DateTimeOffset(DateTime.Parse(status));

            var spec = new DeviceComponent.ProductionSpecificationComponent
            {
                ComponentId    = { Value = Guid.NewGuid().ToString() },
                ProductionSpec = updateVersionId
            };
            var coding = new Coding(null, specType, specType.ToUpper());

            spec.SpecType.Coding.Add(coding);

            var ps = new DeviceComponent().ProductionSpecification;

            ps.Add(spec);

            // Update the LastSystemChange value
            var deviceComponentUpdate = new DeviceComponent
            {
                Id                          = _documentId,
                Type                        = doc.Type,
                Text                        = doc.Text,
                Source                      = doc.Source,
                Parent                      = doc.Parent,
                FhirComments                = doc.FhirComments,
                ImplicitRules               = doc.ImplicitRules,
                Identifier                  = doc.Identifier,
                OperationalStatus           = doc.OperationalStatus,
                Contained                   = doc.Contained,
                LastSystemChange            = instant,
                MeasurementPrinciple        = doc.MeasurementPrinciple,
                ParameterGroup              = doc.ParameterGroup,
                MeasurementPrincipleElement = doc.MeasurementPrincipleElement,
                LanguageCode                = doc.LanguageCode,
                ProductionSpecification     = ps,
                Language                    = doc.Language
            };

            var json = JsonConvert.SerializeObject(deviceComponentUpdate);


            // Update the Device Docuument
            var upDate = await context.CallActivityAsync <HttpResponseMessage>(nameof(UpdateDoc), json);

            if (!upDate.IsSuccessStatusCode)
            {
                return(upDate.StatusCode.ToString());
            }

            var cal = new DeviceMetric.CalibrationComponent
            {
                Type =
                    null,                     //values: unspecified, ofset, gin, two-point see https://www.hl7.org/fhir/valueset-metric-calibration-type.html
                State =
                    null,                     // values: not-calibrated,  calibration-required, calibrated, unspecified see https://www.hl7.org/fhir/valueset-metric-calibration-state.html
                Time = instant                // value: DateTimeOffset
            };
            var calibration = new DeviceMetric().Calibration;

            calibration.Add(cal);


            var deviceMetric = new DeviceMetric
            {
                OperationalStatus = doc.Result.OperationalStatus,
                Category          = doc.Result.Category,
                Color             = doc.Result.Color,
                Contained         = doc.Result.Contained,
                Type              = doc.Result.Type,
                Unit              = doc.Result.Unit,
                Source            = doc.Result.Source,
                Parent            = doc.Result.Parent,
                MeasurementPeriod = null,
                Calibration       = calibration
            };

            json = JsonConvert.SerializeObject(deviceMetric);

            upDate = await context.CallActivityAsync <HttpResponseMessage>(nameof(UpdateDoc), json);


            return(upDate.StatusCode.ToString());
        }