Skip to content

Commit 37ab513

Browse files
authored
Merge pull request #15 from compas-dev/msg_components
Add GH components
2 parents 2b551a0 + ec62b77 commit 37ab513

File tree

13 files changed

+359
-0
lines changed

13 files changed

+359
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
* Add Grasshopper components for publishing and subscribing to topics, for creating messages and connecting to MQTT transports.
13+
1214
### Changed
1315

1416
### Removed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""
2+
Create a message.
3+
4+
COMPAS EVE v0.5.0
5+
"""
6+
7+
import System
8+
9+
import Rhino.Geometry as rg
10+
import rhinoscriptsyntax as rs
11+
12+
from compas.geometry import Brep
13+
from compas_eve import Message
14+
from compas_rhino import conversions
15+
16+
component = ghenv.Component # noqa: F821
17+
18+
local_values = locals()
19+
data = dict()
20+
21+
for input_param in component.Params.Input:
22+
name = input_param.NickName
23+
value = local_values[name]
24+
if isinstance(value, System.Guid):
25+
try:
26+
value = rs.coercegeometry(value)
27+
except: # noqa: E722
28+
pass
29+
if isinstance(value, rg.Point3d):
30+
value = conversions.point_to_compas(value)
31+
elif isinstance(value, rg.Box):
32+
value = conversions.box_to_compas(value)
33+
elif isinstance(value, rg.Vector3d):
34+
value = conversions.vector_to_compas(value)
35+
elif isinstance(value, rg.Arc):
36+
value = conversions.arc_to_compas(value)
37+
elif isinstance(value, rg.Circle):
38+
value = conversions.circle_to_compas(value)
39+
elif isinstance(value, rg.Curve):
40+
value = conversions.curve_to_compas(value)
41+
elif isinstance(value, rg.Cone):
42+
value = conversions.cone_to_compas(value)
43+
elif isinstance(value, rg.Cylinder):
44+
value = conversions.cylinder_to_compas(value)
45+
elif isinstance(value, rg.Line):
46+
value = conversions.line_to_compas(value)
47+
elif isinstance(value, rg.Mesh):
48+
value = conversions.mesh_to_compas(value)
49+
elif isinstance(value, rg.Plane):
50+
value = conversions.plane_to_compas_frame(value)
51+
elif isinstance(value, rg.Sphere):
52+
value = conversions.sphere_to_compas(value)
53+
elif isinstance(value, rg.PolylineCurve):
54+
value = conversions.polygon_to_compas(value)
55+
elif isinstance(value, rg.Polyline):
56+
value = conversions.polyline_to_compas(value)
57+
elif isinstance(value, rg.Surface):
58+
value = conversions.surface_to_compas(value)
59+
elif isinstance(value, rg.Brep):
60+
value = Brep.from_native(value)
61+
62+
data[name] = value
63+
64+
message = Message(**data)
1.47 KB
Loading
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "Create message",
3+
"nickname": "Message",
4+
"category": "COMPAS EVE",
5+
"subcategory": "Events",
6+
"description": "Create a new message.",
7+
"exposure": 2,
8+
9+
"ghpython": {
10+
"isAdvancedMode": false,
11+
"iconDisplay": 2,
12+
"inputParameters": [
13+
{
14+
"name": "x",
15+
"description": "Example input field. Add or remove fields as needed."
16+
}
17+
],
18+
"outputParameters": [
19+
{
20+
"name": "message",
21+
"description": "The newly created message."
22+
}
23+
]
24+
}
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Connect or disconnect to an MQTT broker.
3+
4+
COMPAS EVE v0.5.0
5+
"""
6+
7+
from threading import Event
8+
9+
from compas_ghpython import create_id
10+
from ghpythonlib.componentbase import executingcomponent as component
11+
from scriptcontext import sticky as st
12+
13+
from compas_eve.mqtt import MqttTransport
14+
15+
16+
class MqttConnectComponent(component):
17+
def RunScript(self, host, port, connect):
18+
mqtt_transport = None
19+
20+
host = host or "127.0.0.1"
21+
port = port or 1883
22+
23+
key = create_id(self, "mqtt_transport")
24+
mqtt_transport = st.get(key, None)
25+
26+
if mqtt_transport:
27+
st[key].close()
28+
29+
if connect:
30+
event = Event()
31+
transport = MqttTransport(host, port)
32+
transport.on_ready(event.set)
33+
if not event.wait(5):
34+
raise Exception("Failed to connect to MQTT broker.")
35+
36+
st[key] = transport
37+
38+
mqtt_transport = st.get(key, None)
39+
is_connected = mqtt_transport._is_connected if mqtt_transport else False
40+
return (mqtt_transport, is_connected)
1.41 KB
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "MQTT Transport",
3+
"nickname": "MQTT",
4+
"category": "COMPAS EVE",
5+
"subcategory": "Events",
6+
"description": "Connect or disconnect to an MQTT broker.",
7+
"exposure": 2,
8+
9+
"ghpython": {
10+
"isAdvancedMode": true,
11+
"iconDisplay": 2,
12+
"inputParameters": [
13+
{
14+
"name": "host",
15+
"description": "The host address of the MQTT broker. Defaults to 127.0.0.1.",
16+
"typeHintID": "str"
17+
},
18+
{
19+
"name": "port",
20+
"description": "The port of MQTT broker. Defaults to 1883.",
21+
"typeHintID": "int"
22+
},
23+
{
24+
"name": "connect",
25+
"description": "If True, connects to MQTT broker. If False, disconnects from MQTT. Defaults to False.",
26+
"typeHintID": "bool"
27+
}
28+
],
29+
"outputParameters": [
30+
{
31+
"name": "mqtt_transport",
32+
"description": "The MQTT transport instance."
33+
},
34+
{
35+
"name": "is_connected",
36+
"description": "True if connection established."
37+
}
38+
]
39+
}
40+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Publish messages to a topic.
3+
4+
COMPAS EVE v0.5.0
5+
"""
6+
7+
import time
8+
9+
from ghpythonlib.componentbase import executingcomponent as component
10+
from scriptcontext import sticky as st
11+
12+
from compas_eve import Topic
13+
from compas_eve import Publisher
14+
from compas_ghpython import create_id
15+
16+
17+
class PublishComponent(component):
18+
def RunScript(self, transport, topic_name, message, on):
19+
if not topic_name:
20+
raise ValueError("Please specify the name of the topic")
21+
22+
if on is None:
23+
on = True
24+
25+
key = create_id(self, "publisher_{}".format(id(transport)))
26+
key_count = create_id(self, "publisher_count_{}".format(id(transport)))
27+
publisher = st.get(key, None)
28+
29+
if not publisher:
30+
topic = Topic(topic_name)
31+
publisher = Publisher(topic, transport=transport)
32+
publisher.advertise()
33+
time.sleep(0.2)
34+
35+
st[key] = publisher
36+
st[key_count] = 0
37+
38+
if on and message:
39+
st[key_count] += 1
40+
publisher.publish(message)
41+
self.Message = "Published {} messages".format(st[key_count])
42+
43+
return st[key_count]
1.64 KB
Loading
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "Publish to topic",
3+
"nickname": "Publish",
4+
"category": "COMPAS EVE",
5+
"subcategory": "Events",
6+
"description": "Publish messages to a topic.",
7+
"exposure": 4,
8+
9+
"ghpython": {
10+
"isAdvancedMode": true,
11+
"iconDisplay": 2,
12+
"inputParameters": [
13+
{
14+
"name": "transport",
15+
"description": "An instance of transport. If no transport is specified, it will use in-memory transport."
16+
},
17+
{
18+
"name": "topic_name",
19+
"description": "The name of the topic to publish to to, e.g. `/compas_eve/hello_world/`.",
20+
"typeHintID": "str"
21+
},
22+
{
23+
"name": "message",
24+
"description": "The message to publish. It can be an instance of `Message` or a dictionary"
25+
},
26+
{
27+
"name": "on",
28+
"description": "Turn ON or OFF the publisher. Defaults to True.",
29+
"typeHintID": "bool"
30+
}
31+
32+
],
33+
"outputParameters": [
34+
{
35+
"name": "count",
36+
"description": "The count of published messages on the selected topic."
37+
}
38+
]
39+
}
40+
}

0 commit comments

Comments
 (0)