|
| 1 | +-- Copyright 2025 SmartThings |
| 2 | +-- |
| 3 | +-- Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +-- you may not use this file except in compliance with the License. |
| 5 | +-- You may obtain a copy of the License at |
| 6 | +-- |
| 7 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +-- |
| 9 | +-- Unless required by applicable law or agreed to in writing, software |
| 10 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +-- See the License for the specific language governing permissions and |
| 13 | +-- limitations under the License. |
| 14 | +local utils = require "st.utils" |
| 15 | +local capabilities = require "st.capabilities" |
| 16 | +local INITIAL_INDEX = 1 |
| 17 | + |
| 18 | +local new_lock_utils = { |
| 19 | + -- Constants |
| 20 | + ADD_CREDENTIAL = "addCredential", |
| 21 | + ADD_USER = "addUser", |
| 22 | + BUSY = "busy", |
| 23 | + COMMAND_NAME = "commandName", |
| 24 | + CREDENTIAL_TYPE = "pin", |
| 25 | + CHECKING_CODE = "checkingCode", |
| 26 | + DELETE_ALL_CREDENTIALS = "deleteAllCredentials", |
| 27 | + DELETE_ALL_USERS = "deleteAllUsers", |
| 28 | + DELETE_CREDENTIAL = "deleteCredential", |
| 29 | + DELETE_USER = "deleteUser", |
| 30 | + LOCK_CREDENTIALS = "lockCredentials", |
| 31 | + LOCK_USERS = "lockUsers", |
| 32 | + ACTIVE_CREDENTIAL = "activeCredential", |
| 33 | + STATUS_BUSY = "busy", |
| 34 | + STATUS_DUPLICATE = "duplicate", |
| 35 | + STATUS_FAILURE = "failure", |
| 36 | + STATUS_INVALID_COMMAND = "invalidCommand", |
| 37 | + STATUS_OCCUPIED = "occupied", |
| 38 | + STATUS_RESOURCE_EXHAUSTED = "resourceExhausted", |
| 39 | + STATUS_SUCCESS = "success", |
| 40 | + UPDATE_CREDENTIAL = "updateCredential", |
| 41 | + UPDATE_USER = "updateUser", |
| 42 | + USER_INDEX = "userIndex", |
| 43 | + USER_NAME = "userName", |
| 44 | + USER_TYPE = "userType" |
| 45 | +} |
| 46 | + |
| 47 | +-- check if we are currently busy performing a task. |
| 48 | +-- if we aren't then set as busy. |
| 49 | +new_lock_utils.busy_check_and_set = function (device, command, override_busy_check) |
| 50 | + if override_busy_check then |
| 51 | + -- the function was called by an injected command. |
| 52 | + return false |
| 53 | + end |
| 54 | + |
| 55 | + local c_time = os.time() |
| 56 | + local busy_state = device:get_field(new_lock_utils.BUSY) or false |
| 57 | + |
| 58 | + if busy_state == false or c_time - busy_state > 10 then |
| 59 | + device:set_field(new_lock_utils.COMMAND_NAME, command) |
| 60 | + device:set_field(new_lock_utils.BUSY, c_time) |
| 61 | + return false |
| 62 | + else |
| 63 | + local command_result_info = { |
| 64 | + commandName = command.name, |
| 65 | + statusCode = new_lock_utils.STATUS_BUSY |
| 66 | + } |
| 67 | + if command.type == new_lock_utils.LOCK_USERS then |
| 68 | + device:emit_event(capabilities.lockUsers.commandResult( |
| 69 | + command_result_info, { state_change = true, visibility = { displayed = true } } |
| 70 | + )) |
| 71 | + else |
| 72 | + device:emit_event(capabilities.lockCredentials.commandResult( |
| 73 | + command_result_info, { state_change = true, visibility = { displayed = true } } |
| 74 | + )) |
| 75 | + end |
| 76 | + return true |
| 77 | + end |
| 78 | +end |
| 79 | + |
| 80 | +new_lock_utils.clear_busy_state = function(device, status, override_busy_check) |
| 81 | + if override_busy_check then |
| 82 | + return |
| 83 | + end |
| 84 | + local command = device:get_field(new_lock_utils.COMMAND_NAME) |
| 85 | + local active_credential = device:get_field(new_lock_utils.ACTIVE_CREDENTIAL) |
| 86 | + if command ~= nil then |
| 87 | + local command_result_info = { |
| 88 | + commandName = command.name, |
| 89 | + statusCode = status |
| 90 | + } |
| 91 | + if command.type == new_lock_utils.LOCK_USERS then |
| 92 | + if active_credential ~= nil and active_credential.userIndex ~= nil then |
| 93 | + command_result_info.userIndex = active_credential.userIndex |
| 94 | + end |
| 95 | + device:emit_event(capabilities.lockUsers.commandResult( |
| 96 | + command_result_info, { state_change = true, visibility = { displayed = true } } |
| 97 | + )) |
| 98 | + else |
| 99 | + if active_credential ~= nil and active_credential.userIndex ~= nil then |
| 100 | + command_result_info.userIndex = active_credential.userIndex |
| 101 | + end |
| 102 | + if active_credential ~= nil and active_credential.credentialIndex ~= nil then |
| 103 | + command_result_info.credentialIndex = active_credential.credentialIndex |
| 104 | + end |
| 105 | + device:emit_event(capabilities.lockCredentials.commandResult( |
| 106 | + command_result_info, { state_change = true, visibility = { displayed = true } } |
| 107 | + )) |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + device:set_field(new_lock_utils.ACTIVE_CREDENTIAL, nil) |
| 112 | + device:set_field(new_lock_utils.COMMAND_NAME, nil) |
| 113 | + device:set_field(new_lock_utils.BUSY, false) |
| 114 | +end |
| 115 | + |
| 116 | + |
| 117 | +new_lock_utils.reload_tables = function(device) |
| 118 | + local users = device:get_latest_state("main", capabilities.lockUsers.ID, capabilities.lockUsers.users.NAME, {}) |
| 119 | + local credentials = device:get_latest_state("main", capabilities.lockCredentials.ID, capabilities.lockCredentials.credentials.NAME, {}) |
| 120 | + device:set_field(new_lock_utils.LOCK_USERS, users) |
| 121 | + device:set_field(new_lock_utils.LOCK_CREDENTIALS, credentials) |
| 122 | +end |
| 123 | + |
| 124 | +new_lock_utils.get_users = function(device) |
| 125 | + local users = device:get_field(new_lock_utils.LOCK_USERS) |
| 126 | + return users ~= nil and users or {} |
| 127 | +end |
| 128 | + |
| 129 | +new_lock_utils.get_user = function(device, user_index) |
| 130 | + for _, user in pairs(new_lock_utils.get_users(device)) do |
| 131 | + if user.userIndex == user_index then |
| 132 | + return user |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + return nil |
| 137 | +end |
| 138 | + |
| 139 | +new_lock_utils.get_available_user_index = function(device) |
| 140 | + local max = device:get_latest_state("main", capabilities.lockUsers.ID, |
| 141 | + capabilities.lockUsers.totalUsersSupported.NAME, 0) |
| 142 | + local current_users = new_lock_utils.get_users(device) |
| 143 | + local available_index = nil |
| 144 | + local used_index = {} |
| 145 | + for _, user in pairs(current_users) do |
| 146 | + used_index[user.userIndex] = true |
| 147 | + end |
| 148 | + if current_users ~= {} then |
| 149 | + for index = 1, max do |
| 150 | + if used_index[index] == nil then |
| 151 | + available_index = index |
| 152 | + break |
| 153 | + end |
| 154 | + end |
| 155 | + else |
| 156 | + available_index = INITIAL_INDEX |
| 157 | + end |
| 158 | + return available_index |
| 159 | +end |
| 160 | + |
| 161 | +new_lock_utils.get_credentials = function(device) |
| 162 | + local credentials = device:get_field(new_lock_utils.LOCK_CREDENTIALS) |
| 163 | + return credentials ~= nil and credentials or {} |
| 164 | +end |
| 165 | + |
| 166 | +new_lock_utils.get_credential = function(device, credential_index) |
| 167 | + for _, credential in pairs(new_lock_utils.get_credentials(device)) do |
| 168 | + if credential.credentialIndex == credential_index then |
| 169 | + return credential |
| 170 | + end |
| 171 | + end |
| 172 | + return nil |
| 173 | +end |
| 174 | + |
| 175 | +new_lock_utils.get_credential_by_user_index = function(device, user_index) |
| 176 | + for _, credential in pairs(new_lock_utils.get_credentials(device)) do |
| 177 | + if credential.userIndex == user_index then |
| 178 | + return credential |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + return nil |
| 183 | +end |
| 184 | + |
| 185 | +new_lock_utils.get_available_credential_index = function(device) |
| 186 | + local max = device:get_latest_state("main", capabilities.lockCredentials.ID, |
| 187 | + capabilities.lockCredentials.pinUsersSupported.NAME, 0) |
| 188 | + local current_credentials = new_lock_utils.get_credentials(device) |
| 189 | + local available_index = nil |
| 190 | + local used_index = {} |
| 191 | + for _, credential in pairs(current_credentials) do |
| 192 | + used_index[credential.credentialIndex] = true |
| 193 | + end |
| 194 | + if current_credentials ~= {} then |
| 195 | + for index = 1, max do |
| 196 | + if used_index[index] == nil then |
| 197 | + available_index = index |
| 198 | + break |
| 199 | + end |
| 200 | + end |
| 201 | + else |
| 202 | + available_index = INITIAL_INDEX |
| 203 | + end |
| 204 | + return available_index |
| 205 | +end |
| 206 | + |
| 207 | +new_lock_utils.create_user = function(device, user_name, user_type, user_index) |
| 208 | + if user_name == nil then |
| 209 | + user_name = "Guest" .. user_index |
| 210 | + end |
| 211 | + |
| 212 | + local current_users = new_lock_utils.get_users(device) |
| 213 | + table.insert(current_users, { userIndex = user_index, userType = user_type, userName = user_name }) |
| 214 | + device:set_field(new_lock_utils.LOCK_USERS, current_users) |
| 215 | +end |
| 216 | + |
| 217 | +new_lock_utils.delete_user = function(device, user_index) |
| 218 | + local current_users = new_lock_utils.get_users(device) |
| 219 | + local status_code = new_lock_utils.STATUS_FAILURE |
| 220 | + |
| 221 | + for index, user in pairs(current_users) do |
| 222 | + if user.userIndex == user_index then |
| 223 | + table.remove(current_users, index) |
| 224 | + device:set_field(new_lock_utils.LOCK_USERS, current_users) |
| 225 | + status_code = new_lock_utils.STATUS_SUCCESS |
| 226 | + break |
| 227 | + end |
| 228 | + end |
| 229 | + return status_code |
| 230 | +end |
| 231 | + |
| 232 | +new_lock_utils.add_credential = function(device, user_index, credential_type, credential_index) |
| 233 | + local credentials = new_lock_utils.get_credentials(device) |
| 234 | + table.insert(credentials, |
| 235 | + { userIndex = user_index, credentialIndex = credential_index, credentialType = credential_type }) |
| 236 | + device:set_field(new_lock_utils.LOCK_CREDENTIALS, credentials) |
| 237 | + return new_lock_utils.STATUS_SUCCESS |
| 238 | +end |
| 239 | + |
| 240 | +new_lock_utils.delete_credential = function(device, credential_index) |
| 241 | + local credentials = new_lock_utils.get_credentials(device) |
| 242 | + local status_code = new_lock_utils.STATUS_FAILURE |
| 243 | + |
| 244 | + for index, credential in pairs(credentials) do |
| 245 | + if credential.credentialIndex == credential_index then |
| 246 | + new_lock_utils.delete_user(device, credential.userIndex) |
| 247 | + table.remove(credentials, index) |
| 248 | + device:set_field(new_lock_utils.LOCK_CREDENTIALS, credentials) |
| 249 | + status_code = new_lock_utils.STATUS_SUCCESS |
| 250 | + break |
| 251 | + end |
| 252 | + end |
| 253 | + |
| 254 | + return status_code |
| 255 | +end |
| 256 | + |
| 257 | +new_lock_utils.update_credential = function(device, credential_index, user_index, credential_type) |
| 258 | + local credentials = new_lock_utils.get_credentials(device) |
| 259 | + local status_code = new_lock_utils.STATUS_FAILURE |
| 260 | + |
| 261 | + for _, credential in pairs(credentials) do |
| 262 | + if credential.credentialIndex == credential_index then |
| 263 | + credential.credentialType = credential_type |
| 264 | + credential.userIndex = user_index |
| 265 | + device:set_field(new_lock_utils.LOCK_CREDENTIALS, credentials) |
| 266 | + status_code = new_lock_utils.STATUS_SUCCESS |
| 267 | + break |
| 268 | + end |
| 269 | + end |
| 270 | + return status_code |
| 271 | +end |
| 272 | + |
| 273 | +return new_lock_utils |
0 commit comments