scripts/mycroft/session/__init__.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
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 #
15 import time
16 from threading import Lock
17 from uuid import uuid4
18 
19 from mycroft.configuration import Configuration
20 from mycroft.util.log import LOG
21 
22 
23 class Session:
24  """
25  An class representing a Mycroft Session Identifier
26  """
27 
28  def __init__(self, session_id, expiration_seconds=180):
29  self.session_id = session_id
30  self.touch_time = int(time.time())
31  self.expiration_seconds = expiration_seconds
32 
33  def touch(self):
34  """
35  update the touch_time on the session
36 
37  :return:
38  """
39  self.touch_time = int(time.time())
40 
41  def expired(self):
42  """
43  determine if the session has expired
44 
45  :return:
46  """
47  return int(time.time()) - self.touch_time > self.expiration_seconds
48 
49  def __str__(self):
50  return "{%s,%d}" % (str(self.session_id), self.touch_time)
51 
52 
54  """ Keeps track of the current active session. """
55  __current_session = None
56  __lock = Lock()
57 
58  @staticmethod
59  def get():
60  """
61  get the active session.
62 
63  :return: An active session
64  """
65  config = Configuration.get().get('session')
66 
67  with SessionManager.__lock:
68  if (not SessionManager.__current_session or
69  SessionManager.__current_session.expired()):
70  SessionManager.__current_session = Session(
71  str(uuid4()), expiration_seconds=config.get('ttl', 180))
72  LOG.info(
73  "New Session Start: " +
74  SessionManager.__current_session.session_id)
75  return SessionManager.__current_session
76 
77  @staticmethod
78  def touch():
79  """
80  Update the last_touch timestamp on the current session
81 
82  :return: None
83  """
84  SessionManager.get().touch()
def __init__(self, session_id, expiration_seconds=180)


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40