utils.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python3
  2. from dataclasses import dataclass, asdict
  3. from panoramisk import Message
  4. from datetime import datetime as dt
  5. from datetime import timedelta as td
  6. TRUEs = ('true', '1', 'y', 'yes')
  7. NONEs = (None,'none','')
  8. # userstate presencestate maping:
  9. _ustates = ['idle', 'inuse', 'busy', 'unavailable', 'ringing', 'inuse&ringing','hold', 'inuse&hold'] #presence:
  10. _states = [['available', 'busy', 'busy', 'unavailable', 'ringing', 'busy', 'busy', 'busy'], #not_set
  11. ['unavailable', 'busy', 'busy', 'unavailable', 'ringing', 'busy', 'busy', 'busy'], #unavailable
  12. ['available', 'busy', 'busy', 'unavailable', 'ringing', 'busy', 'busy', 'busy'], #available
  13. ['away', 'busy', 'busy', 'unavailable', 'ringing', 'busy', 'busy', 'busy'], #away
  14. ['available', 'busy', 'busy', 'unavailable', 'ringing', 'busy', 'busy', 'busy'], #xa
  15. ['available', 'busy', 'busy', 'unavailable', 'ringing', 'busy', 'busy', 'busy'], #chat
  16. ['dnd', 'busy', 'busy', 'unavailable', 'ringing', 'busy', 'busy', 'busy']] #dnd
  17. _pstates = ['not_set',
  18. 'unavailable',
  19. 'available',
  20. 'away',
  21. 'xa',
  22. 'chat',
  23. 'dnd']
  24. # combinedStates[userstate][presencestate]=combinedState
  25. combinedStates = {_u: {_p: _states[_pstates.index(_p)][_ustates.index(_u)] for _p in _pstates} for _u in _ustates}
  26. presenceStates = _pstates
  27. NO_AUTH_ROUTES = ('/ui','/openapi.json','/favicon.ico')
  28. SWAGGER_JS_URL = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.37.2/swagger-ui-bundle.js"
  29. SWAGGER_CSS_URL = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.37.2/swagger-ui.min.css"
  30. SWAGGER_TEMPLATE = '''
  31. <head>
  32. <link type="text/css" rel="stylesheet" href="{{ css_url }}">
  33. <title>{{ title }}</title>
  34. </head>
  35. <body>
  36. <div id="swagger-ui"></div>
  37. <script src="{{ js_url }}"></script>
  38. <script>
  39. const ui = SwaggerUIBundle({
  40. deepLinking: true,
  41. dom_id: "#swagger-ui",
  42. layout: "BaseLayout",
  43. presets: [
  44. SwaggerUIBundle.presets.apis,
  45. SwaggerUIBundle.SwaggerUIStandalonePreset
  46. ],
  47. showExtensions: true,
  48. showCommonExtensions: true,
  49. url: "/openapi.json"
  50. });
  51. </script>
  52. </body>'''
  53. def parseDatetime(dateString):
  54. _dt = None
  55. if dateString is not None:
  56. try:
  57. _dt = dt.strptime(dateString, '%d%m%Y%H%M%S')
  58. except ValueError:
  59. pass
  60. if _dt is None:
  61. try:
  62. _dt = dt.fromtimestamp(int(dateString))
  63. except ValueError:
  64. pass
  65. if _dt is None:
  66. try:
  67. _dt = dt.fromisoformat(dateString)
  68. except ValueError:
  69. pass
  70. return _dt
  71. def followMe2DevState(followMeState):
  72. if followMeState == 'DIRECT':
  73. return 'INUSE'
  74. if followMeState == 'EXTENSION':
  75. return 'NOT_INUSE'
  76. return 'INVALID'
  77. @dataclass
  78. class QueueMember:
  79. user: str
  80. name: str = ''
  81. location: str = ''
  82. membership: str = ''
  83. stateinterface: str = ''
  84. status: str = ''
  85. def fromMessage(self, _m: Message):
  86. for key in asdict(self).keys():
  87. if key in _m:
  88. setattr(self, key, _m[key])
  89. return self
  90. @dataclass
  91. class GlobalVars:
  92. FMDEVSTATE: str = ''
  93. QUEDEVSTATE: str = ''
  94. QUEUETOGGLE: str = ''
  95. QUEUEPAUSETOGGLE: str = ''
  96. INTERCOMCODE: str = ''
  97. CAMPONTOGGLE: str = ''
  98. DNDDEVSTATE: str = ''
  99. CFDEVSTATE: str = ''
  100. def d(self):
  101. return asdict(self)
  102. def jsonAPIReply(status='success', data=None, message=None):
  103. return {'status':status, 'data': data, 'message': message}
  104. def errorReply(message=None):
  105. return jsonAPIReply(status='error', data=None, message=message)
  106. def successReply(data=None,message=None):
  107. return jsonAPIReply(status='success', data=data, message=message)
  108. def noUser(user):
  109. return errorReply('User {} does not exist'.format(user))
  110. def noDevice(device):
  111. return errorReply('Device {} does not exist'.format(device))
  112. def noUserDevice(user):
  113. return errorReply('User {} does not exist or is not bound to device'.format(user))
  114. def noUserChannel(user):
  115. return errorReply('User {} does not have active calls'.format(user))
  116. def stateCacheEmpty():
  117. return errorReply('Users states cache update failed')
  118. def invalidState(state):
  119. return errorReply('Invalid state "{}" provided'.format(state))
  120. def alreadyBound(user, device):
  121. return errorReply('User {} is already bound to device {}'.format(user, device))
  122. def bindError(user, device):
  123. return errorReply('Failed binding user {} to device {}'.format(user, device))
  124. def hintError(user, device):
  125. return errorReply('Failed setting hint for user {} on device {}'.format(user, device))
  126. def noUserBound(device):
  127. return errorReply('No user is bound to device {}'.format(device))
  128. def successfullyTransfered(userA, userB):
  129. return successReply({'userA':userA,'userB':userB},
  130. 'Call was successfully transfered from user {} to user {}'.format(userA, userB))
  131. def successfullyBound(user, device):
  132. return successReply({'user':user,'device':device},
  133. 'User {} is successfully bound to device {}'.format(user, device))
  134. def successfullyUnbound(user, device):
  135. return successReply({'user':user,'device':device},
  136. 'User {} was successfully unbound from device {}'.format(user, device))
  137. def successfullySetState(user, state):
  138. return successReply({'user':user,'state':state},
  139. 'State "{}" was successfully set for user {}'.format(state, user))