garmsync garmsync
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.5 KiB

3 months ago
  1. import Trimlog from './lib/client.mjs';
  2. async function readStdin() {
  3. return new Promise((resolve) => {
  4. const allLines = [];
  5. let lingeringLine = "";
  6. process.stdin.resume();
  7. process.stdin.setEncoding('utf8');
  8. process.stdin.on('data', function(chunk) {
  9. const lines = chunk.split("\n");
  10. lines[0] = lingeringLine + lines[0];
  11. lingeringLine = lines.pop();
  12. allLines.push(...lines);
  13. });
  14. process.stdin.on('end', function() {
  15. allLines.push(lingeringLine);
  16. resolve(allLines);
  17. });
  18. })
  19. }
  20. function parseLine(line) {
  21. const split = line.split(" ");
  22. const split2 = split[0].split(/[:,.]/g);
  23. let seconds = parseInt(split2[0], 10) * 60;
  24. if (split2.length > 1) {
  25. seconds += parseInt(split2[1], 10);
  26. }
  27. return ({
  28. seconds,
  29. calories: parseInt(split[1]),
  30. });
  31. }
  32. async function main() {
  33. const [id] = process.argv.slice(2);
  34. if (!id) {
  35. return;
  36. }
  37. const stuff = (await readStdin())
  38. .filter(l => l)
  39. .map(parseLine)
  40. .map(l => ({...l, roundId: id}))
  41. console.log(stuff);
  42. const trimlog = new Trimlog({
  43. username: process.env.TRIMLOG_USERNAME,
  44. password: process.env.TRIMLOG_PASSWORD,
  45. })
  46. await trimlog.connect();
  47. console.log("CONNECTED!")
  48. trimlog.send("addRoundMeasurementBatch", {
  49. measurements: stuff,
  50. overwrite: true,
  51. });
  52. process.on('SIGINT', function() {
  53. console.log("INTERRUPTED");
  54. process.exit(0);
  55. });
  56. process.on('SIGTERM', function() {
  57. console.log("TERMINATED");
  58. process.exit(0);
  59. });
  60. }
  61. main();