import { features } from 'process'; import readLine = require('readline') import util = require('util') import { RemoteMmiPriority, RemoteMmiSequence, RemoteMmiType } from '../main'; import { initSdk, YLSdkImpl } from "../main/app" import { BusylightStatus, ContactInfo, DateTime, DeviceFeature, HidWorkState } from '../main/common-types'; import { DeviceImpl } from '../main/device'; import { DeviceSettings, SettingInfo } from '../main/settings-type'; async function sleep(ms: number) { return new Promise((resolve) => { setTimeout(() => { resolve(''); }, ms) }); } const read = new Proxy(readLine.createInterface({ input: process.stdin, output: process.stdout }), { get: function (original: any, prop: any, proxy: any) { if (prop != 'question') { return Reflect.get(original, prop, proxy); } return (function (text: string) { return new Promise(resolve => original.question(text, resolve)); }).bind(original); } }); const deviceAPIs: Array<{ description: string, operation: (deviceImpl: DeviceImpl) => Promise }> = [ { description: 'Features APIs', operation: async deviceImpl => { var supportFeatures = await deviceImpl.getSupportedFeaturesAsync(); console.log("support feature:" + supportFeatures); var supportRing = await deviceImpl.isRingSupportedAsync(); var supportOffHook = await deviceImpl.isOffHookSupportedAsync(); var supportMute = await deviceImpl.isMuteSupportedAsync(); var supportHold = await deviceImpl.isHoldSupportedAsync(); console.log('call control supprt, ring:', supportRing, 'offhook:', supportOffHook, 'mute:', supportMute, 'hold:', supportHold); try { const result = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaReboot); console.log("isFeatureSupportedAsync FeaReboot returns " + JSON.stringify(result)); } catch (err) { console.error("Failed isFeatureSupportedAsync FeaReboot with error: " + err); } try { const result = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaBusyLight); console.log("isFeatureSupportedAsync FeaBusyLight returns " + JSON.stringify(result)); } catch (err) { console.error("Failed isFeatureSupportedAsync FeaBusyLight with error: " + err); } try { const result = await deviceImpl.isFeatureSupportedAsync(2000); console.log("isFeatureSupportedAsync returns " + JSON.stringify(result)); } catch (err) { console.error("Failed isFeatureSupportedAsync with error: " + err); } } }, { description: 'Settings APIs', operation: async deviceImpl => { var settings: DeviceSettings; try { settings = await deviceImpl.getSettingsAsync(); console.log('getSettings succeded'); for (let settingInfo of settings.listSettingInfo) { let name = settingInfo.name let settingGuid = settingInfo.settingGuid try { let setting = await deviceImpl.getSettingAsync(settingGuid); let settingInfo = setting.listSettingInfo[0] console.log(`get name:${name}, guid:${settingGuid} success. val:` + setting.listSettingInfo[0].currValue); } catch (error) { console.log(`get name:${name}, guid:${settingGuid} faield!`) } } try { //sysloglevel let syslogSeting = await deviceImpl.getSettingAsync('e5dc726f3e9442d0b3c7f0cd5efaf0b3'); let settingInfo = syslogSeting.listSettingInfo[0] let lastValue = settingInfo.currValue; settingInfo.currValue = lastValue == 5 ? 0 : 5; let settingsNew: DeviceSettings = { RetCode: 0, listSettingInfo: Array() }; settingsNew.listSettingInfo.push(settingInfo); console.log(`current value ${lastValue}, change to: ` + settingInfo.currValue); const result = await deviceImpl.setSettingsAsync(settingsNew); let newSetting = await deviceImpl.getSettingAsync("e5dc726f3e9442d0b3c7f0cd5efaf0b3"); console.log('check value: ' + settingInfo.currValue + ', ' + newSetting.listSettingInfo[0].currValue); settingInfo.currValue = lastValue; settingsNew = { RetCode: 0, listSettingInfo: Array() }; settingsNew.listSettingInfo.push(settingInfo); await deviceImpl.setSettingsAsync(settingsNew); } catch (error) { console.error(`getSetting failed, error: ${error}, code = ${error.code}`); } } catch (error) { console.error(`getSettings failed, error: ${error}, code = ${error.code}`); } } }, { description: 'Battery APIs', operation: async deviceImpl => { try { let result = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaBattery); console.log("is support battery feature:" + result); if (!result) { return; } } catch (err) { console.error("is support battery feature failed, error: " + err); } console.info("subscribing to battery events"); deviceImpl.RegEvent('onBatteryStatusUpdate', (levelInPercent, isCharging, isBatteryLow) => { console.info(`New battery status for my device ${deviceImpl.deviceId}: level == ${levelInPercent}%, isCharging == ${isCharging}, isBatteryLow == ${isBatteryLow}`); }); try { let result = await deviceImpl.getBatteryStatusAsync(); console.log("get battery status: " + JSON.stringify(result)); } catch (err) { console.error("get battery status returns failed, error: " + err); } } }, { description: 'Ringtone APIs', operation: async deviceImpl => { try { const result = await deviceImpl.isUploadRingtoneSupportedAsync(); console.log("is support upload ringtone feature: " + result); if (result) { try { await deviceImpl.uploadRingtoneAsync("dummy.notwav"); console.log("upload ringtone success"); } catch (error) { console.log("upload ring tone failed, error" + error); } } } catch (err) { console.error("is support upload ringtone feature, error: " + err); } const result = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaPlayRingtone); console.log("is support play ringtone feature: " + result); if (result) { try { await deviceImpl.playRingtoneAsync(0); console.log("play ring tone success"); } catch (error) { console.log("play ring tone failed, error" + error); } } } }, { description: 'DateTime APIs', operation: async deviceImpl => { const support = await deviceImpl.isSetDateTimeSupportedAsync(); console.log("set datetime support:" + support); if (!support) { return; } try { const newDate = new Date(); const dateTime: DateTime = { sec: newDate.getSeconds(), min: newDate.getMinutes(), hour: newDate.getHours() + 8, mon: newDate.getMonth(), year: newDate.getFullYear() - 1900, mday: newDate.getDate(), wday: newDate.getDay() }; await deviceImpl.setDateTimeAsync(dateTime); console.log("set datetime successful "); } catch (err) { console.error("set datetime successful failed, error " + err); } } }, { description: 'Reboot APIs', operation: async deviceImpl => { try { deviceImpl.rebootDeviceAsync() console.log("reboot device successful"); } catch (error) { console.log("reboot device failed, error" + error); } } }, { description: 'Reset APIs', operation: async deviceImpl => { try { deviceImpl.factoryResetAsync() console.log("reset device successful"); } catch (error) { console.log("reset device failed, error" + error); } } }, { description: 'Busylight APIs', operation: async deviceImpl => { const support = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaBusyLight); console.log("busylight support:" + support); if (!support) { return; } try { const status = await deviceImpl.getBusylightStatusAsync() console.log(`get busylight status: ${status} successful`); } catch (error) { console.log("get busylight status failed, error" + error); } try { await deviceImpl.setBusylightStatusAsync(BusylightStatus.BUSYLIGHT_RING); console.log(`set busylight status: ${BusylightStatus.BUSYLIGHT_RING} successful`); } catch (error) { console.log("set busylight status failed, error" + error); } try { const status = await deviceImpl.getBusylightStatusAsync() console.log(`get busylight status: ${status} successful`); } catch (error) { console.log("get busylight status failed, error" + error); } } }, { description: 'Device Image APIs', operation: async deviceImpl => { try { const path = await deviceImpl.getDeviceImagePathAsync(); console.log(`get device image path: ${path}`); } catch (error) { console.info(`get device image failed, error` + error); } } }, { description: 'Device log APIs', operation: async deviceImpl => { // export and upload both are independent operations var path = process.env.APPDATA; try { if (typeof path != "undefined") { path = path + "/YealinkSdk/"; console.info(`export device log, target path => ` + path); // export log to local path await deviceImpl.exportDevLogAsync(path); } } catch (error) { console.info(`export device log failed, error` + error); } (async () => { try { console.info(`upload device log to cloud`); // upload log to yealink cloud server await sleep(5000); await deviceImpl.uploadDevLogToCloudAsync(); } catch (error) { console.info(`export device log failed, error` + error); } })(); deviceImpl.RegEvent("onDevLogEvent", (logEventType, ret) => { console.info(`on device log event: ${logEventType}, reg: ${ret}`); }); } }, { description: 'Export audio APIs', operation: async deviceImpl => { var path = process.env.APPDATA; try { if (typeof path != "undefined") { path = path + "/YealinkSdk/"; console.info(`export device audio, target path => ` + path); await deviceImpl.startExportAudioAsync(path); } } catch (error) { console.info(`export device log failed, error` + error); } await sleep(5000); try { await deviceImpl.stopExportAudioAsync(); console.info(`export device audio successfil`); } catch (error) { console.info(`export device audio failed, error` + error); } } }, { description: 'RemoteMmi', operation: async deviceImpl => { deviceImpl.RegEvent('onRemoteMmiEvent', async (type, input) => { console.log("on remote mmi event: ", type, input); try { await deviceImpl.releaseRemoteMmiFocusAsync(type); } catch (error) { console.log("release remote mmi forcus, error: " + error); } }); try { let types = await deviceImpl.getRemoteMmiTypesAsync(); console.log("get remote mmi types: " + JSON.stringify(types)); } catch (error) { console.log("get remote mmi types, error: " + error); } try { await deviceImpl.getRemoteMmiFocusAsync(RemoteMmiType.MMI_TYPE_TEAMS, 255, RemoteMmiPriority.MMI_PRIORITY_HIGH); } catch (error) { console.log("get remote mmi forcus, error: " + error); } try { let isInFocus = await deviceImpl.isRemoteMmiInFocusaAsync(RemoteMmiType.MMI_TYPE_TEAMS); console.log('is in focus: ', isInFocus) } catch (error) { console.log("is in focus, error: " + error); } try { let actionOutput = { red: 100, green: 0, blue: 0, sequence: RemoteMmiSequence.MMI_LED_SEQUENCE_FAST } await deviceImpl.setRemoteMmiActionAsync(RemoteMmiType.MMI_TYPE_TEAMS, actionOutput); console.log("set focus, action output: " + JSON.stringify(actionOutput)); } catch (error) { console.log("is in focus, error: " + error); } } }, { description: 'Contact Info', operation: async deviceImpl => { let feaContact = await deviceImpl.isFeatureSupportedAsync(DeviceFeature.FeaContact); console.log('support contact feature:' + feaContact); if (!feaContact) { return; } let feaOffHook = await deviceImpl.isOffHookSupportedAsync(); if (!feaOffHook) { console.log('not support offhook'); return; } try { console.log('set offhook 1'); await deviceImpl.setOffHookAsync(true); } catch (error) { console.log('set offhook 1 failed, error:', error); } try { let currContact = { name: 'Bob', number: '955552' }; console.log("set contact info: " + currContact); await deviceImpl.setCurrentTalkContactAsync(currContact); } catch (error) { console.log("set contact info failed, error: " + error); } try { console.log('set offhook 0'); await deviceImpl.setOffHookAsync(false); } catch (error) { console.log('set offhook 0 failed, error:', error); } } }, { description: 'Dect', operation: async deviceImpl => { let regionVersion = await deviceImpl.getRegionVersionAsync(); let dectInDocked = await deviceImpl.hasDectInDockedAsync(); console.log(`dect region version: ${regionVersion}, dect in docked: ${dectInDocked}`); } } ]; (async () => { try { // This appId is internal demo used and may log out at any time let ylSdk = await initSdk('05d4af257a5d45c3a18c1e416b37848d'); var chosenDeviceOperation: ((app: DeviceImpl) => Promise) | null = null; var deviceCallback = async (deviceImpl: DeviceImpl) => { console.log(`device attach, device#${deviceImpl.deviceId}, info = ${JSON.stringify(deviceImpl)}`); console.info('device API to test?'); for (let [index, operation] of deviceAPIs.entries()) { console.info(`${index + 1}. ${operation.description} `); } var selectedNumber = await read.question('Enter a number: '); var index = parseInt(selectedNumber.trim()); //var index = 13; if (index !== 0) { const operation = deviceAPIs[index - 1]; if (!operation) { throw new Error(`Unknown operation: ${index} `); } chosenDeviceOperation = operation.operation; if (chosenDeviceOperation) { await chosenDeviceOperation(deviceImpl); let selectName = operation.description; console.log(`[${selectName}] test finish.`); } } }; // setting up listeners ylSdk.RegEvent('firstScanDone', () => { console.log('sdk first scan done') }) ylSdk.RegEvent('attach', deviceCallback); ylSdk.RegEvent('detach', (deviceImpl) => { console.log(`device detach, device#${deviceImpl.deviceId} ${deviceImpl.deviceName} `) }) } catch (e) { console.error("excenption: " + e); } })();