2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschindego.internal;
15 import java.time.Duration;
16 import java.time.Instant;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
21 * Session for storing Bosch Indego context information.
23 * @author Jacob Laursen - Initial contribution
26 public class IndegoSession {
28 private static final Duration DEFAULT_EXPIRATION_PERIOD = Duration.ofSeconds(10);
30 private String contextId;
31 private String serialNumber;
32 private Instant expirationTime;
34 public IndegoSession() {
35 this("", "", Instant.MIN);
38 public IndegoSession(String contextId, String serialNumber, Instant expirationTime) {
39 this.contextId = contextId;
40 this.serialNumber = serialNumber;
41 this.expirationTime = expirationTime.equals(Instant.MIN) ? Instant.now().plus(DEFAULT_EXPIRATION_PERIOD)
46 * Get context id for HTTP requests (headers "x-im-context-id: <contextId>" and
47 * "Cookie: BOSCH_INDEGO_SSO=<contextId>").
49 * @return current context id
51 public String getContextId() {
56 * Get serial number of device.
58 * @return serial number
60 public String getSerialNumber() {
65 * Get expiration time of session as {@link Instant}.
67 * @return expiration time
69 public Instant getExpirationTime() {
70 return expirationTime;
74 * Check if session is initialized, i.e. has serial number.
77 * @return true if session is initialized
79 public boolean isInitialized() {
80 return !serialNumber.isEmpty();
84 * Check if session is valid, i.e. has not yet expired.
86 * @return true if session is still valid
88 public boolean isValid() {
89 return !contextId.isEmpty() && expirationTime.isAfter(Instant.now());
95 public void invalidate() {
97 expirationTime = Instant.MIN;
101 public String toString() {
102 return String.format("%s (serialNumber %s, expirationTime %s)", contextId, serialNumber, expirationTime);