Skip to content
This repository has been archived by the owner on Jul 5, 2019. It is now read-only.

Commit

Permalink
Furter refactoring
Browse files Browse the repository at this point in the history
Eliminated dependencies
  • Loading branch information
qm3ster committed Jan 13, 2019
1 parent c3c9716 commit c2a54e3
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 167 deletions.
7 changes: 1 addition & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/clusterWithNewFormat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Enum = require('./enum')
import { Enum } from './enum'

export default function clusterWithNewFormat({ attrId, cmd, cmdRsp }) {
const attrs = {}
Expand Down
4 changes: 2 additions & 2 deletions src/definitions/enum.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
interface EnumItem<T> {
export interface EnumItem<T> {
key: string
value: T
}

export = class Enum<T> {
export class Enum<T> {
byKey: Map<string, EnumItem<T>>
byValue: Map<T, EnumItem<T>>
enums: EnumItem<T>[]
Expand Down
215 changes: 60 additions & 155 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand All @@ -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()
Expand All @@ -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,
Expand All @@ -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 }
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"strict": false,
"target": "es6",
"module": "commonjs",
"lib": ["es7"],
Expand Down

0 comments on commit c2a54e3

Please sign in to comment.