NightscoutFoundation / xDrip

Nightscout version of xDrip+
https://jamorham.github.io/#xdrip-plus
GNU General Public License v3.0
1.4k stars 1.14k forks source link

Fitbit Ionic and Garmin API support #261

Closed jamorham closed 3 years ago

jamorham commented 6 years ago

I believe that both of these watches support a web based API which can receive remote data from a web server.

I propose attempting to incorporate an optional web service in to xDrip to respond to queries on the localhost address but I am not familiar with watch-face development for either of these devices at the moment to know fully whether this will work and the ideal way to structure it.

What is the best format for data to be provided? json? What information should be provided to the watch? Does anyone have any of these devices to test/develop with? Can we respond to events, for example an action on the watch to generate a specific request which we could use for example to snooze alerts? I could attempt to open up the tasker interface via http etc...

jamorham commented 6 years ago

@danpowell88 @Rytiggy you may be interested in this.

Rytiggy commented 6 years ago

Hey, I was able to connect to a REST API server when the fetch is formatted like shown below on my Fitbit Ionic companion app. my REST API is just the a basic heroku API that was set up through this tutorial ,

Code:

const test = () => {
  console.log('TEST IS A CONNECTION')
  let url = 'YOUR URL'
  fetch('url',{
    method: 'GET',
    mode: 'cors',
    headers: new Headers({
      "Content-Type": 'application/json; charset=utf-8'
    })
  })
    .then(response => {
      console.log('Get Data On Phone');
      response.text().then(data => {
        console.log( data);
        sendVal(data);

      })
      .catch(responseParsingError => {
        console.log('fetchError');
        console.log(responseParsingError.name);
        console.log(responseParsingError.message);
        console.log(responseParsingError.toString());
        console.log(responseParsingError.stack);
      });
    }).catch(fetchError => {
      console.log('fetchError');
      console.log(fetchError.name);
      console.log(fetchError.message);
      console.log(fetchError.toString());
      console.log(fetchError.stack);
    })
};

test(); // test right away

function sendVal(data) {
  console.log('sendVal')
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
      console.log('Sending Values')
      messaging.peerSocket.send(data);
  }

}
jamorham commented 6 years ago

@Rytiggy so this is with the garmin api? Is there any existing watch-face for garmin which connects to nightscout? I am thinking if one already exists I can emulate the same endpoint within xDrip

Rytiggy commented 6 years ago

@jamorham I did not use the garmin API, I am developing this on the fitbit Ionic using the fitbit sdk, and fitbit studio.

jamorham commented 6 years ago

If you make it work either with the /pebble endpoint or /api/v1/entries/sgv.json?count=24 endpoint then I can make sure that it is supported out of the box with xDrip

danpowell88 commented 6 years ago

The garmin api actually supports sending messages to the watch directly and can handle processing them order etc. I believe it just requires the watch face an xdrip to share an 'inbox' address

danpowell88 commented 6 years ago

See https://developer.garmin.com/connect-iq/programmers-guide/communication/ directly messaging

danpowell88 commented 6 years ago

I believe Andreas May had some kind of fork of xdrip and a watch face built that would take advantage of it, I don't have his github d to ails though, he goes by Swissalpine on gittter

jamorham commented 6 years ago

@danpowell88 yes, @swissalpine has sent me that fork which I can look over. The problem is that without a device for me to test it with its going to be really tricky to get reliable etc and if we need to implement a http api for fitbit then the same interface I assume could be used for garmin and its going to be that much easier to maintain.

danpowell88 commented 6 years ago

True, when I last looked through the Fitbit forums people were complaining and asking for api integration as fitbit is the only one that doesnt really have it, didn't see any official response though

I can test the garmin integration currently nt using a garmin vivoactive hr 24/7 with @swissalpine watch face, but it is sort of limited due to requiring a data connection to always be available, would love to have it working off local data

jamorham commented 6 years ago

@Rytiggy can you try with either of the endpoints which are now available in the Jan 7th nightly version and let me know if the fitbit api can connect to them? https://github.com/NightscoutFoundation/xDrip/blob/master/Documentation/technical/Local_Web_Services.md

Rytiggy commented 6 years ago

@jamorham I'm not quite sure how to get the local end points set up, I have x drip running and linked to my rest API receiving data points.

With the fetch I have set up on my Fitbit ionic it will work with any rest API

jamorham commented 6 years ago

@Rytiggy You should be able to go to Settings -> Inter-App settings -> xDrip Web Service -> ON

Then with the ionic code you have, see if you can create a connection to url: http://127.0.0.1:17580/sgv.json

This should pull a nightscout style json but from xDrip's internal database.

Rytiggy commented 6 years ago

@jamorham I don't think I have the local web server set up right, here is what my inner app settings look like screenshot_20180107-165304

jamorham commented 6 years ago

Upgrade to the latest version to see the new settings item:

https://github.com/NightscoutFoundation/xDrip/wiki/How-to-get-the-Nightlies-on-Automatic-Update

Rytiggy commented 6 years ago

@jamorham okay tested and can confirm that when you set up the local rest API with the endpoint (http://127.0.0.1:17580/sgv.json) through xdrip that you can do a fetch on the fitbit ionic companion app, here is the fetch request that i made.

   fetch('http://127.0.0.1:17580/sgv.json',{
      method: 'GET',
      mode: 'cors',
      headers: new Headers({
        "Content-Type": 'application/json; charset=utf-8'
      })
    })
      .then(response => {
        console.log('Get Data On Phone');
        response.text().then(data => {
          console.log('fetched Data from API');
         // Do somthing with the data
          sendVal(data);

        })
        .catch(responseParsingError => {
          console.log('fetchError');
          console.log(responseParsingError.name);
          console.log(responseParsingError.message);
          console.log(responseParsingError.toString());
          console.log(responseParsingError.stack);
        });
      }).catch(fetchError => {
        console.log('fetchError');
        console.log(fetchError.name);
        console.log(fetchError.message);
        console.log(fetchError.toString());
        console.log(fetchError.stack);
      })
  }
};
jamorham commented 6 years ago

Okay, so that means with this local web service it should be possible to create a fitbit watchface for xdrip. I assume you got good looking data coming back?

danpowell88 commented 6 years ago

@swissalpine any chance you could publish a garmin watchface with that api and I can test it

Rytiggy commented 6 years ago

I have been working on a watch face for the Fitbit ionic that will display blood sugars that just takes in a REST API URL, its functional now but needs some refactoring. definitely going to publish it once I finish it up. here's where it's at now. 26772249_10210944460307572_1822186339_o

jamorham commented 6 years ago

Okay that looks great! So is the watchface now able to get the data directly from xDrip without needing to go over the internet to Nightscout?

Rytiggy commented 6 years ago

Exactly it's all local to the phone now!

jamorham commented 6 years ago

@Rytiggy Is it possible to side load apps for the ionic? It would be really good for ionic users if there was a way you could publish the watch face even if its not 100% complete!

Rytiggy commented 6 years ago

@jamorham yeah it is possible to side load through the fitbit companion app (https://dev.fitbit.com/build/reference/companion-api/)

I would love to publish the watch face, I would also love it go be part of the xdrip community, what would I need to do to be a part of it?

jamorham commented 6 years ago

@Rytiggy I'm not exactly sure what you're asking but within xDrip we have automated facilities to install Pebble and Android wear watchfaces and they come bundled within the app for simplicity. We could do the same for a fitbit watchface if it can be sideloaded easily.

If you're just talking about the watch face source code then just publish a repository and we can link to it in the documentation. Anything else feel free to drop me an email, my username at gmail

Rytiggy commented 6 years ago

@jamorham okay awesome I will doublecheck the side loading; if not we can link it in the documentation. We'll be in touch :)

jamorham commented 6 years ago

@Rytiggy Is it possible to access the step counter / heart rate within your watch face? Can you send the step counter in to xDrip using the ?steps= interface I just added: See updated documentation: https://github.com/NightscoutFoundation/xDrip/blob/master/Documentation/technical/Local_Web_Services.md

Rytiggy commented 6 years ago

@jamorham I will see if it's possible, I think It should be.

danpowell88 commented 6 years ago

@Rytiggy do you the watch face anywhere, I'm considering buying an ionic as my garmin just died and would like to try it

Rytiggy commented 6 years ago

@danpowell88 I am going to upload the code soon but I do have a repo that I am working on that will eventually show you how to set up the watch.

https://github.com/Rytiggy/api-watchface

Rytiggy commented 6 years ago

@danpowell88 @jamorham code has been posted, @jamorham I still have not found the time to test the step counter.

Rytiggy commented 6 years ago

@jamorham I looked into this and it should be possible to send the steps to xdrip. What would need to happen is that the watch would need to send the data (steps) to the companion app which would then send the step with a fetch request to xdrips endpoint. I can implement this next functionality soon.

jamorham commented 6 years ago

@Rytiggy nice one! How do users install it? Is there some package file?

Is it suitable to enter fitbit competion? https://dev.fitbit.com/app-champ-2017/ (last day to enter!)

Rytiggy commented 6 years ago

@jamorham I have the .fba file but haven't finished the instructions yet on how to get everything working. I will be posting them soon to the git.

I didn't even know that that was going on! thank you for letting me know I have submitted my code for review.

danpowell88 commented 6 years ago

Managed to get the code loaded on mine, need a way to change it to mmol though, @jamorham wondering if the json should automatically output in the units that xdrip is setup to use rather than defaulting to mg/dl?

PedanticAvenger commented 6 years ago

Wrapping my head around the ionic code @Rytiggy has. For a UX consistency perspective I would back the suggestion from @danpowell88 for the json exporting units based on xdrip settings, that way the app on either fitbit or garmin would only need to look for the units to set the graph parameters and not have to do any calculations on each reading. Small calculation definitely but why do something you don't need to?

swissalpine commented 6 years ago

Please don't change it. Let it be the same output as the "official" Nightscout json response. All my apps are recalculating this and I don't like to change all. And I wish to have the possibility to build apps, where the user can decide: Nightscout Download or xDrip+.

Thanks! Andreas

2018-01-13 21:25 GMT+01:00 raymond-richmond notifications@github.com:

Wrapping my head around the ionic code @Rytiggy https://github.com/rytiggy has. For a UX consistency perspective I would back the suggestion from @danpowell88 https://github.com/danpowell88 for the json exporting units based on xdrip settings, that way the app on either fitbit or garmin would only need to look for the units to set the graph parameters and not have to do any calculations on each reading. Small calculation definitely but why do something you don't need to?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/NightscoutFoundation/xDrip/issues/261#issuecomment-357464864, or mute the thread https://github.com/notifications/unsubscribe-auth/AL8BPdA_wyiTCi4zqV4WeGmLdwFeBkMPks5tKRFIgaJpZM4RVT2J .

PedanticAvenger commented 6 years ago

Ahhh, wasn't considering that we used an "official" format. In that case yeah, it would complicate matters to start changing. If something were to be "bundled" with Xdrip however I would resume this discussion.

swissalpine commented 6 years ago

Thank you for coming back and understand my wishes!

You can test the "official" formats with your Nightscout url, f. e.: your-name.herokuapp.com/api/v1/entries/sgv.json&count=24 ~.herokuapp.com/api/v1/profile ~.herokuapp.com/api/v1/status ~.herokuapp.com/pebble?count=24

In every case Nightscout is sending values in mg/dl (afaik), all values stored in mlab are in mg/dl. So it is calculate in every app which works with these data. That's sound unnecessary, but I thinks it is not bad, it's easier! All my calculations in my apps (for plotting the graph, firering alarms) are done only with mg/dl. The difference is "only" the shown scale. I don't have two different formulas. The only thing I have is one function to change mg to mmol. I use it only three times for displaying Delta, the scale of the graph und the BG himself. In the function is something like that (it's GARMINS monkey c):

function mmol( bg ) { return (0.0555 * bg).format("%.1f"); }

If Jamorham gives you a sole response it doesn't concern me, but please let the sgv.json output unchanged. Andreas

2018-01-14 3:13 GMT+01:00 raymond-richmond notifications@github.com:

Ahhh, wasn't considering that we used an "official" format. In that case yeah, it would complicate matters to start changing. If something were to be "bundled" with Xdrip however I would resume this discussion.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/NightscoutFoundation/xDrip/issues/261#issuecomment-357483203, or mute the thread https://github.com/notifications/unsubscribe-auth/AL8BPbU0uVgMiIsv4UAIK40P5HFMWwEaks5tKWLggaJpZM4RVT2J .

jamorham commented 6 years ago

@raymond-richmond I could add an extra field to indicate whether xDrip is set to mmol/mgdl so that the watchface can assume the same conversion without requiring the user to specify it.

For all developers I have updated the documentation which explains how to use the heart rate and tasker endpoints and how to trigger those from the sgv endpoint. Also note the new OSNOOZE tasker feature which would be suitable for uncontrolled button presses on the watchface.

https://github.com/NightscoutFoundation/xDrip/blob/master/Documentation/technical/Local_Web_Services.md

jamorham commented 6 years ago

For reference @swissalpine has published the garmin watchface which can be obtained here: https://apps.garmin.com/en-EN/apps/fc892009-996a-4907-b277-0c5bfa6fdab2

Klaus3d3 commented 6 years ago

does'nt seem to work with my garmin fenix3hr. i'dont' know if there are any additional permitions are needed.

swissalpine commented 6 years ago

Hi Klaus,

can you describe your problem. Do get an error with the code 2? Then you have to downgrade Garmin Connect Mobile. As you can read in Google Play Store the actual version has massive problems with bluetooth connections. I'm fine with this Version: https://www.apkmirror.com/apk/garmin/garmin-connect-mobile/garmin-connect-mobile-4-1-5-release/garmin-connect-mobile-4-1-5-android-apk-download/

If the app crashes then please deinstall and reinstall the app. I've build a new version.

Regards Andreas

swissalpine commented 6 years ago

And: Have you activated the web service feature in xDrip+? Do you have the actual nightly?

Klaus3d3 commented 6 years ago

What have you done? I was already on Connect 4.1.5 and reseted the watch and my phone multible times. Still only got the IQ! Screen. I reinstalled the app on the watch and now it works. But before and after reinstallation its version 1.21.

swissalpine commented 6 years ago

The shown version in Garmin Connect Mobile App is the last version in the app store, not the installed one. ;-) I haven't secured the case if the json-response contains no aaps strings. I expected that the string will be empty but part of the response. This should be fixed now. I'm happy to read that the app does its works now.

Klaus3d3 commented 6 years ago

Thanks for your work. One very minor thing: Your app seems to round differently than xdrip. Your app just cuts off the decimals, or the xdrip web service does this. But the values are not the same very often.

swissalpine commented 6 years ago

Yes. That's possible and it's the same thing if you look at the readings in Nightscout. There are also sometime different than xDrip. Are you using mmol? That's the only thing I can influence and my formula could be an other than xDrip.

Klaus3d3 commented 6 years ago

No, i'm using mg/dl. It's just a diffence of max 1 mg/dl, so not a big deal. But it confuses a bit if you look on your watch and then on your phone and its not the same.

swissalpine commented 6 years ago

I've looked in the code. @jamorham constant MG_DL_TO_MMOL_L is 0.05556, mine is 0.0555. I will update this but I don't think that's a big deal ... Garmins SDK is restricted in mathematics, so I couldn't do so much. But if Nightscout and xDrip differs (at this moment Nightscout: 86 mg/dl, xDrip 87 mg/dl) and we think about the relliability of CGM readings I think that not a big thing ...

swissalpine commented 6 years ago

Oh, and I don't round mg/dl values. That's the json output which I take unchanged and I could not influence (and I think the same difference as between Nightscout and xDrip). You can see the xDrip output in your smartphone browser by entering 127.0.0.1:17580/sgv.json