8slp 中文文档教程

发布于 4年前 浏览 32 更新于 3年前

8slp

未发布的 EightSleep API 的客户端。

这个客户端是 v1 版本,有一些可以帮助实现的愿望清单项目:

  • Calling methods score() on past sessions
  • Implementing a better cache
  • More tests!
  • Improved documentation

Installation

npm install --save 8slp

yarn install 8slp

Usage

const EightSleep = require('8slp')

const eightSleep = new EightSleep('America/New_York')
await eightSleep.authenticate('fakeemail@email.com', 'hunter7')

const isActive = eightSleep.me.isHeating()
const mySide = eightSleep.me.whoami()

console.log(`I sleep on the ${mySide} side of the bed.`)

if (isActive) {
  console.log(`My side of the bed is currently on!`)
}

const [date, roomTempC, roomTempF] = eightSleep.me.roomTemp()

console.log(`My room is currently ${roomTempF}F`)

API

EightSleep< /code> 是包含和管理床两侧信息的全局类。 当调用authenticate 时,会实例化三个成员:meleftright

这些成员中的每一个都可以调用方法来获取有关床那一侧的特定信息。 me 是登录用户的别名。

new EightSleep

可以使用 .cache 直接访问内部缓存

  • async authenticate(email, password): void
    • Authenticate using the given email and password
    • After authentication, getSides and currentDeviceId are called and return values persisted to internal cache
    • Returns void
  • async getSides(): [ leftUserId, rightUserId ]
    • Get user IDs for both sides of the Pod
    • Persists return to internal cache
    • Returns user IDs as array
  • `async currentDeviceId(): string
    • Get current device ID of the account
    • Persists return to internal cache
    • Returns device ID

Sides

在实例化 new EightSleep 并调用 authenticate() 之后,您的实例将具有 meleftright 填充了实例化的 EightSleepSide 类。 这些方面可以单独解决,并要求提供有关当前会话或过去会话的信息。

内部缓存可以直接用 eightSleep..cache 寻址,这对于检索所有以前会话的信息很有用,而不仅仅是当前的或以前

  • async refresh()
    • Request refresh data, useful for getting up-to-date info of current session
    • Data is persisted to internal cache and retrievable through following methods
  • whoami()
    • Returns either left or right. Useful for determining which side me is referencing
  • async setLevel(level: number, duration: number): object
    • Set desired heating / cooling level for a specific duration
    • Levels range from 0 - 100
      • TODO: Figure out the mapping between app and 0 - 100 levels
    • Returns response from API call (I dunno)
  • isHeating(): boolean
    • Whether the side is currently on, has nothing to do with what level it is on, just that is active.
  • targetLevel(): number
    • Heating / cooling level being targeted by the side
  • lastSeen(): datestring
    • Datestring of the last time this side was active
  • status(): object
    • Object of [ targetHeatingLevel, heatingLevel, heatingDuration, nowHeating, presenceEnd ]
    • Useful for gathering info from isHeating(), targetLevel(), and lastSeen() in one call
  • session({ [date] }): object
    • Takes optional date param
      • If date is not specified, it tries to return current session. If a session isn't active, it returns null
    • Returns dump of information about a session: trends, intervals and scores.
      • If you want all the info at once, use this.
  • sleepStage(): string
    • Get the latest sleep stage of the current session
    • If a session is not active, returns null
  • roomTemp(): [ date, roomTempC, roomTempF ]
    • Get the latest room temp event of the current session
    • If a session is not active, returns null
  • bedTemp(): [ date, bedTempC, bedTempF ]
    • Get the latest bed temp event of the current session
    • If a session is not active, returns null
  • tossAndTurns(): [ date, tossAndTurns ]
    • Get the latest toss-and-turns event of the current session
    • If a session is not active, returns null
  • heartRate(): [ date, heartRate ]
    • Get the latest heart-rate event of the current session
    • If a session is not active, returns null
  • heartRateVariability(): [ date, heartRateVariability ]
    • Get the latest heart-rate variability event of the current session
    • If a session is not active, returns null
  • respiratoryRate(): [ date, respiratoryRate ]
    • Get the latest respiratory rate event of the current session
    • If a session is not active, returns null
  • scores({ [date] }): object
    • Takes optional date param
      • If date is not specified, it tries to return current session. If a session isn't active, it returns null
    • Returns base scores for given session
      • score
      • sleepFitnessScore
        • total
        • sleepDurationSeconds (Score Object)
          • current
          • average
          • score
          • weighted
        • latencyAsleepSeconds (Score Object)
        • latencyOutSeconds (Score Object)
        • wakeupConsistency(Score Object)
  • sleepBreakdown({ [date] }): object
    • Summarized sleep breakdown by stages
    • Returns map of { awake, light, deep, rem } total duration of given session
  • previousSession(): object
    • Returns dump of information about previous session: trends, intervals and scores.
      • If you want all the info at once, use this.

Cache

的转储主类,me、leftright共享一个缓存(实际上只是一个对象)来共享信息,防止重新请求信息

  • get(key)
    • Get a specific key
    • Allows nested keys such as "<userId>.currentSession"
  • `put(key, value)
    • Overwrite a specific key
    • Allows nested keys such as "<userId>.currentSession"
  • userGet(key)
    • Only works on me, left and right
    • Allows nested keys such as "<userId>.currentSession"
    • Allows you to skip adding <userId> in get calls
  • userPut(key, value)
    • Only works on me, left and right
    • Allows nested keys such as "<userId>.currentSession"
    • Allows you to skip adding <userId> in put calls
  • merge(key, value, [mergeKey])
    • mergeKey is only necessary when merging arrays
      • If you want to merge elements on date, you would pass 'date' as mergeKey
    • Merge objects or arrays in the cache together
      • value gets precedence and overwrites pre-existing values
      • Allows nested keys such as "<userId>.currentSession"

8slp

Client for unpublished EightSleep API.

This client is a v1 and has some wishlist items you could help implement:

  • Calling methods score() on past sessions
  • Implementing a better cache
  • More tests!
  • Improved documentation

Installation

npm install --save 8slp

yarn install 8slp

Usage

const EightSleep = require('8slp')

const eightSleep = new EightSleep('America/New_York')
await eightSleep.authenticate('fakeemail@email.com', 'hunter7')

const isActive = eightSleep.me.isHeating()
const mySide = eightSleep.me.whoami()

console.log(`I sleep on the ${mySide} side of the bed.`)

if (isActive) {
  console.log(`My side of the bed is currently on!`)
}

const [date, roomTempC, roomTempF] = eightSleep.me.roomTemp()

console.log(`My room is currently ${roomTempF}F`)

API

EightSleep is the global class that contains and manages info for both sides of the bed. When authenticate is called, three members are instantiated: me, left and right.

Each of these members can call methods to get specific info about that side of the bed. me is an alias the side of the logged-in user.

new EightSleep

Internal cache can be directly accessed using <instance>.cache

  • async authenticate(email, password): void
    • Authenticate using the given email and password
    • After authentication, getSides and currentDeviceId are called and return values persisted to internal cache
    • Returns void
  • async getSides(): [ leftUserId, rightUserId ]
    • Get user IDs for both sides of the Pod
    • Persists return to internal cache
    • Returns user IDs as array
  • `async currentDeviceId(): string
    • Get current device ID of the account
    • Persists return to internal cache
    • Returns device ID

Sides

After instantiating new EightSleep and calling authenticate(), your instance will have me, left and right populated with instatiated classes of EightSleepSide. These sides can individually be addressed and have information requested about the current session or past sessions.

Internal cache can be addressed directly with eightSleep.<side>.cache, useful for retrieving info of all previous sessions rather than just current or a dump of previous

  • async refresh()
    • Request refresh data, useful for getting up-to-date info of current session
    • Data is persisted to internal cache and retrievable through following methods
  • whoami()
    • Returns either left or right. Useful for determining which side me is referencing
  • async setLevel(level: number, duration: number): object
    • Set desired heating / cooling level for a specific duration
    • Levels range from 0 - 100
      • TODO: Figure out the mapping between app and 0 - 100 levels
    • Returns response from API call (I dunno)
  • isHeating(): boolean
    • Whether the side is currently on, has nothing to do with what level it is on, just that is active.
  • targetLevel(): number
    • Heating / cooling level being targeted by the side
  • lastSeen(): datestring
    • Datestring of the last time this side was active
  • status(): object
    • Object of [ targetHeatingLevel, heatingLevel, heatingDuration, nowHeating, presenceEnd ]
    • Useful for gathering info from isHeating(), targetLevel(), and lastSeen() in one call
  • session({ [date] }): object
    • Takes optional date param
      • If date is not specified, it tries to return current session. If a session isn't active, it returns null
    • Returns dump of information about a session: trends, intervals and scores.
      • If you want all the info at once, use this.
  • sleepStage(): string
    • Get the latest sleep stage of the current session
    • If a session is not active, returns null
  • roomTemp(): [ date, roomTempC, roomTempF ]
    • Get the latest room temp event of the current session
    • If a session is not active, returns null
  • bedTemp(): [ date, bedTempC, bedTempF ]
    • Get the latest bed temp event of the current session
    • If a session is not active, returns null
  • tossAndTurns(): [ date, tossAndTurns ]
    • Get the latest toss-and-turns event of the current session
    • If a session is not active, returns null
  • heartRate(): [ date, heartRate ]
    • Get the latest heart-rate event of the current session
    • If a session is not active, returns null
  • heartRateVariability(): [ date, heartRateVariability ]
    • Get the latest heart-rate variability event of the current session
    • If a session is not active, returns null
  • respiratoryRate(): [ date, respiratoryRate ]
    • Get the latest respiratory rate event of the current session
    • If a session is not active, returns null
  • scores({ [date] }): object
    • Takes optional date param
      • If date is not specified, it tries to return current session. If a session isn't active, it returns null
    • Returns base scores for given session
      • score
      • sleepFitnessScore
        • total
        • sleepDurationSeconds (Score Object)
          • current
          • average
          • score
          • weighted
        • latencyAsleepSeconds (Score Object)
        • latencyOutSeconds (Score Object)
        • wakeupConsistency(Score Object)
  • sleepBreakdown({ [date] }): object
    • Summarized sleep breakdown by stages
    • Returns map of { awake, light, deep, rem } total duration of given session
  • previousSession(): object
    • Returns dump of information about previous session: trends, intervals and scores.
      • If you want all the info at once, use this.

Cache

The main class, me, left and right share a cache (actually just an object) to share information and prevent re-requesting information

  • get(key)
    • Get a specific key
    • Allows nested keys such as "<userId>.currentSession"
  • `put(key, value)
    • Overwrite a specific key
    • Allows nested keys such as "<userId>.currentSession"
  • userGet(key)
    • Only works on me, left and right
    • Allows nested keys such as "<userId>.currentSession"
    • Allows you to skip adding <userId> in get calls
  • userPut(key, value)
    • Only works on me, left and right
    • Allows nested keys such as "<userId>.currentSession"
    • Allows you to skip adding <userId> in put calls
  • merge(key, value, [mergeKey])
    • mergeKey is only necessary when merging arrays
      • If you want to merge elements on date, you would pass 'date' as mergeKey
    • Merge objects or arrays in the cache together
      • value gets precedence and overwrites pre-existing values
      • Allows nested keys such as "<userId>.currentSession"
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文