Created
May 31, 2017 08:56
-
-
Save grundyoso/fbc89f5421a9ed99491427156c65f071 to your computer and use it in GitHub Desktop.
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
"use strict"; | |
/* -------------------------------- REQUIRES -------------------------------- */ | |
Object.defineProperty(exports, "__esModule", { value: true }); | |
const crypto = require("crypto"); | |
const util = require("util"); | |
const bleno = require("bleno"); | |
const fs = require('fs'); | |
const childProcess = require('child_process') | |
const SERVICE_UID = '13333333333333333333333333333337'; | |
const NOTIFY_UID = '13333333333333333333333333330003'; | |
const encodeBuffer = require('./msg-encode').encodeBuffer | |
/* ---------------------------------- CONTS --------------------------------- */ | |
const WIFI_IP_TAG = 1 | |
/* -------------------------- NOTIFY CHARACTERISTIC ------------------------- */ | |
function CartNotifyCharacteristic() { | |
bleno.Characteristic.call(this, { | |
uuid: NOTIFY_UID, | |
properties: ['notify', 'write'], | |
descriptors: [ | |
new bleno.Descriptor({ | |
uuid: '2901', | |
value: 'Notifies of tags added/removed from cart' | |
}) | |
] | |
}); | |
} | |
function sendWIFISettingsAndIP(notifyChar) { | |
if (!notifyChar.updateValueCallback) { | |
return; | |
} | |
// wifi settings | |
function len(buffer) { | |
const lenBuf = Buffer.alloc(1) | |
lenBuf.writeUInt8(buffer.length, 0) | |
return lenBuf | |
} | |
childProcess.exec('hostname -I', (err, stdout, stderr) => { | |
const ip = | |
new Buffer(stdout | |
.replace(/\s/g, '') | |
.split('.') | |
.map(Number)); | |
const creds = JSON.parse( | |
fs.readFileSync(__dirname + '/WIFI_CREDS') | |
.toString()); | |
const ssid = creds.ssid | |
const pwd = creds.password | |
const fields = Buffer.concat([ | |
len(ssid), | |
ssid, | |
len(pwd), | |
pwd, | |
len(ip), | |
ip | |
]) | |
const chunks = encodeBuffer( | |
WIFI_IP_TAG, | |
fields | |
) | |
chunks.forEach(chunk => { | |
console.log('sending wifi/ip chunk with length: ', chunk.length); | |
notifyChar.updateValueCallback(chunk) | |
}) | |
}) | |
} | |
util.inherits(CartNotifyCharacteristic, bleno.Characteristic); | |
CartNotifyCharacteristic.prototype.onWriteRequest = function (data, offset, withoutResponse, callback) { | |
if (offset) { | |
callback(this.RESULT_ATTR_NOT_LONG); | |
} | |
else if (data.length !== 2) { | |
callback(this.RESULT_INVALID_ATTRIBUTE_LENGTH); | |
} | |
else { | |
sendWIFISettingsAndIP(this) | |
if (this.updateValueCallback) { | |
// Send the latest tag data on reconnect | |
if (exports.messages.length) { | |
exports.messages.forEach(m => this.updateValueCallback(m)); | |
} | |
} | |
callback(this.RESULT_SUCCESS); | |
} | |
}; | |
var notifyInstance = new CartNotifyCharacteristic(); | |
/* ------------------------------ CART-SERVICE ----------------------------- */ | |
function CartService() { | |
bleno.PrimaryService.call(this, { | |
uuid: SERVICE_UID, | |
characteristics: [ | |
notifyInstance | |
] | |
}); | |
} | |
util.inherits(CartService, bleno.PrimaryService); | |
var cartService = new CartService(); | |
/* ---------------------------------- START --------------------------------- */ | |
function startPeripheral(name) { | |
// | |
// Wait until the BLE radio powers on before attempting to advertise. | |
// If you don't have a BLE radio, then it will never power on! | |
// | |
bleno.on('stateChange', function (state) { | |
if (state === 'poweredOn') { | |
// | |
// We will also advertise the service ID in the advertising packet, | |
// so it's easier to find. | |
// | |
bleno.startAdvertising(name, [cartService.uuid], function (err) { | |
if (err) { | |
console.log(err); | |
} | |
}); | |
} | |
else { | |
bleno.stopAdvertising(); | |
} | |
}); | |
bleno.on('advertisingStart', function (err) { | |
if (!err) { | |
console.log('advertising...', name); | |
// | |
// Once we are advertising, it's time to set up our services, | |
// along with our characteristics. | |
// | |
bleno.setServices([ | |
cartService | |
]); | |
} | |
}); | |
} | |
exports.messages = [] | |
exports.notifyInstance = notifyInstance; | |
exports.startPeripheral = startPeripheral; | |
if (require.main === module) { | |
startPeripheral('Flomio SmartCart'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment