-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
76 lines (56 loc) · 2.61 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import socketio
import time
import json
from datetime import datetime, timedelta
sio = socketio.Client()
json_file_path = 'data.json'
current_datetime = datetime.now()
@sio.on('connect')
def on_connect():
print('Connected to server')
def get_current_value_inc_vat(json_file_path):
# Load JSON data from the file
with open(json_file_path, 'r') as file:
data = json.load(file)
# Get the current date and time
#current_datetime = datetime.now()
# Iterate through the "results" list to find the corresponding "value_inc_vat"
for result in data['results']:
valid_from = datetime.strptime(result['valid_from'], "%Y-%m-%dT%H:%M:%SZ")
valid_to = datetime.strptime(result['valid_to'], "%Y-%m-%dT%H:%M:%SZ")
# Check if the current time is within the valid range
if valid_from <= current_datetime <= valid_to:
return result['value_inc_vat']
# If no matching time range is found, return None or an appropriate value
return None
def get_value_at_future_time(json_file_path, minutes_offset=0):
# Load JSON data from the file
with open(json_file_path, 'r') as file:
data = json.load(file)
# Get the current date and time
#current_datetime = datetime.now()
# Calculate the future time by adding the specified offset (in minutes)
future_datetime = current_datetime + timedelta(minutes=minutes_offset)
# Iterate through the "results" list to find the corresponding value at the future time
for result in data['results']:
valid_from = datetime.strptime(result['valid_from'], "%Y-%m-%dT%H:%M:%SZ")
valid_to = datetime.strptime(result['valid_to'], "%Y-%m-%dT%H:%M:%SZ")
# Check if the future time is within the valid range
if valid_from <= future_datetime <= valid_to:
return result['value_inc_vat']
# If no matching time range is found, return None or an appropriate value
return None
def update_rates(current_rate, next_rate):
sio.emit('update_rates', {'current_rate': current_rate, 'next_rate': next_rate})
print('Sent update_rates event to server')
print(f"current rate: {current_rate} - next rate: {next_rate}")
if __name__ == '__main__':
try:
sio.connect('http://localhost:5000') # Adjust the server URL as needed
# Update the rates here
current_rate = get_current_value_inc_vat(json_file_path)
next_rate = get_value_at_future_time(json_file_path, minutes_offset=30)
# Send the updated rates to the server
update_rates(current_rate, next_rate)
except KeyboardInterrupt:
print("Client closed by the user.")