3737appid = None
3838token = None
3939expiration = None
40+ logged_in = False
41+
42+ ric_name = 'EUR='
4043
4144expire_time_in_seconds = None
4245time_before_expire_in_seconds = 15 * 60 # 15 Minutes to Seconds
@@ -91,41 +94,44 @@ def CreateAuthorization(username, password, appid):
9194
9295## ------------------------------------------ TRKD WebSocket functions ------------------------------------------ ##
9396
94- def process_message (ws , message_json ):
97+ def process_message (message_json ):
9598 """ Parse at high level and output JSON of message """
9699 message_type = message_json ['Type' ]
97100
98101 if message_type == "Refresh" :
99102 if 'Domain' in message_json :
100103 message_domain = message_json ['Domain' ]
101104 if message_domain == "Login" :
102- process_login_response (ws , message_json )
105+ process_login_response (message_json )
103106 elif message_type == "Ping" :
104107 pong_json = { 'Type' :'Pong' }
105- ws .send (json .dumps (pong_json ))
108+ web_socket_app .send (json .dumps (pong_json ))
106109 print ("SENT:" )
107110 print (json .dumps (pong_json , sort_keys = True , indent = 2 , separators = (',' , ':' )))
108111
109112
110- def process_login_response (ws , message_json ):
113+ def process_login_response (message_json ):
111114 """ Send item request """
112- send_market_price_request (ws )
115+ global logged_in
116+
117+ logged_in = True
118+ send_market_price_request (ric_name )
113119
114120
115- def send_market_price_request (ws ):
121+ def send_market_price_request (ric_name ):
116122 """ Create and send simple Market Price request """
117123 mp_req_json = {
118124 'ID' : 2 ,
119125 'Key' : {
120- 'Name' : 'EUR=' ,
126+ 'Name' : ric_name ,
121127 },
122128 }
123- ws .send (json .dumps (mp_req_json ))
129+ web_socket_app .send (json .dumps (mp_req_json ))
124130 print ("SENT:" )
125131 print (json .dumps (mp_req_json , sort_keys = True , indent = 2 , separators = (',' , ':' )))
126132
127133
128- def send_login_request (ws ):
134+ def send_login_request (is_refresh_token = False ):
129135 """ Generate a login request from command line data (or defaults) and send """
130136 login_json = {
131137 'ID' : 1 ,
@@ -146,40 +152,44 @@ def send_login_request(ws):
146152 login_json ['Key' ]['Elements' ]['Position' ] = position
147153 login_json ['Key' ]['Elements' ]['AuthenticationToken' ] = token
148154
149- ws .send (json .dumps (login_json ))
155+ # If the token is a refresh token, this is not our first login attempt.
156+ if is_refresh_token :
157+ login_json ['Refresh' ] = False
158+
159+ web_socket_app .send (json .dumps (login_json ))
150160 print ("SENT:" )
151161 print (json .dumps (login_json , sort_keys = True , indent = 2 , separators = (',' , ':' )))
152162
153163
154- def on_message (ws , message ):
164+ def on_message (_ , message ):
155165 """ Called when message received, parse message into JSON for processing """
156166 print ("RECEIVED: " )
157167 message_json = json .loads (message )
158168 print (json .dumps (message_json , sort_keys = True , indent = 2 , separators = (',' , ':' )))
159169
160170 for singleMsg in message_json :
161- process_message (ws , singleMsg )
171+ process_message (singleMsg )
162172
163173
164- def on_error (ws , error ):
174+ def on_error (__file__ , error ):
165175 """ Called when websocket error has occurred """
166176 print (error )
167177
168178
169- def on_close (ws ):
179+ def on_close (_ ):
170180 """ Called when websocket is closed """
171181 global web_socket_open
172182 print ("WebSocket Closed" )
173183 web_socket_open = False
174184
175185
176- def on_open (ws ):
186+ def on_open (_ ):
177187 """ Called when handshake is complete and websocket is open, send login """
178188
179189 print ("WebSocket successfully connected!" )
180190 global web_socket_open
181191 web_socket_open = True
182- send_login_request (ws )
192+ send_login_request (is_refresh_token = False )
183193
184194## ------------------------------------------ Main App ------------------------------------------ ##
185195
@@ -205,13 +215,30 @@ def on_open(ws):
205215 on_close = on_close ,
206216 subprotocols = [ws_protocol ])
207217 web_socket_app .on_open = on_open
218+ # for test
208219
220+ # expire_time_in_seconds = 120
221+ # time_before_expire_in_seconds = 15
209222 # Event loop
210223 wst = threading .Thread (target = web_socket_app .run_forever )
211224 wst .start ()
212225
213226 try :
214227 while True :
215- time .sleep (1 )
228+ #time.sleep(1)
229+ if (expire_time_in_seconds > time_before_expire_in_seconds ):
230+ time .sleep (expire_time_in_seconds - time_before_expire_in_seconds )
231+ else :
232+ # failt the refresh sine value too small
233+ sys .exit (1 )
234+ token , expiration , expire_time_in_seconds = CreateAuthorization (username ,password ,appid )
235+ print ('new Token = %s' % (token ))
236+ print ('new Expiration = %s' % (expiration ))
237+ print ('new Expiration in next = %d seconds' % (expire_time_in_seconds ))
238+ if not token :
239+ sys .exit (1 )
240+ if logged_in :
241+ print ('############### Re-new Authentication to TRKD ###############' )
242+ send_login_request (is_refresh_token = True )
216243 except KeyboardInterrupt :
217244 web_socket_app .close ()
0 commit comments