Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Added BinarySubscriber and BinaryPublisher with examples #1526

Merged
merged 5 commits into from Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions lang/python/core/ecal/core/publisher.py
Expand Up @@ -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
"""
Expand Down
45 changes: 45 additions & 0 deletions lang/python/core/ecal/core/subscriber.py
Expand Up @@ -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
"""
Expand Down
32 changes: 32 additions & 0 deletions 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 minimal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PROJECT_GROUP should be "binary" here


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()
49 changes: 49 additions & 0 deletions 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_byte_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, msg))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Embedding the binary into a regular doesn't look very pretty as non-printable characters will be escaped with leading backslashes which make it looks kind of messy . Maybe a hexadecimal representation would be better here such as "FF EE CC 1A".

else:
print("Subscriber timeout ..")

# finalize eCAL API
ecal_core.finalize()

if __name__ == "__main__":
main()
35 changes: 35 additions & 0 deletions samples/python/pubsub/binary/binary_rec/binary_rec.pyproj
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectHome>.</ProjectHome>
<StartupFile>binary_rec.py</StartupFile>
<SearchPath>..\..\..\lang\python\src</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<Name>binary_rec</Name>
<RootNamespace>binary_rec</RootNamespace>
<ProjectGuid>{093990a3-9395-3bb2-b625-473f1bba2ce6}</ProjectGuid>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="binary_rec.py" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
</PropertyGroup>
<Target Name="CoreCompile" />
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
</Project>
32 changes: 32 additions & 0 deletions 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 minimal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PROJECT_GROUP is wrong


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()
52 changes: 52 additions & 0 deletions 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, msg))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with embedded binary data.


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()

35 changes: 35 additions & 0 deletions samples/python/pubsub/binary/binary_rec_cb/binary_rec_cb.pyproj
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectHome>.</ProjectHome>
<StartupFile>binary_rec_cb.py</StartupFile>
<SearchPath>..\..\..\lang\python\src</SearchPath>
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<Name>binary_rec_cb</Name>
<RootNamespace>binary_rec_cb</RootNamespace>
<ProjectGuid>{a38cc1fc-76f7-3172-81ac-b754dc0f9d60}</ProjectGuid>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugSymbols>true</DebugSymbols>
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
</PropertyGroup>
<ItemGroup>
<Compile Include="binary_rec_cb.py" />
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
</PropertyGroup>
<Target Name="CoreCompile" />
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
</Project>
32 changes: 32 additions & 0 deletions 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 minimal)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong PROJECT_GROUP


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()