Skip to content

Commit

Permalink
Separation of OAuth from connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonic853 committed Feb 12, 2023
1 parent 1278a37 commit 6467acc
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 115 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ dist

bin/
package-lock.json
token.txt
128 changes: 15 additions & 113 deletions app.js
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()
})

40 changes: 40 additions & 0 deletions auth.js
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
97 changes: 97 additions & 0 deletions main.js
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
19 changes: 18 additions & 1 deletion method.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const WebSocket = require('ws')
const fs = require('fs')
class WS {
ip = ""
port = ""
Expand Down Expand Up @@ -187,5 +188,21 @@ const stringIsnullOrEmpty = (str) => {
if (str == null || str == undefined || str == "") return true
return false
}
/**
*
* @param {string} path
* @returns {string}
*/
const ReadFile = (path) => {
// 判断文件是否存在
if (!fs.existsSync(path)) {
return ""
}
// 读取文件
const main = fs.readFileSync(path, "utf-8")
return main
}
exports.getUuid = getUuid
exports.WS = WS
exports.WS = WS
exports.stringIsnullOrEmpty = stringIsnullOrEmpty
exports.ReadFile = ReadFile
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "",
"main": "app.js",
"scripts": {
"run": "node --inspect app.js"
"run": "node --inspect app.js",
"authAndRun": "node --inspect startAuth.js"
},
"author": "Sonic853",
"license": "MIT",
Expand Down
12 changes: 12 additions & 0 deletions startAuth.js
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)
})

0 comments on commit 6467acc

Please sign in to comment.