Guest User

Untitled

a guest
Dec 20th, 2025
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.84 KB | None | 0 0
  1. import Toybox.Application;
  2. import Toybox.Lang;
  3. import Toybox.WatchUi;
  4. using Toybox.Timer;
  5. using Toybox.Sensor;
  6. using Toybox.Time;
  7. using Toybox.System;
  8. using Toybox.SensorHistory;
  9. using Toybox.ActivityRecording;
  10. using Toybox.FitContributor as Fit;
  11. using Toybox.Activity;
  12.  
  13. class tempLogApp extends Application.AppBase {
  14. private var tempTimer;
  15. private var isRecording;
  16. private var usingFallback;
  17. private var fitSession;
  18. private var fitTempField;
  19. private var fitHrField;
  20.  
  21. function initialize() {
  22. AppBase.initialize();
  23. tempTimer = null;
  24. isRecording = false;
  25. usingFallback = false;
  26. fitSession = null;
  27. fitTempField = null;
  28. fitHrField = null;
  29. }
  30.  
  31. // onStart() is called on application start up
  32. function onStart(state as Dictionary?) as Void {
  33. // Enable temperature sensor
  34. if (Toybox has :Sensor) {
  35. try {
  36. Sensor.setEnabledSensors([Sensor.SENSOR_TEMPERATURE]);
  37. System.println("Temperature sensor enabled");
  38. } catch(e) {
  39. System.println("Failed to enable temperature sensor: " + e.toString());
  40. }
  41. }
  42. }
  43.  
  44. // onStop() is called when your application is exiting
  45. function onStop(state as Dictionary?) as Void {
  46. pauseRecording();
  47. finalizeFitRecording(true);
  48. }
  49.  
  50. // Return the initial view of your application here
  51. function getInitialView() as [Views] or [Views, InputDelegates] {
  52. return [ new tempLogView(), new tempLogDelegate() ];
  53. }
  54.  
  55. public function startRecording() as Void {
  56. if (isRecording) {
  57. return;
  58. }
  59.  
  60. isRecording = true;
  61. Properties.setValue("recActive", true);
  62.  
  63. // Check if we're resuming from a pause
  64. var isPaused = Properties.getValue("isPaused");
  65.  
  66. if (!isPaused) {
  67. // Set the startTime property to the current timestamp
  68. Application.Properties.setValue("startTime", Time.now().value());
  69.  
  70. // Clear previous temperature data only if not paused
  71. Application.Properties.setValue("temperatureData", "");
  72. }
  73.  
  74. // Clear the paused flag
  75. Properties.setValue("isPaused", false);
  76.  
  77. try {
  78. ensureFitRecordingStarted();
  79. } catch(e) {
  80. System.println("FIT setup error: " + e.toString());
  81. }
  82.  
  83. // Start timer that runs every 10 seconds for both export and FIT recording
  84. tempTimer = new Timer.Timer();
  85. tempTimer.start(method(:recordData), 10000, true);
  86.  
  87. // Record first data immediately
  88. recordData();
  89. recordFitSample();
  90. }
  91.  
  92. public function stopRecording() as Void {
  93. if (!isRecording && !Properties.getValue("isPaused")) {
  94. return;
  95. }
  96.  
  97. isRecording = false;
  98. Properties.setValue("recActive", false);
  99. Properties.setValue("isPaused", false);
  100.  
  101. // Set the endTime property to the current timestamp
  102. Application.Properties.setValue("endTime", Time.now().value());
  103.  
  104. if (tempTimer != null) {
  105. tempTimer.stop();
  106. tempTimer = null;
  107. }
  108.  
  109. finalizeFitRecording(true);
  110. }
  111.  
  112. public function pauseRecording() as Void {
  113. if (!isRecording) {
  114. return;
  115. }
  116.  
  117. // If already paused, don't do anything
  118. if (Properties.getValue("isPaused")) {
  119. return;
  120. }
  121.  
  122. isRecording = false;
  123. Properties.setValue("recActive", false);
  124. Properties.setValue("isPaused", true);
  125.  
  126. if (tempTimer != null) {
  127. tempTimer.stop();
  128. tempTimer = null;
  129. }
  130.  
  131. pauseFitRecording();
  132. }
  133.  
  134. function recordData() as Void {
  135. recordFitSample();
  136. }
  137.  
  138.  
  139. function getIsRecording() as Boolean {
  140. return isRecording;
  141. }
  142.  
  143. public function recordFitSample() as Void {
  144. // Record current temperature and heart rate to FIT every 10 seconds
  145. if (fitSession == null) {
  146. System.println("recordFitSample: fitSession is null");
  147. return;
  148. }
  149.  
  150. if (!fitSession.isRecording()) {
  151. System.println("recordFitSample: session not recording");
  152. return;
  153. }
  154.  
  155. var temp = null;
  156. var hr = null;
  157.  
  158. if (Toybox has :Sensor) {
  159. var info = Sensor.getInfo();
  160. if (info has :temperature && info.temperature != null) {
  161. temp = info.temperature;
  162. }
  163. if (info has :heartRate && info.heartRate != null) {
  164. hr = info.heartRate;
  165. }
  166. }
  167.  
  168. System.println("recordFitSample: temp=" + temp + " hr=" + hr);
  169.  
  170. if (fitTempField != null && temp != null) {
  171. try {
  172. var scaledTemp = (temp * 100).toNumber();
  173. fitTempField.setData(scaledTemp);
  174. System.println("Set temperature to FIT: " + scaledTemp);
  175. } catch(e) {
  176. System.println("Error setting temperature: " + e.toString());
  177. }
  178. }
  179.  
  180. if (fitHrField != null && hr != null) {
  181. try {
  182. fitHrField.setData(hr);
  183. System.println("Set HR to FIT: " + hr);
  184. } catch(e2) {
  185. System.println("Error setting heart rate: " + e2.toString());
  186. }
  187. }
  188. }
  189.  
  190. private function ensureFitRecordingStarted() as Void {
  191. if (!(Toybox has :ActivityRecording)) {
  192. System.println("ActivityRecording not supported");
  193. return;
  194. }
  195.  
  196. if (fitSession == null) {
  197. try {
  198. fitSession = ActivityRecording.createSession({
  199. :name => "TempLog",
  200. :sport => Activity.SPORT_GENERIC,
  201. :subSport => Activity.SUB_SPORT_GENERIC
  202. });
  203. System.println("FIT session created successfully");
  204.  
  205. // Create fields immediately after session creation, before starting
  206. createFitFields();
  207.  
  208. // Now start the session
  209. fitSession.start();
  210. System.println("FIT session started");
  211. } catch(e) {
  212. System.println("FIT session creation/start failed: " + e.toString());
  213. fitSession = null;
  214. fitTempField = null;
  215. fitHrField = null;
  216. }
  217. }
  218. }
  219.  
  220. private function createFitFields() as Void {
  221. if (fitSession == null) {
  222. return;
  223. }
  224.  
  225. if (fitTempField == null) {
  226. try {
  227. fitTempField = fitSession.createField("temperature", 100, Fit.DATA_TYPE_SINT16, {
  228. :mesgType => Fit.MESG_TYPE_RECORD,
  229. :units => "C"
  230. });
  231. System.println("Temperature field created successfully");
  232. } catch(e) {
  233. System.println("Temperature field creation failed: " + e.toString());
  234. fitTempField = null;
  235. }
  236. }
  237.  
  238. if (fitHrField == null) {
  239. try {
  240. fitHrField = fitSession.createField("heart_rate", 101, Fit.DATA_TYPE_UINT8, {
  241. :mesgType => Fit.MESG_TYPE_RECORD,
  242. :units => "bpm"
  243. });
  244. System.println("HR field created successfully");
  245. } catch(e) {
  246. System.println("HR field creation failed: " + e.toString());
  247. fitHrField = null;
  248. }
  249. }
  250. }
  251.  
  252. private function pauseFitRecording() as Void {
  253. if (fitSession != null && fitSession.isRecording()) {
  254. fitSession.stop();
  255. }
  256. }
  257.  
  258. private function finalizeFitRecording(save as Boolean) as Void {
  259. if (fitSession != null) {
  260. if (fitSession.isRecording()) {
  261. fitSession.stop();
  262. }
  263.  
  264. if (save) {
  265. fitSession.save();
  266. } else {
  267. fitSession.discard();
  268. }
  269. }
  270.  
  271. fitSession = null;
  272. fitTempField = null;
  273. fitHrField = null;
  274. }
  275.  
  276. public function discardFitSession() as Void {
  277. if (fitSession != null) {
  278. if (fitSession.isRecording()) {
  279. fitSession.stop();
  280. }
  281. fitSession.discard();
  282. }
  283. fitSession = null;
  284. fitTempField = null;
  285. fitHrField = null;
  286. System.println("FIT activity discarded");
  287. }
  288.  
  289. }
  290.  
  291. function getApp() as tempLogApp {
  292. return Application.getApp() as tempLogApp;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment