blob: fce2b68394b529b4f393730d6dedd7559265a79f [file] [log] [blame]
Tony Mack79a49c82013-06-15 23:51:57 -04001import threading
2import requests, json
Sapan Bhatia66f4e612013-07-02 12:12:38 -04003
Scott Baker86e132c2015-02-11 21:38:09 -08004from xos.config import Config, XOS_DIR
Tony Mack79a49c82013-06-15 23:51:57 -04005
Scott Bakerc1c45f82014-01-21 16:23:51 -08006import uuid
Sapan Bhatia9182b322013-06-25 16:22:14 -04007import os
Scott Baker6ecd4262014-01-21 23:15:21 -08008import imp
9import inspect
Sapan Bhatia9182b322013-06-25 16:22:14 -040010import base64
Sapan Bhatiadbaf1932013-09-03 11:28:52 -040011import json
Scott Baker6ecd4262014-01-21 23:15:21 -080012import traceback
Sapan Bhatia9182b322013-06-25 16:22:14 -040013
Scott Baker7648c582015-03-10 12:52:29 -070014if getattr(Config(),"observer_fofum_disabled", False) != True:
15 from fofum import Fofum
16 fofum_enabled = True
17else:
18 fofum_enabled = False
19
Scott Baker3d5a2782014-05-13 09:58:02 -070020random_client_id=None
21def get_random_client_id():
22 global random_client_id
23
Scott Baker3bd7c862015-02-04 16:54:10 -080024 if (random_client_id is None) and os.path.exists(XOS_DIR + "/random_client_id"):
Scott Baker3d5a2782014-05-13 09:58:02 -070025 # try to use the last one we used, if we saved it
26 try:
Scott Baker3bd7c862015-02-04 16:54:10 -080027 random_client_id = open(XOS_DIR+"/random_client_id","r").readline().strip()
Scott Baker3d5a2782014-05-13 09:58:02 -070028 print "get_random_client_id: loaded %s" % random_client_id
29 except:
Scott Baker3bd7c862015-02-04 16:54:10 -080030 print "get_random_client_id: failed to read " + XOS_DIR + "/random_client_id"
Scott Baker3d5a2782014-05-13 09:58:02 -070031
32 if random_client_id is None:
33 random_client_id = base64.urlsafe_b64encode(os.urandom(12))
34 print "get_random_client_id: generated new id %s" % random_client_id
35
36 # try to save it for later (XXX: could race with another client here)
37 try:
Scott Baker3bd7c862015-02-04 16:54:10 -080038 open(XOS_DIR + "/random_client_id","w").write("%s\n" % random_client_id)
Scott Baker3d5a2782014-05-13 09:58:02 -070039 except:
Scott Baker3bd7c862015-02-04 16:54:10 -080040 print "get_random_client_id: failed to write " + XOS_DIR + "/random_client_id"
Scott Baker3d5a2782014-05-13 09:58:02 -070041
42 return random_client_id
43
44# decorator that marks dispatachable event methods
Tony Mack79a49c82013-06-15 23:51:57 -040045def event(func):
Sapan Bhatia1b84d662014-04-29 23:45:48 -040046 setattr(func, 'event', func.__name__)
47 return func
Tony Mack79a49c82013-06-15 23:51:57 -040048
49class EventHandler:
Sapan Bhatia1b84d662014-04-29 23:45:48 -040050 # This code is currently not in use.
51 def __init__(self):
52 pass
Tony Mack79a49c82013-06-15 23:51:57 -040053
Sapan Bhatia1b84d662014-04-29 23:45:48 -040054 @staticmethod
55 def get_events():
56 events = []
57 for name in dir(EventHandler):
58 attribute = getattr(EventHandler, name)
59 if hasattr(attribute, 'event'):
60 events.append(getattr(attribute, 'event'))
61 return events
Tony Mack79a49c82013-06-15 23:51:57 -040062
Sapan Bhatia1b84d662014-04-29 23:45:48 -040063 def dispatch(self, event, *args, **kwds):
64 if hasattr(self, event):
65 return getattr(self, event)(*args, **kwds)
66
Scott Baker3d5a2782014-05-13 09:58:02 -070067
Sapan Bhatia66f4e612013-07-02 12:12:38 -040068class EventSender:
Sapan Bhatia1b84d662014-04-29 23:45:48 -040069 def __init__(self,user=None,clientid=None):
70 try:
71 user = Config().feefie_client_user
72 except:
73 user = 'pl'
Sapan Bhatia66f4e612013-07-02 12:12:38 -040074
Sapan Bhatia1b84d662014-04-29 23:45:48 -040075 try:
76 clid = Config().feefie_client_id
77 except:
78 clid = get_random_client_id()
Sapan Bhatiafc69f3d2014-07-21 20:09:06 -040079 print "EventSender: no feefie_client_id configured. Using random id %s" % clid
Sapan Bhatia66f4e612013-07-02 12:12:38 -040080
Scott Baker7648c582015-03-10 12:52:29 -070081 if fofum_enabled:
82 self.fofum = Fofum(user=user)
83 self.fofum.make(clid)
Sapan Bhatia9faf7b02013-10-09 10:27:14 -040084
Sapan Bhatia1b84d662014-04-29 23:45:48 -040085 def fire(self,**kwargs):
86 kwargs["uuid"] = str(uuid.uuid1())
Scott Baker7648c582015-03-10 12:52:29 -070087 if fofum_enabled:
88 self.fofum.fire(json.dumps(kwargs))
Tony Mack79a49c82013-06-15 23:51:57 -040089
90class EventListener:
Sapan Bhatia1b84d662014-04-29 23:45:48 -040091 def __init__(self,wake_up=None):
92 self.handler = EventHandler()
93 self.wake_up = wake_up
Scott Baker6ecd4262014-01-21 23:15:21 -080094
Sapan Bhatia1b84d662014-04-29 23:45:48 -040095 def handle_event(self, payload):
96 payload_dict = json.loads(payload)
Sapan Bhatia43c3a772013-07-03 11:19:07 -040097
Sapan Bhatia1b84d662014-04-29 23:45:48 -040098 if (self.wake_up):
99 self.wake_up()
Sapan Bhatiadbaf1932013-09-03 11:28:52 -0400100
Sapan Bhatia1b84d662014-04-29 23:45:48 -0400101 def run(self):
102 # This is our unique client id, to be used when firing and receiving events
103 # It needs to be generated once and placed in the config file
Scott Bakerc1c45f82014-01-21 16:23:51 -0800104
Sapan Bhatia1b84d662014-04-29 23:45:48 -0400105 try:
106 user = Config().feefie_client_user
107 except:
108 user = 'pl'
Sapan Bhatia66f4e612013-07-02 12:12:38 -0400109
Sapan Bhatia1b84d662014-04-29 23:45:48 -0400110 try:
111 clid = Config().feefie_client_id
112 except:
113 clid = get_random_client_id()
114 print "EventListener: no feefie_client_id configured. Using random id %s" % clid
Scott Bakerc1c45f82014-01-21 16:23:51 -0800115
Scott Baker7648c582015-03-10 12:52:29 -0700116 if fofum_enabled:
117 f = Fofum(user=user)
118
119 listener_thread = threading.Thread(target=f.listen_for_event,args=(clid,self.handle_event))
120 listener_thread.start()