utils.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. presenceStates = _pstates
  25. NO_AUTH_ROUTES = ('/ui','/openapi.json','/favicon.ico')
  26. SWAGGER_JS_URL = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.37.2/swagger-ui-bundle.js"
  27. SWAGGER_CSS_URL = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.37.2/swagger-ui.min.css"
  28. SWAGGER_TEMPLATE = '''
  29. <head>
  30. <link type="text/css" rel="stylesheet" href="{{ css_url }}">
  31. <title>{{ title }}</title>
  32. </head>
  33. <body>
  34. <div id="swagger-ui"></div>
  35. <script src="{{ js_url }}"></script>
  36. <script>
  37. const ui = SwaggerUIBundle({
  38. deepLinking: true,
  39. dom_id: "#swagger-ui",
  40. layout: "BaseLayout",
  41. presets: [
  42. SwaggerUIBundle.presets.apis,
  43. SwaggerUIBundle.SwaggerUIStandalonePreset
  44. ],
  45. showExtensions: true,
  46. showCommonExtensions: true,
  47. url: "/openapi.json"
  48. });
  49. </script>
  50. </body>'''
  51. def followMe2DevState(followMeState):
  52. if followMeState == 'DIRECT':
  53. return 'INUSE'
  54. if followMeState == 'EXTENSION':
  55. return 'NOT_INUSE'
  56. return 'INVALID'
  57. @dataclass
  58. class QueueMember:
  59. user: str
  60. name: str = ''
  61. location: str = ''
  62. membership: str = ''
  63. stateinterface: str = ''
  64. status: str = ''
  65. def fromMessage(self, _m: Message):
  66. for key in asdict(self).keys():
  67. if key in _m:
  68. setattr(self, key, _m[key])
  69. return self
  70. @dataclass
  71. class GlobalVars:
  72. FMDEVSTATE: str = ''
  73. QUEDEVSTATE: str = ''
  74. QUEUETOGGLE: str = ''
  75. QUEUEPAUSETOGGLE: str = ''
  76. INTERCOMCODE: str = ''
  77. CAMPONTOGGLE: str = ''
  78. DNDDEVSTATE: str = ''
  79. CFDEVSTATE: str = ''
  80. def d(self):
  81. return asdict(self)
  82. def jsonAPIReply(status='success', data=None, message=None):
  83. return {'status':status, 'data': data, 'message': message}
  84. def errorReply(message=None):
  85. return jsonAPIReply(status='error', data=None, message=message)
  86. def successReply(data=None,message=None):
  87. return jsonAPIReply(status='success', data=data, message=message)
  88. def noUser(user):
  89. return errorReply('User {} does not exist'.format(user))
  90. def noDevice(device):
  91. return errorReply('Device {} does not exist'.format(device))
  92. def noUserDevice(user):
  93. return errorReply('User {} does not exist or is not bound to device'.format(user))
  94. def noUserChannel(user):
  95. return errorReply('User {} does not have active calls'.format(user))
  96. def stateCacheEmpty():
  97. return errorReply('Users states cache update failed')
  98. def invalidState(state):
  99. return errorReply('Invalid state "{}" provided'.format(state))
  100. def alreadyBound(user, device):
  101. return errorReply('User {} is already bound to device {}'.format(user, device))
  102. def bindError(user, device):
  103. return errorReply('Failed binding user {} to device {}'.format(user, device))
  104. def hintError(user, device):
  105. return errorReply('Failed setting hint for user {} on device {}'.format(user, device))
  106. def noUserBound(device):
  107. return errorReply('No user is bound to device {}'.format(device))
  108. def successfullyTransfered(userA, userB):
  109. return successReply({'userA':userA,'userB':userB},
  110. 'Call was successfully transfered from user {} to user {}'.format(userA, userB))
  111. def successfullyBound(user, device):
  112. return successReply({'user':user,'device':device},
  113. 'User {} is successfully bound to device {}'.format(user, device))
  114. def successfullyUnbound(user, device):
  115. return successReply({'user':user,'device':device},
  116. 'User {} was successfully unbound from device {}'.format(user, device))
  117. def successfullySetState(user, state):
  118. return successReply({'user':user,'state':state},
  119. 'State "{}" was successfully set for user {}'.format(state, user))