Skip to content

Commit 2cca3b4

Browse files
committed
add speckle controls
1 parent 6b4006b commit 2cca3b4

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
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/speckle/data.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
class Data(Base):
77
data = dict
88

9-
def __init__(self, data={}, **kwargs) -> None:
9+
def __init__(self, data={}, stream_id=None, **kwargs) -> None:
1010
super().__init__(**kwargs)
1111
self.data = json.loads(json_dumps(data))
12+
if stream_id:
13+
self.data['stream_id'] = stream_id

src/compas_cloud/speckle/speckle.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
from specklepy.api.client import SpeckleClient
2-
from specklepy.api.credentials import get_account_from_token
3-
from specklepy.transports.server import ServerTransport
4-
from specklepy.api import operations
5-
from compas_cloud.speckle.data import Data as SpeckleData
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+
611
from compas.data import json_loads
712
import json
813

@@ -20,20 +25,20 @@ def process_command(self, command):
2025
return "Command not found"
2126

2227
def connect(self, host="speckle.xyz", token=None):
28+
if not specklepy:
29+
raise ImportError("Specklepy is not installed")
2330
print("Connecting to {} with {}".format(host, token))
2431
self.client = SpeckleClient(host=host)
2532
account = get_account_from_token(token, host)
2633
self.client.authenticate_with_account(account)
27-
return "Connected"
34+
return "Connected to {} with {}".format(host, token)
2835

2936
def update_item(self, item, stream_id=None, name=None, message=None):
30-
print("CHECK!", item)
31-
print("CHECK!", stream_id)
3237
# Create new stream if stream_id is None
3338
if not stream_id:
3439
stream_id = self.client.stream.create(name=name)
3540
transport = ServerTransport(client=self.client, stream_id=stream_id)
36-
hash = operations.send(base=SpeckleData(item), transports=[transport])
41+
hash = operations.send(base=SpeckleData(item, stream_id), transports=[transport])
3742
self.client.commit.create(
3843
stream_id=stream_id,
3944
object_id=hash,
@@ -49,9 +54,6 @@ def get_item(self, stream_id):
4954
data = received_base.data
5055
return json_loads(json.dumps(data))
5156

52-
# def watch_item(self, stream_id, callback):
53-
# item = self.get_item(stream_id)
54-
# callback(item)
5557

5658
if __name__ == "__main__":
5759

0 commit comments

Comments
 (0)