How to work with Enum Flags API response

Hi,

I have an API response for door status, that returns in a sum of status.

e.g.
1 Open
2 Closed
4 Unlocked
8 Locked.

If the door is closed and locked it will return 9 and if its closed and unlocked it will return 5.

How would I break down the response in bubble? I have created a matching option set with the integer and strings, but not sure how to caculate to get the status without doing one for every combination.

Thanks!

1 Like

Can you give an idea of the complete response?

The response is pretty simple, just state in number, and most recent activity.

{
“State”: 513,
“Info1”: “User API Manually Unlocked the Door with the Web Interface at 2023-08-16 12:50:37 pm”,
“Info2”: “”
}

These are the states

Integer Value Hex Value String Value Description
1 0x00000001 Unlocked Door is unlocked.
2 0x00000002 Open Door is open.
4 0x00000004 LockedOut Door is locked out.
8 0x00000008 Forced Door has been forced open.
16 0x00000010 HeldOpenWarning Door has nearly been held open too long.
32 0x00000020 HeldOpenTooLong Door has been held open too long.
64 0x00000040 Breakglass Door’s breakglass detector has been triggered.
128 0x00000080 ReaderTamper A reader connected to the door has been tampered with.
256 0x00000100 Locked Door is locked.
512 0x00000200 Closed Door is closed.
1024 0x00000400 HeldResponseMuted Door’s Held Open response has been muted by a user.

A bitmask is hard to work with in Bubble, it lacks the “bit-wise AND” operator.

In javascript it is doable.

I assume you want to get a list of the enum flag numbers, here is Expression for on the page:

var flags = Input flags's value ;
var flaglist = [];
for(var i = 0; i < 15 && 2 ** i < flags; i++) {
  if (2 ** i & flags) flaglist.push(2 ** i)
}
flaglist;

Note the constraint of 15, to keep the list small even for large input values. You can set yours to 11 if you want to stop at 1024.

Can run it on a backend workflow in server script with almost the same script.

1 Like