Skip to content

Commit

Permalink
✨ Add UART
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Apr 27, 2023
1 parent 727aa00 commit 5eb8ad6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
40 changes: 33 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import fetch from 'isomorphic-fetch'

type GetterType = "nodeinfo" | "usb" | "power" | "other" | "sdcard"
type SetterType = "usb" | "power" | "firmware" | "network"
type GetterType = "nodeinfo" | "usb" | "power" | "other" | "sdcard" | "uart"
type SetterType = "usb" | "power" | "firmware" | "network" | "uart"
type OnOff = 0 | 1
type NodeIndex = 0 | 1 | 2 | 3


interface UART {
uart: string
}

interface UARTResponse{
response: UART[]
}
interface Other {
version: string
buildtime: string
Expand Down Expand Up @@ -57,7 +65,7 @@ interface NodeInfoResponse {
response: NodeInfo[]
}

type GetResponse = Promise<NodeInfoResponse | NodePowerResponse | SDCardResponse | USBResponse | OtherResponse>
type GetResponse = Promise<NodeInfoResponse | NodePowerResponse | SDCardResponse | USBResponse | OtherResponse | UARTResponse>

interface USBQuery {
[k: string]: OnOff | NodeIndex
Expand All @@ -84,7 +92,18 @@ interface FirmwareQuery {
file: File
}

type Query = USBQuery | PowerQuery | NetworkQuery | FirmwareQuery
interface UARTQuery {
[k: string]: NodeIndex | string
node: NodeIndex,
cmd: string,
}

interface UARTGetQuery {
[k: string]: NodeIndex
node: NodeIndex
}

type Query = USBQuery | PowerQuery | NetworkQuery | FirmwareQuery | UARTQuery

interface OkResult {
result: "ok"
Expand All @@ -97,7 +116,7 @@ interface OkResponse {
type SetResponse = Promise<OkResponse>

interface TuringPiInterface {
get(type: GetterType, options?: RequestInit): GetResponse
get(type: GetterType, query?: UARTGetQuery, options?: RequestInit): GetResponse
set(type: SetterType, query: Query, options?: RequestInit): SetResponse
}

Expand All @@ -110,16 +129,23 @@ export const tpi = (url: URL): TuringPiInterface => {
/**
* Get State from the BMC
* @param type
* @param options
* @param [query]
* @param [options]
*/
async get(
type: GetterType,
options?: RequestInit
query?: UARTGetQuery, // Only UART has extra parameters, might change in the future
options?: RequestInit,
): GetResponse {
// Set Params
const params = new URLSearchParams();
params.set('opt', 'get')
params.set('type', type)
if(typeof query !== 'undefined'){
Object.keys(query).forEach((k)=>{
params.set(k, query[k].toString())
})
}
// Fetch
return fetch(`${url}?${params}`, options)
.then(r => r.json())
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "turing-pi-js",
"version": "1.0.1",
"version": "1.1.0",
"description": "Fetch library for Turing Pi 2 BMC API",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down

0 comments on commit 5eb8ad6

Please sign in to comment.