-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
185 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,3 +106,4 @@ dist | |
|
||
bin/ | ||
package-lock.json | ||
token.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,114 +1,16 @@ | ||
|
||
const { Client } = require('node-osc') | ||
const WebSocket = require('ws') | ||
const readline = require('readline') | ||
const { stdin: input, stdout: output } = require('node:process') | ||
const open = require('open') | ||
const rl = readline.createInterface({ input, output }) | ||
const { WS } = require('./method') | ||
const { pulsoidApi } = require('./pulsoidApi') | ||
const pulsoidAuthPage = () => { | ||
return `${pulsoidApi.baseUrl}?client_id=${pulsoidApi.client_id}&redirect_uri=${pulsoidApi.redirect_uri}&response_type=${pulsoidApi.response_type}&scope=${pulsoidApi.scope}&state=${pulsoidApi.state}&response_mode=${pulsoidApi.response_mode}` | ||
const { ReadFile, stringIsnullOrEmpty } = require('./method') | ||
const { StartAuth } = require('./auth') | ||
const { RunPulsoid } = require('./main') | ||
const token = ReadFile('token.txt') | ||
if (stringIsnullOrEmpty(token)) { | ||
StartAuth().then(_token => { | ||
if (stringIsnullOrEmpty(_token)){ | ||
console.log('no Enter.') | ||
process.exit(1) | ||
} | ||
RunPulsoid(token) | ||
}) | ||
} | ||
else { | ||
RunPulsoid(token) | ||
} | ||
console.log('Open Pulsoid Auth Page...') | ||
open(pulsoidAuthPage()) | ||
/** | ||
* @type {string} | ||
*/ | ||
rl.question('Enter Pulsoid Auth Token: ', (token) => { | ||
if (token == '') { | ||
console.log('no Enter.') | ||
return | ||
} | ||
let Start = () => { | ||
const wspath = `/api/v1/data/real_time?access_token=${token}` | ||
const ws = new WS("dev.pulsoid.net", "", wspath, true) | ||
let hbToggle = false | ||
ws.message(ev => { | ||
/** | ||
* @type {{ | ||
* data: { | ||
* heartRate: number | ||
* } | ||
* }} | ||
*/ | ||
const data = JSON.parse(ev) | ||
const heartRate = data?.data?.heartRate | ||
if (!heartRate) return console.log('Got heart rate: 0 bpm, skipping parameter update...') | ||
console.log('Got heart rate: %s bpm', heartRate) | ||
const client = new Client('localhost', 9000) | ||
// 参考自该代码: | ||
// https://github.com/vard88508/vrc-osc-miband-hrm/blob/f60c3422c36921d317168ed38b1362528e8364e9/app.js#L24-L50 | ||
const Heartrates = [ | ||
{ | ||
address: '/avatar/parameters/Heartrate', | ||
args: | ||
{ | ||
type: 'f', | ||
value: data.data.heartRate / 127 - 1 | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/HeartRateFloat", | ||
args: | ||
{ | ||
type: "f", | ||
value: data.data.heartRate / 127 - 1 | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/Heartrate2", | ||
args: | ||
{ | ||
type: "f", | ||
value: data.data.heartRate / 255 | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/HeartRateFloat01", | ||
args: | ||
{ | ||
type: "f", | ||
value: data.data.heartRate / 255 | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/Heartrate3", | ||
args: | ||
{ | ||
type: "i", | ||
value: data.data.heartRate | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/HeartRateInt", | ||
args: | ||
{ | ||
type: "i", | ||
value: data.data.heartRate | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/HeartBeatToggle", | ||
args: | ||
{ | ||
type: "b", | ||
value: hbToggle | ||
} | ||
} | ||
] | ||
Heartrates.forEach(element => { | ||
client.send(element) | ||
if (element.address === "/avatar/parameters/HeartBeatToggle") { | ||
hbToggle = !hbToggle | ||
} | ||
}); | ||
}) | ||
ws.realclose = false | ||
ws.connect().then(() => { | ||
console.log('Connected to Pulsoid.') | ||
}) | ||
} | ||
Start() | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const readline = require('readline') | ||
const { stdin: input, stdout: output } = require('node:process') | ||
const open = require('open') | ||
const rl = readline.createInterface({ input, output }) | ||
const { pulsoidApi } = require('./pulsoidApi') | ||
const fs = require('fs') | ||
const pulsoidAuthPage = () => { | ||
return `${pulsoidApi.baseUrl}?client_id=${pulsoidApi.client_id}&redirect_uri=${pulsoidApi.redirect_uri}&response_type=${pulsoidApi.response_type}&scope=${pulsoidApi.scope}&state=${pulsoidApi.state}&response_mode=${pulsoidApi.response_mode}` | ||
} | ||
/** | ||
* | ||
* @param {*} callback | ||
* @returns {Promise<string>} | ||
*/ | ||
const StartAuth = async (callback) => { | ||
return new Promise((resolve, reject) => { | ||
console.log('Open Pulsoid Auth Page...') | ||
open(pulsoidAuthPage()) | ||
/** | ||
* @type {string} | ||
*/ | ||
rl.question('Enter Pulsoid Auth Token: ', (token) => { | ||
if (token == '') { | ||
console.log('no Enter.') | ||
return reject() | ||
} | ||
// 写入到 token.txt | ||
fs.writeFile('token.txt', token, (err) => { | ||
if (err) { | ||
console.log(err) | ||
return reject() | ||
} | ||
console.log('token.txt saved.') | ||
callback && callback(token) | ||
return resolve(token) | ||
}) | ||
}) | ||
}) | ||
} | ||
exports.StartAuth = StartAuth |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
|
||
const { WS } = require('./method') | ||
const { Client } = require('node-osc') | ||
/** | ||
* | ||
* @param {string} token | ||
*/ | ||
const RunPulsoid = (token) => { | ||
const wspath = `/api/v1/data/real_time?access_token=${token}` | ||
const ws = new WS("dev.pulsoid.net", "", wspath, true) | ||
let hbToggle = false | ||
ws.message(ev => { | ||
/** | ||
* @type {{ | ||
* data: { | ||
* heartRate: number | ||
* } | ||
* }} | ||
*/ | ||
const data = JSON.parse(ev) | ||
const heartRate = data?.data?.heartRate | ||
if (!heartRate) return console.log('Got heart rate: 0 bpm, skipping parameter update...') | ||
console.log('Got heart rate: %s bpm', heartRate) | ||
const client = new Client('localhost', 9000) | ||
// 参考自该代码: | ||
// https://github.com/vard88508/vrc-osc-miband-hrm/blob/f60c3422c36921d317168ed38b1362528e8364e9/app.js#L24-L50 | ||
const Heartrates = [ | ||
{ | ||
address: '/avatar/parameters/Heartrate', | ||
args: | ||
{ | ||
type: 'f', | ||
value: data.data.heartRate / 127 - 1 | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/HeartRateFloat", | ||
args: | ||
{ | ||
type: "f", | ||
value: data.data.heartRate / 127 - 1 | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/Heartrate2", | ||
args: | ||
{ | ||
type: "f", | ||
value: data.data.heartRate / 255 | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/HeartRateFloat01", | ||
args: | ||
{ | ||
type: "f", | ||
value: data.data.heartRate / 255 | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/Heartrate3", | ||
args: | ||
{ | ||
type: "i", | ||
value: data.data.heartRate | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/HeartRateInt", | ||
args: | ||
{ | ||
type: "i", | ||
value: data.data.heartRate | ||
} | ||
}, | ||
{ | ||
address: "/avatar/parameters/HeartBeatToggle", | ||
args: | ||
{ | ||
type: "b", | ||
value: hbToggle | ||
} | ||
} | ||
] | ||
Heartrates.forEach(element => { | ||
client.send(element) | ||
if (element.address === "/avatar/parameters/HeartBeatToggle") { | ||
hbToggle = !hbToggle | ||
} | ||
}); | ||
}) | ||
ws.realclose = false | ||
ws.connect().then(() => { | ||
console.log('Connected to Pulsoid.') | ||
}) | ||
} | ||
exports.RunPulsoid = RunPulsoid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const { stringIsnullOrEmpty } = require('./method') | ||
const { StartAuth } = require('./auth') | ||
const { RunPulsoid } = require('./main') | ||
|
||
StartAuth().then(token => { | ||
if (stringIsnullOrEmpty(token)) { | ||
// 当用户按下回车键时,process.exit() | ||
console.log('no Enter.') | ||
process.exit(1) | ||
} | ||
RunPulsoid(token) | ||
}) |