Skip to content

Commit 6ed6299

Browse files
feat!: improve types check and clean up repeated patterns (#48)
* feat: improve types check and clean up repeated patterns * fix: remove unused import Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: enhance type checking for receiverOrOptions in BACnetClient * fix: update sendBvlc method to require a non-null receiver parameter * fix: update receiver parameter type in _segmentAckResponse method to AddressParameter * fix: remove unused DecodedAddress import from client.ts * fix: update AddressParameter type to use a consistent structure and modify related interfaces * fix: update encode function to accept both AddressParameter and BACNetAddress types for destinationAddr * fix: handle undefined destinationAddr in encode function for better type safety * fix: simplify sender address handling in _segmentAckResponse method * fix: update address parameter handling in various integration tests to use object structure * fix: enhance NPDU handling by updating sender address structure and improving DecodedNpdu interface documentation * style: fix lint * fix: streamline sender address handling in transport send method * fix: update address handling to use AddressParameter type in readProperty methods * fix: replace AddressParameter with BACNetAddress type across multiple files for consistency * fix: enhance BACNetAddress type documentation for clarity and consistency * fix: allow null receiver in sendBvlc method for improved robustness * fix: refactor buffer handling to use _send method for improved clarity and consistency * fix: simplify BACNetAddress interface by merging optional properties and improving documentation * fix: update discoveredAddress type to BACNetAddress for consistency across compliance tests * fix: update asyncWritePropertyMultiple to use BACNetAddress for receiver parameter * fix: update readProperty call to remove unnecessary cast to string for address --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent b86554a commit 6ed6299

40 files changed

Lines changed: 333 additions & 375 deletions

emulator/bacnet-device-emulator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ client.on('readProperty', (data) => {
138138
if (!object) {
139139
debug(`Object not found: ${objectKey}, sending error response`)
140140
return client.errorResponse(
141-
address,
141+
{ address },
142142
ConfirmedServiceChoice.READ_PROPERTY,
143143
invokeId,
144144
ErrorClass.OBJECT,
@@ -154,7 +154,7 @@ client.on('readProperty', (data) => {
154154
`Property not found: ${request.property?.id}, sending error response`,
155155
)
156156
return client.errorResponse(
157-
address,
157+
{ address },
158158
ConfirmedServiceChoice.READ_PROPERTY,
159159
invokeId,
160160
ErrorClass.PROPERTY,
@@ -167,7 +167,7 @@ client.on('readProperty', (data) => {
167167
`Sending readPropertyResponse to ${address} for ${objectKey}:${request.property?.id} with invokeId ${invokeId}`,
168168
)
169169
client.readPropertyResponse(
170-
address,
170+
{ address },
171171
invokeId,
172172
request.objectId,
173173
request.property,
@@ -180,7 +180,7 @@ client.on('readProperty', (data) => {
180180
`Property index not found: ${request.property?.index}, sending error response`,
181181
)
182182
return client.errorResponse(
183-
address,
183+
{ address },
184184
ConfirmedServiceChoice.READ_PROPERTY,
185185
invokeId,
186186
ErrorClass.PROPERTY,
@@ -192,7 +192,7 @@ client.on('readProperty', (data) => {
192192
`Sending readPropertyResponse (with index) to ${address} for ${objectKey}:${request.property?.id}[${request.property?.index}] with invokeId ${invokeId}`,
193193
)
194194
client.readPropertyResponse(
195-
address,
195+
{ address },
196196
invokeId,
197197
request.objectId,
198198
request.property,

examples/discover-devices.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ bacnetClient.on('iAm', (device: any) => {
5252
const deviceObjectId: BACNetObjectID = { type: 8, instance: deviceId }
5353

5454
bacnetClient.readProperty(
55-
address as string,
55+
address,
5656
deviceObjectId,
5757
PropertyIdentifier.OBJECT_NAME,
5858
(err, value: DecodeAcknowledgeSingleResult | undefined) => {
@@ -63,7 +63,7 @@ bacnetClient.on('iAm', (device: any) => {
6363
console.log(err)
6464
} else {
6565
bacnetClient.readProperty(
66-
address as string, // Cast to string here as well
66+
address,
6767
deviceObjectId,
6868
PropertyIdentifier.VENDOR_NAME,
6969
(err2, valueVendor) => {

examples/read-device.ts

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import Bacnet, {
4141
DeviceObjectResult,
4242
PropertyResult,
4343
getEnumValues,
44+
BACNetAddress,
4445
} from '../src'
4546
import * as process from 'process'
4647

@@ -209,7 +210,7 @@ const debug = process.argv.includes('--debug')
209210
* Retrieve all properties manually because ReadPropertyMultiple is not available
210211
*/
211212
function getAllPropertiesManually(
212-
address: string | { address: string; forwardedFrom?: string },
213+
address: BACNetAddress,
213214
objectId: BACNetObjectID,
214215
callback: (result: DeviceObjectResult) => void,
215216
propList?: number[],
@@ -245,7 +246,7 @@ function getAllPropertiesManually(
245246

246247
// Read only object-list property
247248
bacnetClient.readProperty(
248-
address as string,
249+
address,
249250
objectId,
250251
prop,
251252
{}, // Options object
@@ -334,7 +335,7 @@ function handleBitString(
334335
* Parses a property value
335336
*/
336337
function parseValue(
337-
address: string | { address: string; forwardedFrom?: string },
338+
address: BACNetAddress,
338339
objId: number,
339340
parentType: number,
340341
value: any,
@@ -445,7 +446,7 @@ function parseValue(
445446
},
446447
]
447448
bacnetClient.readPropertyMultiple(
448-
address as string,
449+
address,
449450
requestArray,
450451
(err, resValue) => {
451452
//console.log(JSON.stringify(value.value) + ': ' + JSON.stringify(resValue));
@@ -517,7 +518,7 @@ function parseValue(
517518
* Parse an object structure
518519
*/
519520
function parseDeviceObject(
520-
address: string | { address: string; forwardedFrom?: string },
521+
address: BACNetAddress,
521522
obj: DeviceObjectResult | any,
522523
parent: BACNetObjectID,
523524
supportsMultiple: boolean,
@@ -778,39 +779,35 @@ bacnetClient.on('iAm', (device) => {
778779
},
779780
]
780781

781-
bacnetClient.readPropertyMultiple(
782-
address.address,
783-
requestArray,
784-
(err, value) => {
785-
if (err) {
786-
console.log(
787-
deviceId,
788-
'No ReadPropertyMultiple supported:',
789-
err.message,
790-
)
791-
getAllPropertiesManually(
792-
address,
793-
{ type: 8, instance: deviceId },
794-
(result) => {
795-
parseDeviceObject(
796-
address,
797-
result,
798-
{ type: 8, instance: deviceId },
799-
false,
800-
(res) => printResultObject(deviceId, res),
801-
)
802-
},
803-
)
804-
} else {
805-
console.log(deviceId, 'ReadPropertyMultiple supported ...')
806-
parseDeviceObject(
807-
address,
808-
value,
809-
{ type: 8, instance: deviceId },
810-
true,
811-
(res) => printResultObject(deviceId, res),
812-
)
813-
}
814-
},
815-
)
782+
bacnetClient.readPropertyMultiple(address, requestArray, (err, value) => {
783+
if (err) {
784+
console.log(
785+
deviceId,
786+
'No ReadPropertyMultiple supported:',
787+
err.message,
788+
)
789+
getAllPropertiesManually(
790+
address,
791+
{ type: 8, instance: deviceId },
792+
(result) => {
793+
parseDeviceObject(
794+
address,
795+
result,
796+
{ type: 8, instance: deviceId },
797+
false,
798+
(res) => printResultObject(deviceId, res),
799+
)
800+
},
801+
)
802+
} else {
803+
console.log(deviceId, 'ReadPropertyMultiple supported ...')
804+
parseDeviceObject(
805+
address,
806+
value,
807+
{ type: 8, instance: deviceId },
808+
true,
809+
(res) => printResultObject(deviceId, res),
810+
)
811+
}
812+
})
816813
})

0 commit comments

Comments
 (0)