|
| 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) |
0 commit comments