diff --git a/package-lock.json b/package-lock.json index 7c6b0d7..2fe9c8e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@zigbee/zcl-id", - "version": "0.5.0-2", + "version": "0.5.0-3", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -56,11 +56,6 @@ "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A==", "dev": true }, - "busyman": { - "version": "0.3.0", - "resolved": "https://registry.npmjs.org/busyman/-/busyman-0.3.0.tgz", - "integrity": "sha1-FN6kn2JgfOSy0wsgPGMG72zKiKc=" - }, "chai": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/chai/-/chai-4.2.0.tgz", diff --git a/package.json b/package.json index 6d832c9..1124f05 100644 --- a/package.json +++ b/package.json @@ -42,9 +42,7 @@ "url": "https://github.com/ZigBeans/zcl-id/issues" }, "homepage": "https://github.com/ZigBeans/zcl-id#readme", - "dependencies": { - "busyman": "^0.3.0" - }, + "dependencies": {}, "devDependencies": { "@types/mocha": "^5.2.5", "@types/node": "^10.12.18", diff --git a/src/definitions/clusterWithNewFormat.ts b/src/definitions/clusterWithNewFormat.ts index 8c58d74..8bcd7a1 100644 --- a/src/definitions/clusterWithNewFormat.ts +++ b/src/definitions/clusterWithNewFormat.ts @@ -1,4 +1,4 @@ -import Enum = require('./enum') +import { Enum } from './enum' export default function clusterWithNewFormat({ attrId, cmd, cmdRsp }) { const attrs = {} diff --git a/src/definitions/enum.ts b/src/definitions/enum.ts index fd62d0c..2570b8c 100644 --- a/src/definitions/enum.ts +++ b/src/definitions/enum.ts @@ -1,9 +1,9 @@ -interface EnumItem { +export interface EnumItem { key: string value: T } -export = class Enum { +export class Enum { byKey: Map> byValue: Map> enums: EnumItem[] diff --git a/src/index.ts b/src/index.ts index 9140af2..72c9d3c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,4 @@ -import _ = require('busyman') -import Enum = require('./definitions/enum') +import { Enum } from './definitions/enum' import common = require('./definitions/common.json') import clusterDefs = require('./definitions/cluster_defs.json') @@ -16,14 +15,23 @@ export const statusId = new Enum(_common.status) export const clusterId = new Enum(_common.clusterId) export const deviceId = { HA: new Enum(_common.haDevId) } -function isValidArgType(param) { - if (typeof param !== 'number' && typeof param !== 'string') { - return false +function isValidArgType(param: any): param is string | number { + if (typeof param === 'string') { + return true } if (typeof param === 'number') { return !isNaN(param) } - return true + return false +} + +const isNil = (val: any): boolean => val == undefined + +const assertAndParse = (val: any, name: string): string | number => { + if (!isValidArgType(val)) + throw new TypeError(name + ' should be a number or a string.') + const num = parseInt(val as string, 10) + return isNaN(num) ? val : num } const newFormatClusters = new Map() @@ -41,7 +49,7 @@ export function _getCluster(cluster) { clusterDefs[cluster] = null return newCluster } - throw new Error(`Cluster ${cluster} not found in cluster_defs.json`) + // throw new Error(`Cluster ${cluster} not found in cluster_defs.json`) // return: { // attr, // attrType, @@ -52,220 +60,117 @@ export function _getCluster(cluster) { export function profile(profId) { // profId: String | Number - if (!isValidArgType(profId)) - throw new TypeError('profId should be a number or a string.') - - const profNumber = parseInt(profId) - - if (!isNaN(profNumber)) profId = profNumber - - const profItem = profileId.get(profId) + profId = assertAndParse(profId, 'profId') - if (profItem) return { key: profItem.key, value: profItem.value } // { key: 'HA', value: 260 } + return profileId.get(profId) // { key: 'HA', value: 260 } } export function device(profId, devId) { // profId: String | Number, devId: String | Number - if (!isValidArgType(profId)) - throw new TypeError('profId should be a number or a string.') - - if (!isValidArgType(devId)) - throw new TypeError('devId should be a number or a string.') - - const profNumber = parseInt(profId) - const devNumber = parseInt(devId) - - if (!isNaN(profNumber)) profId = profNumber - - if (!isNaN(devNumber)) devId = devNumber + profId = assertAndParse(profId, 'profId') + devId = assertAndParse(devId, 'devId') const profItem = profileId.get(profId) + if (!profItem) return - let devItem - if (profItem) devItem = deviceId[profItem.key].get(devId) - - if (devItem) return { key: devItem.key, value: devItem.value } // { key: 'ON_OFF_SWITCH', value: 0 } + return deviceId[profItem.key].get(devId) // { key: 'ON_OFF_SWITCH', value: 0 } } export function cluster(cId: string | number): { key: string; value: number } { // cId: String | Number - if (!isValidArgType(cId)) - throw new TypeError('cId should be a number or a string.') - - const cNumber = parseInt(cId as string) + cId = assertAndParse(cId, 'cId') - if (!isNaN(cNumber)) cId = cNumber - - const cItem = clusterId.get(cId) - - return cItem // { key: 'genBasic', value: 0 } + return clusterId.get(cId) // { key: 'genBasic', value: 0 } } export function foundation(cmdId) { // cmdId: String | Number - if (!isValidArgType(cmdId)) - throw new TypeError('cmdId should be a number or a string.') - - const cmdNumber = parseInt(cmdId) - - if (!isNaN(cmdNumber)) cmdId = cmdNumber - - const cmdItem = foundationId.get(cmdId) + cmdId = assertAndParse(cmdId, 'cmdId') - if (cmdItem) return { key: cmdItem.key, value: cmdItem.value } // { key: 'read', value: 0 } + return foundationId.get(cmdId) // { key: 'read', value: 0 } } export function functional(cId, cmdId) { // cId: String | Number, cmdId: String | Number - if (!isValidArgType(cId)) - throw new TypeError('cId should be a number or a string.') + cId = assertAndParse(cId, 'cId') - if (!isValidArgType(cmdId)) - throw new TypeError('cmdId should be a number or a string.') - - const cNumber = parseInt(cId) - const cmdNumber = parseInt(cmdId) - - if (!isNaN(cNumber)) cId = cNumber - - let cmdItem - if (!isNaN(cmdNumber)) cmdId = cmdNumber + cmdId = assertAndParse(cmdId, 'cmdId') const cItem = clusterId.get(cId) + if (!cItem) return - let cInfo - if (cItem) cInfo = _getCluster(cItem.key) + const cInfo = _getCluster(cItem.key) + if (!cInfo || isNil(cInfo.cmd)) return - if (cInfo && !_.isNil(cInfo.cmd)) cmdItem = cInfo.cmd.get(cmdId) - - if (cmdItem) return { key: cmdItem.key, value: cmdItem.value } // { key: 'view', value: 1 } + return cInfo.cmd.get(cmdId) // { key: 'view', value: 1 } } export function getCmdRsp(cId: string | number, rspId: string | number) { // TODO - if (!isValidArgType(cId)) - throw new TypeError('cId should be a number or a string.') - - if (!isValidArgType(rspId)) - throw new TypeError('rspId should be a number or a string.') - - const cNumber = parseInt(cId as string) - const cmdNumber = parseInt(rspId as string) - - if (!isNaN(cNumber)) cId = cNumber - - if (!isNaN(cmdNumber)) rspId = cmdNumber + cId = assertAndParse(cId, 'cId') + rspId = assertAndParse(rspId, 'rspId') const cItem = clusterId.get(cId) + if (!cItem) return - let cInfo - if (cItem) cInfo = _getCluster(cItem.key) - - let cmdItem - if (cInfo && !_.isNil(cInfo.cmdRsp)) cmdItem = cInfo.cmdRsp.get(rspId) + const cInfo = _getCluster(cItem.key) + if (!cInfo || isNil(cInfo.cmdRsp)) return - if (cmdItem) return { key: cmdItem.key, value: cmdItem.value } // { key: 'viewRsp', value: 1 } + return cInfo.cmdRsp.get(rspId) // { key: 'viewRsp', value: 1 } } export function attrList(cId: string | number) { - if (!isValidArgType(cId)) - throw new TypeError('cId should be a number or a string.') - const cItem = cluster(cId) const clst = cItem ? _getCluster(cItem.key) : undefined if (!cItem || !clst) return - const attrs = _.map(clst.attr.enums, function(item) { - return { attrId: item.value } - }) - - _.forEach(attrs, function(item) { - const type = attrType(cItem.key, item.attrId) - item.dataType = type ? type.value : 255 + return clst.attr.enums.map(function(item) { + const attrId = item.value + const type = attrType(cItem.key, attrId) + const dataType = type ? type.value : 255 + return { attrId, dataType } }) - - return attrs } export function attr(cId: string | number, attrId: string | number) { - if (!isValidArgType(cId)) - throw new TypeError('cId should be a number or a string.') - - if (!isValidArgType(attrId)) - throw new TypeError('attrId should be a number or a string.') - - const cNumber = parseInt(cId as string) - const attrNumber = parseInt(attrId as string) - - if (!isNaN(cNumber)) cId = cNumber - - if (!isNaN(attrNumber)) attrId = attrNumber + cId = assertAndParse(cId, 'cId') + attrId = assertAndParse(attrId, 'attrId') const cItem = clusterId.get(cId) + if (!cItem) return - let cInfo - if (cItem) cInfo = _getCluster(cItem.key) - - let attrItem - if (cInfo && !_.isNil(cInfo.attr)) attrItem = cInfo.attr.get(attrId) + const cInfo = _getCluster(cItem.key) + if (!cInfo || isNil(cInfo.attr)) return - if (attrItem) return { key: attrItem.key, value: attrItem.value } // { key: 'modelId', value: 5 } + return cInfo.attr.get(attrId) // { key: 'modelId', value: 5 } } export function attrType(cId: string | number, attrId: string | number) { - if (!isValidArgType(cId)) - throw new TypeError('cId should be a number or a string.') - - if (!isValidArgType(attrId)) - throw new TypeError('attrId should be a number or a string.') - - const cNumber = parseInt(cId as string) - const attrNumber = parseInt(attrId as string) - - if (!isNaN(cNumber)) cId = cNumber - - if (!isNaN(attrNumber)) attrId = attrNumber + cId = assertAndParse(cId, 'cId') + attrId = assertAndParse(attrId, 'attrId') const cItem = clusterId.get(cId) + if (!cItem) return - let cInfo - if (cItem) cInfo = _getCluster(cItem.key) + const cInfo = _getCluster(cItem.key) + if (!cInfo || isNil(cInfo.attr)) return const attrName = attr(cId, attrId) + if (!attrName) return - let attrItem - let attrType - if (cInfo && !_.isNil(cInfo.attrType) && attrName) { - attrItem = cInfo.attrType.get(attrName.key) - attrType = dataType(attrItem.value) - } - - if (attrType) return { key: attrType.key, value: attrType.value } // { key: 'CHAR_STR', value: 66 } + const attrItem = cInfo.attrType.get(attrName.key) + return dataType(attrItem.value) // { key: 'CHAR_STR', value: 66 } } export function dataType(type: string | number) { - if (!isValidArgType(type)) - throw new TypeError('dataType should be a number or a string.') + type = assertAndParse(type, 'type') - const typeNumber = parseInt(type as string) - - if (!isNaN(typeNumber)) type = typeNumber - - const typeItem = dataTypeId.get(type) - - if (typeItem) return { key: typeItem.key, value: typeItem.value } // { key: 'DATA8', value: 8 } + return dataTypeId.get(type) // { key: 'DATA8', value: 8 } } export function status(status: string | number) { - if (!isValidArgType(status)) - throw new TypeError('status should be a number or a string.') - - const statusNumber = parseInt(status as string) - - if (!isNaN(statusNumber)) status = statusNumber - - const statusItem = statusId.get(status) + status = assertAndParse(status, 'status') - if (statusItem) return { key: statusItem.key, value: statusItem.value } // { key: 'DATA8', value: 8 } + return statusId.get(status) // { key: 'DATA8', value: 8 } } diff --git a/tsconfig.json b/tsconfig.json index 7bd2bbb..62c3491 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "strict": false, "target": "es6", "module": "commonjs", "lib": ["es7"],