Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
milesstoetzner committed Sep 20, 2023
1 parent 12bd060 commit 9df3e45
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/core/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export type BridgeOptions = {
sourceName?: string
sourceId?: string
sourceData?: string[]
sourceExt?: boolean
sourceRtr?: boolean
sourceFile?: string
sourceBidirectional?: boolean
target?: string
Expand Down Expand Up @@ -81,6 +83,8 @@ function createSource(options: BridgeOptions) {
return new ConsoleSource({
id: Number(options.sourceId),
data: options.sourceData.map(Number),
ext: options.sourceExt ?? false,
rtr: options.sourceRtr ?? false,
})
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Source from '#/source/source'
import Target from '#/target/target'
import * as assert from '#assert'
import std from '#std'
import hae from '#utils/hae'

Expand Down Expand Up @@ -29,6 +30,7 @@ export class Bridge {
await this.source.receive(
hae.log(async message => {
std.log('bridging forward', {message})
assert.isMessage(message)
await this.target.send(message)
if (!this.source.continuous) await this.stop()
})
Expand All @@ -37,6 +39,7 @@ export class Bridge {
await this.target.receive(
hae.log(async message => {
std.log('bridging backward', {message})
assert.isMessage(message)
await this.source.send(message)
})
)
Expand Down
19 changes: 10 additions & 9 deletions src/core/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import {Message as CANMessage} from '*can.node'
export default class Message {
id: number
data: number[]
ext: boolean
rtr: boolean

// TODO: ext
// TODO: rtr

private constructor(id: number, data: number[]) {
private constructor(id: number, data: number[], ext: boolean, rtr: boolean) {
assert.isNumber(id)
assert.isNumbers(data)

this.id = id
this.data = data
this.ext = ext
this.rtr = rtr
}

static fromJSON(message: {id: number; data: number[]}) {
return new Message(message.id, message.data)
static fromJSON(message: {id: number; data: number[]; ext: boolean; rtr: boolean}) {
return new Message(message.id, message.data, message.ext, message.rtr)
}

toJSON() {
return {id: this.id, data: this.id}
return {id: this.id, data: this.data}
}

static fromString(message: string) {
Expand All @@ -34,11 +35,11 @@ export default class Message {
}

static fromCAN(message: CANMessage): Message {
return this.fromJSON({id: message.id, data: Array.from(message.data)})
return this.fromJSON({id: message.id, data: Array.from(message.data), ext: message.ext, rtr: message.rtr})
}

toCAN(): CANMessage {
return {id: this.id, data: Buffer.from(this.data), ext: false, rtr: false}
return {id: this.id, data: Buffer.from(this.data), ext: this.ext, rtr: this.rtr}
}

static fromArrayBuffer(message: ArrayBuffer | ArrayBuffer[]) {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ bridge
.option('--source-name [string]', '', 'can2x')
.option('--source-id [number]', '')
.option('--source-data [numbers...]', '')
.option('--source-ext [boolean]', '', false) // TODO: docs
.option('--source-rtr [boolean]', '', false) // TODO: docs
.option('--source-bidirectional [boolean]', '', true)
.addOption(
new Option('--target [string]', '')
Expand Down
9 changes: 7 additions & 2 deletions src/source/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Message from '#core/message'
import {Processor} from '#core/types'
import std from '#std'

export type ConsoleSourceOptions = {id: number; data: number[]}
export type ConsoleSourceOptions = {id: number; data: number[]; ext: boolean; rtr: boolean}

export class ConsoleSource extends Source {
options: ConsoleSourceOptions
Expand All @@ -14,7 +14,12 @@ export class ConsoleSource extends Source {
}

async receive(processor: Processor) {
const message = Message.fromJSON({id: this.options.id, data: this.options.data})
const message = Message.fromJSON({
id: this.options.id,
data: this.options.data,
ext: this.options.ext,
rtr: this.options.rtr,
})
std.log('console received', {message})
this.processor = processor
this.processor(message)
Expand Down
2 changes: 1 addition & 1 deletion src/source/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class HTTPSource extends Source {
std.log('http source received', {message: req.body})

if (check.isDefined(this.processor)) {
this.processor(req.body)
this.processor(Message.fromJSON(req.body))
} else {
std.log('no processor defined')
}
Expand Down
4 changes: 2 additions & 2 deletions src/source/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export class WSSource extends Source {
std.log('websocket source error', {error})
})

this.ws.on('message', (message: string) => {
this.ws.on('message', (message: Buffer) => {
std.log('websocket source received', {message})
if (check.isUndefined(this.processor)) return std.log('no processor defined')
this.processor(Message.fromString(message))
this.processor(Message.fromBuffer(message))
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/target/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class ConsoleTarget extends Target {

async send(message: Message) {
std.log('console target sending', {message})
std.out(message.id, message.data)
std.out(message.toString())
std.log('console target sent')
}
}
5 changes: 5 additions & 0 deletions src/utils/assert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as check from '#check'
import Message from '#core/message'
import * as utils from '#utils'

export function isDefined<T>(element: T | undefined | null, msg: string): asserts element is T {
Expand Down Expand Up @@ -51,3 +52,7 @@ export function isName(name: string) {
export function isBuffer(element: unknown): asserts element is Buffer {
if (!check.isBuffer(element)) throw new Error(`Element "${element} is not a buffer`)
}

export function isMessage(element: any): asserts element is Message {
if (!(element instanceof Message)) throw new Error(`Object "${element}" is not a message"`)
}

0 comments on commit 9df3e45

Please sign in to comment.