Skip to content

Commit

Permalink
Merge pull request #314 from cph-cachet/301-timezone-question
Browse files Browse the repository at this point in the history
Added timezone measure and probe
  • Loading branch information
bardram authored Feb 13, 2023
2 parents f1c481b + 509d6cf commit eb7ab55
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,32 @@ class LocalStudyProtocolManager implements StudyProtocolManager {
// ..addMeasure(Measure(type: ContextSamplingPackage.LOCATION)),
// locationService);

// // Add a background task that continously collects geolocation and mobility
// // Add a background task that continuously collects geolocation and mobility
// protocol.addTriggeredTask(
// ImmediateTrigger(),
// BackgroundTask()
// ..addMeasure(Measure(type: ContextSamplingPackage.GEOLOCATION))
// ..addMeasure(Measure(type: ContextSamplingPackage.MOBILITY)),
// locationService);

// // Define the online weather service and add it as a 'device'
// WeatherService weatherService =
// WeatherService(apiKey: '12b6e28582eb9298577c734a31ba9f4f');
// protocol.addConnectedDevice(weatherService);
// Define the online weather service and add it as a 'device'
WeatherService weatherService =
WeatherService(apiKey: '12b6e28582eb9298577c734a31ba9f4f');
protocol.addConnectedDevice(weatherService);

// // Add a background task that collects weather every 30 miutes.
// protocol.addTriggeredTask(
// IntervalTrigger(period: Duration(minutes: 30)),
// BackgroundTask()
// ..addMeasure(Measure(type: ContextSamplingPackage.WEATHER)),
// weatherService);
// Add a background task that collects weather every 30 minutes.
protocol.addTriggeredTask(
IntervalTrigger(period: Duration(seconds: 30)),
BackgroundTask()
..addMeasure(Measure(type: ContextSamplingPackage.WEATHER)),
weatherService);

// // Define the online air quality service and add it as a 'device'
// AirQualityService airQualityService =
// AirQualityService(apiKey: '9e538456b2b85c92647d8b65090e29f957638c77');
// protocol.addConnectedDevice(airQualityService);

// // Add a background task that air quality every 30 miutes.
// // Add a background task that air quality every 30 minutes.
// protocol.addTriggeredTask(
// IntervalTrigger(period: Duration(minutes: 30)),
// BackgroundTask()
Expand Down
4 changes: 4 additions & 0 deletions carp_mobile_sensing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.40.13

* Added support for `timezone` data type (issue [#301](https://github.com/cph-cachet/carp.sensing-flutter/issues/301)).

## 0.40.12

* Better support for removing a study deployment from a client manager
Expand Down
2 changes: 1 addition & 1 deletion carp_mobile_sensing/example/.flutter-plugins-dependencies

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ export "FLUTTER_APPLICATION_PATH=/Users/bardram/dev/carp/carp.sensing-flutter/ca
export "COCOAPODS_PARALLEL_CODE_SIGN=true"
export "FLUTTER_TARGET=lib/main.dart"
export "FLUTTER_BUILD_DIR=build"
export "FLUTTER_BUILD_NAME=0.40.1"
export "FLUTTER_BUILD_NUMBER=0.40.1"
export "FLUTTER_BUILD_NAME=0.40.9"
export "FLUTTER_BUILD_NUMBER=0.40.9"
export "DART_OBFUSCATION=false"
export "TRACK_WIDGET_CREATION=true"
export "TREE_SHAKE_ICONS=false"
Expand Down
14 changes: 13 additions & 1 deletion carp_mobile_sensing/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ class LocalStudyProtocolManager implements StudyProtocolManager {
var phone = Smartphone();
protocol.addMasterDevice(phone);

// Collect timezone info every time the app restarts.
protocol.addTriggeredTask(
ImmediateTrigger(),
BackgroundTask(measures: [
Measure(type: DeviceSamplingPackage.TIMEZONE),
]),
phone);

// // // Add measures from the [DeviceSamplingPackage] and [SensorSamplingPackage]
// // // sampling packages.
// protocol.addTriggeredTask(
Expand Down Expand Up @@ -273,15 +281,19 @@ class LocalStudyProtocolManager implements StudyProtocolManager {
// on the phone. However, nothing will happen when you click on it.
// See the PulmonaryMonitor demo app for a full-scale example of how to use
// the App Task model.
//
// Note also that the timezone measure is added. This entails that timezone
// information is collected when the user 'executes' this app task.
protocol.addTriggeredTask(
ElapsedTimeTrigger(
elapsedTime: const Duration(minutes: 2),
),
AppTask(
measures: [Measure(type: DeviceSamplingPackage.TIMEZONE)],
type: BackgroundSensingUserTask.ONE_TIME_SENSING_TYPE,
title: "Elapsed Time - 2 minutes",
notification: true,
)..addMeasure(Measure(type: DeviceSamplingPackage.DEVICE)),
),
phone);

// // add an app task at exact date & time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:json_annotation/json_annotation.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:screen_state/screen_state.dart';
import 'package:system_info2/system_info2.dart';
import 'package:flutter_timezone/flutter_timezone.dart';

part 'device.g.dart';
part 'device_datum.dart';
Expand Down
22 changes: 22 additions & 0 deletions carp_mobile_sensing/lib/sampling_packages/device/device.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions carp_mobile_sensing/lib/sampling_packages/device/device_datum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,31 @@ class ScreenDatum extends Datum {
@override
String toString() => '${super.toString()}, screenEvent: $screenEvent';
}

/// Holds timezone information about the mobile device.
///
/// See [List of tz database time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
/// for an overview of timezones.
@JsonSerializable(fieldRename: FieldRename.snake, includeIfNull: false)
class TimezoneDatum extends Datum {
@override
DataFormat get format =>
DataFormat.fromString(DeviceSamplingPackage.TIMEZONE);

/// The timezone as a string.
String timezone;

TimezoneDatum(this.timezone) : super();

/// Returns `true` if the [deviceId] is equal.
@override
bool equivalentTo(ConditionalEvent? event) => timezone == event!['timezone'];

factory TimezoneDatum.fromJson(Map<String, dynamic> json) =>
_$TimezoneDatumFromJson(json);
@override
Map<String, dynamic> toJson() => _$TimezoneDatumToJson(this);

@override
String toString() => '${super.toString()}, timezone: $timezone';
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ class DeviceSamplingPackage extends SmartphoneSamplingPackage {
/// * No sampling configuration needed.
static const String SCREEN = 'dk.cachet.carp.screen';

/// Measure type for collection of the time zone of the device.
/// See [List of tz database time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
/// for an overview of timezones.
/// * One-time measure.
/// * Uses the [Smartphone] master device for data collection.
/// * No sampling configuration needed.
static const String TIMEZONE = 'dk.cachet.carp.timezone';

@override
List<String> get dataTypes => [
DEVICE,
MEMORY,
BATTERY,
SCREEN,
TIMEZONE,
];

@override
Expand All @@ -45,6 +54,8 @@ class DeviceSamplingPackage extends SmartphoneSamplingPackage {
return BatteryProbe();
case SCREEN:
return (Platform.isAndroid) ? ScreenProbe() : null;
case TIMEZONE:
return TimezoneProbe();
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,10 @@ class DeviceProbe extends DatumProbe {
hardware: DeviceInfo().hardware,
);
}

/// A probe that collects the device's current timezone.
class TimezoneProbe extends DatumProbe {
@override
Future<Datum?> getDatum() async =>
TimezoneDatum(await FlutterTimezone.getLocalTimezone());
}
5 changes: 3 additions & 2 deletions carp_mobile_sensing/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: carp_mobile_sensing
description: Mobile Sensing Framework for Flutter. A software framework for collecting sensor data from the phone and attached wearable devices via probes. Can be extended.
version: 0.40.12
version: 0.40.13
homepage: https://github.com/cph-cachet/carp.sensing-flutter

environment:
Expand All @@ -15,7 +15,7 @@ dependencies:
carp_core: ^0.40.7 # the core CARP domain model

json_annotation: ^4.7.0
system_info2: ^2.0.0
system_info2: ^3.0.0
uuid: ^3.0.4
async: ^2.7.0
path_provider: ^2.0.0
Expand All @@ -35,6 +35,7 @@ dependencies:
device_info_plus: ^8.0.0
sensors_plus: ^2.0.0
battery_plus: ^3.0.0
flutter_timezone: ^1.0.0

# the CACHET plugins
pedometer: ^3.0.0 # Step count from local OS
Expand Down

0 comments on commit eb7ab55

Please sign in to comment.