Skip to content

Commit aab0828

Browse files
committed
Merge branch 'speckle' into main
2 parents 11ae656 + 2cca3b4 commit aab0828

File tree

7 files changed

+123
-1
lines changed

7 files changed

+123
-1
lines changed

src/compas_cloud/proxy.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class Proxy():
9292
9393
"""
9494

95-
def __init__(self, host='127.0.0.1', port=9009, background=True, errorHandler=None, once=True, start_server=True):
95+
def __init__(self, host='127.0.0.1', port=9009, background=True, errorHandler=None, once=True, start_server=True, speckle={}):
9696
"""init function that starts a remote server then assigns corresponding client(websockets/.net) to the proxy"""
9797
self._python = compas._os.select_python(None)
9898
self.host = host
@@ -112,6 +112,9 @@ def __init__(self, host='127.0.0.1', port=9009, background=True, errorHandler=No
112112

113113
self.callbacks = {}
114114
self.errorHandler = errorHandler
115+
if speckle:
116+
result = self.speckle_connect(host=speckle['host'], token=speckle['token'])
117+
print(result)
115118

116119
def package(self, function, cache=False):
117120
raise RuntimeError("Proxy.package() has been deprecated, please use Proxy.function() instead.")
@@ -296,7 +299,15 @@ def check(self):
296299
def once(self):
297300
"""Set the server to close once this client disconnet"""
298301
return self.send({'control': 'once'})
302+
303+
def speckle_connect(self, host="speckle.xyz", token=None):
304+
return self.send({"speckle": {"connect": {"host": host, "token": token}}})
299305

306+
def speckle_push(self, stream_id, item):
307+
return self.send({"speckle": {"update": {"stream_id": stream_id, "item": item}}})
308+
309+
def speckle_pull(self, stream_id):
310+
return self.send({"speckle": {"get": {"stream_id": stream_id}}})
300311

301312
class Sessions_client():
302313

src/compas_cloud/server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
import traceback
1010
import pkg_resources
11+
from compas_cloud.speckle import Speckle
1112

1213
try:
1314
from compas.data import DataEncoder
@@ -22,6 +23,7 @@ class CompasServerProtocol(WebSocketServerProtocol):
2223
cached = {}
2324
sessions = None
2425
server_type = "NORMAL"
26+
speckle = Speckle()
2527

2628
def onConnect(self, request):
2729
"""print client info on connection"""
@@ -178,6 +180,9 @@ def process(self, data):
178180
if 'version' in data:
179181
result = self.version()
180182

183+
if 'speckle' in data:
184+
result = self.speckle.process_command(data['speckle'])
185+
181186
except BaseException as error:
182187

183188
if isinstance(error, KeyboardInterrupt):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .speckle import Speckle

src/compas_cloud/speckle/data.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from specklepy.objects import Base
2+
from compas.data import json_dumps
3+
import json
4+
5+
6+
class Data(Base):
7+
data = dict
8+
9+
def __init__(self, data={}, stream_id=None, **kwargs) -> None:
10+
super().__init__(**kwargs)
11+
self.data = json.loads(json_dumps(data))
12+
if stream_id:
13+
self.data['stream_id'] = stream_id
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
try:
2+
import specklepy
3+
from specklepy.api.client import SpeckleClient
4+
from specklepy.api.credentials import get_account_from_token
5+
from specklepy.transports.server import ServerTransport
6+
from specklepy.api import operations
7+
from compas_cloud.speckle.data import Data as SpeckleData
8+
except ImportError:
9+
specklepy = None
10+
11+
from compas.data import json_loads
12+
import json
13+
14+
15+
class Speckle():
16+
17+
def process_command(self, command):
18+
if 'connect' in command:
19+
return self.connect(host=command['connect']['host'], token=command['connect']['token'])
20+
if 'get' in command:
21+
return self.get_item(command['get']['stream_id'])
22+
if 'update' in command:
23+
return self.update_item(command['update']['item'], stream_id=command['update']['stream_id'])
24+
else:
25+
return "Command not found"
26+
27+
def connect(self, host="speckle.xyz", token=None):
28+
if not specklepy:
29+
raise ImportError("Specklepy is not installed")
30+
print("Connecting to {} with {}".format(host, token))
31+
self.client = SpeckleClient(host=host)
32+
account = get_account_from_token(token, host)
33+
self.client.authenticate_with_account(account)
34+
return "Connected to {} with {}".format(host, token)
35+
36+
def update_item(self, item, stream_id=None, name=None, message=None):
37+
# Create new stream if stream_id is None
38+
if not stream_id:
39+
stream_id = self.client.stream.create(name=name)
40+
transport = ServerTransport(client=self.client, stream_id=stream_id)
41+
hash = operations.send(base=SpeckleData(item, stream_id), transports=[transport])
42+
self.client.commit.create(
43+
stream_id=stream_id,
44+
object_id=hash,
45+
message=message,
46+
)
47+
return stream_id
48+
49+
def get_item(self, stream_id):
50+
stream = self.client.stream.get(id=stream_id)
51+
transport = ServerTransport(client=self.client, stream_id=stream_id)
52+
latest_commit = stream.branches.items[0].commits.items[0]
53+
received_base = operations.receive(obj_id=latest_commit.referencedObject, remote_transport=transport)
54+
data = received_base.data
55+
return json_loads(json.dumps(data))
56+
57+
58+
if __name__ == "__main__":
59+
60+
import compas
61+
from compas.datastructures import Mesh
62+
63+
client = Speckle()
64+
client.connect(token="4b6a06f4c7b114e3b4115e1bba5536261cb4d3bf20")
65+
66+
stream_id = "c0538fd521"
67+
mesh = Mesh.from_obj(compas.get('faces.obj'))
68+
stream_id = client.update_item(mesh, name="faces", stream_id=stream_id)
69+
print(stream_id)
70+
71+
# stream_id = "c0538fd521"
72+
# item = client.get_item(stream_id)
73+
# print(item)

temp/speckle.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import compas
2+
from compas.datastructures import Mesh
3+
4+
from compas_cloud.proxy import Proxy
5+
6+
p = Proxy()
7+
result = p.send({"speckle": {"connect": {"host": "speckle.xyz", "token": "4b6a06f4c7b114e3b4115e1bba5536261cb4d3bf20"}}})
8+
print("check", result)
9+
10+
mesh = Mesh.from_obj(compas.get('faces.obj'))
11+
# mesh = Mesh.from_off(compas.get('tubemesh.off'))
12+
13+
14+
mesh = p.send({"speckle": {"update": {"stream_id": "c0538fd521", "item": mesh}}})
15+
print(mesh)

temp/test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from compas_cloud.proxy import Proxy
2+
3+
p = Proxy(speckle={"host": "speckle.xyz", "token": "__YOUR_TOKEN__"})
4+
p.speckle.watch([stream1, stream2, stream3])

0 commit comments

Comments
 (0)