-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2d15876
commit e7f7e8f
Showing
22 changed files
with
398 additions
and
76 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# can2x | ||
|
||
`can2x` is a simple utility for connecting a CAN bus bidirectional with another CAN bus over the network using common web protocols, such as HTTP, MQTT, Socket.IO, and WebSockets. | ||
`can2x` is a simple utility for connecting a CAN bus bidirectional with one or multiple CAN busses over the network using common web protocols, such as HTTP, MQTT, Socket.IO, and WebSockets. | ||
Please check out our [repository](https://github.com/milesstoetzner/stoetzms-can2x). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {startBus} from '#/actions/start-bus' | ||
import {startVCAN} from '#/actions/start-vcan' | ||
import {stopVCAN} from '#/actions/stop-vcan' | ||
import {startBridge} from './start-bridge' | ||
|
||
export default { | ||
bridge: { | ||
start: startBridge, | ||
}, | ||
bus: { | ||
start: startBus, | ||
}, | ||
vcan: { | ||
start: startVCAN, | ||
stop: stopVCAN, | ||
}, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {SocketIOBus} from '#/bus/socketio' | ||
import std from '#std' | ||
|
||
export type BusOptions = { | ||
bus?: string | ||
port?: number | ||
host?: string | ||
event?: string | ||
} | ||
|
||
export async function startBus(options: BusOptions) { | ||
std.log('can2x bus', {options}) | ||
|
||
const bus = createBus(options) | ||
|
||
std.log('starting bus') | ||
await bus.start() | ||
await bus.ready() | ||
return bus | ||
} | ||
|
||
function createBus(options: BusOptions) { | ||
if (options.bus === 'socketio') | ||
return new SocketIOBus({ | ||
port: options.port ? Number(options.port) : 3000, | ||
host: options.host ?? 'localhost', | ||
event: options.event ?? 'can2x', | ||
}) | ||
|
||
throw new Error(`Bus of type "${options.bus}" unknown`) | ||
} |
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,11 @@ | ||
import {VCAN, VCANOptions} from '#core/vcan' | ||
import std from '#std' | ||
|
||
export async function startVCAN(options: VCANOptions) { | ||
std.log('can2x vcan create', {options}) | ||
|
||
std.log('creating vcan') | ||
const vcan = new VCAN(options) | ||
await vcan.start() | ||
return vcan | ||
} |
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,11 @@ | ||
import {VCAN, VCANOptions} from '#core/vcan' | ||
import std from '#std' | ||
|
||
export async function stopVCAN(options: VCANOptions) { | ||
std.log('can2x vcan delete', {options}) | ||
|
||
std.log('deleting vcan') | ||
const vcan = new VCAN(options) | ||
await vcan.stop() | ||
return vcan | ||
} |
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,17 @@ | ||
import * as utils from '#utils' | ||
|
||
export default abstract class Bus { | ||
protected readyPromise | ||
|
||
protected constructor() { | ||
this.readyPromise = utils.createDecomposedPromise() | ||
} | ||
|
||
abstract start(): Promise<void> | ||
|
||
abstract stop(): Promise<void> | ||
|
||
async ready() { | ||
return this.readyPromise.promise | ||
} | ||
} |
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,65 @@ | ||
import Bus from '#/bus/bus' | ||
import * as check from '#check' | ||
import {JSONMessage} from '#core/message' | ||
import std from '#std' | ||
import http from 'http' | ||
import SocketIO from 'socket.io' | ||
|
||
export type SocketIOBusOptions = { | ||
port: number | ||
host: string | ||
event: string | ||
} | ||
|
||
export class SocketIOBus extends Bus { | ||
io?: SocketIO.Server | ||
server?: http.Server | ||
options: SocketIOBusOptions | ||
|
||
constructor(options: SocketIOBusOptions) { | ||
super() | ||
this.options = options | ||
} | ||
|
||
async start() { | ||
std.log('starting socketio bus') | ||
|
||
this.server = http.createServer() | ||
|
||
this.io = new SocketIO.Server(this.server) | ||
this.io.on('connection', socket => { | ||
std.log(`socketio client connected`, {id: socket.id}) | ||
|
||
socket.on(this.options.event, (message: JSONMessage) => { | ||
std.log('socketio source received', {message}) | ||
socket.broadcast.emit(this.options.event, message) | ||
}) | ||
}) | ||
|
||
this.server.listen({port: this.options.port, host: this.options.host}, () => { | ||
std.log(`socketio bus running on "http://${this.options.host}:${this.options.port}"`) | ||
this.readyPromise.resolve() | ||
}) | ||
|
||
this.server.on('error', error => { | ||
std.log('socketio bus error', {error}) | ||
}) | ||
} | ||
|
||
async stop() { | ||
std.log('stopping socketio bus') | ||
await this.stopServer() | ||
std.log('socketio bus stopped') | ||
} | ||
|
||
private async stopServer() { | ||
if (check.isUndefined(this.server)) return std.log('socketio http server not defined') | ||
const server = this.server | ||
return new Promise<void>(resolve => { | ||
server.close(error => { | ||
if (check.isDefined(error)) std.log(error) | ||
return resolve() | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.