Advertisement
kohpriwniranam

poll

Mar 19th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import threading
  3. import traceback
  4.  
  5.  
  6. class Poll(object):
  7.  
  8. def __init__(self):
  9. self.subscriptionId = 0
  10. self.eventSyncToken = None
  11.  
  12. def __fetchOps(self, count=100):
  13. fetchOps = self.fetchOps
  14. if self.DEVICE_TYPE in self.SYNC_SUPPORT:
  15. fetchOps = self.sync
  16. ops = fetchOps(self.revision, count)
  17. if 'error' in ops:
  18. raise Exception(ops['error'])
  19. for op in ops:
  20. opType = self.checkAndGetValue(op, 'type', 3)
  21. if opType != -1:
  22. self.setRevision(self.checkAndGetValue(op, 'revision', 1))
  23. yield op
  24.  
  25. def __fetchMyEvents(self, count: int = 100, initLastSyncToken: bool = False):
  26. resp = self.fetchMyEvents(self.subscriptionId, self.eventSyncToken, count)
  27. events = self.checkAndGetValue(resp, 'events', 2)
  28. for event in events:
  29. syncToken = self.checkAndGetValue(event, 'syncToken', 5)
  30. self.setEventSyncToken(syncToken)
  31. yield event
  32. if initLastSyncToken:
  33. syncToken = self.checkAndGetValue(resp, 'syncToken', 3)
  34. self.setEventSyncToken(syncToken)
  35.  
  36. def __execute(self, op, func):
  37. try:
  38. func(op, self)
  39. except Exception as e:
  40. self.log(traceback.format_exc())
  41.  
  42. def setRevision(self, revision):
  43. if revision is None:
  44. self.log(f'revision is None!!')
  45. revision = 0
  46. self.revision = max(revision, self.revision)
  47.  
  48. def setEventSyncToken(self, syncToken):
  49. if syncToken is None:
  50. self.log(f'syncToken is None!!')
  51. syncToken = 0
  52. if self.eventSyncToken is None:
  53. self.eventSyncToken = syncToken
  54. else:
  55. self.eventSyncToken = max(int(syncToken), int(self.eventSyncToken))
  56.  
  57. def trace(self, func, isThreading=True):
  58. while self.is_login:
  59. for op in self.__fetchOps():
  60. opType = self.checkAndGetValue(op, 'type', 'val_3', 3)
  61. if opType != 0 and opType != -1:
  62. if isThreading:
  63. _td = threading.Thread(target=self.__execute, args=(op, func))
  64. _td.daemon = True
  65. _td.start()
  66. else:
  67. self.__execute(op, func)
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement