iobroker.fitbit-fitness
Version:
retrieves data from FITBIT
261 lines (233 loc) • 9.66 kB
HTML
<html>
<head>
<!-- Load ioBroker scripts and styles-->
<!-- ported from @germanbluefox thx-->
<link rel="stylesheet" type="text/css" href="../../css/adapter.css" />
<link rel="stylesheet" type="text/css" href="../../lib/css/materialize.css">
<script type="text/javascript" src="../../lib/js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="../../socket.io/socket.io.js"></script>
<script type="text/javascript" src="../../js/translate.js"></script>
<script type="text/javascript" src="../../lib/js/materialize.js"></script>
<script type="text/javascript" src="../../js/adapter-settings.js"></script>
<!-- Load our own files -->
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="words.js"></script>
<script type="text/javascript">
var clientID = '2387KZ'; //2387KZ
var clientSecret = 'bf343e0474cca869afb218975585b2e2';
// Original
// var clientID = '22BD68';
// var clientSecret = 'c4612114c93436901b6affb03a1e5ec8';
// var clientID = '2387KZ';
// var clientSecret = '66f64352fbee230e076360245871bb09';
// This will be called by the admin adapter when the settings page loads
function load(settings, onChange) {
// example: select elements with id=key and class=value and insert value
if (!settings) return;
$('.value').each(function () {
var $key = $(this);
var id = $key.attr('id');
if ($key.attr('type') === 'checkbox') {
// do not call onChange direct, because onChange could expect some arguments
$key.prop('checked', settings[id])
.on('change', () => onChange())
;
} else {
// do not call onChange direct, because onChange could expect some arguments
$key.val(settings[id])
.on('change', () => onChange())
.on('keyup', () => onChange())
;
}
});
$('.clientid').show();
$('.clientsecret').show();
$('.generate_selector').on('change', function () {
var id = $(this).attr('id');
if (id === 'yes_generate') {
$('#no_generate').prop('checked', false);
} else {
$('#yes_generate').prop('checked', false);
}
});
var ip = location.host;
$('.table-button-do').on('click', function () {
var url = `https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=${clientID}&redirect_uri=https%3A%2F%2FChris-656.github.io%2Ffitbit.web%2FgetCode.html&scope=activity%20heartrate%20location%20nutrition%20profile%20settings%20sleep%20social%20weight&expires_in=2592000000`;
var popup = window.open(url, "mywindow", "width=850, height=850");
});
var eventMethod = window.addEventListener ? 'addEventListener' : 'attachEvent';
var eventer = window[eventMethod];
var messageEvent = eventMethod === 'attachEvent' ? 'onmessage' : 'message';
eventer(messageEvent, function (e) {
//$('#iframe_div').hide();
$('.tokens').show();
$('.no-iframe').show();
if (e.data.code) {
var urlGetAuth = `grant_type=authorization_code&clientId=${clientID}&redirect_uri=https%3A%2F%2FChris-656.github.io%2Ffitbit.web%2FgetCode.html&code=${e.data.code}`;
$.ajax({
url: 'https://api.fitbit.com/oauth2/token',
type: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + btoa(clientID + ':' + clientSecret),
},
data: urlGetAuth
}).done(function (data) {
var time = new Date();
time.setSeconds(time.getSeconds() + data.expires_in);
socket.emit('setState', 'fitbit-fitness.' + instance + '.tokens.access', data.access_token, true);
socket.emit('setState', 'fitbit-fitness.' + instance + '.tokens.refresh', data.refresh_token, true);
socket.emit('setState', 'fitbit-fitness.' + instance + '.tokens.expire', time.toISOString(), true);
//window.alert(`AT:${data.access_token} RT:${data.refresh_token} ex:${time.toISOString()}`);
$('#accessToken').val(data.access_token);
$('#refreshToken').val(data.refresh_token);
$('#expireOn').val(time.toISOString());
M.updateTextFields();
}).fail(function (err) {
window.alert("Error" + err);
});
}
}, false);
socket.emit('getState', 'fitbit-fitness.' + instance + '.tokens.access', function (err, state) {
$('#accessToken').val(state ? state.val || '' : '');
});
socket.emit('getState', 'fitbit-fitness.' + instance + '.tokens.refresh', function (err, state) {
$('#refreshToken').val(state ? state.val || '' : '');
});
socket.emit('getState', 'fitbit-fitness.' + instance + '.tokens.expire', function (err, state) {
$('#expireOn').val(state ? state.val || '' : '');
if (state && state.val) {
$('.table-button-do span').html(_('Update token'));
}
setTimeout(function () { M.updateTextFields() }, 500);
});
var $sleeprecords = $('#sleeprecords');
$sleeprecords.on('change', function () {
var val = $(this).prop('checked');
if (val) {
$('.sleepschedule').show();
} else {
$('.sleepschedule').hide();
onChange();
}
});
onChange(false);
// reinitialize all the Materialize labels on the page if you are dynamically adding inputs:
if (M) M.updateTextFields();
}
// This will be called by the admin adapter when the user presses the save button
function save(callback) {
// example: select elements with class=value and build settings object
var obj = {};
$('.value').each(function () {
var $this = $(this);
if ($this.attr('type') === 'checkbox') {
obj[$this.attr('id')] = $this.prop('checked');
} else if ($this.attr('type') === 'number') {
obj[$this.attr('id')] = parseFloat($this.val());
} else {
obj[$this.attr('id')] = $this.val();
}
});
callback(obj);
}
</script>
</head>
<body>
<div class="m adapter-container">
<div class="row">
<div class="col s12">
<ul class="tabs">
<li class="tab col s2"><a href="#tab-main" class="translate active">Main settings</a></li>
<li class="tab col s2"><a href="#tab-services" class="translate">Services</a></li>
</ul>
</div>
<div id="tab-main" class="col s12 page">
<div class="row">
<div class="col s6">
<img src="fitbit-fitness.png" alt="logo" class="logo">
</div>
</div>
<div class="row generate no-iframe">
<a class="waves-effect waves-light btn blue table-button-do"><i class="material-icons">add</i><span
class="translate">Authorize</span></a>
</div>
<div class="row generate" id="iframe_div">
<iframe id="iframe" name="iframe" src=""
style="display: none; width: 100%; height: calc(100% - 50px); border: 0;">
</iframe>
</div>
<div class="row tokens generate no-iframe">
<div class="input-field col s22">
<i class="material-icons prefix">account_circle</i>
<input id="accessToken" type="text" readonly>
<label for="accessToken" class="translate">accessToken</label>
</div>
</div>
<div class="row tokens generate no-iframe">
<div class="input-field col s22">
<i class="material-icons prefix">account_circle</i>
<input id="refreshToken" type="text" readonly>
<label for="refreshToken" class="translate">Refresh token</label>
</div>
</div>
<div class="row tokens generate no-iframe">
<div class="input-field col s12 m6 24">
<i class="material-icons prefix">timelapse</i>
<input id="expireOn" type="text" readonly>
<label for="expireOn" class="translate">Expires on</label>
</div>
</div>
</div>
<div id="tab-services" class="col s12 page">
<div class="row">
<div class="col s6">
<img src="fitbit-fitness.png" alt="logo" class="logo">
</div>
</div>
<div class="row no-iframe">
<div class="input-field col s3">
<i class="material-icons prefix">refresh</i>
<input class="value" id="refresh" type="number" placeholder="10">
<label for="refresh" class="translate">refresh</label>
</div>
</div>
<div class="row">
<div class="input-field col s12 m6 l4">
<input class="value" id="bodyrecords" type="checkbox">
<span for="bodyrecords" class="translate">bodyrecords</span>
</div>
</div>
<div class="row">
<div class="input-field col s12 m6 l4">
<input class="value" id="activityrecords" type="checkbox">
<span for="activityrecords" class="translate">activityrecords</span>
</div>
</div>
<div class="row">
<div class="input-field col s3 m6 l4">
<input class="value" id="sleeprecords" type="checkbox">
<span for="sleeprecords" class="translate">sleeprecords</span>
</div>
<div class="input-field sleepschedule col s6">
<input class="value" id="sleeprecordsschedule" type="checkbox">
<span for="sleeprecordsschedule" class="translate">sleeprecordsschedule</span>
</div>
</div>
<div class="row">
<div class="input-field col s12 m6 l4">
<input class="value" id="foodrecords" type="checkbox">
<span for="foodrecords" class="translate">foodrecords</span>
</div>
</div>
<div class="row">
<div class="input-field col s12 m6 l4">
<input class="value" id="devicerecords" type="checkbox">
<span for="devicerecords" class="translate">devicerecords</span>
</div>
</div>
</div>
</div>
</div>
</body>
</html>