Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Toybox.Application;
- import Toybox.Lang;
- import Toybox.WatchUi;
- using Toybox.Timer;
- using Toybox.Sensor;
- using Toybox.Time;
- using Toybox.System;
- using Toybox.SensorHistory;
- using Toybox.ActivityRecording;
- using Toybox.FitContributor as Fit;
- using Toybox.Activity;
- class tempLogApp extends Application.AppBase {
- private var tempTimer;
- private var isRecording;
- private var usingFallback;
- private var fitSession;
- private var fitTempField;
- private var fitHrField;
- function initialize() {
- AppBase.initialize();
- tempTimer = null;
- isRecording = false;
- usingFallback = false;
- fitSession = null;
- fitTempField = null;
- fitHrField = null;
- }
- // onStart() is called on application start up
- function onStart(state as Dictionary?) as Void {
- // Enable temperature sensor
- if (Toybox has :Sensor) {
- try {
- Sensor.setEnabledSensors([Sensor.SENSOR_TEMPERATURE]);
- System.println("Temperature sensor enabled");
- } catch(e) {
- System.println("Failed to enable temperature sensor: " + e.toString());
- }
- }
- }
- // onStop() is called when your application is exiting
- function onStop(state as Dictionary?) as Void {
- pauseRecording();
- finalizeFitRecording(true);
- }
- // Return the initial view of your application here
- function getInitialView() as [Views] or [Views, InputDelegates] {
- return [ new tempLogView(), new tempLogDelegate() ];
- }
- public function startRecording() as Void {
- if (isRecording) {
- return;
- }
- isRecording = true;
- Properties.setValue("recActive", true);
- // Check if we're resuming from a pause
- var isPaused = Properties.getValue("isPaused");
- if (!isPaused) {
- // Set the startTime property to the current timestamp
- Application.Properties.setValue("startTime", Time.now().value());
- // Clear previous temperature data only if not paused
- Application.Properties.setValue("temperatureData", "");
- }
- // Clear the paused flag
- Properties.setValue("isPaused", false);
- try {
- ensureFitRecordingStarted();
- } catch(e) {
- System.println("FIT setup error: " + e.toString());
- }
- // Start timer that runs every 10 seconds for both export and FIT recording
- tempTimer = new Timer.Timer();
- tempTimer.start(method(:recordData), 10000, true);
- // Record first data immediately
- recordData();
- recordFitSample();
- }
- public function stopRecording() as Void {
- if (!isRecording && !Properties.getValue("isPaused")) {
- return;
- }
- isRecording = false;
- Properties.setValue("recActive", false);
- Properties.setValue("isPaused", false);
- // Set the endTime property to the current timestamp
- Application.Properties.setValue("endTime", Time.now().value());
- if (tempTimer != null) {
- tempTimer.stop();
- tempTimer = null;
- }
- finalizeFitRecording(true);
- }
- public function pauseRecording() as Void {
- if (!isRecording) {
- return;
- }
- // If already paused, don't do anything
- if (Properties.getValue("isPaused")) {
- return;
- }
- isRecording = false;
- Properties.setValue("recActive", false);
- Properties.setValue("isPaused", true);
- if (tempTimer != null) {
- tempTimer.stop();
- tempTimer = null;
- }
- pauseFitRecording();
- }
- function recordData() as Void {
- recordFitSample();
- }
- function getIsRecording() as Boolean {
- return isRecording;
- }
- public function recordFitSample() as Void {
- // Record current temperature and heart rate to FIT every 10 seconds
- if (fitSession == null) {
- System.println("recordFitSample: fitSession is null");
- return;
- }
- if (!fitSession.isRecording()) {
- System.println("recordFitSample: session not recording");
- return;
- }
- var temp = null;
- var hr = null;
- if (Toybox has :Sensor) {
- var info = Sensor.getInfo();
- if (info has :temperature && info.temperature != null) {
- temp = info.temperature;
- }
- if (info has :heartRate && info.heartRate != null) {
- hr = info.heartRate;
- }
- }
- System.println("recordFitSample: temp=" + temp + " hr=" + hr);
- if (fitTempField != null && temp != null) {
- try {
- var scaledTemp = (temp * 100).toNumber();
- fitTempField.setData(scaledTemp);
- System.println("Set temperature to FIT: " + scaledTemp);
- } catch(e) {
- System.println("Error setting temperature: " + e.toString());
- }
- }
- if (fitHrField != null && hr != null) {
- try {
- fitHrField.setData(hr);
- System.println("Set HR to FIT: " + hr);
- } catch(e2) {
- System.println("Error setting heart rate: " + e2.toString());
- }
- }
- }
- private function ensureFitRecordingStarted() as Void {
- if (!(Toybox has :ActivityRecording)) {
- System.println("ActivityRecording not supported");
- return;
- }
- if (fitSession == null) {
- try {
- fitSession = ActivityRecording.createSession({
- :name => "TempLog",
- :sport => Activity.SPORT_GENERIC,
- :subSport => Activity.SUB_SPORT_GENERIC
- });
- System.println("FIT session created successfully");
- // Create fields immediately after session creation, before starting
- createFitFields();
- // Now start the session
- fitSession.start();
- System.println("FIT session started");
- } catch(e) {
- System.println("FIT session creation/start failed: " + e.toString());
- fitSession = null;
- fitTempField = null;
- fitHrField = null;
- }
- }
- }
- private function createFitFields() as Void {
- if (fitSession == null) {
- return;
- }
- if (fitTempField == null) {
- try {
- fitTempField = fitSession.createField("temperature", 100, Fit.DATA_TYPE_SINT16, {
- :mesgType => Fit.MESG_TYPE_RECORD,
- :units => "C"
- });
- System.println("Temperature field created successfully");
- } catch(e) {
- System.println("Temperature field creation failed: " + e.toString());
- fitTempField = null;
- }
- }
- if (fitHrField == null) {
- try {
- fitHrField = fitSession.createField("heart_rate", 101, Fit.DATA_TYPE_UINT8, {
- :mesgType => Fit.MESG_TYPE_RECORD,
- :units => "bpm"
- });
- System.println("HR field created successfully");
- } catch(e) {
- System.println("HR field creation failed: " + e.toString());
- fitHrField = null;
- }
- }
- }
- private function pauseFitRecording() as Void {
- if (fitSession != null && fitSession.isRecording()) {
- fitSession.stop();
- }
- }
- private function finalizeFitRecording(save as Boolean) as Void {
- if (fitSession != null) {
- if (fitSession.isRecording()) {
- fitSession.stop();
- }
- if (save) {
- fitSession.save();
- } else {
- fitSession.discard();
- }
- }
- fitSession = null;
- fitTempField = null;
- fitHrField = null;
- }
- public function discardFitSession() as Void {
- if (fitSession != null) {
- if (fitSession.isRecording()) {
- fitSession.stop();
- }
- fitSession.discard();
- }
- fitSession = null;
- fitTempField = null;
- fitHrField = null;
- System.println("FIT activity discarded");
- }
- }
- function getApp() as tempLogApp {
- return Application.getApp() as tempLogApp;
- }
Advertisement
Add Comment
Please, Sign In to add comment