immersive-home/app/content/entities/line_chart/line_chart.gd
2024-04-11 16:51:30 +02:00

40 lines
919 B
GDScript

extends Entity
const Entity = preload ("../entity.gd")
@onready var line_chart = $LineChart
@onready var timer = $Timer
@onready var label = $Label3D
func _ready():
super()
label.text = entity_id
if HomeApi.has_connected() == false:
await HomeApi.on_connect
request_history()
timer.timeout.connect(request_history)
func request_history():
# Request history from the server
var now = Time.get_unix_time_from_datetime_dict(Time.get_datetime_dict_from_system())
# 2 days ago
var two_days_ago = now - 2 * 24 * 60 * 60
var start = Time.get_datetime_string_from_unix_time(two_days_ago) + ".000Z"
var result = await HomeApi.get_history(entity_id, start)
var points = result.data.map(func(point):
# Divide by 1000 to convert milliseconds to seconds
return Vector2((point.start + point.end) / (2 * 1000), point.mean)
)
line_chart.points.value = points
func get_interface():
return "line_chart"