Skip to content

Instantly share code, notes, and snippets.

@wisnubaldas
Last active June 14, 2022 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wisnubaldas/86d16b91e9040b7bcdc45e474b2b1513 to your computer and use it in GitHub Desktop.
Save wisnubaldas/86d16b91e9040b7bcdc45e474b2b1513 to your computer and use it in GitHub Desktop.
get status and open lock
import socket
import sys
import codecs
class Kerong:
def __init__(self, ip, port):
self.ip = ip
self.port = port
# Create a TCP/IP socket
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(10)
server_address = (self.ip, self.port)
print(sys.stderr, 'connecting to %s port %s' % server_address)
# Connect the socket to the port where the server is listening
self.sock.connect(server_address)
# this command is used to open the locker, it does not return data
# -----------------------------------------------------------------
def lock_open(self, locker):
try:
self.sock.setblocking(True)
# Send data
m = bytes.fromhex(locker)
print(sys.stderr, 'sending "%s"' % m)
self.sock.send(m)
finally:
print(sys.stderr, 'closing socket')
self.sock.close()
# this command is used to get locker status based on specific locker address
# it returns binary data
# -----------------------------------------------------------------
def lock_stat(self, locker):
try:
# self.sock.setblocking(True)
# Send data
m = bytes.fromhex(locker)
print(sys.stderr, 'sending "%s"' % m)
self.sock.send(m)
r = self.sock.recv(1024)
# print(len(r))
s = str(r)
print(s)
finally:
print(sys.stderr, 'closing socket')
self.sock.close()
# this command is used to get all locker status, it will return data
# -----------------------------------------------------------------
def stat_lock_all(self):
try:
m = bytes.fromhex('02 F0 32 03 27')
print(sys.stderr, 'sending "%s"' % m)
self.sock.send(m)
r = self.sock.recv(1024)
# print(len(r))
# s = str(r)
print('data nya #############')
print(r.decode(encoding='utf-8', errors='strict'))
finally:
print('close socket')
self.sock.close()
tes = Kerong('192.168.123.49', 5000)
# open lock
# tes.lock_open('02 00 31 03 36')
# show all status lock
tes.stat_lock_all()
# this response
# <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'> connecting to 192.168.123.49 port 5000
# <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'> sending "b"\x02\xf02\x03'""
# data nya #############
# �6����;
# close socket
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment