import { initSdk, YLSdkImpl } from "../main/app" import { DeviceBtnType, HidWorkState } from "../main/common-types"; import { DeviceImpl } from "../main/device"; async function sleep(ms: number) { return new Promise((resolve) => { setTimeout(() => { resolve(''); }, ms) }); } (async () => { try { let ylSdk = await initSdk("test_App_Id"); let stateOffHoook: boolean = false; let stateMute: boolean = false; let stateRing: boolean = false; let currentHidWorkState: HidWorkState; ylSdk.getSDKVersionAsync().then((result) => { console.log("current sdk version is:" + result); }).catch((error) => { console.log("get current error:" + error); }) // test softphone setting interface // Yealink application(Yealink USB Connect ) need to setup for this case let connectRet = await ylSdk.connectToYealinkApplicationAsync("bb0f6ade-b014-4f5b-90e4-15ced02cfcb2", "Test4NodeSdk"); console.log("conenct to yealink application ret: " + connectRet); if (connectRet) { try { await ylSdk.setSoftphoneReadyAsync(true); } catch (error) { console.log("set softphone ready exception:" + error); } try { await sleep(1000); let focus = await ylSdk.isSoftphoneInFocusAsync(); console.log("get softphone ready, is in focus: " + focus); } catch (error) { console.log("get softphone ready exception:" + error); } try { await sleep(1000); await ylSdk.disconnectFromYealinkApplicationAsync(); } catch (error) { console.log("dis conenct form yl app exception:" + error); } } let testDevice = async (deviceImpl: DeviceImpl) => { console.log("device attached, name:" + deviceImpl.deviceName); var supportRing = await deviceImpl.isRingSupportedAsync(); var supportOffHook = await deviceImpl.isOffHookSupportedAsync(); var supportMute = await deviceImpl.isMuteSupportedAsync(); var supportHold = await deviceImpl.isHoldSupportedAsync(); var supportYLHid = await deviceImpl.IsYlHidStdHidSupportedAsync(); console.log('call control supprt, ring:', supportRing, 'offhook:', supportOffHook, 'mute:', supportMute, 'hold:', supportHold, 'YL hid:', supportYLHid); try { await deviceImpl.setHidWorkingStateAsync(HidWorkState.STD_HID); } catch (error) { console.log("get error:" + error); } try { currentHidWorkState = await deviceImpl.getHidWorkingStateAsync(); var isStdHidEventsFromYealinkDevices = await deviceImpl.isStdHidEventsFromYealinkDevicesAsync(); console.log("current hid work mode:" + currentHidWorkState + ", std hid event from yealink device:" + isStdHidEventsFromYealinkDevices); if (HidWorkState.STD_HID == currentHidWorkState && !isStdHidEventsFromYealinkDevices) { await deviceImpl.setStdHidEventsFromYealinkDevicesAsync(true); console.log("set std hid event from device enable"); } } catch (error) { console.log("get error:" + error); } var isRingSupport = await deviceImpl.isRingSupportedAsync(); if (isRingSupport) { console.log("set ring 1"); try { await deviceImpl.setRingAsync(true); stateRing = true; } catch (error) { console.log("get error:" + error); } } // receive btn state change and respone relate state deviceImpl.RegEvent('btnPress', async (translateInData, btnInData) => { console.log("translateInData:" + translateInData + ", btnInData:", btnInData); try { switch (translateInData) { case DeviceBtnType.OffHook: if (stateOffHoook) { if (!btnInData) { await deviceImpl.setOffHookAsync(false); stateOffHoook = btnInData; } } else { if (btnInData) { if (stateRing) { stateRing = false; await deviceImpl.setRingAsync(false); } await deviceImpl.setOffHookAsync(true); stateOffHoook = btnInData; } } break; case DeviceBtnType.Mute: if (stateMute) { if (!btnInData) { await deviceImpl.setMuteAsync(false); stateMute = btnInData; if (HidWorkState.STD_HID == currentHidWorkState) { try { var ylHidSupport = await deviceImpl.IsYlHidStdHidSupportedAsync(); console.log("yl hid is suport:" + ylHidSupport); if (ylHidSupport) { await deviceImpl.setHidWorkingStateAsync(HidWorkState.YL_HID); currentHidWorkState = HidWorkState.YL_HID; console.log("change hid work state to YL_HID"); } } catch (error) { console.log("get error:" + error); } } } } else { if (btnInData) { await deviceImpl.setMuteAsync(true); stateMute = btnInData; } } break; default: break; } } catch (error) { console.log("get error:" + error); } }); } ylSdk.RegEvent('detach', (deviceImpl: DeviceImpl) => { console.log("device detached, name:", deviceImpl.deviceName); ylSdk.disposeAsync(); }); let devs = ylSdk.getAttachedDevices(); console.log('\nattached device count:' + devs.length) if (devs.length > 0) { testDevice(devs[0]) } else { // test device call control ylSdk.RegEvent('attach', testDevice) } } catch (error) { console.error("Got exception err " + error); } })();