fishyboteso/fishy/systems/fishy_network.py

41 lines
826 B
Python
Raw Normal View History

2020-04-17 13:08:26 +00:00
import logging
2018-05-03 16:37:18 +00:00
import socket
import json
PORT = 8023
MESSAGE = "yo"
RETRY_LIMIT = 5
IP = 0
s = None
2018-05-03 16:37:18 +00:00
2018-05-03 16:37:18 +00:00
def initialize(ip):
global s, IP
IP = ip
2018-05-03 16:37:18 +00:00
def send_message(message, count=1):
try:
sock = socket.socket()
sock.connect((IP, PORT))
sock.send(bytes(message, "utf-8"))
sock.close()
2018-05-03 16:37:18 +00:00
except ConnectionRefusedError:
2020-04-17 13:08:26 +00:00
logging.info("Connection Refused, please turn on service on mobile")
2018-05-03 16:37:18 +00:00
except TimeoutError:
2020-04-17 13:08:26 +00:00
logging.info("Timeout Error")
2018-05-03 16:37:18 +00:00
if count < RETRY_LIMIT:
send_message(message, count+1)
def sendHoleDeplete(count):
message = {"action": "holeDeplete", "fishCount": count}
2018-05-03 16:37:18 +00:00
jsonString = json.dumps(message)
send_message(jsonString)
if __name__ == "__main__":
initialize("192.168.0.192")
sendHoleDeplete(2)