Skip to content

Commit

Permalink
[Python] Added BinarySubscriber and BinaryPublisher with examples (#1526
Browse files Browse the repository at this point in the history
)

Co-authored-by: Leon Hosch <leon.hosch@continental.com>
  • Loading branch information
LeonHosch and Leon Hosch authored Apr 30, 2024
1 parent 2116e5f commit c0e3049
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lang/python/core/ecal/core/publisher.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
3 changes: 3 additions & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions samples/python/pubsub/binary/binary_rec/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
49 changes: 49 additions & 0 deletions samples/python/pubsub/binary/binary_rec/binary_rec.py
Original file line number Diff line number Diff line change
@@ -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()
35 changes: 35 additions & 0 deletions samples/python/pubsub/binary/binary_rec/binary_rec.pyproj
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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()
52 changes: 52 additions & 0 deletions samples/python/pubsub/binary/binary_rec_cb/binary_rec_cb.py
Original file line number Diff line number Diff line change
@@ -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()

35 changes: 35 additions & 0 deletions samples/python/pubsub/binary/binary_rec_cb/binary_rec_cb.pyproj
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit c0e3049

Please sign in to comment.