This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
sampleApp.js
150 lines (129 loc) · 4.81 KB
/
sampleApp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const blePeripheral = require("./blePeripheral.js");
const fs = require('fs'); // Used to read CPU temperature in /sys/class/thermal/thermal_zone0/temp file.
const cp = require('child_process'); // Used to spawn /bin/hostname -I to get IP Address.
const serviceName = 'com.sampleApp'; // peripheral's DBus service name
const serviceUUID = '27b5244f-94f3-4011-be53-6ac36bf22cf1' // UUID to advertise as an Bluetooh LE service
var bigBuffer = Buffer.alloc(512, 'i');
bigBuffer[0] = 0x00;
bigBuffer[511] = 0xFF;
console.log('Registering ->' + serviceName + '<- as a D-Bus system service...');
const bPrl = new blePeripheral(serviceName, serviceUUID, main);
function main(DBus){
bPrl.logCharacteristicsIO = true;
console.log('Initialize charcteristics...');
var isAuthorized = bPrl.Characteristic('00000001-94f3-4011-be53-6ac36bf22cf1', 'isAuthorized', ["read","write-without-response"]);
var cmd = bPrl.Characteristic('00000002-94f3-4011-be53-6ac36bf22cf1', 'cmd', ["read","write"]);
var bigData = bPrl.Characteristic('00000003-94f3-4011-be53-6ac36bf22cf1', 'bigData');
var myIpAddress = bPrl.Characteristic('00000004-94f3-4011-be53-6ac36bf22cf1', 'myIpAddress', ["encrypt-read"]);
var cpuTemp = bPrl.Characteristic('00000006-94f3-4011-be53-6ac36bf22cf1', 'cpuTemp', ["encrypt-read","notify"]);
console.log('Registering event handlers...');
isAuthorized.on('ReadValue', (device)=>{
console.log(bPrl.client.name + ', ' + device + ' has connected and checking if it is authorized');
if(bPrl.client.paired == true){
console.log('\tpaired = true');
isAuthorized.setValue(Buffer.from('true'));
} else {
console.log('\tpaired = false');
isAuthorized.setValue(Buffer.from('false'));
};
});
cmd.on('WriteValue', (device, arg1)=>{
console.log(device + ' has sent a new cmd = ' + arg1[0]);
var cmdNum = arg1.toString();
switch (cmdNum) {
case '1':
console.log('Enable test pairing command received.');
bPrl.Adapter.pairModeOn(true);
break;
case '2':
console.log('Disable test pairing command received.');
bPrl.Adapter.pairModeOn(false);
break;
case '3':
console.log('Display adapter properties:')
bPrl.Adapter.logAllProperties();
break;
case '4':
console.log('Connected device properties:')
bPrl.Device.logAllProperties(bPrl.client.devicePath);
break;
default:
console.log('no case for ' + cmdNum);
break;
}
});
cpuTemp.on('ReadValue', (device)=>{
console.log(device + ' is reading CPU temperature..');
cpuTemp.setValue(getCpuTemp());
})
setInterval(()=>{
if(cpuTemp.iface.Notifying){
cpuTemp.setValue(getCpuTemp());
cpuTemp.notify();
}
}, 15000);
console.log('setting default characteristic values...');
updateAll();
function updateAll(){
bigData.setValue(bigBuffer);
myIpAddress.setValue(getIP());
cmd.setValue('1=enable pairing, 2=disable pairing. 3=log adapter, 4=log connected device');
cpuTemp.setValue(getCpuTemp());
};
};
bPrl.on('ConnectionChange', (connected)=>{
var bleUserName = '';
if(bPrl.client.name == ''){
bleUserName = bPrl.client.devicePath;
} else {
bleUserName = bPrl.client.name;
};
if(connected == true){
console.log('--> ' + bleUserName + ' has connected to this server at ' + (new Date()).toLocaleTimeString());
if(bPrl.client.paired == false){
console.log('--> ' + 'CAUTION: This BLE device is not authenticated.');
}
} else {
console.log('<-- ' + bleUserName + ' has disconnected from this server at ' + (new Date()).toLocaleTimeString());
if(bPrl.areAnyCharacteristicsNotifying() == true){
console.log('Restarting gatt services to cleanup leftover notifications...')
bPrl.restartGattService();
};
};
});
/**
* Reads the CPU temperature on Raspberry Pi.
* Returns temperature in fahrenheit as a string.
*/
function getCpuTemp(){
cpuTempStr = '';
try{
cpuTempStr = fs.readFileSync('/sys/class/thermal/thermal_zone0/temp');
var f = parseInt(cpuTempStr) * .001;
f = f * 1.8 + 32;
cpuTempStr = f.toFixed(2).toString();
}
catch(err){
console.log('error reading CPU Temperature ');
return 'not supported on this hardware';
};
return cpuTempStr + '°F';
};
/**
* Returns local IP address of Raspberry Pi as a string.
*/
function getIP(){
var ipAdd = ''
try{
// get ip address (may be more than one)
var rsp = cp.execSync('/bin/hostname -I');
var str = rsp.toString();
var y = str.split('\n');
ipAdd = y[0].trim();
}
catch(err){
console.log('error reading IP Address');
return "not supported on this hardware";
};
return ipAdd;
}