bt-sensors-plugin-sk
Version:
Bluetooth Sensors for Signalk - see https://www.npmjs.com/package/bt-sensors-plugin-sk#supported-sensors for a list of supported sensors
1 lines • 9.43 kB
JavaScript
(self.webpackChunkbt_sensors_plugin_sk=self.webpackChunkbt_sensors_plugin_sk||[]).push([[124],{62995:(e,t,n)=>{"use strict";n.r(t),n.d(t,{BTConfig:()=>$,default:()=>D});var a=n(16718),s=n(74810),r=n(40334),o=n(64147),i=n.n(o),c=n(11363),l=n(97493),u=n(27606),m=n(82096),d=n(55877),f=n(95027),g=n(86038),h=n(47041),p=n(39676),E=n(71431),y=n(3768),w=n(26067),S=n(43540),v=n(38250),b=n(31008),A=n(20455),C=n(23399);const k=e=>console.log.bind(console,e);function $(e){const t=(0,m.A)(e=>({root:{"& > *":{margin:e.spacing(1)}}})),[n,$]=(0,o.useState)({}),[D,_]=(0,o.useState)({}),[x,T]=(0,o.useState)({}),[N,O]=(0,o.useState)({"ui:options":{label:!1},paths:{enableMarkdownInDescription:!0},title:{"ui:widget":"hidden"}}),[j,M]=(0,o.useState)(),[U,R]=(0,o.useState)(!1),[B,J]=(0,o.useState)(!0),[L,I]=(0,o.useState)(new Map),[P,W]=(0,o.useState)({progress:0,maxTimeout:100,deviceCount:0,totalDevices:0}),[z,K]=(0,o.useState)("unknown"),[F,G]=(0,o.useState)(),[H,Y]=(0,o.useState)(!1),[q,Q]=(0,o.useState)(""),V=t();function X(e,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 Z(e,t={}){let n;try{const a=Object.keys(t).length?"?"+new URLSearchParams(t).toString():"";n=await fetch(`/plugins/bt-sensors-plugin-sk/${e}${a}`,{credentials:"include",method:"GET"})}catch(e){n={status:500,statusText:e.toString()}}return n}function ee(e){return Object.keys(e.configCopy).length>0}function te(e){const t=ee(e);return i().createElement(A.A,{action:!0,onClick:()=>{e.config.mac_address=e.info.mac,T(e.schema),M(e.config)}},i().createElement("div",{class:"d-flex justify-content-between"},i().createElement("div",{class:"d-flex",style:t?{fontWeight:"normal"}:{fontStyle:"italic"}},`${e._changesMade?"*":""}${e.info.name} MAC: ${e.info.mac} RSSI: ${n=e.info.RSSI,null==n?NaN:n} ${function(e){const t=e.info?.batteryStrength;return t?`BATT: ${100*t}%`:""}(e)}`,function(e){const t=e.info.batteryStrength;return void 0===t?"":i().createElement(w.A,{size:22,orientation:"vertical",customization:{batteryBody:{fill:"silver",strokeColor:"silver",strokeWidth:2},batteryCap:{fill:"silver",strokeColor:"silver",cornerRadius:3,strokeWidth:0,capToBodyRatio:.4},readingText:{lightContrastColor:"purple",darkContrastColor:"yellow",lowBatteryColor:"red",fontFamily:"Arial",fontSize:16}},value:100*t})}(e)),i().createElement("div",{class:"d-flex "},`${e.info.state} ${e.info.error?" (ERROR)":""}`,function(e){return e.info.connected?i().createElement(d.A,null):null==e.info.lastContactDelta||e.info.lastContactDelta>e.config.discoveryTimeout?i().createElement(y.A,null):e.info.signalStrength>80?i().createElement(E.A,null):e.info.signalStrength>60?i().createElement(p.A,null):e.info.signalStrength>40?i().createElement(h.A,null):e.info.signalStrength>20?i().createElement(g.A,null):i().createElement(f.A,null)}(e))));var n}function ne(e){window.open(e,"_blank","noreferrer")}return(0,o.useEffect)(()=>{let e=null;return Z("getPluginState").then(async t=>{if(404==t.status)throw K("unknown"),new Error("unable to get plugin state");const n=await t.json();e=new EventSource("/plugins/bt-sensors-plugin-sk/sse",{withCredentials:!0}),e.addEventListener("newsensor",e=>{!function(e){let t=JSON.parse(e.data);console.log(`New sensor: ${t.info.mac}`),I(e=>(e.set(t.info.mac,t),new Map(e)))}(e)}),e.addEventListener("sensorchanged",e=>{!function(e){console.log("sensorchanged");const t=JSON.parse(e.data);console.log(t),I(e=>{const n=e.get(t.mac);return n&&Object.assign(n.info,t),new Map(e)})}(e)}),e.addEventListener("progress",e=>{const t=JSON.parse(e.data);W(t)}),e.addEventListener("pluginstate",e=>{const t=JSON.parse(e.data);K(t.state)}),K(n.state),(async()=>{const e=await async function(){const e=await Z("getSensors");if(200!=e.status)throw new Error(`Unable get sensor data: ${e.statusText} (${e.status}) `);return await e.json()}();I(new Map(e.map(e=>[e.info.mac,e])))})()}).catch(e=>{G(e.message)}),()=>{console.log("Closing connection to SSE"),e.close()}},[]),(0,o.useEffect)(()=>{if(!U)return;if(!j||!L)return;const e=L.get(j.mac_address);e&&x&&j&&Object.hasOwn(j,"params")&&"UNKNOWN"==e.info.class&&j.params.sensorClass&&"UNKNOWN"!=j.params.sensorClass&&(J(!1),Q(`Please wait. Fetching schema for ${j.params.sensorClass}...`),async function(e,t){const n=await Z("getSensorInfo",{mac_address:e,class:t});if(200!=n.status)throw new Error(`Unable get sensor info: ${n.statusText} (${n.status}) `);return await n.json()}(j.mac_address,j.params.sensorClass).then(e=>{T(e.schema)}).catch(e=>{alert(e.message)}).finally(()=>{Q(""),J(!0),R(!1)}))},[U]),(0,o.useEffect)(()=>{Y(""!=q)},[q]),(0,o.useEffect)(()=>{"started"==z?(async function(){const e=await Z("getBaseData");if(200!=e.status)throw new Error(`Unable to get base data: ${e.statusText} (${e.status}) `);const t=await e.json();return t.schema.htmlDescription=i().createElement("div",null,(0,r.Ay)(t.schema.htmlDescription),i().createElement("p",null)),t}().then(e=>{$(e.schema),_(e.data)}).catch(e=>{G(e.message)}),async function(){const e=await Z("getProgress");if(200!=e.status)throw new Error(`Unable to get progress: ${e.statusText} (${e.status}) `);return await e.json()}().then(e=>{W(e)}).catch(e=>{G(e.message)})):($({}),_({}))},[z]),"stopped"==z||"unknown"==z?i().createElement("h3",null,"Enable plugin to see configuration"):i().createElement("div",null,i().createElement(c.A,{anchorOrigin:{horizontal:"center",vertical:"bottom"},onClose:()=>Y(!1),open:H,message:q,key:"snackbar"}),i().createElement("div",{className:V.root},i().createElement(l.A,{variant:"contained",onClick:()=>{ne("https://github.com/naugehyde/bt-sensors-plugin-sk/tree/1.2.0-beta#configuration")}},"Documentation"),i().createElement(l.A,{variant:"contained",onClick:()=>{ne("https://github.com/naugehyde/bt-sensors-plugin-sk/issues/new/choose")}},"Report Issue"),i().createElement(l.A,{variant:"contained",onClick:()=>{ne("https://discord.com/channels/1170433917761892493/1295425963466952725")}},"Discord Thread"),i().createElement("p",null),i().createElement("p",null)),i().createElement("hr",{style:{width:"100%",height:"1px",color:"gray","background-color":"gray","text-align":"left","margin-left":0}}),F?i().createElement("h2",{style:{color:"red"}},F):"",i().createElement(a.Ay,{schema:n,validator:s.Ay,uiSchema:{"ui:field":"LayoutGridField","ui:layoutGrid":{"ui:row":[{"ui:row":{className:"row",children:[{"ui:columns":{className:"col-xs-4",children:["adapter","transport","duplicateData","discoveryTimeout","discoveryInterval"]}}]}}]}},onChange:e=>_(e.formData),onSubmit:({formData:e},t)=>{var n;n=e,I(new Map),X("updateBaseData",n).then(e=>{200!=e.status&&G(`Unable to update base data: ${e.statusText} (${e.status})`)}),T({})},onError:k("errors"),formData:D}),i().createElement("hr",{style:{width:"100%",height:"1px",color:"gray","background-color":"gray","text-align":"left","margin-left":0}}),i().createElement("p",null),i().createElement("p",null),P.deviceCount<P.totalDevices?i().createElement(C.A,{max:P.maxTimeout,now:P.progress}):"",i().createElement("p",null),i().createElement(v.A,{defaultActiveKey:"_configured",id:"domain-tabs",className:"mb-3"},function(){const e=[...new Set([...L.entries()].map(e=>e[1].info.domain))].sort(),t=Array.from(L.entries()).filter(e=>ee(e[1]));let n={};return n._configured=0==t.length?"Select a device from its domain tab (Electrical etc.) and configure it.":t.map(e=>te(L.get(e[0]))),e.forEach(e=>{var t;n[e]=(t=e,Array.from(L.entries()).filter(e=>e[1].info.domain===t)).map(e=>te(L.get(e[0])))}),Object.keys(n).map(e=>function(e,t){let n=e.slice("_"===e.charAt(0)?1:0);return i().createElement(b.A,{eventKey:e,title:`${n.charAt(0).toUpperCase()}${n.slice(1)}${"string"==typeof t?"":" ("+t.length+")"}`},i().createElement(S.A,{style:{maxHeight:"300px",overflowY:"auto"}},t))}(e,n[e]))}()),i().createElement("div",{style:{paddingLeft:10,paddingTop:10,display:0==Object.keys(x).length?"none":""}},i().createElement(u.A,{container:!0,direction:"column",style:{spacing:5}},i().createElement(u.A,{item:!0},i().createElement("h2",null,x?.title),i().createElement("p",null)),i().createElement(u.A,{item:!0},(0,r.Ay)(x?.htmlDescription))),i().createElement("fieldset",{disabled:!B},i().createElement(a.Ay,{schema:x,validator:s.Ay,uiSchema:N,onChange:(e,t)=>{const n=L.get(e.formData.mac_address);n&&(n._changesMade=!0,n.config=e.formData,M(e.formData)),"root_params_sensorClass"==t&&R(!0)},onSubmit:({formData:e},t)=>{var n;X("updateSensorData",n=e).then(e=>{if(200!=e.status)throw new Error(e.statusText);I(e=>(e.delete(n.mac_address),new Map(e))),T({})}),alert("Changes saved")},onError:k("errors"),formData:j},i().createElement("div",{className:V.root},i().createElement(l.A,{type:"submit",color:"primary",variant:"contained"},"Save"),i().createElement(l.A,{variant:"contained",onClick:()=>{var e;e=j.mac_address,L.get(e)._changesMade=!1,L.get(e).config=JSON.parse(JSON.stringify(L.get(e).configCopy)),M(L.get(e).config)}},"Undo"),i().createElement(l.A,{variant:"contained",color:"secondary",onClick:e=>function(e){const t=L.get(e);(!ee(t)||window.confirm(`Delete configuration for ${t.info.name}?`))&&function(e){try{X("removeSensorData",{mac_address:e}).then(e=>{if(200!=e.status)throw new Error(e.statusText)}),I(t=>(t.delete(e),new Map(t))),T({})}catch{}}(e)}(j.mac_address)},"Delete"))))))}const D=$},95098:()=>{}}]);