var P=Object.defineProperty;var r=(i,n)=>P(i,"name",{value:n,configurable:!0});import e from"lodash";import{SerialPort as c}from"serialport";import{Duplex as x}from"stream";async function y(){let i=e.find(await c.list(),{vendorId:"6868",productId:"8686"});if(e.isNil(i))throw new Error("device not found");return i?.path}r(y,"findDevicePath");var u=class{static{r(this,"SerialPortAdapter")}duplex;name="adapter";ultra;#t(n,...l){this.ultra?.emitter.emit("debug","serial",n,...l)}async install(n,l={}){let t=this.ultra=n.ultra;e.isNil(t.$adapter)||await t.disconnect(new Error("adapter replaced"));let s={isSupported:r(()=>!e.isNil(c),"isSupported")};return t.addHook("connect",async(m,o)=>{if(t.$adapter!==s)return await o();try{if(!s.isSupported())throw new Error("SerialPort not supported");let a=l?.baudRate??115200,d=l?.path??await y();return this.duplex=await new Promise((p,w)=>{let f=new c({baudRate:a,path:d},h=>{e.isNil(h)?p(f):w(h)})}),this.duplex?.once("close",()=>{t.disconnect(new Error("SerialPort closed"))}),this.#t(`port connected, path = ${d}, baudRate = ${a}`),t.port=e.merge(x.toWeb(this.duplex),{isOpen:r(()=>this.duplex?.isOpen??!1,"isOpen"),isDfu:r(()=>!1,"isDfu")}),await o()}catch(a){throw this.#t(a),a}}),t.addHook("disconnect",async(m,o)=>{if(t.$adapter!==s||e.isNil(this.duplex))return await o();await o(),await new Promise((a,d)=>{this.duplex?.close(p=>{e.isNil(p)?a():d(p)})}),delete this.duplex}),s}};((globalThis??{}).ChameleonUltraJS??{}).SerialPortAdapter=u;export{u as default};
//# sourceMappingURL=SerialPortAdapter.mjs.map