-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from 3timeslazy/nsv3-refactor
Moved nightscout code into separated folder
- Loading branch information
Showing
11 changed files
with
491 additions
and
312 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,22 @@ | ||
function readConfig() { | ||
const requiredEnvs = ['NIGHTSCOUT_API_TOKEN', 'NIGHTSCOUT_URL']; | ||
for (let envName of requiredEnvs) { | ||
if (!process.env[envName]) { | ||
throw Error(`Required environment variable ${envName} is not set`); | ||
} | ||
} | ||
|
||
const protocol = | ||
process.env.NIGHTSCOUT_DISABLE_HTTPS === 'true' ? 'http://' : 'https://'; | ||
const url = new URL(protocol + process.env.NIGHTSCOUT_URL); | ||
|
||
return { | ||
nightscoutApiToken: process.env.NIGHTSCOUT_API_TOKEN as string, | ||
nightscoutBaseUrl: url.toString(), | ||
|
||
nightscoutApiV3: process.env.NIGHTSCOUT_API_V3 === 'true', | ||
nightscoutDevice: process.env.DEVICE_NAME || 'nightscout-librelink-up', | ||
}; | ||
} | ||
|
||
export default readConfig; |
This file was deleted.
Oops, something went wrong.
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
This file was deleted.
Oops, something went wrong.
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,53 @@ | ||
import { Entry, NightscoutAPI, NightscoutConfig } from './interface'; | ||
import { OutgoingHttpHeaders } from 'http'; | ||
import axios from 'axios'; | ||
|
||
interface NightscoutHttpHeaders extends OutgoingHttpHeaders { | ||
'api-secret': string | undefined; | ||
} | ||
|
||
export class Client implements NightscoutAPI { | ||
readonly baseUrl: string; | ||
readonly headers: NightscoutHttpHeaders; | ||
readonly device: string; | ||
|
||
constructor(config: NightscoutConfig) { | ||
this.baseUrl = config.nightscoutBaseUrl; | ||
this.headers = { | ||
'api-secret': config.nightscoutApiToken, | ||
'User-Agent': 'FreeStyle LibreLink Up NightScout Uploader', | ||
'Content-Type': 'application/json', | ||
}; | ||
this.device = config.nightscoutDevice; | ||
} | ||
|
||
async lastEntry(): Promise<Entry | null> { | ||
const url = new URL('/api/v1/entries?count=1', this.baseUrl).toString(); | ||
const resp = await axios.get(url, { headers: this.headers }); | ||
if (resp.status !== 200) { | ||
throw Error(`failed to get last entry: ${resp.statusText}`); | ||
} | ||
if (!resp.data || resp.data.length === 0) { | ||
return null; | ||
} | ||
return resp.data.pop(); | ||
} | ||
|
||
async uploadEntries(entries: Entry[]): Promise<void> { | ||
const url = new URL('/api/v1/entries', this.baseUrl).toString(); | ||
const entriesV1 = entries.map((e) => ({ | ||
type: 'sgv', | ||
sgv: e.sgv, | ||
direction: e.direction?.toString(), | ||
device: this.device, | ||
date: e.date.getTime(), | ||
dateString: e.date.toISOString(), | ||
})); | ||
const resp = await axios.post(url, entriesV1, { headers: this.headers }); | ||
if (resp.status !== 200) { | ||
throw Error(`failed to post new entries: ${resp.statusText}`); | ||
} | ||
|
||
return; | ||
} | ||
} |
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,15 @@ | ||
import { Entry, NightscoutAPI, NightscoutConfig } from './interface'; | ||
|
||
export class Client implements NightscoutAPI { | ||
constructor(config: NightscoutConfig) { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
async lastEntry(): Promise<Entry | null> { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
async uploadEntries(entries: Entry[]): Promise<void> { | ||
throw new Error('Not implemented'); | ||
} | ||
} |
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,31 @@ | ||
/** | ||
* Interfaces related to the Nightscout API | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
export interface NightscoutAPI { | ||
lastEntry(): Promise<Entry | null>; | ||
uploadEntries(entries: Entry[]): Promise<void>; | ||
} | ||
|
||
export interface NightscoutConfig { | ||
nightscoutApiToken: string; | ||
nightscoutBaseUrl: string; | ||
nightscoutDevice: string; | ||
} | ||
|
||
export interface Entry { | ||
date: Date; | ||
sgv: number; | ||
direction?: Direction; | ||
} | ||
|
||
export enum Direction { | ||
SingleDown = 'SingleDown', | ||
FortyFiveDown = 'FortyFiveDown', | ||
Flat = 'Flat', | ||
FortyFiveUp = 'FortyFiveUp', | ||
SingleUp = 'SingleUp', | ||
NotComputable = 'NOT COMPUTABLE', | ||
} |
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