Diarkis Room - How To Implement Locking Mechanism

How To Implement Locking Room Mechanism Using Room Properties

You may implement a locking mechanism to rooms with room properties. The example below demonstrates the implementation of a room locking mechanism that requires the client trying to unlock a room to have a passcode that is known to the room. You may implement a similar mechanism using the Room module’s SetJoinCondition method as well.

Note: The example uses the remote room property update technique that is described here.

addr, err := room.GetRoomNodeAddress(roomID)
if err != nil {
    // Handle error
    return
}
req := make(map[string]interface{})
req["roomID"] = roomID
// passcode comes from the client
req["passcode"] = passcode
// mesh.SendRequest is a inter-pod communication function
mesh.SendRequest(cmdID, addr, req, func(err error, res map[string]interface{}) {
    if err != nil {
           // Handle error
           return
      }
      // The room has been unlocked successfully
})
mesh.Command(cmdID, unlockRoom)

func unlockRoom(req map[string]interface{}) (error, map[string]interface{}) {
      roomID := mesh.GetString(req, "roomID")
      passcode := mesh.GetString(req, "passcode")
      var err error
      _ := room.UpdateProperties(roomID, func(props map[string]interface{}) bool {
     if _, ok := props["unlocked"].(bool); !ok {
           // the room is already unlocked
           err := errors.New("Room already unlocked")
           return false
            }
            if _, ok := props["passcode"].(string); !ok {
                   // missing pass code...
                  err := errors.New("Room is missing passcode...")
                  return false
            }
            if passcode != props["passcode"].(string) {
                  // pass code the client has does not match
                  err := errors.New("Pass code does not match")
                  return false
            }
            // pass code the client has matches
            props["unlocked"] = true
            return true
    })
    res := make(map[string]interface{})
    return res, err
}