diff --git a/lang/python/core/ecal/core/publisher.py b/lang/python/core/ecal/core/publisher.py index 8f341dfaa8..3e870080a4 100644 --- a/lang/python/core/ecal/core/publisher.py +++ b/lang/python/core/ecal/core/publisher.py @@ -65,6 +65,18 @@ def __init__(self, name, type_=None): def send(self, msg, time=-1): return self.c_publisher.send(msg.SerializeToString(), time) +class BinaryPublisher(MessagePublisher): + """Spezialized publisher that sends out binary messages + """ + def __init__(self, name): + topic_type = "binary" + topic_enc = "base" + topic_desc = b"" + super(BinaryPublisher, self).__init__(name, topic_type, topic_enc, topic_desc) + + def send(self, msg, time=-1): + return self.c_publisher.send(msg, time) + class StringPublisher(MessagePublisher): """Spezialized publisher that sends out plain strings """ diff --git a/lang/python/core/ecal/core/subscriber.py b/lang/python/core/ecal/core/subscriber.py index f6ca858f02..4011a2a82c 100644 --- a/lang/python/core/ecal/core/subscriber.py +++ b/lang/python/core/ecal/core/subscriber.py @@ -117,6 +117,51 @@ def _on_receive(self, topic_name, msg, time): self.callback(topic_name, proto_message, time) +class BinarySubscriber(MessageSubscriber): + """Specialized subscriber that subscribes to binary messages + """ + def __init__(self, name): + topic_type = "binary" + topic_enc = "base" + topic_desc = b"" + super(BinarySubscriber, self).__init__(name, topic_type, topic_enc, topic_desc) + self.callback = None + + def receive(self, timeout=0): + """ receive subscriber content with timeout + + :param timeout: receive timeout in ms + + """ + ret, msg, time = self.c_subscriber.receive(timeout) + if ret > 0: + msg = msg + else: + msg = b"" + return ret, msg, time + + def set_callback(self, callback): + """ set callback function for incoming messages + + :param callback: python callback function (f(topic_name, msg, time)) + + """ + self.callback = callback + self.c_subscriber.set_callback(self._on_receive) + + def rem_callback(self, callback): + """ remove callback function for incoming messages + + :param callback: python callback function (f(topic_name, msg, time)) + + """ + self.c_subscriber.rem_callback(self._on_receive) + self.callback = None + + def _on_receive(self, topic_name, msg, time): + self.callback(topic_name, msg, time) + + class StringSubscriber(MessageSubscriber): """Spezialized publisher subscribes to plain strings """ diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index dca119e633..22f0b515e8 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -73,6 +73,9 @@ if(BUILD_PY_BINDING) add_subdirectory(python/pubsub/string/minimal_rec) add_subdirectory(python/pubsub/string/minimal_rec_cb) add_subdirectory(python/pubsub/string/minimal_snd) + add_subdirectory(python/pubsub/binary/binary_rec) + add_subdirectory(python/pubsub/binary/binary_rec_cb) + add_subdirectory(python/pubsub/binary/binary_snd) # services add_subdirectory(python/services/minimal_service) diff --git a/samples/python/pubsub/binary/binary_rec/CMakeLists.txt b/samples/python/pubsub/binary/binary_rec/CMakeLists.txt new file mode 100644 index 0000000000..181e6cbaa3 --- /dev/null +++ b/samples/python/pubsub/binary/binary_rec/CMakeLists.txt @@ -0,0 +1,32 @@ +# ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2019 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +project(binary_rec) + +find_package(eCAL REQUIRED) + +set(PROJECT_GROUP binary) + +if(ECAL_INCLUDE_PY_SAMPLES) + if(WIN32) + + include_external_msproject(${PROJECT_NAME}_py ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pyproj) + set_property(TARGET ${PROJECT_NAME}_py PROPERTY FOLDER samples/python/${PROJECT_GROUP}) + + endif() +endif() \ No newline at end of file diff --git a/samples/python/pubsub/binary/binary_rec/binary_rec.py b/samples/python/pubsub/binary/binary_rec/binary_rec.py new file mode 100644 index 0000000000..8e2041b0b9 --- /dev/null +++ b/samples/python/pubsub/binary/binary_rec/binary_rec.py @@ -0,0 +1,49 @@ + # ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2019 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +import sys + +import ecal.core.core as ecal_core +from ecal.core.subscriber import BinarySubscriber + +def main(): + # print eCAL version and date + print("eCAL {} ({})\n".format(ecal_core.getversion(), ecal_core.getdate())) + + # initialize eCAL API + ecal_core.initialize(sys.argv, "py_binary_rec") + + # set process state + ecal_core.set_process_state(1, 1, "I feel good") + + # create subscriber + sub = BinarySubscriber("Hello") + + # receive messages + while ecal_core.ok(): + ret, msg, time = sub.receive(500) + if ret > 0: + print("Received: {} ms {}".format(time, bytes.fromhex(msg.decode("utf-8")))) + else: + print("Subscriber timeout ..") + + # finalize eCAL API + ecal_core.finalize() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/samples/python/pubsub/binary/binary_rec/binary_rec.pyproj b/samples/python/pubsub/binary/binary_rec/binary_rec.pyproj new file mode 100644 index 0000000000..31855265cf --- /dev/null +++ b/samples/python/pubsub/binary/binary_rec/binary_rec.pyproj @@ -0,0 +1,35 @@ + + + + Debug + 2.0 + . + binary_rec.py + ..\..\..\lang\python\src + . + . + binary_rec + binary_rec + {093990a3-9395-3bb2-b625-473f1bba2ce6} + + + true + false + + + true + false + + + + + + 10.0 + + + + + + + + \ No newline at end of file diff --git a/samples/python/pubsub/binary/binary_rec_cb/CMakeLists.txt b/samples/python/pubsub/binary/binary_rec_cb/CMakeLists.txt new file mode 100644 index 0000000000..2887d4a544 --- /dev/null +++ b/samples/python/pubsub/binary/binary_rec_cb/CMakeLists.txt @@ -0,0 +1,32 @@ +# ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2019 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +project(binary_rec_cb) + +find_package(eCAL REQUIRED) + +set(PROJECT_GROUP binary) + +if(ECAL_INCLUDE_PY_SAMPLES) + if(WIN32) + + include_external_msproject(${PROJECT_NAME}_py ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pyproj) + set_property(TARGET ${PROJECT_NAME}_py PROPERTY FOLDER samples/python/${PROJECT_GROUP}) + + endif() +endif() diff --git a/samples/python/pubsub/binary/binary_rec_cb/binary_rec_cb.py b/samples/python/pubsub/binary/binary_rec_cb/binary_rec_cb.py new file mode 100644 index 0000000000..546c8cc1c2 --- /dev/null +++ b/samples/python/pubsub/binary/binary_rec_cb/binary_rec_cb.py @@ -0,0 +1,52 @@ +# ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2019 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +import sys +import time + +import ecal.core.core as ecal_core +from ecal.core.subscriber import BinarySubscriber + +# eCAL receive callback +def callback(topic_name, msg, time): + print("Received: {} ms {}".format(time, bytes.fromhex(msg.decode("utf-8")))) + +def main(): + # print eCAL version and date + print("eCAL {} ({})\n".format(ecal_core.getversion(), ecal_core.getdate())) + + # initialize eCAL API + ecal_core.initialize(sys.argv, "py_binary_rec_cb") + + # set process state + ecal_core.set_process_state(1, 1, "I feel good") + + # create subscriber and connect callback + sub = BinarySubscriber("Hello") + sub.set_callback(callback) + + # idle main thread + while ecal_core.ok(): + time.sleep(0.1) + + # finalize eCAL API + ecal_core.finalize() + +if __name__ == "__main__": + main() + diff --git a/samples/python/pubsub/binary/binary_rec_cb/binary_rec_cb.pyproj b/samples/python/pubsub/binary/binary_rec_cb/binary_rec_cb.pyproj new file mode 100644 index 0000000000..5478851607 --- /dev/null +++ b/samples/python/pubsub/binary/binary_rec_cb/binary_rec_cb.pyproj @@ -0,0 +1,35 @@ + + + + Debug + 2.0 + . + binary_rec_cb.py + ..\..\..\lang\python\src + . + . + binary_rec_cb + binary_rec_cb + {a38cc1fc-76f7-3172-81ac-b754dc0f9d60} + + + true + false + + + true + false + + + + + + 10.0 + + + + + + + + \ No newline at end of file diff --git a/samples/python/pubsub/binary/binary_snd/CMakeLists.txt b/samples/python/pubsub/binary/binary_snd/CMakeLists.txt new file mode 100644 index 0000000000..bdc0dec522 --- /dev/null +++ b/samples/python/pubsub/binary/binary_snd/CMakeLists.txt @@ -0,0 +1,32 @@ +# ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2019 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +project(binary_snd) + +find_package(eCAL REQUIRED) + +set(PROJECT_GROUP binary) + +if(ECAL_INCLUDE_PY_SAMPLES) + if(WIN32) + + include_external_msproject(${PROJECT_NAME}_py ${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}.pyproj) + set_property(TARGET ${PROJECT_NAME}_py PROPERTY FOLDER samples/python/${PROJECT_GROUP}) + + endif() +endif() \ No newline at end of file diff --git a/samples/python/pubsub/binary/binary_snd/binary_snd.py b/samples/python/pubsub/binary/binary_snd/binary_snd.py new file mode 100644 index 0000000000..914765d488 --- /dev/null +++ b/samples/python/pubsub/binary/binary_snd/binary_snd.py @@ -0,0 +1,54 @@ +# ========================= eCAL LICENSE ================================= +# +# Copyright (C) 2016 - 2019 Continental Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# ========================= eCAL LICENSE ================================= + +import sys +import time +import random + +import ecal.core.core as ecal_core +from ecal.core.publisher import BinaryPublisher + +def main(): + # print eCAL version and date + print("eCAL {} ({})\n".format(ecal_core.getversion(), ecal_core.getdate())) + + # initialize eCAL API + ecal_core.initialize(sys.argv, "py_binary_snd") + + # set process state + ecal_core.set_process_state(1, 1, "I feel good") + + # create publisher + pub = BinaryPublisher("Hello") + msg_fox = b"4120717569636b2062726f776e20666f7820" + + # send messages + msg_count = 0 + while ecal_core.ok(): + msg_count += 1 + hex_ascii = bytes(''.join([hex(ord(digit))[2:] for digit in str(msg_count)]), "utf-8") + msg = msg_fox + hex_ascii + pub.send(msg) + print("Sent: ", msg) + time.sleep(0.01) + + # finalize eCAL API + ecal_core.finalize() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/samples/python/pubsub/binary/binary_snd/binary_snd.pyproj b/samples/python/pubsub/binary/binary_snd/binary_snd.pyproj new file mode 100644 index 0000000000..c7223cb68c --- /dev/null +++ b/samples/python/pubsub/binary/binary_snd/binary_snd.pyproj @@ -0,0 +1,35 @@ + + + + Debug + 2.0 + . + binary_snd.py + ..\..\..\lang\python\src + . + . + binary_snd + binary_snd + {5ebed1e0-8867-3120-bbfe-6d6db73e8077} + + + true + false + + + true + false + + + + + + 10.0 + + + + + + + + \ No newline at end of file