Skip to content

Commit

Permalink
fix: cleanup bin source code
Browse files Browse the repository at this point in the history
  • Loading branch information
mman committed Oct 17, 2024
1 parent cc9bfa8 commit fd97815
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/bin/venus-influx-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import { program } from "commander"
// @ts-expect-error
const buildInfo = await import("../buildInfo.cjs")
import buildInfo from "../buildInfo.cjs"

import Server from "../server/server.cjs"
import { Server } from "../server/server"

program
.version(buildInfo.buildVersion)
Expand Down
68 changes: 30 additions & 38 deletions src/bin/venus-upnp-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import { program } from "commander"
// @ts-expect-error
const buildInfo = await import("../buildInfo.cjs")
import buildInfo from "../buildInfo.cjs"

import axios from "axios"

import upnp from "../server/upnp.cjs"
import { UPNP } from "../server/upnp"
import { LogEntry, LogLevel } from "../shared/types"
import { DiscoveredDevice } from "../shared/state"

program
.version(buildInfo.buildVersion)
Expand All @@ -27,52 +29,22 @@ log(`Discovery API: ${options.discoveryApi}`)
const logEndpoint = new URL("log", options.discoveryApi)
const discoveryEndpoint = new URL("upnpDiscovered", options.discoveryApi)

// upnp browser
const browser = upnp({
getLogger: (_name: string) => {
return {
info: (message: string) => {
log(message)
postLog(logEndpoint, { level: "info", message: message })
},
error: (message: string) => {
log(message)
postLog(logEndpoint, { level: "error", message: message })
},
}
},
emit: (event: string, data: any) => {
if (event === "upnpDiscovered") {
postDiscovery(discoveryEndpoint, data)
}
},
})

interface LogMessage {
level: string
message: string
}

interface DiscoveryMessage {
data: string
}

// cache discovered venus devices and log entries
// so they can be posted to discoveryApi when ready
let cachedLogs: LogMessage[] = []
let cachedDiscovery: DiscoveryMessage[] = []
let cachedLogs: LogEntry[] = []
let cachedDiscovery: DiscoveredDevice[] = []

function postLog(endpoint: URL, info: LogMessage) {
function postLog(endpoint: URL, entry: LogEntry) {
axios
.post(endpoint.toString(), info)
.post(endpoint.toString(), entry)
.then((response) => response.data)
.catch((err) => {
log(`Failed to postLog to ${endpoint}, error: ${err.message}`)
cachedLogs.push(info)
cachedLogs.push(entry)
})
}

function postDiscovery(endpoint: URL, info: DiscoveryMessage) {
function postDiscovery(endpoint: URL, info: DiscoveredDevice) {
axios
.post(endpoint.toString(), info)
.then((response) => response.data)
Expand All @@ -96,6 +68,26 @@ setInterval(() => {
})
}, 5000)

// server mock
const serverMock = {
getLogger: (name: string) => {
return {
log: (level: LogLevel, message: string) => {
log(message)
postLog(logEndpoint, { timestamp: Date.now().toString(), label: name, level: level, message: message })
},
}
},
emit: (event: string, data: any) => {
if (event === "upnpDiscovered") {
postDiscovery(discoveryEndpoint, data)
}
},
}

// upnp browser
const browser = new UPNP(serverMock)

// exit on Ctrl-C
process.on("SIGINT", function () {
browser.stop()
Expand Down

0 comments on commit fd97815

Please sign in to comment.