예제 #1
0
        public async Task <JobServiceModel> ScheduleDeviceMethodAsync(
            string jobId,
            string queryCondition,
            MethodParameterServiceModel parameter,
            DateTimeOffset startTimeUtc,
            long maxExecutionTimeInSeconds)
        {
            //var result = await this.jobClient.ScheduleDeviceMethodAsync(
            //    jobId, queryCondition,
            //    parameter.ToAzureModel(),
            //    startTimeUtc.DateTime,
            //    maxExecutionTimeInSeconds);
            var devicelistString = queryCondition.Replace("deviceId in", "").Trim();
            var devicelist       = JsonConvert.DeserializeObject <List <dynamic> >(devicelistString);
            List <DeviceJobServiceModel> devicemodellist = new List <DeviceJobServiceModel>();

            foreach (var item in devicelist)
            {
                DeviceJobServiceModel data = new DeviceJobServiceModel();
                data.DeviceId           = item;
                data.Status             = DeviceJobStatus.Scheduled;
                data.CreatedDateTimeUtc = DateTime.UtcNow;
                devicemodellist.Add(data);
            }
            var             devicecount = devicemodellist.Count();
            JobServiceModel json        = new JobServiceModel();

            json.CreatedTimeUtc  = DateTime.UtcNow;
            json.Devices         = devicemodellist.ToList();
            json.Status          = JobStatus.Scheduled;
            json.MethodParameter = parameter;
            json.Type            = JobType.ScheduleUpdateTwin;
            JobStatistics ResultStatistics = new JobStatistics();

            ResultStatistics.DeviceCount    = devicecount;
            ResultStatistics.SucceededCount = 0;
            ResultStatistics.FailedCount    = 0;
            ResultStatistics.PendingCount   = 0;
            ResultStatistics.RunningCount   = 0;
            json.ResultStatistics           = ResultStatistics;
            var value = JsonConvert.SerializeObject(json, Formatting.Indented, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });
            var result = await this.client.CreateAsync(DEVICE_JOBS_COLLECTION_ID, value);

            var Job = this.CreatejobServiceModel(result);

            return(Job);
        }
        public DeviceJobApiModel(DeviceJobServiceModel serviceModel)
        {
            this.DeviceId               = serviceModel.DeviceId;
            this.Status                 = serviceModel.Status;
            this.StartTimeUtc           = serviceModel.StartTimeUtc;
            this.EndTimeUtc             = serviceModel.EndTimeUtc;
            this.CreatedDateTimeUtc     = serviceModel.CreatedDateTimeUtc;
            this.LastUpdatedDateTimeUtc = serviceModel.LastUpdatedDateTimeUtc;

            if (serviceModel.Outcome != null)
            {
                this.Outcome = new MethodResultApiModel(serviceModel.Outcome);
            }

            if (serviceModel.Error != null)
            {
                this.Error = new DeviceJobErrorApiModel(serviceModel.Error);
            }
        }
예제 #3
0
        public async Task <JobServiceModel> ScheduleTwinUpdateAsync(
            string jobId,
            string queryCondition,
            TwinServiceModel twin,
            DateTimeOffset startTimeUtc,
            long maxExecutionTimeInSeconds)
        {
            //var result = await this.jobClient.ScheduleTwinUpdateAsync(
            //    jobId,
            //    queryCondition,
            //    twin.ToAzureModel(),
            //    startTimeUtc.DateTime,
            //    maxExecutionTimeInSeconds);


            var devicelistString = queryCondition.Replace("deviceId in", "").Trim();
            var devicelist       = JsonConvert.DeserializeObject <List <dynamic> >(devicelistString);
            List <DeviceJobServiceModel> devicemodellist = new List <DeviceJobServiceModel>();

            foreach (var item in devicelist)
            {
                DeviceJobServiceModel data = new DeviceJobServiceModel();
                data.DeviceId           = item;
                data.Status             = DeviceJobStatus.Scheduled;
                data.CreatedDateTimeUtc = DateTime.UtcNow;
                devicemodellist.Add(data);
            }
            var             devicecount = devicemodellist.Count();
            JobServiceModel json        = new JobServiceModel();

            json.CreatedTimeUtc = DateTime.UtcNow;
            json.Devices        = devicemodellist.ToList();
            json.Status         = JobStatus.Scheduled;
            json.UpdateTwin     = twin;
            json.Type           = JobType.ScheduleUpdateTwin;
            JobStatistics ResultStatistics = new JobStatistics();

            ResultStatistics.DeviceCount    = devicecount;
            ResultStatistics.SucceededCount = 0;
            ResultStatistics.FailedCount    = 0;
            ResultStatistics.PendingCount   = 0;
            ResultStatistics.RunningCount   = 0;
            json.ResultStatistics           = ResultStatistics;
            var value = JsonConvert.SerializeObject(json, Formatting.Indented, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });
            var result = await this.client.CreateAsync(DEVICE_JOBS_COLLECTION_ID, value);

            var Job = this.CreatejobServiceModel(result);
            // Update the deviceProperties cache, no need to wait
            var model = new DevicePropertyServiceModel();

            var tagRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(twin.Tags)) as JToken;

            if (tagRoot != null)
            {
                model.Tags = new HashSet <string>(tagRoot.GetAllLeavesPath());
            }

            var reportedRoot = JsonConvert.DeserializeObject(JsonConvert.SerializeObject(twin.ReportedProperties)) as JToken;

            if (reportedRoot != null)
            {
                model.Reported = new HashSet <string>(reportedRoot.GetAllLeavesPath());
            }
            var unused = deviceProperties.UpdateListAsync(model);

            return(Job);
        }