Add basic index.ts functionality.
This commit is contained in:
		
							parent
							
								
									b47277b5f2
								
							
						
					
					
						commit
						2f4cea53ed
					
				
							
								
								
									
										5
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,5 @@ | ||||
| { | ||||
|   "cSpell.words": [ | ||||
|     "DPTS" | ||||
|   ] | ||||
| } | ||||
							
								
								
									
										78
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								src/index.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,78 @@ | ||||
| 
 | ||||
| import { format } from 'util' | ||||
| import { Encoder, Decoder, DPT } from './definitions'; | ||||
| 
 | ||||
| import { DPT1 } from './DPT1'; | ||||
| import { DPT2 } from './DPT2'; | ||||
| import { DPT3 } from './DPT3'; | ||||
| import { DPT4 } from './DPT4'; | ||||
| import { DPT5 } from './DPT5'; | ||||
| import { DPT6 } from './DPT6'; | ||||
| import { DPT7 } from './DPT7'; | ||||
| import { DPT8 } from './DPT8'; | ||||
| import { DPT9 } from './DPT9'; | ||||
| import { DPT10 } from './DPT10'; | ||||
| import { DPT11 } from './DPT11'; | ||||
| import { DPT12 } from './DPT12'; | ||||
| import { DPT13 } from './DPT13'; | ||||
| import { DPT14 } from './DPT14'; | ||||
| import { DPT15 } from './DPT15'; | ||||
| import { DPT16 } from './DPT16'; | ||||
| import { DPT17 } from './DPT17'; | ||||
| import { DPT18 } from './DPT18'; | ||||
| import { DPT19 } from './DPT19'; | ||||
| import { DPT20 } from './DPT20'; | ||||
| import { DPT21 } from './DPT21'; | ||||
| //import { DPT22 } from './DPT22';
 | ||||
| import { DPT232 } from './DPT232'; | ||||
| import { DPT237 } from './DPT237'; | ||||
| import { DPT238 } from './DPT238'; | ||||
| import { InvalidValueError } from './errors/InvalidValueError'; | ||||
| 
 | ||||
| export const DPTS: { [index: string]: DPT } = { | ||||
|   DPT1: new DPT1(), | ||||
|   DPT2: new DPT2(), | ||||
|   DPT3: new DPT3(), | ||||
|   DPT4: new DPT4(), | ||||
|   DPT5: new DPT5(), | ||||
|   DPT6: new DPT6(), | ||||
|   DPT7: new DPT7(), | ||||
|   DPT8: new DPT8(), | ||||
|   DPT9: new DPT9(), | ||||
|   DPT10: new DPT10(), | ||||
|   DPT11: new DPT11(), | ||||
|   DPT12: new DPT12(), | ||||
|   DPT13: new DPT13(), | ||||
|   DPT14: new DPT14(), | ||||
|   DPT15: new DPT15(), | ||||
|   DPT16: new DPT16(), | ||||
|   DPT17: new DPT17(), | ||||
|   DPT18: new DPT18(), | ||||
|   DPT19: new DPT19(), | ||||
|   DPT20: new DPT20(), | ||||
|   DPT21: new DPT21(), | ||||
|   //DPT22: new DPT22(),
 | ||||
| 
 | ||||
|   DPT232: new DPT232(), | ||||
|   DPT237: new DPT237(), | ||||
|   DPT238: new DPT238() | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| export class DataPointType { | ||||
| 
 | ||||
|   static resolve(dptid: any): DPT { | ||||
|     const m = dptid | ||||
|       .toString() | ||||
|       .toUpperCase() | ||||
|       .match(/^(?:DPT)?(\d+)(\.(\d+))?$/); | ||||
|     if (m === null) | ||||
|       throw new InvalidValueError('Invalid DPT format: ' + dptid); | ||||
| 
 | ||||
|     const dpt = DPTS[format('DPT%s', m[1])]; | ||||
|     if (!dpt) | ||||
|       throw new InvalidValueError('Unsupported DPT: ' + dptid); | ||||
| 
 | ||||
|     return dpt; | ||||
|   }; | ||||
| } | ||||
							
								
								
									
										34
									
								
								tests/index.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								tests/index.test.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,34 @@ | ||||
| import { expect } from "chai"; | ||||
| 
 | ||||
| import { DPTS, DataPointType } from "../src/index" | ||||
| import { BufferLengthError } from "../src/errors/BufferLengthError"; | ||||
| import { InvalidValueError } from "../src/errors/InvalidValueError"; | ||||
| 
 | ||||
| import { compareBuffers } from "./util" | ||||
| 
 | ||||
| 
 | ||||
| describe("Test DPT001", (): void => { | ||||
| 
 | ||||
|   let dpt = new DataPointType(); | ||||
|   const message: string = "0C1E" | ||||
| 
 | ||||
|   it("Decode valid", async function () { | ||||
|     const dpt_ = DataPointType.resolve('DPT9.1') | ||||
|     expect(dpt_.id).is.equal('9'); | ||||
|     expect(dpt_.decoder(Buffer.from(message, 'hex'))).is.equal(21.08) | ||||
|   }); | ||||
| 
 | ||||
|   it("Decode invalid DPT string", async function () { | ||||
|     var testFunction = function () { | ||||
|       const dpt_ = DataPointType.resolve('More of the same') | ||||
|     } | ||||
|     expect(testFunction).to.throw(InvalidValueError); | ||||
|   }); | ||||
| 
 | ||||
|   it("Decode unsupported DPT", async function () { | ||||
|     var testFunction = function () { | ||||
|       const dpt_ = DataPointType.resolve('DPT49.1') | ||||
|     } | ||||
|     expect(testFunction).to.throw(InvalidValueError); | ||||
|   }); | ||||
| }); | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user