import { TYSdk , Utils , DevInfo , StringType } from 'tuya-panel-kit' ; import { unsupportedBluetoothPidList } from '../config' ; const { getBitValue } = Utils . NumberUtils ; language description language description export enum DeviceCapability { WIFI = 0 , CABLE = 1 , GPRS = 2 , NBIOT = 3 , BLUETOOTH = 10 , BLEMESH = 11 , ZIGBEE = 12 , INFRARED = 13 , SUBPIECES = 14 , SIGMESH = 15 , MCU = 16 , TYMESH = 17 , ZWAVE = 18 , PLMESH = 19 , CAT1 = 20 , BEACON = 21 } language description param { number } capability param { Array < DeviceCapability | number > } requiredCapabilityList param { Array < DeviceCapability | number > } unneededCapabilityList return { boolean } language description param { number } capability param { Array < DeviceCapability | number > } requiredCapabilityList param { Array < DeviceCapability | number > } unneededCapabilityList return { boolean } const checkCapability = ( capability , requiredCapabilityList = [ ] , unneededCapabilityList = [ ] ) : boolean => { return ( requiredCapabilityList . every ( d => ! ! getBitValue ( capability , d ) ) && ! unneededCapabilityList . some ( d => ! ! getBitValue ( capability , d ) ) ) ; } ; language description return { Promise < Array < DevInfo > > } language description return { Promise < Array < DevInfo > > } const getAllDevice = ( ) : Promise < Array < DevInfo > > => { return new Promise ( ( resolve , reject ) => { TYSdk . native . getDeviceList ( resolve , reject ) ; } ) ; } ; language description param { string } devId return { Promise < Array < DevInfo > > } language description param { string } devId return { Promise < Array < DevInfo > > } const getAllSubDevList = ( devId ? ) : Promise < Array < DevInfo > > => { return new Promise ( ( resolve , reject ) => { TYSdk . native . getSubDeviceList ( { devId : devId || TYSdk . devInfo . devId , } , resolve , reject ) ; } ) ; } ; language description param { Array < ( capability : number , devInfo ? : DevInfo ) => boolean > } rules param { string } devId return { Promise < Array < Array < DevInfo > > > } language description param { Array < ( capability : number , devInfo ? : DevInfo ) => boolean > } rules param { string } devId return { Promise < Array < Array < DevInfo > > > } const getSpecificSubDevList = async ( rules = [ ] , gatewayDevId ? ) : Promise < Array < Array < DevInfo > > > => { const allSubDevList = await getAllSubDevList ( gatewayDevId ) ; const res = [ ] ; if ( Array . isArray ( rules ) ) { rules . forEach ( ( rule , idx ) => { res [ idx ] = allSubDevList . filter ( devItem => rule ( devItem . capability , devItem ) ) ; } ) ; return res ; } return [ allSubDevList ] ; } ; language description param { number } capability return { boolean } language description param { number } capability return { boolean } const isBlueSub = ( capability ) : boolean => { return checkCapability ( capability , [ DeviceCapability . BLUETOOTH ] , [ DeviceCapability . ZIGBEE , DeviceCapability . SIGMESH , DeviceCapability . WIFI ] ) ; } ; language description param { number } capability return { boolean } language description param { number } capability return { boolean } const isSigmeshSub = ( capability ) : boolean => { return checkCapability ( capability , [ DeviceCapability . SIGMESH ] , [ DeviceCapability . WIFI ] ) ; } ; language description param { number } capability return { boolean } language description param { number } capability return { boolean } const isZigbeeSub = ( capability ) : boolean => { return checkCapability ( capability , [ DeviceCapability . ZIGBEE ] , [ DeviceCapability . WIFI , DeviceCapability . CABLE ] ) ; } ; language description param { number } capability return { boolean } language description param { number } capability return { boolean } const isBeaconSub = ( capability ) : boolean => { return checkCapability ( capability , [ DeviceCapability . BEACON ] , [ DeviceCapability . WIFI , DeviceCapability . CABLE ] ) ; } ; language description param { DevInfo } subDevInfo param { DevInfo } gatewayDevInfo return { boolean } language description param { DevInfo } subDevInfo param { DevInfo } gatewayDevInfo return { boolean } const isAddableMesh = ( subDevInfo , gatewayDevInfo = TYSdk . devInfo ) : boolean => { const { meshId , capability } = subDevInfo ; const { sigmeshId : gatewaySigmeshId , devId : gatewayDevId } = gatewayDevInfo ; const isSigmesh = isSigmeshSub ( capability ) ; return isSigmesh || meshId ? meshId === gatewaySigmeshId && meshId !== gatewayDevId : true ; } ; language description param { DevInfo } devInfo param { DevInfo } gatewayDevInfo param { Function } isMeshValid param { Array < String > } bluetoothPidBlackList param { boolean } supportBeacon return { boolean } language description param { DevInfo } devInfo param { DevInfo } gatewayDevInfo param { Function } isMeshValid param { Array < String > } isMeshValid param { boolean } supportBeacon return { boolean } const isAddableDevice = ( { devInfo , gatewayDevInfo = TYSdk . devInfo , isMeshValid = isAddableMesh , bluetoothPidBlackList = unsupportedBluetoothPidList , supportBeacon = false , } ) : boolean => { const { isVDevice , devId , capability , productId } = devInfo ; const { devId : gatewayDevId } = gatewayDevInfo ; return ( isVDevice !== true && devId !== gatewayDevId && checkCapability ( capability , [ ] , [ DeviceCapability . INFRARED ] ) && isMeshValid ( devInfo , gatewayDevInfo ) && ( isSigmeshSub ( capability ) || ( isBlueSub ( capability ) && ! bluetoothPidBlackList . includes ( productId ) ) || ( supportBeacon && isBeaconSub ( capability ) ) ) ) ; } ; language description param { object } originI18n param { string } prefix return { object } language description param { object } originI18n param { string } prefix return { object } const addPrefixToI18n = ( originI18n , prefix ) : { [ key ] : StringType ; } => { const languageTypeList = Object . keys ( originI18n ) ; const copiedOriginI18n = originI18n ; const newI18n = { } ; languageTypeList . forEach ( l => { const singleLanguageObj = copiedOriginI18n [ l ] ; const list = Object . keys ( singleLanguageObj ) ; newI18n [ l ] = { } ; list . forEach ( item => { newI18n [ l ] [ `${ prefix }${ item }` ] = singleLanguageObj [ item ] ; } ) ; } ) ; return newI18n ; } ; export const getOnlineState = ( pcc = '' ) : boolean => { if ( pcc === '' ) return false ; if ( pcc . length > 1 ) { const _head = pcc [ 0 ] ; const _footer = pcc [ pcc . length - 1 ] ; return [ '05' , '50' ] . includes ( `${ _head }${ _footer }` ) ; } return false ; } ; export default { checkCapability , getAllDevice , getAllSubDevList , getSpecificSubDevList , isBlueSub , isSigmeshSub , isZigbeeSub , isBeaconSub , isAddableMesh , isAddableDevice , addPrefixToI18n , getOnlineState , } ;