Add electron
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
type ReadyCallback = () => void
|
||||
type MessageCallback = (topic: string, payload: Buffer) => void
|
||||
|
||||
interface DataSource {
|
||||
connect({readyCallback, messageCallback}: { readyCallback: ReadyCallback, messageCallback: MessageCallback }): void
|
||||
disconnect(): void
|
||||
topicSeparator: string
|
||||
}
|
||||
|
||||
export { DataSource, ReadyCallback, MessageCallback }
|
||||
@@ -1,42 +0,0 @@
|
||||
import { Client, connect as mqttConnect } from 'mqtt'
|
||||
import { DataSource } from './'
|
||||
|
||||
export class MqttSource implements DataSource {
|
||||
private client: Client | undefined
|
||||
private url: string
|
||||
private subscription: string
|
||||
public topicSeparator = '/'
|
||||
|
||||
constructor({url, subscription}: {url: string, subscription: string}) {
|
||||
this.url = url
|
||||
this.subscription = subscription
|
||||
}
|
||||
|
||||
public connect({
|
||||
readyCallback,
|
||||
messageCallback
|
||||
}: {
|
||||
readyCallback: () => void,
|
||||
messageCallback: (topic: string, message: Buffer) => void
|
||||
}) {
|
||||
const client = mqttConnect(this.url)
|
||||
this.client = client
|
||||
|
||||
client.on('connect', () => {
|
||||
readyCallback()
|
||||
client.subscribe(this.subscription, (err: Error) => {
|
||||
if (err) {
|
||||
throw new Error('mqtt connection failed')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
client.on('message', (topic, message) => {
|
||||
messageCallback(topic, message)
|
||||
})
|
||||
}
|
||||
|
||||
public disconnect() {
|
||||
this.client && this.client.end()
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import { DataSource } from './DataSource'
|
||||
import { MqttSource } from './MqttSource'
|
||||
|
||||
export {
|
||||
DataSource,
|
||||
MqttSource,
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
import { Tree, TreeNode } from './Model'
|
||||
|
||||
export class DotExport {
|
||||
public static renderNodeInformation(node: TreeNode): string {
|
||||
return `\t${node.sourceEdge.hash()} [label=${this.renderLabel(node.value)}]`
|
||||
}
|
||||
public static toDot(tree: Tree): string {
|
||||
let i = 1
|
||||
let leaveEdges = Object.values(tree.edges)
|
||||
.map(e => e.node)
|
||||
.map(node => node.leafes())
|
||||
.reduce((a, b) => a.concat(b), [])
|
||||
.map(leave => leave.branch())
|
||||
|
||||
const allEdges: Array<string> = []
|
||||
const nodeInformation: {[s: string]: string} = {}
|
||||
leaveEdges.map(edges => edges.reduce( (prev, current) => {
|
||||
let currentHash = current.sourceEdge.hash()
|
||||
nodeInformation[currentHash] = this.renderNodeInformation(current)
|
||||
if (current && prev) {
|
||||
allEdges.push(`\t${prev.sourceEdge.hash()} -> ${currentHash} [label=${this.renderLabel(current.sourceEdge.name)}]`)
|
||||
}
|
||||
return current
|
||||
}))
|
||||
|
||||
return `strict digraph ethane {
|
||||
${
|
||||
[this.renderNodeInformation(tree)]
|
||||
.concat(Object.values(nodeInformation))
|
||||
.concat(allEdges)
|
||||
.join('\n')
|
||||
}
|
||||
}`;
|
||||
}
|
||||
|
||||
private static renderLabel(value: any): string {
|
||||
let str;
|
||||
if(!isNaN(value)) {
|
||||
str = value
|
||||
} else {
|
||||
str = JSON.stringify(value)
|
||||
if(str && str.length > 0) {
|
||||
str = str.slice(1, -1)
|
||||
}
|
||||
}
|
||||
|
||||
if (!str) {
|
||||
return '""'
|
||||
}
|
||||
|
||||
if(str.length > 20) {
|
||||
str = str.slice(0, 20)+'…'
|
||||
}
|
||||
|
||||
if (str[0] !== '"') {
|
||||
str = `"${str}"`
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
import { Hashable, TreeNode, TopicProperties } from './'
|
||||
const sha1 = require('sha1')
|
||||
|
||||
export class Edge {
|
||||
public name: string
|
||||
|
||||
public node!: TreeNode
|
||||
public source?: TreeNode | undefined
|
||||
private cachedHash?: string
|
||||
|
||||
constructor(name: string) {
|
||||
this.name = name
|
||||
}
|
||||
|
||||
public edges() {
|
||||
return this.node ? Object.values(this.node.edges) : []
|
||||
}
|
||||
|
||||
public hash(): string {
|
||||
if (!this.cachedHash) {
|
||||
let previousHash = (this.source && this.source.sourceEdge) ? this.source.sourceEdge.hash() : ''
|
||||
this.cachedHash = 'H' + sha1(previousHash + this.name)
|
||||
}
|
||||
|
||||
return this.cachedHash
|
||||
}
|
||||
|
||||
public firstEdge(): Edge {
|
||||
if (this.source && this.source.sourceEdge) {
|
||||
return this.source.sourceEdge.firstEdge()
|
||||
} else {
|
||||
return this
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export interface Hashable {
|
||||
hash(): string
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
export class TopicProperties {
|
||||
public topicSeparator: string = '/'
|
||||
public multilevelWildcard: string | null = '#'
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
import { Edge, TreeNode } from './'
|
||||
|
||||
export class Tree extends TreeNode {
|
||||
constructor() {
|
||||
super(undefined, undefined)
|
||||
}
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
import { Edge } from './'
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
export class TreeNode extends EventEmitter {
|
||||
public sourceEdge?: Edge
|
||||
public value?: any | null
|
||||
public edges: {[s: string]: Edge} = {}
|
||||
public collapsed = false
|
||||
|
||||
constructor(sourceEdge?: Edge, value?: any) {
|
||||
super()
|
||||
|
||||
if (sourceEdge) {
|
||||
this.sourceEdge = sourceEdge
|
||||
sourceEdge.target = this
|
||||
}
|
||||
this.value = value
|
||||
}
|
||||
|
||||
public hash(): string {
|
||||
return 'N' + (this.sourceEdge ? this.sourceEdge.hash() : '')
|
||||
}
|
||||
|
||||
public firstNode(): TreeNode {
|
||||
return this.sourceEdge ? this.sourceEdge.firstEdge().node : this
|
||||
}
|
||||
|
||||
public path(): string {
|
||||
return this.branch()
|
||||
.map(node => (node.sourceEdge && node.sourceEdge.name))
|
||||
.filter(name => name !== undefined)
|
||||
.join('/')
|
||||
}
|
||||
|
||||
private previous(): TreeNode | undefined {
|
||||
return this.sourceEdge ? this.sourceEdge.source || undefined : undefined
|
||||
}
|
||||
|
||||
public addEdge(edge: Edge) {
|
||||
this.edges[edge.name] = edge
|
||||
edge.source = this
|
||||
this.emit('update')
|
||||
}
|
||||
|
||||
public branch(): Array<TreeNode> {
|
||||
let previous = this.previous()
|
||||
if (!previous) {
|
||||
return [this]
|
||||
}
|
||||
|
||||
return previous.branch().concat([this])
|
||||
}
|
||||
|
||||
public updateWithNode(node: TreeNode) {
|
||||
if (node.value !== undefined) {
|
||||
this.value = node.value
|
||||
}
|
||||
this.mergeEdges(node)
|
||||
this.emit('update')
|
||||
}
|
||||
|
||||
public leafes(): Array<TreeNode> {
|
||||
if (Object.values(this.edges).length === 0) {
|
||||
return [this]
|
||||
}
|
||||
|
||||
return Object.values(this.edges)
|
||||
.map(e => e.node.leafes())
|
||||
.reduce((a, b) => a.concat(b), [])
|
||||
}
|
||||
|
||||
private mergeEdges(node: TreeNode) {
|
||||
let edgeKeys = Object.keys(node.edges)
|
||||
for (let edgeKey of edgeKeys) {
|
||||
let matchingEdge = this.edges[edgeKey]
|
||||
if (matchingEdge) {
|
||||
matchingEdge.node.updateWithNode(node.edges[edgeKey].node)
|
||||
} else {
|
||||
this.addEdge(node.edges[edgeKey])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import { Edge, Tree, TreeNode } from './'
|
||||
|
||||
export abstract class TreeNodeFactory {
|
||||
public static fromEdgesAndValue(edgeNames: Array<string>, value: any): TreeNode {
|
||||
const lastEdgeIndex = edgeNames.length - 1
|
||||
var edges = edgeNames
|
||||
.map((name, idx) => {
|
||||
const edge = new Edge(name)
|
||||
|
||||
const nodeValue = lastEdgeIndex == idx ? value : undefined
|
||||
const node = new TreeNode(edge, nodeValue)
|
||||
edge.node = node
|
||||
return edge
|
||||
})
|
||||
|
||||
let reversed: Array<Edge> = edges.reverse()
|
||||
let previous: Edge | undefined = undefined;
|
||||
for (let edge of reversed) {
|
||||
if (previous) {
|
||||
edge.node.addEdge(previous)
|
||||
}
|
||||
previous = edge;
|
||||
}
|
||||
|
||||
let leaf = reversed[0].node
|
||||
|
||||
let sourceTree = new Tree()
|
||||
sourceTree.updateWithNode(leaf.firstNode())
|
||||
|
||||
return leaf
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
import { Edge } from './Edge'
|
||||
import { TreeNode } from './TreeNode'
|
||||
import { TreeNodeFactory } from './TreeNodeFactory'
|
||||
import { Tree } from './Tree'
|
||||
import { TopicProperties } from './TopicProperties'
|
||||
import { Hashable } from './Hashable'
|
||||
|
||||
export {
|
||||
Edge, TreeNode, TreeNodeFactory, Tree, TopicProperties, Hashable
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import { Edge, TreeNode } from '../'
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
describe('Edge', () => {
|
||||
|
||||
it('should contain a name', () => {
|
||||
let e = new Edge('foo')
|
||||
expect(e.name).to.equal('foo')
|
||||
});
|
||||
|
||||
it('hash should not be empty', () => {
|
||||
let e = new Edge('bar')
|
||||
expect(e.hash().length).to.be.gt(0)
|
||||
});
|
||||
|
||||
it('hash should be stable', () => {
|
||||
let e = new Edge('bar')
|
||||
let previousHash = e.hash()
|
||||
expect(e.hash()).to.eq(previousHash)
|
||||
});
|
||||
|
||||
it('hash should change when parent is present', () => {
|
||||
let foo = new Edge('foo')
|
||||
let bar = new Edge('bar')
|
||||
|
||||
var previousHash = bar.hash()
|
||||
bar.source = new TreeNode(foo, undefined)
|
||||
expect(bar.hash()).to.not.eq(previousHash)
|
||||
});
|
||||
});
|
||||
@@ -1,18 +0,0 @@
|
||||
import { Edge, Tree, TreeNode, TreeNodeFactory } from '../'
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import './TreeNode.findNode'
|
||||
|
||||
describe('Tree', () => {
|
||||
it('node can be merged into a tree', () => {
|
||||
const tree = new Tree()
|
||||
|
||||
const topics = 'foo/bar'.split('/')
|
||||
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, 3)
|
||||
debugger
|
||||
tree.updateWithNode(leaf.firstNode())
|
||||
let expectedNode = tree.findNode('foo/bar')
|
||||
expect(expectedNode).to.eq(leaf)
|
||||
})
|
||||
});
|
||||
@@ -1,29 +0,0 @@
|
||||
import { TreeNode, TreeNodeFactory } from '../'
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import './TreeNode.findNode'
|
||||
|
||||
describe('TreeNode.findNode', () => {
|
||||
it('findNode should retrieve node', () => {
|
||||
const topics = 'foo/bar/baz'.split('/')
|
||||
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, 5)
|
||||
|
||||
let root = leaf.firstNode()
|
||||
expect(root.sourceEdge.name).to.eq('')
|
||||
|
||||
let barNode = root.findNode('foo/bar')
|
||||
if (!barNode) {
|
||||
expect.fail('did not find node')
|
||||
return
|
||||
}
|
||||
expect(barNode.sourceEdge.name).to.eq('bar')
|
||||
|
||||
let bazNode = root.findNode('foo/bar/baz')
|
||||
if (!bazNode) {
|
||||
expect.fail('did not find node')
|
||||
return
|
||||
}
|
||||
expect(bazNode.sourceEdge.name).to.eq('baz')
|
||||
})
|
||||
})
|
||||
@@ -1,20 +0,0 @@
|
||||
import { TreeNode } from '../'
|
||||
|
||||
declare module "../" {
|
||||
interface TreeNode {
|
||||
findNode(path: String): TreeNode | undefined
|
||||
}
|
||||
}
|
||||
|
||||
TreeNode.prototype.findNode = function(path: String): TreeNode | undefined {
|
||||
const topics = path.split('/')
|
||||
let edge = this.edges[topics[0]]
|
||||
let remainingTopics = topics.slice(1, topics.length)
|
||||
if (edge && remainingTopics.length === 0) {
|
||||
return edge.node
|
||||
} else if (edge) {
|
||||
return edge.node.findNode(remainingTopics.join('/'))
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
import { Edge, Tree, TreeNode, TreeNodeFactory } from '../'
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import './TreeNode.findNode'
|
||||
|
||||
describe('TreeNode', () => {
|
||||
it('updateWithNode should update value', () => {
|
||||
const topics = 'foo/bar'.split('/')
|
||||
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, 3)
|
||||
expect(leaf.value).to.eq(3)
|
||||
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics, 5)
|
||||
leaf.firstNode().updateWithNode(updateLeave.firstNode())
|
||||
|
||||
expect(leaf.firstNode().sourceEdge.name).to.eq(updateLeave.firstNode().sourceEdge.name)
|
||||
expect(leaf.value).to.eq(5)
|
||||
})
|
||||
|
||||
it('updateWithNode should update intermediate nodes', () => {
|
||||
const topics1 = 'foo/bar/baz'.split('/')
|
||||
const leaf = TreeNodeFactory.fromEdgesAndValue(topics1, 3)
|
||||
expect(leaf.value).to.eq(3)
|
||||
|
||||
const topics2 = 'foo/bar'.split('/')
|
||||
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics2, 5)
|
||||
leaf.firstNode().updateWithNode(updateLeave.firstNode())
|
||||
|
||||
let barNode = leaf.firstNode().findNode('foo/bar')
|
||||
expect(barNode && barNode.sourceEdge.name).to.eq('bar')
|
||||
expect(barNode && barNode.value).to.eq(5)
|
||||
|
||||
expect(leaf.sourceEdge.name).to.eq('baz')
|
||||
expect(leaf.value).to.eq(3)
|
||||
})
|
||||
|
||||
it('updateWithNode should add nodes to the tree', () => {
|
||||
const topics1 = 'foo/bar'.split('/')
|
||||
const leaf1 = TreeNodeFactory.fromEdgesAndValue(topics1, 3)
|
||||
|
||||
const topics2 = 'foo/bar/baz'.split('/')
|
||||
const leaf2 = TreeNodeFactory.fromEdgesAndValue(topics2, 5)
|
||||
|
||||
leaf1.firstNode().updateWithNode(leaf2.firstNode())
|
||||
|
||||
let expectedNode = leaf1.firstNode().findNode('foo/bar/baz')
|
||||
if (!expectedNode) {
|
||||
expect.fail('merge seems to have failed')
|
||||
return
|
||||
}
|
||||
})
|
||||
});
|
||||
@@ -1,46 +0,0 @@
|
||||
import { Edge, TreeNode, TreeNodeFactory } from '../'
|
||||
import { expect } from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
describe('TreeNodeFactory', () => {
|
||||
it('should create node', () => {
|
||||
let topic = 'foo/bar'
|
||||
let edges = topic.split('/')
|
||||
let node = TreeNodeFactory.fromEdgesAndValue(edges, 5)
|
||||
|
||||
expect(node).to.not.eq(undefined)
|
||||
expect(node.sourceEdge.name).to.eq('bar')
|
||||
expect(node.value).to.eq(5)
|
||||
|
||||
if (!node.sourceEdge.source) {
|
||||
expect.fail('should not happen')
|
||||
return
|
||||
}
|
||||
|
||||
let foo = node.sourceEdge.source.sourceEdge
|
||||
expect(foo.name).to.eq('foo')
|
||||
});
|
||||
|
||||
it('node should contain edges in order', () => {
|
||||
let topic = 'foo/bar/baz'
|
||||
let edges = topic.split('/')
|
||||
let node = TreeNodeFactory.fromEdgesAndValue(edges, 5)
|
||||
|
||||
expect(node.value).to.eq(5)
|
||||
expect(node.sourceEdge.name).to.eq('baz')
|
||||
|
||||
const barNode = node.sourceEdge.source
|
||||
if (!barNode) {
|
||||
expect.fail('should not fail')
|
||||
return
|
||||
}
|
||||
expect(barNode.sourceEdge.name).to.eq('bar')
|
||||
|
||||
const fooNode = barNode.sourceEdge.source
|
||||
if (!fooNode) {
|
||||
expect.fail('should not fail')
|
||||
return
|
||||
}
|
||||
expect(fooNode.sourceEdge.name).to.eq('foo')
|
||||
});
|
||||
});
|
||||
62
src/index.ts
62
src/index.ts
@@ -1,62 +0,0 @@
|
||||
import { TopicProperties, Tree, TreeNodeFactory } from './Model'
|
||||
import { MqttSource, DataSource } from './DataSource'
|
||||
import { DotExport } from './DotExport'
|
||||
// import { CytoscapeExport } from './CytoscapeExport'
|
||||
// import { VisExport } from './VisExport'
|
||||
import { writeFileSync } from 'fs'
|
||||
import { spawn } from 'child_process'
|
||||
import * as socketIO from 'socket.io'
|
||||
|
||||
const server = require('http').createServer();
|
||||
|
||||
let tree = new Tree()
|
||||
let dataSource: DataSource = new MqttSource({url: 'mqtt://iot.eclipse.org', subscription: '#'})
|
||||
let count = 200
|
||||
|
||||
const a: Array<any> = []
|
||||
|
||||
const io = socketIO(server)
|
||||
io.on('connection', client => {
|
||||
console.log('connection')
|
||||
a.forEach(b => {
|
||||
io.emit('message', b)
|
||||
})
|
||||
client.on('event', data => { /* … */ });
|
||||
client.on('disconnect', () => { /* … */ });
|
||||
});
|
||||
server.listen(3000);
|
||||
|
||||
|
||||
dataSource.connect({
|
||||
readyCallback: () => {
|
||||
console.log('connected')
|
||||
},
|
||||
messageCallback: (topic, payload) => {
|
||||
// a.push({topic, payload})
|
||||
if (payload.length > 10000) {
|
||||
payload = payload.slice(0, 10000)
|
||||
}
|
||||
|
||||
io.emit('message', {topic, payload: payload.toString('base64')})
|
||||
// console.log(topic)
|
||||
const edges = topic.split('/')
|
||||
let value = payload.toString()
|
||||
let node = TreeNodeFactory.fromEdgesAndValue(edges, value)
|
||||
tree.updateWithNode(node.firstNode())
|
||||
}
|
||||
})
|
||||
|
||||
// function writeTree() {
|
||||
// // writeFileSync('./test.dot', DotExport.toDot(tree))
|
||||
// // writeFileSync('./test.json', CytoscapeExport.toDot(tree))
|
||||
// // writeFileSync('./vis.json', VisExport.toDot(tree))
|
||||
// // let p = spawn('dot', '-Tpng test.dot -o test2.png'.split(' '))
|
||||
// }
|
||||
//
|
||||
// setInterval(() => {
|
||||
// writeTree()
|
||||
// }, 2000)
|
||||
|
||||
setTimeout(() => {
|
||||
dataSource.disconnect()
|
||||
}, 1000000)
|
||||
Reference in New Issue
Block a user