<reponame>ancuk-wtf/kernet-dev
#!/usr/bin/env python3

import rospy
import socket
import cv2 as cv
import numpy as np

# Socket
# ======
client_socket_created = False
client_socket = None

# OpenCV
# ======
source = np.zeros((360, 640, 3), dtype=np.uint8)

# =============================================================================


def cllbck_tim_50hz(event):
    if client_routine() == -1:
        rospy.signal_shutdown('signal_shutdown')


def cllbck_tim_25hz(event):
    global source

    cv.imshow('client source', source)

    cv.waitKey(1)

# =============================================================================


def client_init():
    global client_socket_created, client_socket

    try:
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65536)
    except:
        return -1

    client_socket_created = True

    return 0


def client_routine():
    global client_socket_created, client_socket
    global server_ip, server_port
    global source

    if client_socket_created == True:
        data = None
        address = None

        try:
            client_socket.sendto(b'WTF', socket.MSG_DONTWAIT, (server_ip, server_port))
            data, address = client_socket.recvfrom(65536, socket.MSG_DONTWAIT)
        except:
            pass

    if data != None:
        jpeg_source = np.frombuffer(data, dtype=np.uint8)
        source = cv.imdecode(jpeg_source, cv.IMREAD_COLOR)

    return 0

# =============================================================================


if __name__ == '__main__':
    rospy.init_node('client_test')

    # Parameter
    global server_ip, server_port
    # Timer
    global tim_50hz
    global tim_25hz

    # Parameter
    server_ip = rospy.get_param('client/server_ip')
    server_port = rospy.get_param('client/server_port')
    # Timer
    tim_50hz = rospy.Timer(rospy.Duration(0.02), cllbck_tim_50hz)
    tim_25hz = rospy.Timer(rospy.Duration(0.04), cllbck_tim_25hz)

    if client_init() == -1:
        rospy.signal_shutdown('signal_shutdown')

    rospy.spin()

#!/usr/bin/env python3

import rospy
import socket
import cv2 as cv
import numpy as np

# Socket
# ======
client_socket_created = False
client_socket = None

# OpenCV
# ======
source = np.zeros((360, 640, 3), dtype=np.uint8)

# =============================================================================


def cllbck_tim_50hz(event):
    if client_routine() == -1:
        rospy.signal_shutdown('signal_shutdown')


def cllbck_tim_25hz(event):
    global source

    cv.imshow('client source', source)

    cv.waitKey(1)

# =============================================================================


def client_init():
    global client_socket_created, client_socket

    try:
        client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        client_socket.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 65536)
    except:
        return -1

    client_socket_created = True

    return 0


def client_routine():
    global client_socket_created, client_socket
    global server_ip, server_port
    global source

    if client_socket_created == True:
        data = None
        address = None

        try:
            client_socket.sendto(b'WTF', socket.MSG_DONTWAIT, (server_ip, server_port))
            data, address = client_socket.recvfrom(65536, socket.MSG_DONTWAIT)
        except:
            pass

    if data != None:
        jpeg_source = np.frombuffer(data, dtype=np.uint8)
        source = cv.imdecode(jpeg_source, cv.IMREAD_COLOR)

    return 0

# =============================================================================


if __name__ == '__main__':
    rospy.init_node('client_test')

    # Parameter
    global server_ip, server_port
    # Timer
    global tim_50hz
    global tim_25hz

    # Parameter
    server_ip = rospy.get_param('client/server_ip')
    server_port = rospy.get_param('client/server_port')
    # Timer
    tim_50hz = rospy.Timer(rospy.Duration(0.02), cllbck_tim_50hz)
    tim_25hz = rospy.Timer(rospy.Duration(0.04), cllbck_tim_25hz)

    if client_init() == -1:
        rospy.signal_shutdown('signal_shutdown')

    rospy.spin()
#!/usr/bin/env python3 # Socket # ====== # OpenCV # ====== # ============================================================================= # ============================================================================= # ============================================================================= # Parameter # Timer # Parameter # Timer