utils.py 5.0 KB

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