Advertisement
Guest User

Untitled

a guest
Jan 30th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.57 KB | None | 0 0
  1. from logger2 import logger
  2. import requests
  3. import xmlrpclib
  4. import os
  5. import datetime
  6.  
  7. import conf
  8.  
  9.  
  10. class MCUConnector(object):
  11. """
  12. MCUConnector handles requests to Codian MCU API
  13. """
  14.  
  15. def __init__(self):
  16. """
  17. Init the class creating the auth dict for each request
  18. """
  19. # create a dict with username and password to be added to the each request
  20. self._auth_data = {
  21. 'authenticationUser': conf.MCU_API_USERNAME,
  22. 'authenticationPassword': conf.MCU_API_PSW,
  23. }
  24.  
  25. def request(self, method_name, params):
  26. """
  27. Send a request to Codian MCU API, adding username and password
  28. """
  29. try:
  30. logger.debug("API Request - Method: %s - Params: %s" % (method_name, params))
  31. # add authentication params to the request
  32. params = dict(params.items() + self._auth_data.items())
  33. # convert dict to xml for the request
  34. xmlrequest = xmlrpclib.dumps(tuple([params]), method_name)
  35.  
  36. logger.debug("%s" % xmlrequest)
  37.  
  38. headers = {"Content-type": "text/xml", "charset": "utf-8", "Content-Length": "%d" % len(xmlrequest)}
  39.  
  40. session = requests.Session()
  41. response = session.request(method='POST',
  42. url='http://172.30.254.2/RPC2',
  43. data=xmlrequest,
  44. params=None,
  45. headers=headers)
  46.  
  47. logger.debug("Response: %s" % response.text)
  48. response.close()
  49.  
  50. return xmlrpclib.loads(response.text)
  51.  
  52. except xmlrpclib.Fault as err:
  53. logger.exception("XMLRPC request FAILED using Codian MSE API: %s" % err)
  54. except Exception as err:
  55. logger.exception("XMLRPC exception using Codian MSE API: %s" % err)
  56.  
  57.  
  58. class API_Common(object):
  59. """
  60. Common API between different versions of Codian MCU. When conference_name is set, it will be used
  61. for the API calls if it is not overridden by a method param.
  62. """
  63. _conference_name = None
  64.  
  65. def __init__(self, conference_name=None):
  66. self._conn = MCUConnector()
  67. self._conference_name = conference_name
  68.  
  69.  
  70. class API_2_8(API_Common):
  71. """
  72. Common API between different versions of Codian MCU
  73. """
  74.  
  75. def __init__(self, conference_name):
  76. super(API_2_8, self).__init__(conference_name)
  77.  
  78. def get_conference_status(self, conference_name=None):
  79. """
  80. Get the status of a conference and return a dict with all the details.
  81. """
  82. if not conference_name:
  83. conference_name = self._conference_name
  84.  
  85. params = {
  86. 'conferenceName': conference_name
  87. }
  88. response = self._conn.request('conference.status', params)
  89.  
  90. if response:
  91. response = response[0][0]
  92. return response
  93.  
  94. def lock_conference(self, conference_name=None):
  95. """
  96. Try to lock a conference.
  97. """
  98. if not conference_name:
  99. conference_name = self._conference_name
  100.  
  101. params = {
  102. 'conferenceName': conference_name,
  103. 'locked': True
  104. }
  105. response = self._conn.request('conference.modify', params)
  106.  
  107. if response:
  108. response = response[0][0]
  109. if response['status'] != "operation successful":
  110. logger.error("Error trying to lock the conference: %s" % conference_name)
  111.  
  112. def unlock_conference(self, conference_name=None):
  113. """
  114. Try to lock a conference.
  115. """
  116. if not conference_name:
  117. conference_name = self._conference_name
  118.  
  119. params = {
  120. 'conferenceName': conference_name,
  121. 'locked': False
  122. }
  123. response = self._conn.request('conference.modify', params)
  124.  
  125. if response:
  126. response = response[0][0]
  127. if response['status'] != "operation successful":
  128. logger.error("Error trying to lock the conference: %s" % conference_name)
  129.  
  130. def is_participant_connected(self, participant_name, conference_name=None):
  131. """
  132. Return True or False if the paricipant is connected to the conference.
  133. """
  134. if not conference_name:
  135. conference_name = self._conference_name
  136.  
  137. params = {
  138. 'conferenceName': conference_name,
  139. 'participantName': participant_name
  140. }
  141. response = self._conn.request('participant.status', params)
  142.  
  143. if response:
  144. response = response[0][0]
  145. # returning True if the participant is connected
  146. return response['callState'] == "connected"
  147.  
  148. def get_participant_status(self, participant_name, conference_name=None):
  149. """
  150. Return all the details of a participant.
  151. """
  152. if not conference_name:
  153. conference_name = self._conference_name
  154.  
  155. params = {
  156. 'conferenceName': conference_name,
  157. 'participantName': participant_name
  158. }
  159. response = self._conn.request('participant.status', params)
  160.  
  161. if response:
  162. response = response[0][0]
  163. return response
  164.  
  165. def participant_connect(self, participant_name, conference_name=None):
  166. """
  167. Connect a participant (already added) to the confernece.
  168. """
  169. if not conference_name:
  170. conference_name = self._conference_name
  171.  
  172. params = {
  173. 'conferenceName': conference_name,
  174. 'participantName': participant_name
  175. }
  176. response = self._conn.request('participant.connect', params)
  177.  
  178. if response:
  179. response = response[0][0]
  180. if response['status'] != "operation successful":
  181. logger.error("Error trying to connect the partipant %s to the conference " % (participant_name, conference_name))
  182.  
  183. def participant_disconnect(self, participant_name, conference_name=None):
  184. """
  185. Disconnect a participant from a conference.
  186. """
  187. if not conference_name:
  188. conference_name = self._conference_name
  189.  
  190. params = {
  191. 'conferenceName': conference_name,
  192. 'participantName': participant_name
  193. }
  194. response = self._conn.request('participant.disconnect', params)
  195.  
  196. if response:
  197. response = response[0][0]
  198. if response['status'] != "operation successful":
  199. logger.error("Error trying to disconnect the partipant %s to the conference " % (participant_name, conference_name))
  200.  
  201. def restore_layout(self, participant_name, layout_index, conference_name=None):
  202. """
  203. Restore the layout according to the index set in the configuration file.
  204. """
  205. if not conference_name:
  206. conference_name = self._conference_name
  207.  
  208. params = {
  209. 'conferenceName': conference_name,
  210. 'panes': [{
  211. 'index': layout_index,
  212. 'type': 'participant',
  213. 'participantName': participant_name
  214. }]
  215. }
  216. response = self._conn.request('conference.paneplacement.modify', params)
  217.  
  218. if not response:
  219. logger.error("Error trying to set the layout for the conference " % conference_name)
  220.  
  221. def participant_add(self, participant_name, participant_address, participant_display_name, conference_name=None):
  222. """
  223. Add a new participant to the conference.
  224. """
  225. if not conference_name:
  226. conference_name = self._conference_name
  227.  
  228. params = {
  229. 'conferenceName': conference_name,
  230. 'participantName': participant_name,
  231. 'address': participant_address,
  232. 'displayNameOverrideStatus': True,
  233. 'displayNameOverrideValue': participant_display_name
  234. }
  235. response = self._conn.request('participant.add', params)
  236. return response
  237.  
  238. def participant_modify(self, participant_name, participant_address, participant_display_name, conference_name=None):
  239. """
  240. Modify a participant already connected to the conference.
  241. """
  242. if not conference_name:
  243. conference_name = self._conference_name
  244.  
  245. params = {
  246. 'conferenceName': conference_name,
  247. 'participantName': participant_name,
  248. 'address': participant_address,
  249. 'displayNameOverrideStatus': True,
  250. 'displayNameOverrideValue': participant_display_name
  251. }
  252. response = self._conn.request('participant.modify', params)
  253. return response
  254.  
  255. def participant_remove(self, participant_name, conference_name=None):
  256. """
  257. Remove a participant from the conference.
  258. """
  259. if not conference_name:
  260. conference_name = self._conference_name
  261.  
  262. params = {
  263. 'conferenceName': conference_name,
  264. 'participantName': participant_name
  265. }
  266. response = self._conn.request('participant.remove', params)
  267. return response
  268.  
  269.  
  270. def participant_enumerate(self, conference_name=None):
  271. """
  272. Remove a participant from the conference.
  273. """
  274. if not conference_name:
  275. conference_name = self._conference_name
  276.  
  277. params = {
  278. 'operationScope': ['displayName', 'currentState', 'conferenceName'],
  279. # 'operationScope': ['displayName', 'conferenceName'],
  280. 'enumerateFilter': 'connected',
  281. 'participantType': 'by_name'
  282. }
  283. response = self._conn.request('participant.enumerate', params)
  284.  
  285. if response:
  286. response = response[0][0]
  287. return response
  288.  
  289. def conference_enumerate(self):
  290.  
  291. params = {}
  292.  
  293. response = self._conn.request('conference.enumerate', params)
  294.  
  295. if response:
  296. response = response[0][0]
  297. return response
  298.  
  299. def conference_create(self, conference_name=None):
  300.  
  301. if not conference_name:
  302. conference_name = '123'
  303.  
  304. params = {
  305. 'conferenceName': '123456',
  306. 'durationSeconds': 0
  307. }
  308.  
  309. response = self._conn.request('conference.create', params)
  310.  
  311. return response
  312.  
  313. class API():
  314. """
  315. Factory class to return the proper class to use
  316. """
  317. @staticmethod
  318. def get_instance(version, conference_name):
  319. if version == '2.8':
  320. return API_2_8(conference_name)
  321. if version == '2.9':
  322. return API_2_9(conference_name)
  323.  
  324.  
  325. if __name__ == '__main__':
  326.  
  327. # check if logs and temp folder exit
  328. if not os.path.exists('logs'):
  329. os.mkdir('logs')
  330.  
  331. try:
  332. # for conference in conf.WATCHDOG_CONFERENCES:
  333. # get the API instance
  334. api = API.get_instance('2.8', conference['name'])
  335.  
  336. # print api.conference_create()
  337.  
  338. # conferences = api.conference_enumerate()
  339. # for c in conferences["conferences"]:
  340. # print c["conferenceName"]
  341. e = api.participant_enumerate()
  342. print e
  343. # if "participants" in e:
  344. # for p in e["participants"]:
  345. # ps = api.get_participant_status(p["participantName"], '123')
  346. # print ps
  347.  
  348.  
  349. except Exception as err:
  350. logger.exception("Exception occurred: %s" % err)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement