bt-sensors-plugin-sk 1.2.0-beta.0.1.0 → 1.2.0-beta.0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BTSensor.js +21 -14
- package/README.md +7 -0
- package/index.js +20 -11
- package/package.json +2 -3
- package/public/159.js +1 -1
- package/sensor_classes/ATC.js +6 -3
- package/sensor_classes/GobiusCTankMeter.js +3 -2
- package/sensor_classes/MopekaTankSensor.js +1 -1
- package/src/components/PluginConfigurationPanel.js +13 -7
package/BTSensor.js
CHANGED
|
@@ -465,23 +465,24 @@ class BTSensor extends EventEmitter {
|
|
|
465
465
|
|
|
466
466
|
deviceConnect() {
|
|
467
467
|
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
Bluez for some cockamamie reason (It's 2025 for chrissake.
|
|
471
|
-
BLUETOOTH ISN'T JUST FOR DESKTOPS ANYMORE, BLUEZ DEVS!)
|
|
472
|
-
SUSPENDS scanning while connected to a device.
|
|
473
|
-
|
|
474
|
-
The next line of code gives the scanner a kick in the arse,
|
|
475
|
-
starting it up again so, I dunno, another device might be able
|
|
476
|
-
to connect and sensor classes could maybe get beacon updates.
|
|
477
|
-
|
|
478
|
-
You know, the little things.
|
|
479
|
-
*/
|
|
468
|
+
|
|
480
469
|
return connectQueue.enqueue( async ()=>{
|
|
481
470
|
this.debug(`Connecting to ${this.getName()}`)
|
|
482
471
|
await this.device.connect()
|
|
483
472
|
try {
|
|
484
473
|
|
|
474
|
+
/* CAUTION: HACK AHEAD
|
|
475
|
+
|
|
476
|
+
Bluez for some cockamamie reason (It's 2025 for chrissake.
|
|
477
|
+
BLUETOOTH ISN'T JUST FOR DESKTOPS ANYMORE, BLUEZ DEVS!)
|
|
478
|
+
SUSPENDS scanning while connected to a device.
|
|
479
|
+
|
|
480
|
+
The next line of code gives the scanner a kick in the arse,
|
|
481
|
+
starting it up again so, I dunno, another device might be able
|
|
482
|
+
to connect and sensor classes could maybe get beacon updates.
|
|
483
|
+
|
|
484
|
+
You know, the little things.
|
|
485
|
+
*/
|
|
485
486
|
await this._adapter.helper.callMethod('StopDiscovery')
|
|
486
487
|
await this._adapter.helper.callMethod('SetDiscoveryFilter', {
|
|
487
488
|
Transport: new Variant('s', this._adapter?._transport??"le")
|
|
@@ -492,9 +493,13 @@ class BTSensor extends EventEmitter {
|
|
|
492
493
|
//probably ignorable error. probably.
|
|
493
494
|
console.log(e)
|
|
494
495
|
}
|
|
496
|
+
/* END HACK*/
|
|
497
|
+
this.device.on("disconnect", ()=>{
|
|
498
|
+
this.debug(`${this.macAndName()} disconnected`)
|
|
499
|
+
})
|
|
495
500
|
})
|
|
496
|
-
|
|
497
|
-
|
|
501
|
+
|
|
502
|
+
}
|
|
498
503
|
|
|
499
504
|
/**
|
|
500
505
|
*
|
|
@@ -771,7 +776,9 @@ class BTSensor extends EventEmitter {
|
|
|
771
776
|
|
|
772
777
|
async stopListening(){
|
|
773
778
|
this.removeAllListeners()
|
|
779
|
+
this.device.removeAllListeners()
|
|
774
780
|
this.device.helper.removeListeners()
|
|
781
|
+
|
|
775
782
|
if (this.intervalID){
|
|
776
783
|
clearInterval(this.intervalID)
|
|
777
784
|
}
|
package/README.md
CHANGED
package/index.js
CHANGED
|
@@ -125,6 +125,7 @@ module.exports = function (app) {
|
|
|
125
125
|
var discoveryIntervalID, progressID, progressTimeoutID, deviceHealthID
|
|
126
126
|
var adapter
|
|
127
127
|
const channel = createChannel()
|
|
128
|
+
|
|
128
129
|
const classMap = loadClassMap(app)
|
|
129
130
|
const sensorMap=new Map()
|
|
130
131
|
|
|
@@ -248,17 +249,20 @@ module.exports = function (app) {
|
|
|
248
249
|
});
|
|
249
250
|
|
|
250
251
|
router.get('/getPluginState', async (req, res) => {
|
|
251
|
-
|
|
252
252
|
res.status(200).json({
|
|
253
|
+
"connectionId": Date.now(),
|
|
253
254
|
"state":(plugin.started?"started":"stopped")
|
|
254
255
|
})
|
|
255
256
|
});
|
|
256
257
|
router.get("/sse", async (req, res) => {
|
|
257
|
-
const session = await createSession(req,
|
|
258
|
+
const session = await createSession(req,res)
|
|
258
259
|
channel.register(session)
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
260
|
+
req.on("close", ()=>{
|
|
261
|
+
app.debug("deregistering session")
|
|
262
|
+
channel.deregister(session)
|
|
263
|
+
})
|
|
264
|
+
});
|
|
265
|
+
|
|
262
266
|
};
|
|
263
267
|
|
|
264
268
|
function sensorsToJSON(){
|
|
@@ -339,18 +343,22 @@ module.exports = function (app) {
|
|
|
339
343
|
if (startNumber != starts ) {
|
|
340
344
|
return
|
|
341
345
|
}
|
|
342
|
-
//app.debug(`Found ${config.mac_address}`)
|
|
343
346
|
s = await instantiateSensor(device,config)
|
|
344
|
-
|
|
347
|
+
//app.debug(`Instantiated ${config.mac_address}`)
|
|
348
|
+
|
|
345
349
|
if (s instanceof BLACKLISTED)
|
|
346
350
|
reject ( `Device is blacklisted (${s.reasonForBlacklisting()}).`)
|
|
347
351
|
else{
|
|
352
|
+
//app.debug(`Adding sensor to list ${config.mac_address}`)
|
|
353
|
+
|
|
348
354
|
addSensorToList(s)
|
|
349
355
|
s._lastRSSI=-1*Infinity
|
|
350
356
|
s.on("RSSI",(()=>{
|
|
351
|
-
if (Date.now()-s._lastRSSI >
|
|
352
|
-
//app.debug(
|
|
357
|
+
if (Date.now()-s._lastRSSI > 30000) { //only update RSSI on client every 30 seconds
|
|
358
|
+
//app.debug(`Updating ${s.getMacAddress()} RSSI after ${Date.now()-s._lastRSSI} ms`)
|
|
359
|
+
|
|
353
360
|
s._lastRSSI=Date.now()
|
|
361
|
+
|
|
354
362
|
updateSensor(s)
|
|
355
363
|
}
|
|
356
364
|
|
|
@@ -524,8 +532,9 @@ module.exports = function (app) {
|
|
|
524
532
|
}
|
|
525
533
|
|
|
526
534
|
sensorMap.clear()
|
|
527
|
-
if (channel)
|
|
535
|
+
if (channel){
|
|
528
536
|
channel.broadcast({state:"started"},"pluginstate")
|
|
537
|
+
}
|
|
529
538
|
deviceConfigs=options?.peripherals??[]
|
|
530
539
|
|
|
531
540
|
if (plugin.stopped) {
|
|
@@ -591,7 +600,7 @@ module.exports = function (app) {
|
|
|
591
600
|
const dt = config?.discoveryTimeout??options.discoveryTimeout
|
|
592
601
|
const lc=sensor.elapsedTimeSinceLastContact()
|
|
593
602
|
if (lc > dt) {
|
|
594
|
-
app.debug(`${sensor.getMacAddress()} not heard from in ${lc} seconds`)
|
|
603
|
+
//app.debug(`${sensor.getMacAddress()} not heard from in ${lc} seconds`)
|
|
595
604
|
channel.broadcast(getSensorInfo(sensor), "sensorchanged")
|
|
596
605
|
}
|
|
597
606
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bt-sensors-plugin-sk",
|
|
3
|
-
"version": "1.2.0-beta.0.1.
|
|
3
|
+
"version": "1.2.0-beta.0.1.2",
|
|
4
4
|
"description": "Bluetooth Sensors for Signalk -- support for Victron devices, RuuviTag, Xiaomi, ATC and Inkbird, Ultrasonic wind meters, Mopeka tank readers, Renogy Battery and Solar Controllers, Aranet4 environment sensors, SwitchBot temp and humidity sensors, KilovaultHLXPlus smart batteries, and Govee GVH51xx temp sensors",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
@@ -10,9 +10,8 @@
|
|
|
10
10
|
"better-sse": "^0.14.1",
|
|
11
11
|
"dbus-next": "^0.10.2",
|
|
12
12
|
"int24": "^0.0.1",
|
|
13
|
-
"kaitai-struct": "^0.10.0",
|
|
13
|
+
"kaitai-struct": "^0.10.0",
|
|
14
14
|
"node-ble": "^1.13.0",
|
|
15
|
-
"rimraf": "^6.0.1",
|
|
16
15
|
"semver": "^7.7.1"
|
|
17
16
|
},
|
|
18
17
|
"devDependencies": {
|
package/public/159.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";(self.webpackChunkbt_sensors_plugin_sk=self.webpackChunkbt_sensors_plugin_sk||[]).push([[159],{2995:(e,t,n)=>{n.r(t),n.d(t,{default:()=>A});var a=n(3490),s=n(4810),o=n(4147),r=n.n(o),l=n(7606),c=n(6452),i=n(3768),u=n(1431),g=n(9676),d=n(7041),m=n(3657),f=n(5027),p=n(3540),h=n(4712),E=n(5056),w=n(6890),S=n(3399);const v=e=>console.log.bind(console,e);var y,b={};const A=e=>{const[t,n]=(0,o.useState)({}),[A,D]=(0,o.useState)({}),[$,_]=(0,o.useState)({}),[k,
|
|
1
|
+
"use strict";(self.webpackChunkbt_sensors_plugin_sk=self.webpackChunkbt_sensors_plugin_sk||[]).push([[159],{2995:(e,t,n)=>{n.r(t),n.d(t,{default:()=>A});var a=n(3490),s=n(4810),o=n(4147),r=n.n(o),l=n(7606),c=n(6452),i=n(3768),u=n(1431),g=n(9676),d=n(7041),m=n(3657),f=n(5027),p=n(3540),h=n(4712),E=n(5056),w=n(6890),S=n(3399);const v=e=>console.log.bind(console,e);var y,b={};const A=e=>{const[t,n]=(0,o.useState)({}),[A,D]=(0,o.useState)({}),[$,_]=(0,o.useState)({}),[k,C]=(0,o.useState)({"ui:submitButtonOptions":{props:{disabled:!1,className:"btn btn-info"},norender:!0,submitText:"Submit"}}),[x,T]=(0,o.useState)([]),[j,M]=(0,o.useState)(),[O,N]=(0,o.useState)(new Map),[B,U]=(0,o.useState)({progress:0,maxTimeout:100,deviceCount:0,totalDevices:0}),[J,L]=(0,o.useState)("unknown"),[P,K]=(0,o.useState)();function z(e,t){console.log(`sending ${e}`),console.log(t);const n=new Headers;return n.append("Content-Type","application/json"),fetch(`/plugins/bt-sensors-plugin-sk/${e}`,{credentials:"include",method:"POST",body:JSON.stringify(t),headers:n})}async function H(e){return console.log(`fetching ${e}`),fetch(`/plugins/bt-sensors-plugin-sk/${e}`,{credentials:"include"})}async function I(){console.log("getProgress");const e=await H("getProgress");if(200!=e.status)throw new Error(`Unable get progres: ${e.statusText} (${e.status}) `);const t=await e.json();return console.log(t),t}function R(){console.log("refreshing sensor map"),async function(){console.log("getSensors");const e=await H("getSensors");if(200!=e.status)throw new Error(`Unable get sensor data: ${e.statusText} (${e.status}) `);const t=await e.json();return console.log(t),t}().then((e=>{N(new Map(e.map((e=>[e.info.mac,e]))))})).catch((e=>{K(e)}))}function W(e){return Object.keys(e.config).length>0}function Y(e){const t=W(e);return r().createElement(w.A,{action:!0,onClick:()=>{e.config.mac_address=e.info.mac,_(e.schema),M(e.config)}},r().createElement("div",{class:"d-flex justify-content-between align-items-center",style:t?{fontWeight:"normal"}:{fontStyle:"italic"}},`${e._changesMade?"*":""}${e.info.name} MAC: ${e.info.mac} RSSI: ${n=e.info.RSSI,null==n?NaN:n}`,r().createElement("div",{class:"d-flex justify-content-between "},function(e){return null==e.info.lastContactDelta||e.info.lastContactDelta>e.config.discoveryTimeout?r().createElement(i.A,null):e.info.signalStrength>80?r().createElement(u.A,null):e.info.signalStrength>60?r().createElement(g.A,null):e.info.signalStrength>40?r().createElement(d.A,null):e.info.signalStrength>20?r().createElement(m.A,null):r().createElement(f.A,null)}(e))));var n}return(0,o.useEffect)((()=>{console.log("useEffect([])");let e=null;return H("getPluginState").then((async t=>{if(404==t.status)throw L("unknown"),new Error("unable to get plugin state");const n=await t.json();console.log("Setting up eventsource"),e=new EventSource("/plugins/bt-sensors-plugin-sk/sse",{withCredentials:!0}),L(n.state),await async function(){console.log("getDomains");const e=await H("getDomains");if(200!=e.status)throw new Error(`Unable get domain data: ${e.statusText} (${e.status}) `);const t=await e.json();return console.log(t),t}(),e.addEventListener("newsensor",(e=>{console.log("newsensor");let t=JSON.parse(e.data);y.has(t.info.mac)||(console.log(`New sensor: ${t.info.mac}`),N(new Map(y.set(t.info.mac,t))))})),e.addEventListener("sensorchanged",(e=>{let t=JSON.parse(e.data);if(console.log("sensorchanged"),console.log(t),y.has(t.mac)){let e=y.get(t.mac);Object.assign(e.info,t),N(new Map(y))}})),e.addEventListener("progress",(e=>{console.log("progress");const t=JSON.parse(e.data);U(t),console.log(t)})),e.addEventListener("pluginstate",(e=>{console.log("pluginstate");const t=JSON.parse(e.data);L(t.state)}))})).catch((e=>{K(e)})),()=>{console.log("Closing connection to SSE"),e.close()}}),[]),(0,o.useEffect)((()=>{console.log("useEffect([pluginState])"),"started"==J?(R(),async function(){console.log("getBaseData");const e=await H("getBaseData");if(200!=e.status)throw new Error(`Unable get base data: ${e.statusText} (${e.status}) `);const t=await e.json();return console.log(t),t}().then((e=>{n(e.schema),D(e.data)})).catch((e=>{K(e)})),I().then((e=>{U(e)})).catch((e=>{K(e)}))):(N(new Map),n({}),D({}))}),[J]),(0,o.useEffect)((()=>{console.log("useEffect([sensorMap])"),y=O;const e=new Set(O.entries().map((e=>e[1].info.domain))),t={_configured:Array.from(O.entries()).filter((e=>W(e[1]))).map((e=>Y(O.get(e[0]))))};e.forEach((e=>{var n;t[e]=(n=e,Array.from(O.entries()).filter((e=>e[1].info.domain===n))).map((e=>Y(O.get(e[0]))))})),b=t}),[O]),"stopped"==J||"unknown"==J?r().createElement("h1",null,"Enable plugin to see configuration (if plugin is Enabled and you're seeing this message, restart SignalK)"):r().createElement("div",null,P?r().createElement("h2",{style:"color: red;"},P):"",r().createElement(a.Ay,{schema:t,validator:s.Ay,onChange:e=>D(e.formData),onSubmit:({formData:e},t)=>{var n;n=e,console.log("updateBaseData"),z("updateBaseData",n).then((e=>{200!=e.status?K(new Error(`Unable to update base data: ${e.statusText} (${e.status})`)):(I().then((e=>{U(e)})).catch((e=>{K(e)})),R())})),_({})},onError:v("errors"),formData:A}),r().createElement("p",null),r().createElement("p",null),B.deviceCount<B.totalDevices?r().createElement(S.A,{max:B.maxTimeout,now:B.progress}):"",r().createElement("h2",null,O.size>0?"Bluetooth Devices - Select to configure":""),r().createElement("h2",null,O.size>0?"(* = sensor has unsaved changes)":""),r().createElement("p",null),r().createElement(h.A,{defaultActiveKey:"_configured",id:"domain-tabs",className:"mb-3"},Object.keys(b).map((e=>{return n=(t=e).slice("_"===t.charAt(0)?1:0),r().createElement(E.A,{eventKey:t,title:n.charAt(0).toUpperCase()+n.slice(1)},r().createElement("div",{style:{paddingBottom:20},class:"d-flex flex-wrap justify-content-start align-items-start"},r().createElement(p.A,{style:{maxHeight:"300px",overflowY:"auto"}},function(e){return b[e]}(t)),r().createElement("div",{style:{paddingLeft:10,paddingTop:10,display:0==Object.keys($).length?"none":""}},r().createElement(a.Ay,{schema:$,validator:s.Ay,uiSchema:k,onChange:e=>{O.get(e.formData.mac_address)._changesMade=!0,M(e.formData)},onSubmit:({formData:e},t)=>{var n;console.log(e),n=e,console.log("updateSensorData"),z("updateSensorData",n).then((e=>{if(200!=e.status)throw new Error(e.statusText);O.get(n.mac_address)._changesMade=!1,O.get(n.mac_address).config=n})),_({}),alert("Changes saved")},onError:v("errors"),formData:j},r().createElement("div",null,r().createElement(l.A,{direction:"row",style:{spacing:5}},r().createElement(c.A,{type:"submit",color:"primary",variant:"contained"},"Save"),r().createElement(c.A,{variant:"contained",onClick:()=>{var e;e=j.mac_address,console.log("undoChanges"),O.get(e)._changesMade=!1,M(O.get(e).config)}},"Undo"),r().createElement(c.A,{variant:"contained",color:"secondary",onClick:e=>{return t=j.mac_address,void(window.confirm(`Delete configuration for ${t}?`)&&function(e){console.log("removeSensorData");try{z("removeSensorData",{mac_address:e}).then((e=>{if(200!=e.status)throw new Error(e.statusText)})),y.delete(e),N(new Map(y)),_({})}catch{}}(t));var t}},"Delete")))))));var t,n}))))}}}]);
|
package/sensor_classes/ATC.js
CHANGED
|
@@ -25,7 +25,7 @@ class ATC extends BTSensor{
|
|
|
25
25
|
title:'data parsing strategy',
|
|
26
26
|
type: 'string',
|
|
27
27
|
enum:["ATC-LE","ATC-BE"],
|
|
28
|
-
default:
|
|
28
|
+
default: "ATC-LE"
|
|
29
29
|
}
|
|
30
30
|
)
|
|
31
31
|
if (!this.parser){
|
|
@@ -66,9 +66,12 @@ class ATC extends BTSensor{
|
|
|
66
66
|
super.propertiesChanged(props)
|
|
67
67
|
if (!props.hasOwnProperty("ServiceData")) return
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
var buff = this.getServiceData("0000181a-0000-1000-8000-00805f9b34fb")
|
|
70
70
|
if (!buff)
|
|
71
|
-
|
|
71
|
+
buff = this.getServiceData("0000fcd2-0000-1000-8000-00805f9b34fb")
|
|
72
|
+
if (!buff)
|
|
73
|
+
throw new Error("Unable to get service data for "+this.getDisplayName())
|
|
74
|
+
|
|
72
75
|
this.emitData("temp", buff)
|
|
73
76
|
this.emitData("humidity", buff)
|
|
74
77
|
this.emitData("voltage", buff)
|
|
@@ -295,7 +295,8 @@ class GobiusCTankMeter extends BTSensor{
|
|
|
295
295
|
]
|
|
296
296
|
})
|
|
297
297
|
|
|
298
|
-
this.
|
|
298
|
+
this.addDefaultParam("id")
|
|
299
|
+
.default="1"
|
|
299
300
|
|
|
300
301
|
this.addMetadatum("mst","","Device state",
|
|
301
302
|
(buffer)=>{return GobiusState.get(buffer.readInt8(0))}
|
|
@@ -319,7 +320,7 @@ class GobiusCTankMeter extends BTSensor{
|
|
|
319
320
|
.default='tanks.{type}.{id}.currentLevel'
|
|
320
321
|
|
|
321
322
|
this.addMetadatum("minc","rad","Sensor inclination",
|
|
322
|
-
(buffer)=>{return buffer.readInt8(5) * Math.
|
|
323
|
+
(buffer)=>{return buffer.readInt8(5) * Math.PI/180}
|
|
323
324
|
)
|
|
324
325
|
.default='tanks.{type}.{id}.inclination'
|
|
325
326
|
|
|
@@ -50,6 +50,7 @@ export default (props) => {
|
|
|
50
50
|
const [pluginState, setPluginState ] = useState("unknown")
|
|
51
51
|
const [error, setError ] = useState()
|
|
52
52
|
|
|
53
|
+
|
|
53
54
|
function sendJSONData(cmd, data){
|
|
54
55
|
|
|
55
56
|
console.log(`sending ${cmd}`)
|
|
@@ -188,20 +189,21 @@ export default (props) => {
|
|
|
188
189
|
|
|
189
190
|
useEffect(()=>{
|
|
190
191
|
console.log("useEffect([])")
|
|
192
|
+
let eventSource=null
|
|
193
|
+
|
|
191
194
|
fetchJSONData("getPluginState").then( async (response)=> {
|
|
192
195
|
if (response.status==404) {
|
|
193
196
|
setPluginState("unknown")
|
|
194
197
|
throw new Error("unable to get plugin state")
|
|
195
198
|
}
|
|
196
199
|
const json = await response.json()
|
|
197
|
-
|
|
200
|
+
console.log("Setting up eventsource")
|
|
201
|
+
eventSource = new EventSource("/plugins/bt-sensors-plugin-sk/sse", { withCredentials: true })
|
|
198
202
|
|
|
203
|
+
setPluginState(json.state)
|
|
199
204
|
|
|
200
205
|
_sensorDomains = await getDomains()
|
|
201
206
|
|
|
202
|
-
console.log("Setting up eventsource")
|
|
203
|
-
const eventSource = new EventSource("/plugins/bt-sensors-plugin-sk/sse")
|
|
204
|
-
|
|
205
207
|
eventSource.addEventListener("newsensor", (event) => {
|
|
206
208
|
console.log("newsensor")
|
|
207
209
|
let json = JSON.parse(event.data)
|
|
@@ -235,14 +237,18 @@ export default (props) => {
|
|
|
235
237
|
const json = JSON.parse(event.data)
|
|
236
238
|
setPluginState(json.state)
|
|
237
239
|
});
|
|
238
|
-
|
|
240
|
+
|
|
239
241
|
})
|
|
240
242
|
|
|
241
243
|
.catch( (e) => {
|
|
242
244
|
setError(e)
|
|
243
245
|
}
|
|
244
|
-
)
|
|
245
|
-
|
|
246
|
+
)
|
|
247
|
+
return () => {
|
|
248
|
+
console.log("Closing connection to SSE")
|
|
249
|
+
eventSource.close()
|
|
250
|
+
};
|
|
251
|
+
},[])
|
|
246
252
|
|
|
247
253
|
useEffect(()=>{
|
|
248
254
|
console.log("useEffect([pluginState])")
|