Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question: design limitation precludes a common event handler? #226

Open
toddaustin07 opened this issue Jun 28, 2022 · 2 comments
Open

question: design limitation precludes a common event handler? #226

toddaustin07 opened this issue Jun 28, 2022 · 2 comments
Labels
needs triage Needs investigation and prioritization question Further information is requested reviewed

Comments

@toddaustin07
Copy link

When defining device subscription handlers, you cannot name a common handler across subscriptions. The SDK design seems to impose a unique handler name for each subscription. If I want to support nearly all SmartThings capabilities, I would have a potentially LARGE number of subscriptions to handle, but the current design forces me to declare a unique handler for each, resulting in an unnecessary proliferation of code.

What I want to do is this:

context.api.subscriptions.subscribeToDevices(context.config.buttonList, 'button', 'button', 'commonhandler'))
context.api.subscriptions.subscribeToDevices(context.config.contactList, contactSensor', contact', 'commonhandler'))
.
.
.
context.api.subscriptions.subscribeToDevices(context.config.motionList, 'motionSensor', motion', 'commonhandler'))

subscribedEventHandler('commonHandler', (context, deviceEvent) => {
   <do common stuff>
}

However, I get a subscriptionName conflict error if I try to do that. What I seem to be forced to do is define unique handlers for each like this:

context.api.subscriptions.subscribeToDevices(context.config.buttonList, 'button', 'button', 'buttonhandler'))
context.api.subscriptions.subscribeToDevices(context.config.contactList, contactSensor', contact', 'contacthandler'))
.
.
.
context.api.subscriptions.subscribeToDevices(context.config.motionList, 'motionSensor', motion', 'motionhandler'))

subscribedEventHandler('buttonHandler', (context, deviceEvent) => {
   <do same stuff, or call a common do-stuff routine here>
}

subscribedEventHandler('contactHandler', (context, deviceEvent) => {
   <do same stuff, or call a common do-stuff routine here>
}
.
.
.
subscribedEventHandler('motionHandler', (context, deviceEvent) => {
   <do same stuff, or call a common do-stuff routine here>
}

Is there a better way to accomplish this or perhaps this is a needed design change in the SDK?

@toddaustin07 toddaustin07 added the question Further information is requested label Jun 28, 2022
@bflorian
Copy link
Contributor

Unique subscription names are a limitation of the underlying API, not just the SDK, but I can't think of any reason offhand why they need to be unique. We'll investigate.

@bflorian bflorian added reviewed needs triage Needs investigation and prioritization labels Jun 30, 2022
@bflorian
Copy link
Contributor

bflorian commented Jul 6, 2022

We're pursuing an API change that will no longer require subscription names to be unique. We'll leave this issue open and update it when we have a release date.

A work-around you could do in the meantime is to declare the handler separately from the handler registration commands. It's still more code than should be necessary, but a little more maintainable than having a separate inline function in each subscribedEventHandler call.

const deviceHandler = (context, deviceEvent) => {
  <do some stuff>
}

subscribedEventHandler('buttonHandler', deviceHandler)
subscribedEventHandler('contactHandler', deviceHandler)
.
.
.
subscribedEventHandler('motionHandler', , deviceHandler)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Needs investigation and prioritization question Further information is requested reviewed
Projects
None yet
Development

No branches or pull requests

2 participants