added all info here

This commit is contained in:
Qangan
2024-02-23 10:00:48 +07:00
commit b6156ae81d
19 changed files with 1035 additions and 0 deletions
View File
Binary file not shown.
Binary file not shown.
+109
View File
@@ -0,0 +1,109 @@
import socket, json, random
bufferSize = 1024
gets_data = {}
def initialize():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('192.168.1.35', 7777))
s.settimeout(0.5)
with open('data.json') as f:
data = json.load(f)
return [s, data]
def update_json(data):
with open('data.json', 'w') as f:
json.dump(data, f)
'''def get_smth(s, data):
req = {"SensorType": data}
while True:
print("Sending")
s.sendto(json.dumps(req).encode(), (('192.168.1.77', 7777)))
try:
a = s.recvfrom(bufferSize)
except:
continue
try:
j = json.loads(a[0].decode())
if data in j.get('SensorType'):
print(j.get("value"), True)
s.sendto("GOT".encode(), ('192.168.1.77', 7777))
else:
print(a[0].decode())
continue
except Exception as err:
print("lol", err, a[0].decode())
continue
#finally:
#print("lol")
def change_smth(s, data, value):
req = {"SensorType": data,
"value": value}
while True:
try:
a = s.recvfrom(bufferSize)
j = json.loads(a[0].decode())
if data in j.get("SensorType"):
print(j[data], True)
return j[data]
break
else:
print(a[0].decode())
except Exception as err:
print(a)
continue
'''
def send_command(s, SensorType: str,command: str, value = None):
if value:
req = {"Command": command,
"SensorType": SensorType,
"Value": value}
else:
req = {"Command": command,
"SensorType": SensorType}
while True:
s.sendto(json.dumps(req).encode(), (('192.168.1.77', 7777)))
try:
a = s.recvfrom(bufferSize)
except:
continue
try:
response = json.loads(a[0].decode())
if command == 'GetValue' and response["SensorType"] == SensorType:
print(j.get("Value"), True)
elif command == 'ChangeValue' and response["SensorType"] == SensorType:
print("Changed to " + str(value), True)
else:
continue
except Exception as err:
print("We've got an error: ", err, a[1])
def check_sos(s):
'''req = {"SensorType": "SOS"}
while True:
s.sendto(json.dumps(req).encode(), (('192.168.1.77', 7777)))
try:
a = s.recvfrom(bufferSize)
except:
continue
try:
j = json.loads(a[0].decode())
if data in j.get('SensorType'):
print(j.get("value"), True)
s.sendto("GOT".encode(), ('192.168.1.77', 7777))
break
else:
print(a[0].decode())
continue
except Exception as err:
print("lol", err, a[0].decode())
continue
'''
return json.dumps([{"stationName": str(random.randint(1,255)), "alertType": 'alert!!'}, {"stationName": str(random.randint(1,255)), "alertType": 'alert2!!'}])