Add electron
This commit is contained in:
13
backend/src/DataSource/DataSource.ts
Normal file
13
backend/src/DataSource/DataSource.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { DataSourceState } from './'
|
||||
|
||||
type MessageCallback = (topic: string, payload: Buffer) => void
|
||||
|
||||
// A DataSource should automatically reconnect if connection was broken
|
||||
interface DataSource<DataSourceOptions> {
|
||||
connect(options: DataSourceOptions): DataSourceState
|
||||
disconnect(): void
|
||||
onMessage(messageCallback: MessageCallback): void
|
||||
topicSeparator: string
|
||||
}
|
||||
|
||||
export { DataSource, MessageCallback }
|
||||
41
backend/src/DataSource/DataSourceState.ts
Normal file
41
backend/src/DataSource/DataSourceState.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
interface InternalState {
|
||||
connecting: boolean
|
||||
connected: boolean
|
||||
error?: Error
|
||||
}
|
||||
|
||||
export class DataSourceState {
|
||||
private state: InternalState = {
|
||||
error: undefined,
|
||||
connected: false,
|
||||
connecting: false
|
||||
}
|
||||
|
||||
public setConnected(connected: boolean) {
|
||||
this.state = {
|
||||
error: undefined,
|
||||
connected: connected,
|
||||
connecting: false
|
||||
}
|
||||
}
|
||||
|
||||
public setError(error: Error) {
|
||||
this.state = {
|
||||
error: error,
|
||||
connected: false,
|
||||
connecting: false
|
||||
}
|
||||
}
|
||||
|
||||
public setConnecting() {
|
||||
this.state = {
|
||||
error: undefined,
|
||||
connected: false,
|
||||
connecting: true
|
||||
}
|
||||
}
|
||||
|
||||
public toJSON() {
|
||||
return this.state
|
||||
}
|
||||
}
|
||||
58
backend/src/DataSource/MqttSource.ts
Normal file
58
backend/src/DataSource/MqttSource.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Client, connect as mqttConnect } from 'mqtt'
|
||||
import { DataSource, DataSourceState } from './'
|
||||
|
||||
export interface MqttOptions {
|
||||
url: string
|
||||
}
|
||||
|
||||
export class MqttSource implements DataSource<MqttOptions> {
|
||||
private client: Client | undefined
|
||||
private messageCallback?: (topic: string, message: Buffer) => void
|
||||
private rootSubscription = '#'
|
||||
public topicSeparator = '/'
|
||||
|
||||
public onMessage(messageCallback: (topic: string, message: Buffer) => void) {
|
||||
this.messageCallback = messageCallback
|
||||
}
|
||||
|
||||
public connect(options: MqttOptions): DataSourceState {
|
||||
const state = new DataSourceState()
|
||||
|
||||
const client = mqttConnect(options.url, {
|
||||
resubscribe: false
|
||||
})
|
||||
|
||||
this.client = client
|
||||
|
||||
client.on('error', (error: Error) => {
|
||||
state.setError(error)
|
||||
})
|
||||
|
||||
client.on('close', () => {
|
||||
state.setConnected(false)
|
||||
})
|
||||
|
||||
client.on('reconnect', () => {
|
||||
state.setConnecting()
|
||||
})
|
||||
|
||||
client.on('connect', () => {
|
||||
state.setConnected(true)
|
||||
client.subscribe(this.rootSubscription, (err: Error) => {
|
||||
if (err) {
|
||||
throw new Error('mqtt subscription failed')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
client.on('message', (topic, message) => {
|
||||
this.messageCallback && this.messageCallback(topic, message)
|
||||
})
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
public disconnect() {
|
||||
this.client && this.client.end()
|
||||
}
|
||||
}
|
||||
10
backend/src/DataSource/index.ts
Normal file
10
backend/src/DataSource/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { DataSource } from './DataSource'
|
||||
import { DataSourceState } from './DataSourceState'
|
||||
import { MqttOptions, MqttSource } from './MqttSource'
|
||||
|
||||
export {
|
||||
DataSource,
|
||||
DataSourceState,
|
||||
MqttOptions,
|
||||
MqttSource,
|
||||
}
|
||||
Reference in New Issue
Block a user