33import eventlet
44import socketio
55from datetime import datetime
6- import time
6+ import time , threading
7+
8+
79
810sio = socketio .Server (cors_allowed_origins = ["http://localhost:3000" ])
911app = socketio .WSGIApp (sio )
1618@sio .event
1719def connect (sid , environ ):
1820 print ('connect ' , sid )
19- val = ser .read (100 ).decode ('utf-8' )
20- current_date = datetime .now ()
21- timestamp = current_date .isoformat ()
22- sio .emit ("pedal_value" , {"timestamp" : timestamp , "number" : val })
23- print ("MESSAGE ID " + str (random .randint (1 ,20 ))+ " RECIEVED! VALUE IS: " + str (val ))
2421
2522
2623@sio .event
@@ -31,6 +28,32 @@ def my_message(sid, data):
3128def disconnect (sid ):
3229 print ('disconnect ' , sid )
3330
31+ def action () :
32+ val = ser .read (100 ).decode ('utf-8' )
33+ current_date = datetime .now ()
34+ timestamp = current_date .isoformat ()
35+ sio .emit ("pedal_value" , {"timestamp" : timestamp , "number" : val })
36+ print ("MESSAGE ID " + str (random .randint (1 ,20 ))+ " RECIEVED! VALUE IS: " + str (val ))
37+
38+
39+ class setInterval :
40+ def __init__ (self ,interval ,action ) :
41+ self .interval = interval
42+ self .action = action
43+ self .stopEvent = threading .Event ()
44+ thread = threading .Thread (target = self .__setInterval )
45+ thread .start ()
46+
47+ def __setInterval (self ) :
48+ nextTime = time .time ()+ self .interval
49+ while not self .stopEvent .wait (nextTime - time .time ()) :
50+ nextTime += self .interval
51+ self .action ()
52+
53+ inter = setInterval (1 ,action )
54+
55+ sio .start_background_task (action ())
56+
3457if __name__ == '__main__' :
3558 eventlet .wsgi .server (eventlet .listen (('' , 5050 )), app )
3659
0 commit comments