@danidoble/webserial
Version:
WebSerial API wrapper
2 lines (1 loc) • 1.6 kB
JavaScript
;Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("./kernel-9JeWIxgz.cjs"),a=require("./relay-E3NCcSjS.cjs");class r extends n.Kernel{constructor({filters:t=null,config_port:e,no_device:s=1,socket:i=!1}={}){if(super({filters:t,config_port:e,no_device:s,socket:i}),this.__internal__.device.type="relay",this.__internal__.auto_response=!1,this.__internal__.serial.auto_response=[2,6,221,221,240,207,3],n.Devices.getCustom(this.typeDevice,s))throw new Error(`Device ${this.typeDevice} ${s} already exists`);n.Devices.add(this)}serialMessage(t){t=this.fixHexArray(t);const e={code:t,name:null,description:null,request:null,no_code:0};t[0]==="a0"&&t[2]=="00"?(e.name="Relay turned off",e.description="The relay has been turned off successfully.",e.request=this.lastAction,e.no_code=101):t[0]==="a0"&&t[2]=="01"?(e.name="Relay turned on",e.description="The relay has been turned on successfully.",e.request=this.lastAction,e.no_code=102):(e.name="Unrecognized response",e.description="The response of application was received, but dont identify with any of current parameters",e.request="undefined",e.no_code=400),this.dispatch("serial:message",e)}serialSetConnectionConstant(t=1){return a.e.connection(t)}async turnOn(){const t=a.e.activate();await this.appendToQueue(t,"relay:turn-on")}async turnOff(){const t=a.e.deactivate();await this.appendToQueue(t,"relay:turn-off")}async toggle({inverse:t=!1,ms:e=300}={}){if(!t){await this.turnOn(),await n.wait(e),await this.turnOff();return}await this.turnOff(),await n.wait(e),await this.turnOn()}}exports.Relay=r;