-
Notifications
You must be signed in to change notification settings - Fork 450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
initial websocket bindings #124
Open
jacobrosenthal
wants to merge
1
commit into
noble:master
Choose a base branch
from
jacobrosenthal:websocket
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,157 @@ | ||
var events = require('events'); | ||
var util = require('util'); | ||
|
||
var debug = require('debug')('bindings'); | ||
var WebSocket = require('ws'); | ||
|
||
var BlenoBindings = function() { | ||
var port = 0xB1f; | ||
var ip = process.env.IP || 'localhost'; | ||
|
||
this._ws = new WebSocket('ws://' + ip + ':' + port); | ||
|
||
this._deviceUUID = null; | ||
|
||
this.on('message', this._onMessage.bind(this)); | ||
|
||
if (!this._ws.on) { | ||
this._ws.on = this._ws.addEventListener; | ||
} | ||
|
||
this._ws.on('open', this._onOpen.bind(this)); | ||
this._ws.on('close', this._onClose.bind(this)); | ||
this._ws.on('error', this._onError.bind(this)); | ||
|
||
var _this = this; | ||
this._ws.on('message', function(event) { | ||
var data = (process.title === 'browser') ? event.data : event; | ||
|
||
_this.emit('message', JSON.parse(data)); | ||
}); | ||
}; | ||
|
||
util.inherits(BlenoBindings, events.EventEmitter); | ||
|
||
BlenoBindings.prototype._onOpen = function() { | ||
}; | ||
|
||
BlenoBindings.prototype._onClose = function() { | ||
this.emit('stateChange', 'poweredOff'); | ||
}; | ||
|
||
BlenoBindings.prototype._onError = function(error) { | ||
this.emit('xpcError', error); | ||
}; | ||
|
||
BlenoBindings.prototype._onMessage = function(event) { | ||
var type = event.type; | ||
var state = event.state; | ||
var error = event.error; | ||
var clientAddress = event.clientAddress; | ||
var rssi = event.rssi; | ||
var address = event.address; | ||
var OSPlatform = event.OSPlatform; | ||
|
||
debug('on -> message: ' + JSON.stringify(event, undefined, 2)); | ||
|
||
if (type === 'stateChange') { | ||
debug('state change ' + state); | ||
this.emit('stateChange', state); | ||
} else if (type === 'advertisingStart') { | ||
this.emit('advertisingStart', error); | ||
} else if (type === 'advertisingStartError') { | ||
this.emit('advertisingStartError', error); | ||
} else if (type === 'advertisingStop') { | ||
this.emit('advertisingStop'); | ||
} else if (type === 'servicesSet') { | ||
this.emit('servicesSet', error); | ||
} else if (type === 'servicesSetError') { | ||
this.emit('servicesSetError', error); | ||
} else if (type === 'accept') { | ||
this._deviceUUID = clientAddress; | ||
this.emit('accept', clientAddress); | ||
} else if (type === 'disconnect') { | ||
this.emit('disconnect', clientAddress); | ||
} else if (type === 'rssiUpdate') { | ||
this.emit('rssiUpdate', rssi); | ||
} else if (type === 'addressChange') { | ||
this.emit('addressChange', address); | ||
} else if (type === 'OSPlatform'){ | ||
this.emit('OSPlatform', OSPlatform); | ||
} | ||
}; | ||
|
||
BlenoBindings.prototype._sendCommand = function(command) { | ||
debug('on -> sendMessage: ' + JSON.stringify(command, undefined, 2)); | ||
var message = JSON.stringify(command); | ||
this._ws.send(message); | ||
}; | ||
|
||
BlenoBindings.prototype.disconnect = function() { | ||
this._sendCommand({ | ||
action: 'disconnect' | ||
}); | ||
}; | ||
|
||
var blenoBindings = new BlenoBindings(); | ||
|
||
blenoBindings.platform = function(){ | ||
this._sendCommand({ | ||
action: 'OSPlatform' | ||
}); | ||
} | ||
|
||
blenoBindings.startAdvertising = function(name, serviceUuids) { | ||
this._sendCommand({ | ||
action: 'startAdvertising', | ||
name: name, | ||
serviceUuids: serviceUuids | ||
}); | ||
}; | ||
|
||
blenoBindings.startAdvertisingIBeacon = function(data) { | ||
this._sendCommand({ | ||
action: 'startAdvertisingIBeacon', | ||
data: data.toString('hex'), | ||
}); | ||
}; | ||
|
||
blenoBindings.startAdvertisingWithEIRData = function(advertisementData, scanData) { | ||
if(!scanData){ | ||
scanData = new Buffer([]); | ||
} | ||
|
||
this._sendCommand({ | ||
action: 'startAdvertisingWithEIRData', | ||
advertisementData: advertisementData.toString('hex'), | ||
scanData: scanData.toString('hex') | ||
}); | ||
}; | ||
|
||
blenoBindings.stopAdvertising = function() { | ||
this._sendCommand({ | ||
action: 'stopAdvertising' | ||
}); | ||
}; | ||
|
||
blenoBindings.setServices = function(services) { | ||
// todo serialize services more fully | ||
this._sendCommand({ | ||
action: 'setServices', | ||
services: services | ||
}); | ||
}; | ||
|
||
|
||
blenoBindings.updateRssi = function() { | ||
if (this._deviceUUID === null) { | ||
this.emit('rssiUpdate', 127); // not supported | ||
} else { | ||
this._sendCommand({ | ||
action: 'updateRssi', | ||
deviceUUID: this._deviceUUID | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = blenoBindings; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arbitrarily chose 0xB1f, one up from nobles websocket.