public async void InsertData() { // 1. Build a DataCollector object. DataCollector MyDataCollector = new DataCollector.Builder().SetPackageName(MyContext) .SetDataType(DataType.DtContinuousStepsDelta) .SetDataStreamName("STEPS_DELTA") .SetDataGenerateType(DataCollector.DataTypeRaw) .Build(); // 2. Create a sampling dataset set based on the data collector. SampleSet sampleSet = SampleSet.Create(MyDataCollector); // 3. Build the start time, end time, and incremental step count for a DT_CONTINUOUS_STEPS_DELTA sampling point. DateTime startDate = DateTime.Parse("2020-12-15 09:00:00"); DateTime endDate = DateTime.Parse("2020-12-15 09:05:00"); int stepsDelta = 1000; // 4. Build a DtContinuousStepsDelta sampling point. SamplePoint samplePoint = sampleSet.CreateSamplePoint() .SetTimeInterval(GetTime(startDate), GetTime(endDate), TimeUnit.Milliseconds); samplePoint.GetFieldValue(Field.FieldStepsDelta).SetIntValue(stepsDelta); // 5. Save a DtContinuousStepsDelta sampling point to the sampling dataset. // You can repeat steps 3 through 5 to add more sampling points to the sampling dataset. sampleSet.AddSample(samplePoint); // 6. Call the data controller to insert the sampling dataset into the Health platform. // 7. Calling the data controller to insert the sampling dataset is an asynchronous Task. var InsertTask = MyDataController.InsertAsync(sampleSet); try { await InsertTask; if (InsertTask.IsCompleted) { if (InsertTask.Exception == null) { Logger("Success insert an SampleSet into HMS core"); ShowSampleSet(sampleSet); Logger(Split); } else { PrintFailureMessage(InsertTask.Exception, "Insert"); } } } catch (Exception ex) { PrintFailureMessage(ex, "Insert"); } }
// Returns the callback data in SamplePoint mode. // @param samplePoint Reported data public static void ShowSamplePoint(SamplePoint samplePoint) { if (samplePoint != null) { Logger("Sample point type: " + samplePoint.DataType.Name); foreach (Field field in samplePoint.DataType.Fields) { Logger("Field: " + field.Name + " Value: " + samplePoint.GetFieldValue(field)); Logger(StampToData(DateTime.Now.ToString())); } } else { Logger("samplePoint is null!! "); Logger(Split); } }
public async void UpdateData() { // 1. Build a DataCollector object. DataCollector MyDataCollector = new DataCollector.Builder().SetPackageName(MyContext) .SetDataType(DataType.DtContinuousStepsDelta) .SetDataStreamName("STEPS_DELTA") .SetDataGenerateType(DataCollector.DataTypeRaw) .Build(); // 2. Build the sampling dataset for the update: create a sampling dataset // for the update based on the data collector. SampleSet sampleSet = SampleSet.Create(MyDataCollector); // 3. Build the start time, end time, and incremental step count for // a DtContinuousStepsDelta sampling point for the update. DateTime startDate = DateTime.Parse("2020-12-15 09:00:00"); DateTime endDate = DateTime.Parse("2020-12-15 09:05:00"); int stepsDelta = 2000; // 4. Build a DtContinuousStepsDelta sampling point for the update. SamplePoint samplePoint = sampleSet.CreateSamplePoint() .SetTimeInterval(GetTime(startDate), GetTime(endDate), TimeUnit.Milliseconds); samplePoint.GetFieldValue(Field.FieldStepsDelta).SetIntValue(stepsDelta); // 5. Add an updated DtContinuousStepsDelta sampling point to the sampling dataset for the update. // You can repeat steps 3 through 5 to add more updated sampling points to the sampling dataset for the update. sampleSet.AddSample(samplePoint); // 6. Build a parameter object for the update. // Note: (1) The start time of the modified object updateOptions cannot be greater than the minimum // value of the start time of all sample data points in the modified data sample set // (2) The end time of the modified object updateOptions cannot be less than the maximum value of the // end time of all sample data points in the modified data sample set UpdateOptions updateOptions = new UpdateOptions.Builder().SetTimeInterval(GetTime(startDate), GetTime(endDate), TimeUnit.Milliseconds) .SetSampleSet(sampleSet) .Build(); // 7. Use the specified parameter object for the update to call the // data controller to modify the sampling dataset. // 8.Calling the data controller to modify the sampling dataset is an asynchronous Task. var UpdateTask = MyDataController.UpdateAsync(updateOptions); try { await UpdateTask; if (UpdateTask.IsCompleted) { if (UpdateTask.Exception == null) { Logger("Success update sample data from HMS core"); Logger(Split); } else { PrintFailureMessage(UpdateTask.Exception, "Update"); } } } catch (Exception ex) { PrintFailureMessage(ex, "Update"); } }
// Add an activity record to the Health platform public async void AddActivityRecord() { Logger(Split + "Add ActivityRecord"); // Build the time range of the request object: start time and end time Calendar Cal = Calendar.Instance; Date Now = new Date(); Cal.Time = Now; long EndTime = Cal.TimeInMillis; Cal.Add(CalendarField.HourOfDay, -1); long StartTime = Cal.TimeInMillis; // Build the data collector object. DataCollector dataCollector = new DataCollector.Builder().SetDataType(DataType.DtContinuousStepsDelta) .SetDataGenerateType(DataCollector.DataTypeRaw) .SetPackageName(this) .SetDataCollectorName("DataCollector1") .Build(); // Build the activity statistics and activity record request objects. ActivitySummary activitySummary = GetActivitySummary(); // Create a data collector for total step count statistics. // ActivitySummary is used to bear the statistical data. DataCollector dataCollector2 = new DataCollector.Builder().SetDataType(DataType.DtContinuousStepsTotal) .SetDataGenerateType(DataCollector.DataTypeRaw) .SetPackageName(this) .SetDataCollectorName("DataCollector2") .Build(); SamplePoint samplePoint = new SamplePoint.Builder(dataCollector2).Build(); samplePoint.GetFieldValue(Field.FieldSteps).SetIntValue(1024); activitySummary.DataSummary = new[] { samplePoint }; // Build the activity record request object ActivityRecord activityRecord = new ActivityRecord.Builder().SetName("AddActivityRecordTest") .SetDesc("Add ActivityRecord") .SetId("MyAddActivityRecordId") .SetActivityTypeId(HiHealthActivities.Running) .SetStartTime(StartTime, TimeUnit.Milliseconds) .SetEndTime(EndTime, TimeUnit.Milliseconds) .SetActivitySummary(activitySummary) .SetTimeZone("+0800") .Build(); // Build the sampling sampleSet based on the dataCollector SampleSet sampleSet = SampleSet.Create(dataCollector); // Build the (DtContinuousStepsDelta) sampling data object and add it to the sampling dataSet SamplePoint samplePointDetail = sampleSet.CreateSamplePoint().SetTimeInterval(StartTime, EndTime, TimeUnit.Milliseconds); samplePointDetail.GetFieldValue(Field.FieldStepsDelta).SetIntValue(1024); sampleSet.AddSample(samplePointDetail); // Build the activity record addition request object ActivityRecordInsertOptions insertRequest = new ActivityRecordInsertOptions.Builder().SetActivityRecord(activityRecord).AddSampleSet(sampleSet).Build(); CheckConnect(); // Call the related method in the ActivityRecordsController to add activity records var AddTask = MyActivityRecordsController.AddActivityRecordAsync(insertRequest); try { await AddTask; if (AddTask.IsCompleted) { if (AddTask.Exception == null) { Logger("Add ActivityRecord was successful!"); } else { PrintFailureMessage(AddTask.Exception, "AddActivityRecord"); } } } catch (System.Exception ex) { PrintFailureMessage(ex, "AddActivityRecord"); } }