Spaces:
Paused
Paused
Update SpaceFace.py
Browse files- SpaceFace.py +10 -9
SpaceFace.py
CHANGED
|
@@ -3,19 +3,23 @@ import scratchattach as scratch3
|
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
import random
|
|
|
|
| 6 |
global conn
|
| 7 |
global projectid
|
| 8 |
global endpoint
|
| 9 |
global users
|
|
|
|
| 10 |
users = {}
|
| 11 |
-
|
| 12 |
-
session = scratch3.login("PolyPenguin", f.read())
|
| 13 |
class User:
|
| 14 |
def __init__(self, name, id):
|
|
|
|
| 15 |
self.name = name
|
| 16 |
self.id = id
|
| 17 |
class Responder:
|
| 18 |
-
def __init__(self, projectid, channel, can_respond, can_stream, value):
|
|
|
|
|
|
|
| 19 |
self.projectid = projectid
|
| 20 |
self.can_respond = can_respond
|
| 21 |
self.can_stream = can_stream
|
|
@@ -26,6 +30,7 @@ class Responder:
|
|
| 26 |
def poll(self):
|
| 27 |
value = scratch3.get_var(self.projectid, "channel "+str(self.channel))
|
| 28 |
if value != self.last_value and value != None:
|
|
|
|
| 29 |
self.last_value = value
|
| 30 |
binary = coding.decimal_to_binary(int(value))
|
| 31 |
if binary[1:3]=="01":
|
|
@@ -70,8 +75,6 @@ class Responder:
|
|
| 70 |
t=time.time()
|
| 71 |
self.wrote = True
|
| 72 |
class ConnectionEndpoint:
|
| 73 |
-
def __init__(self):
|
| 74 |
-
pass
|
| 75 |
def receivedMessage(self, message, user, responder):
|
| 76 |
global users
|
| 77 |
r=random.randrange(1, 2047)
|
|
@@ -107,21 +110,19 @@ def thread(n):
|
|
| 107 |
payload = "true" if binary[23:]=='1' else "false"
|
| 108 |
if payloadformat == "11":
|
| 109 |
payload = coding.convert_to_text(binary[23:])
|
| 110 |
-
respond = Responder(projectid, n, acceptstaticpayload, acceptstreamingpayload, value)
|
| 111 |
if header in users:
|
| 112 |
user = users[header]
|
| 113 |
else:
|
| 114 |
user = None
|
|
|
|
| 115 |
endpoint[reqendpoint].receivedMessage(payload, user, respond)
|
| 116 |
|
| 117 |
|
| 118 |
-
def start_server(endpoints
|
| 119 |
-
global projectid
|
| 120 |
global conn
|
| 121 |
global endpoint
|
| 122 |
endpoints.insert(0, ConnectionEndpoint())
|
| 123 |
endpoint = endpoints
|
| 124 |
-
projectid = project_id
|
| 125 |
conn = session.connect_cloud(project_id)
|
| 126 |
threads = [threading.Thread(target=thread, args=(i+1,)) for i in range(10)]
|
| 127 |
for t in threads:
|
|
|
|
| 3 |
import threading
|
| 4 |
import time
|
| 5 |
import random
|
| 6 |
+
import os
|
| 7 |
global conn
|
| 8 |
global projectid
|
| 9 |
global endpoint
|
| 10 |
global users
|
| 11 |
+
projectid = os.getenv("ProjectID")
|
| 12 |
users = {}
|
| 13 |
+
session = scratch3.login(os.getenv("Username"), os.getenv("Password"))
|
|
|
|
| 14 |
class User:
|
| 15 |
def __init__(self, name, id):
|
| 16 |
+
self.last_request = time.time()
|
| 17 |
self.name = name
|
| 18 |
self.id = id
|
| 19 |
class Responder:
|
| 20 |
+
def __init__(self, projectid, channel, can_respond, can_stream, value, user):
|
| 21 |
+
user.last_request = time.time()
|
| 22 |
+
self.user = user
|
| 23 |
self.projectid = projectid
|
| 24 |
self.can_respond = can_respond
|
| 25 |
self.can_stream = can_stream
|
|
|
|
| 30 |
def poll(self):
|
| 31 |
value = scratch3.get_var(self.projectid, "channel "+str(self.channel))
|
| 32 |
if value != self.last_value and value != None:
|
| 33 |
+
self.user.last_request = time.time()
|
| 34 |
self.last_value = value
|
| 35 |
binary = coding.decimal_to_binary(int(value))
|
| 36 |
if binary[1:3]=="01":
|
|
|
|
| 75 |
t=time.time()
|
| 76 |
self.wrote = True
|
| 77 |
class ConnectionEndpoint:
|
|
|
|
|
|
|
| 78 |
def receivedMessage(self, message, user, responder):
|
| 79 |
global users
|
| 80 |
r=random.randrange(1, 2047)
|
|
|
|
| 110 |
payload = "true" if binary[23:]=='1' else "false"
|
| 111 |
if payloadformat == "11":
|
| 112 |
payload = coding.convert_to_text(binary[23:])
|
|
|
|
| 113 |
if header in users:
|
| 114 |
user = users[header]
|
| 115 |
else:
|
| 116 |
user = None
|
| 117 |
+
respond = Responder(projectid, n, acceptstaticpayload, acceptstreamingpayload, value, user)
|
| 118 |
endpoint[reqendpoint].receivedMessage(payload, user, respond)
|
| 119 |
|
| 120 |
|
| 121 |
+
def start_server(endpoints):
|
|
|
|
| 122 |
global conn
|
| 123 |
global endpoint
|
| 124 |
endpoints.insert(0, ConnectionEndpoint())
|
| 125 |
endpoint = endpoints
|
|
|
|
| 126 |
conn = session.connect_cloud(project_id)
|
| 127 |
threads = [threading.Thread(target=thread, args=(i+1,)) for i in range(10)]
|
| 128 |
for t in threads:
|