Skip to content

Commit

Permalink
implement BLE Sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido Doumen committed Dec 16, 2024
1 parent 8c956b9 commit 7e625b7
Show file tree
Hide file tree
Showing 18 changed files with 846 additions and 920 deletions.
193 changes: 104 additions & 89 deletions samples/ble/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,10 @@ const { MockBinding } = require('incyclist-devices/lib/ble/bindings');
const { HrMock } = require('incyclist-devices/lib/ble/hr/mock');
const platform = os.platform()

const {BleInterface} = require('incyclist-devices/lib/ble/base/interface');
const { Interface } = require('readline');

EventLogger.registerAdapter(new ConsoleAdapter())
const Logger = new EventLogger('BleSampleApp')

let ble,binding

if (process.env.USE_MOCK) {
binding = MockBinding
MockBinding.addMock(HrMock)
}
else {
// Select binding (based on OS)
switch (platform) {
case 'win32': binding= new Noble(new WinrtBindings());break;
case 'linux': //break; // TODO
case 'darwin': binding = new Noble(defaultBinding()); break;
default:
process.exit()
}
}

const parseArgs = ()=> {
const args = process.argv.slice(2)
Expand Down Expand Up @@ -66,12 +48,25 @@ const parseArgs = ()=> {
console.log(' scan [proctocol,protocol,....]')
console.log(' connect <protocol> [id=device id|name=device name|address=device address]')
}

return
}

const main = async(props = {})=> {

const initInterface = ()=> {
let binding

if (process.env.USE_MOCK) {
binding = MockBinding
MockBinding.addMock(HrMock)
}
else {
// Select binding (based on OS)
switch (platform) {
case 'win32': binding= new Noble(new WinrtBindings());break;
case 'linux': break; // not supported
case 'darwin': binding = new Noble(defaultBinding()); break;
default:
process.exit()
}
}

const ble = InterfaceFactory.create('ble', {logger:Logger,binding})
//ble = new BleInterface(legacy)
Expand All @@ -82,95 +77,115 @@ const main = async(props = {})=> {

if (binding)
binding.on('error',(err)=>{console.log('>binding error',err.message)})
return ble

}


const scan = async (props) => {

const ble = initInterface()
const {protocols} = props

const {command,id,name,address,protocol,protocols} = props

let cntStartet = 0
if (command==='scan') {

const connected = await ble.connect(20000);
if (!connected) {
console.log('> error could not connect')
onAppExit()
return;
}

let devices
const connected = await ble.connect(20000);
if (!connected) {
console.log('> error could not connect')
onAppExit()
return;
}

for (let i=0; i<10; i++) {

devices = await ble.scan({protocols,timeout:10000})
console.log('> info', `${devices.length} device(s) found`)
}

let devices

for (let i=0; i<10; i++) {

if (devices.length===0) {
await sleep(2000)
devices = await ble.scan({protocols,timeout:5000})
}
devices = await ble.scan({protocols,timeout:10000})
console.log('> info', `${devices.length} device(s) found`)
}


if (devices.length===0) {
await sleep(2000)
devices = await ble.scan({protocols,timeout:5000})
}

if (devices.length>0) {
console.log('> info', `${devices.length} device(s) found`)
let adapter;
devices.forEach( async device => {
adapter = AdapterFactory.create(device)
try {
const started = await adapter.start()
if (started) {
adapter.on('data', (device,data)=>{ console.log('> data', {...device, ...data})})
cntStartet++;
}
if (devices.length>0) {
console.log('> info', `${devices.length} device(s) found`)
let adapter;
devices.forEach( async device => {
adapter = AdapterFactory.create(device)
try {
const started = await adapter.start()
if (started) {
adapter.on('data', (device,data)=>{ console.log('> data', {...device, ...data})})
cntStartet++;
}
catch(err) {
}
catch(err) {

console.log('> error',err.message)
if (adapter)
adapter.close()
console.log('> error',err.message)
if (adapter)
adapter.close()

}
})
}
else {
console.log('> info', 'no device found')
}
setTimeout( ()=>{
if (cntStartet===0) {
onAppExit()
}
}, 1000)
})
}
else {
const connected = await ble.connect(20000);
if (!connected) {
console.log('> error could not connect')
onAppExit()
return;
console.log('> info', 'no device found')
}
setTimeout( ()=>{
if (cntStartet===0) {
onAppExit()
}
}, 1000)

await sleep(5000)

}


try {
//const iface = InterfaceFactory.create('ble', {logger:Logger,binding})
//console.log('~~~ stopping scan on', iface.getName())
//await iface.pauseDiscovery()
const pair = async (props) => {
const ble = initInterface()
const {id,name,address,protocol} = props

const connected = await ble.connect(20000);
if (!connected) {
console.log('> error could not connect')
onAppExit()
return;
}

console.log('~~~ starting adapter')
const adapter = AdapterFactory.create( {interface: 'ble',protocol,id,name,address})
const started = await adapter.start()
if (started)
adapter.on('data', (device,data)=>{ console.log('> data', {...device, ...data})})
}
catch(err) {
console.log('> error',err.message, err.stack)
onAppExit()
}
await sleep(5000)



try {
console.log('starting adapter')
const adapter = AdapterFactory.create( {interface: 'ble',protocol,id,name,address})
const started = await adapter.start()
console.log('started', started)

if (started)
adapter.on('data', (device,data)=>{ console.log('> data', {...device, ...data})})
}
catch(err) {
console.log('> error',err.message, err.stack)
onAppExit()
}

}

const main = async(props = {})=> {
if (props?.command==='scan') {
await scan(props)

}
else {
await pair(props)
}
}

const onAppExit = async()=> {
const ble = InterfaceFactory.create('ble')
if (!ble)
return process.exit();

Expand Down
72 changes: 39 additions & 33 deletions samples/direct-connect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ class MDNSBinding {
EventLogger.registerAdapter(new ConsoleAdapter())

const logger = new EventLogger('DirectConnectSampleApp')
const isDebug = process.env.DEBUG

logger.log('DC Sample')
var args= process.argv.slice(2);
const args= process.argv.slice(2);



Expand All @@ -63,42 +62,49 @@ function runDevice(device) {

logger.logEvent( {message:'starting device',device:device.getDisplayName()})

return new Promise ( async (resolve) => {
return new Promise ( (resolve) => {


device.onData( (data)=> { logger.logEvent( {message:'onData',device:device.getDisplayName(),data}) })
device.on('disconnected', ()=>{console.log('disconnected')})
device.on('device-info', (info)=>{console.log('device-info:',info)})

try {
await device.start();
}
catch(err) {
logger.logEvent( {message:'Device start failed', error:err.message})
return resolve(true);
}

let iv;
if (device.hasCapability(IncyclistCapability.Control)) {
logger.logEvent( {message:'Device is controllable'})
let slope =0
// setting power to 200W every 1s
iv = setInterval( async ()=>{
//logger.logEvent( {message:'setting Power',power:200,device:device.getName()})
await device.sendUpdate( {slope});
slope+=0.1
const start = async () => {
device.onData( (data)=> { logger.logEvent( {message:'onData',device:device.getDisplayName(),data}) })
device.on('disconnected', ()=>{console.log('disconnected')})
device.on('device-info', (info)=>{console.log('device-info:',info)})

try {
await device.start();
}
catch(err) {
logger.logEvent( {message:'Device start failed', error:err.message})
resolve(true);
return
}

let iv;
if (device.hasCapability(IncyclistCapability.Control)) {
logger.logEvent( {message:'Device is controllable'})
let slope =0
// setting power to 200W every 1s
iv = setInterval( async ()=>{
//logger.logEvent( {message:'setting Power',power:200,device:device.getName()})
await device.sendUpdate( {slope});
slope+=0.1

}, 1000)

}, 1000)
}

// stopping device after 30s
setTimeout( async ()=>{
logger.logEvent( {message:'stopping device',device:device.getName()})
if (iv) clearInterval(iv)
await device.stop();
resolve(true)
}, 30000)


}

start()

// stopping device after 30s
setTimeout( async ()=>{
logger.logEvent( {message:'stopping device',device:device.getName()})
if (iv) clearInterval(iv)
await device.stop();
resolve(true)
}, 30000)

})

Expand Down
Loading

0 comments on commit 7e625b7

Please sign in to comment.