diff --git a/app/addons/debug_draw_3d/README.md b/app/addons/debug_draw_3d/README.md index a2c272b..5f5f49d 100644 --- a/app/addons/debug_draw_3d/README.md +++ b/app/addons/debug_draw_3d/README.md @@ -77,33 +77,33 @@ Simple test: ```gdscript func _process(delta: float) -> void: - var _time = Time.get_ticks_msec() / 1000.0 - var box_pos = Vector3(0, sin(_time * 4), 0) - var line_begin = Vector3(-1, sin(_time * 4), 0) - var line_end = Vector3(1, cos(_time * 4), 0) + var _time = Time.get_ticks_msec() / 1000.0 + var box_pos = Vector3(0, sin(_time * 4), 0) + var line_begin = Vector3(-1, sin(_time * 4), 0) + var line_end = Vector3(1, cos(_time * 4), 0) - DebugDraw3D.draw_box(box_pos, Vector3(1, 2, 1), Color(0, 1, 0)) - DebugDraw3D.draw_line(line_begin, line_end, Color(1, 1, 0)) - DebugDraw2D.set_text("Time", _time) - DebugDraw2D.set_text("Frames drawn", Engine.get_frames_drawn()) - DebugDraw2D.set_text("FPS", Engine.get_frames_per_second()) - DebugDraw2D.set_text("delta", delta) + DebugDraw3D.draw_box(box_pos, Vector3(1, 2, 1), Color(0, 1, 0)) + DebugDraw3D.draw_line(line_begin, line_end, Color(1, 1, 0)) + DebugDraw2D.set_text("Time", _time) + DebugDraw2D.set_text("Frames drawn", Engine.get_frames_drawn()) + DebugDraw2D.set_text("FPS", Engine.get_frames_per_second()) + DebugDraw2D.set_text("delta", delta) ``` ```csharp public override void _Process(float delta) { - var _time = Time.GetTicksMsec() / 1000.0f; - var box_pos = new Vector3(0, Mathf.Sin(_time * 4f), 0); - var line_begin = new Vector3(-1, Mathf.Sin(_time * 4f), 0); - var line_end = new Vector3(1, Mathf.Cos(_time * 4f), 0); + var _time = Time.GetTicksMsec() / 1000.0f; + var box_pos = new Vector3(0, Mathf.Sin(_time * 4f), 0); + var line_begin = new Vector3(-1, Mathf.Sin(_time * 4f), 0); + var line_end = new Vector3(1, Mathf.Cos(_time * 4f), 0); - DebugDraw3D.DrawBox(box_pos, new Vector3(1, 2, 1), new Color(0, 1, 0)); - DebugDraw3D.DrawLine(line_begin, line_end, new Color(1, 1, 0)); - DebugDraw2D.SetText("Time", _time); - DebugDraw2D.SetText("Frames drawn", Engine.GetFramesDrawn()); - DebugDraw2D.SetText("FPS", Engine.GetFramesPerSecond()); - DebugDraw2D.SetText("delta", delta); + DebugDraw3D.DrawBox(box_pos, new Vector3(1, 2, 1), new Color(0, 1, 0)); + DebugDraw3D.DrawLine(line_begin, line_end, new Color(1, 1, 0)); + DebugDraw2D.SetText("Time", _time); + DebugDraw2D.SetText("Frames drawn", Engine.GetFramesDrawn()); + DebugDraw2D.SetText("FPS", Engine.GetFramesPerSecond()); + DebugDraw2D.SetText("delta", delta); } ``` @@ -117,11 +117,11 @@ A list of all functions is available in the documentation inside the editor. Besides `DebugDraw2D/3D`, you can also use `Dbg2/3`. ```gdscript - DebugDraw3D.draw_box_xf(Transform3D(), Color.GREEN) - Dbg3.draw_box_xf(Transform3D(), Color.GREEN) + DebugDraw3D.draw_box_xf(Transform3D(), Color.GREEN) + Dbg3.draw_box_xf(Transform3D(), Color.GREEN) - DebugDraw2D.set_text("delta", delta) - Dbg2.set_text("delta", delta) + DebugDraw2D.set_text("delta", delta) + Dbg2.set_text("delta", delta) ``` But unfortunately at the moment `GDExtension` does not support adding documentation. diff --git a/app/content/entities/line_chart/line_chart.gd b/app/content/entities/line_chart/line_chart.gd new file mode 100644 index 0000000..b8c8083 --- /dev/null +++ b/app/content/entities/line_chart/line_chart.gd @@ -0,0 +1,46 @@ +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 + + var stateInfo = await HomeApi.get_state(entity_id) + if stateInfo["attributes"]["friendly_name"] != null: + label.text = stateInfo["attributes"]["friendly_name"] + + 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 + line_chart.y_axis_label.value = result.unit + +func get_interface(): + return "line_chart" \ No newline at end of file diff --git a/app/content/entities/line_chart/line_chart.tscn b/app/content/entities/line_chart/line_chart.tscn new file mode 100644 index 0000000..0c05b7c --- /dev/null +++ b/app/content/entities/line_chart/line_chart.tscn @@ -0,0 +1,33 @@ +[gd_scene load_steps=5 format=3 uid="uid://cltcs0gokeald"] + +[ext_resource type="Script" path="res://content/entities/line_chart/line_chart.gd" id="1_5dxim"] +[ext_resource type="PackedScene" uid="uid://cwvykoymlrrel" path="res://content/ui/components/line_chart/line_chart.tscn" id="2_k4shm"] +[ext_resource type="Script" path="res://content/functions/movable.gd" id="3_lidml"] + +[sub_resource type="BoxShape3D" id="BoxShape3D_rmm5v"] +size = Vector3(0.5, 0.3, 0.001) + +[node name="LineChart" type="StaticBody3D" groups=["entity"]] +collision_layer = 5 +collision_mask = 0 +script = ExtResource("1_5dxim") + +[node name="LineChart" parent="." instance=ExtResource("2_k4shm")] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.25, 0.15, 0) +shape = SubResource("BoxShape3D_rmm5v") + +[node name="Movable" type="Node" parent="."] +script = ExtResource("3_lidml") + +[node name="Timer" type="Timer" parent="."] +wait_time = 60.0 +autostart = true + +[node name="Label3D" type="Label3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.25, -0.03, 0) +pixel_size = 0.001 +text = "sensor.tada" +font_size = 18 +outline_size = 4 diff --git a/app/content/entities/number/number.tscn b/app/content/entities/number/number.tscn index fd0e532..f8dba61 100644 --- a/app/content/entities/number/number.tscn +++ b/app/content/entities/number/number.tscn @@ -8,7 +8,7 @@ [sub_resource type="BoxShape3D" id="BoxShape3D_7mk8w"] size = Vector3(0.0390625, 0.114258, 0.0142822) -[node name="Number" type="StaticBody3D"] +[node name="Number" type="StaticBody3D" groups=["entity"]] script = ExtResource("1_26xwp") [node name="Slider" parent="." instance=ExtResource("2_sninv")] diff --git a/app/content/entities/sensor/sensor.gd b/app/content/entities/sensor/sensor.gd index cb82054..5f3f1c9 100644 --- a/app/content/entities/sensor/sensor.gd +++ b/app/content/entities/sensor/sensor.gd @@ -4,9 +4,11 @@ const Entity = preload ("../entity.gd") @onready var label: Label3D = $Label @onready var collision_shape = $CollisionShape3D +@onready var chart_button = $Button var sensor_data = {} var unit = null +var is_text = true # Called when the node enters the scene tree for the first time. func _ready(): @@ -19,12 +21,30 @@ func _ready(): set_text(new_state) ) + remove_child(chart_button) + + chart_button.on_button_down.connect(func(): + House.body.create_entity(entity_id, global_position, "line_chart") + remove_child(chart_button) + ) + +func _on_click(_event): + if is_text: + return + + if chart_button.is_inside_tree() == false: + add_child(chart_button) + else: + remove_child(chart_button) + func set_text(stateInfo): if stateInfo == null: return var text = stateInfo["state"] + is_text = text.is_valid_float() == false&&text.is_valid_int() == false + if stateInfo["attributes"]["friendly_name"] != null: text = stateInfo["attributes"]["friendly_name"] + "\n" + text diff --git a/app/content/entities/sensor/sensor.tscn b/app/content/entities/sensor/sensor.tscn index 024519d..65f1118 100644 --- a/app/content/entities/sensor/sensor.tscn +++ b/app/content/entities/sensor/sensor.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=6 format=3 uid="uid://xsiy71rsqulj"] +[gd_scene load_steps=7 format=3 uid="uid://xsiy71rsqulj"] [ext_resource type="Script" path="res://content/entities/sensor/sensor.gd" id="1_57ac8"] [ext_resource type="FontVariation" uid="uid://d2ofyimg5s65q" path="res://assets/fonts/ui_font_500.tres" id="2_4np3x"] [ext_resource type="Script" path="res://content/functions/movable.gd" id="2_fpq5q"] [ext_resource type="Script" path="res://content/functions/occludable.gd" id="3_l3sp5"] +[ext_resource type="PackedScene" uid="uid://bsjqdvkt0u87c" path="res://content/ui/components/button/button.tscn" id="5_bmtkc"] [sub_resource type="BoxShape3D" id="BoxShape3D_phuot"] resource_local_to_scene = true @@ -29,3 +30,10 @@ script = ExtResource("2_fpq5q") [node name="Occludable" type="Node" parent="."] script = ExtResource("3_l3sp5") + +[node name="Button" parent="." instance=ExtResource("5_bmtkc")] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, -0.1, 0) +label = "add_chart" +icon = true + +[node name="HoverTimer" type="Timer" parent="."] diff --git a/app/content/main.tscn b/app/content/main.tscn index 18fcdf6..519f72d 100644 --- a/app/content/main.tscn +++ b/app/content/main.tscn @@ -36,7 +36,7 @@ material = SubResource("StandardMaterial3D_m58yb") size = Vector3(0.01, 0.01, 0.01) [node name="Main" type="Node3D"] -transform = Transform3D(1, -0.000296142, 0.000270963, 0.000296143, 1, -4.61078e-06, -0.000270962, 4.67014e-06, 1, 0, 0, 0) +transform = Transform3D(1, -0.000296142, 0.000270963, 0.000296143, 1, -4.5899e-06, -0.000270962, 4.67014e-06, 1, 0, 0, 0) script = ExtResource("1_uvrd4") [node name="WorldEnvironment" type="WorldEnvironment" parent="."] diff --git a/app/content/system/house/house.gd b/app/content/system/house/house.gd index 92b2877..acaa35f 100644 --- a/app/content/system/house/house.gd +++ b/app/content/system/house/house.gd @@ -38,7 +38,7 @@ func update_house(): for entity_index in range(Store.house.state.entities.size()): var entity = Store.house.state.entities[entity_index] - var entity_instance = create_entity_in(entity.id, entity.room) + var entity_instance = create_entity_in(entity.id, entity.room, entity.get("interface", null)) if entity_instance == null: continue @@ -163,13 +163,13 @@ func get_level_aabb(level: int): func get_rooms(level: int): return get_level(level).get_children() -func create_entity(entity_id: String, entity_position: Vector3): +func create_entity(entity_id: String, entity_position: Vector3, type=null): var room = find_room_at(entity_position) if room == null: return null - var entity = EntityFactory.create_entity(entity_id) + var entity = EntityFactory.create_entity(entity_id, type) if entity == null: return null @@ -181,13 +181,13 @@ func create_entity(entity_id: String, entity_position: Vector3): return entity -func create_entity_in(entity_id: String, room_name: String): +func create_entity_in(entity_id: String, room_name: String, type=null): var room = find_room(room_name) if room == null: return null - var entity = EntityFactory.create_entity(entity_id) + var entity = EntityFactory.create_entity(entity_id, type) if entity == null: return null @@ -239,9 +239,12 @@ func save_all_entities(): "id": entity.entity_id, "position": entity.global_position, "rotation": entity.global_rotation, - "room": String(room.name) + "room": String(room.name), } + if entity.has_method("get_interface"): + entity_data["interface"] = entity.get_interface() + Store.house.state.entities.append(entity_data) Store.house.save_local() diff --git a/app/content/ui/components/button/button.gd b/app/content/ui/components/button/button.gd index 740f69e..5b71137 100644 --- a/app/content/ui/components/button/button.gd +++ b/app/content/ui/components/button/button.gd @@ -72,7 +72,7 @@ func _ready(): if initial_active: active = true - Update.props(self, ["active", "external_value", "icon", "label", "font_size"]) + Update.props(self, ["active", "external_value", "icon", "label", "font_size", "disabled"]) if echo: echo_timer = Timer.new() diff --git a/app/content/ui/components/line_chart/axis_label.tres b/app/content/ui/components/line_chart/axis_label.tres new file mode 100644 index 0000000..937c7d0 --- /dev/null +++ b/app/content/ui/components/line_chart/axis_label.tres @@ -0,0 +1,6 @@ +[gd_resource type="LabelSettings" format=3 uid="uid://bc8arfh1k2gd2"] + +[resource] +font_size = 80 +outline_size = 10 +outline_color = Color(0, 0, 0, 1) diff --git a/app/content/ui/components/line_chart/line_chart.gd b/app/content/ui/components/line_chart/line_chart.gd new file mode 100644 index 0000000..5a1e6a9 --- /dev/null +++ b/app/content/ui/components/line_chart/line_chart.gd @@ -0,0 +1,97 @@ +@tool + +extends Node3D + +@onready var mesh: MeshInstance3D = $Line +@onready var plane: MeshInstance3D = $Plane +@onready var x_axis: Control = $XAxis/SubViewport/XAxis +@onready var y_axis: Control = $YAxis/SubViewport/YAxis + +const WIDTH = 0.001 +const DEPTH = 0.0005 +const STEPS = 5 + +@export var size: Vector2 = Vector2(0.5, 0.3) + +var points = R.state([]) +var show_dates = R.state(false) +var x_axis_label = R.state("X") +var y_axis_label = R.state("Y") + +var minmax = R.computed(func(_arg): + if points.value.size() == 0: + return [Vector2(0, 0), Vector2(0, 0)] + + var min=points.value[0] + var max=points.value[0] + + for point in points.value: + min.x=min(min.x, point.x) + min.y=min(min.y, point.y) + max.x=max(max.x, point.x) + max.y=max(max.y, point.y) + + return [min, max] +) + +var relative_points = R.computed(func(_arg): + var min=minmax.value[0] + var max=minmax.value[1] + + return points.value.map(func(point): + return Vector2(inverse_lerp(min.x, max.x, point.x), inverse_lerp(min.y, max.y, point.y)) + ) +) + +func _ready(): + for i in range(50): + points.value.append(Vector2(i, sin(i / 8.0))) + + R.effect(func(_arg): + mesh.mesh=generate_mesh(relative_points.value) + ) + + R.effect(func(_arg): + x_axis.minmax=Vector2(minmax.value[0].x, minmax.value[1].x) + y_axis.minmax=Vector2(minmax.value[0].y, minmax.value[1].y) + y_axis.label=y_axis_label.value + x_axis.queue_redraw() + y_axis.queue_redraw() + ) + + plane.position = Vector3(size.x / 2, size.y / 2, -0.001) + plane.mesh.size = size + +func generate_mesh(points: Array): + if points.size() < 2: + return null + + var sf = SurfaceTool.new() + + sf.begin(Mesh.PRIMITIVE_TRIANGLE_STRIP) + + for i in range(points.size()): + var prev_point = points[i - 1] if i > 0 else (points[i] + Vector2( - 1, 0)) + var point = points[i] + var next_point = points[i + 1] if i < points.size() - 1 else (points[i] + Vector2(1, 0)) + + var prev_angle = (point - prev_point).angle_to(Vector2(1, 0)) + var next_angle = (next_point - point).angle_to(Vector2(1, 0)) + + var angle = (prev_angle + next_angle) / 2.0 + + var up = Vector2(0, 1).rotated( - angle) + + var p0 = (point + up * WIDTH * 2) * size + var p1 = (point + up * - WIDTH * 2) * size + + sf.add_vertex(Vector3(p1.x, p1.y, DEPTH)) + sf.add_vertex(Vector3(p0.x, p0.y, DEPTH)) + + sf.index() + sf.generate_normals() + + var _mesh = sf.commit() + + return _mesh + \ No newline at end of file diff --git a/app/content/ui/components/line_chart/line_chart.gdshader b/app/content/ui/components/line_chart/line_chart.gdshader new file mode 100644 index 0000000..505c6de --- /dev/null +++ b/app/content/ui/components/line_chart/line_chart.gdshader @@ -0,0 +1,32 @@ +shader_type spatial; + +render_mode cull_disabled, unshaded; + +uniform vec2 steps = vec2(10.0,10.0); +uniform vec2 size= vec2(0.002, 0.002); +uniform vec2 offset = vec2(0.001, 0.001); + +void vertex() { + // Called for every vertex the material is visible on. +} + +void fragment() { + ALBEDO = vec3(0.5, 0.5, 0.5); + vec2 parts = 1.0 / steps; + + float y_mod = mod(UV.y + offset.y, parts.y); + float x_mod = mod(UV.x + offset.x, parts.x); + + if (x_mod < size.x / 2.0 || x_mod > parts.x - size.x / 2.0 || y_mod < size.y || y_mod > parts.y - size.y / 2.0) { + + ALPHA = 0.7; + } else { + ALPHA = 0.3; + } + +} + +//void light() { + // Called for every pixel for every light affecting the material. + // Uncomment to replace the default light processing function with this one. +//} diff --git a/app/content/ui/components/line_chart/line_chart.tscn b/app/content/ui/components/line_chart/line_chart.tscn new file mode 100644 index 0000000..6cf4679 --- /dev/null +++ b/app/content/ui/components/line_chart/line_chart.tscn @@ -0,0 +1,76 @@ +[gd_scene load_steps=11 format=3 uid="uid://cwvykoymlrrel"] + +[ext_resource type="Script" path="res://content/ui/components/line_chart/line_chart.gd" id="1_n7fu8"] +[ext_resource type="PackedScene" uid="uid://bb3shmvedk1oh" path="res://content/ui/components/line_chart/x_axis.tscn" id="2_2ow77"] +[ext_resource type="Shader" path="res://content/ui/components/line_chart/line_chart.gdshader" id="2_ryi4h"] +[ext_resource type="PackedScene" uid="uid://bs5wjs1sf67il" path="res://content/ui/components/line_chart/y_axis.tscn" id="3_48ptx"] + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_20gpn"] +cull_mode = 2 +shading_mode = 0 +albedo_color = Color(0.109804, 0.721569, 0.262745, 1) + +[sub_resource type="ArrayMesh" id="ArrayMesh_raxtd"] +_surfaces = [{ +"aabb": AABB(-0.000587015, -0.000596339, 0.0005, 0.501171, 0.301189, 1e-05), +"format": 34359742465, +"index_count": 100, +"index_data": PackedByteArray(0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9, 0, 10, 0, 11, 0, 12, 0, 13, 0, 14, 0, 15, 0, 16, 0, 17, 0, 18, 0, 19, 0, 20, 0, 21, 0, 22, 0, 23, 0, 24, 0, 25, 0, 26, 0, 27, 0, 28, 0, 29, 0, 30, 0, 31, 0, 32, 0, 33, 0, 34, 0, 35, 0, 36, 0, 37, 0, 38, 0, 39, 0, 40, 0, 41, 0, 42, 0, 43, 0, 44, 0, 45, 0, 46, 0, 47, 0, 48, 0, 49, 0, 50, 0, 51, 0, 52, 0, 53, 0, 54, 0, 55, 0, 56, 0, 57, 0, 58, 0, 59, 0, 60, 0, 61, 0, 62, 0, 63, 0, 64, 0, 65, 0, 66, 0, 67, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 0, 73, 0, 74, 0, 75, 0, 76, 0, 77, 0, 78, 0, 79, 0, 80, 0, 81, 0, 82, 0, 83, 0, 84, 0, 85, 0, 86, 0, 87, 0, 88, 0, 89, 0, 90, 0, 91, 0, 92, 0, 93, 0, 94, 0, 95, 0, 96, 0, 97, 0, 98, 0, 99, 0), +"primitive": 4, +"uv_scale": Vector4(0, 0, 0, 0), +"vertex_count": 100, +"vertex_data": PackedByteArray(229, 225, 25, 58, 64, 41, 25, 62, 111, 18, 3, 58, 229, 225, 25, 186, 236, 39, 26, 62, 111, 18, 3, 58, 129, 190, 54, 60, 26, 163, 44, 62, 111, 18, 3, 58, 137, 159, 23, 60, 148, 5, 45, 62, 111, 18, 3, 58, 255, 241, 174, 60, 49, 129, 63, 62, 111, 18, 3, 58, 11, 108, 159, 60, 208, 229, 63, 62, 111, 18, 3, 58, 119, 64, 1, 61, 133, 198, 81, 62, 111, 18, 3, 58, 33, 12, 243, 60, 223, 46, 82, 62, 111, 18, 3, 58, 120, 5, 43, 61, 238, 41, 99, 62, 111, 18, 3, 58, 146, 88, 35, 61, 223, 151, 99, 62, 111, 18, 3, 58, 5, 199, 84, 61, 177, 101, 115, 62, 111, 18, 3, 58, 137, 46, 77, 61, 133, 219, 115, 62, 111, 18, 3, 58, 99, 131, 126, 61, 74, 28, 129, 62, 111, 18, 3, 58, 173, 9, 119, 61, 159, 92, 129, 62, 111, 18, 3, 58, 188, 27, 148, 61, 98, 179, 135, 62, 111, 18, 3, 58, 142, 118, 144, 61, 30, 251, 135, 62, 111, 18, 3, 58, 181, 238, 168, 61, 96, 93, 141, 62, 111, 18, 3, 58, 85, 111, 165, 61, 60, 175, 141, 62, 111, 18, 3, 58, 245, 180, 189, 61, 17, 3, 146, 62, 111, 18, 3, 58, 215, 116, 186, 61, 197, 98, 146, 62, 111, 18, 3, 58, 117, 99, 210, 61, 86, 145, 149, 62, 111, 18, 3, 58, 25, 146, 207, 61, 125, 3, 150, 62, 111, 18, 3, 58, 153, 231, 230, 61, 103, 250, 151, 62, 111, 18, 3, 58, 181, 217, 228, 61, 122, 130, 152, 62, 111, 18, 3, 58, 204, 49, 251, 61, 57, 56, 153, 62, 111, 18, 3, 58, 68, 91, 250, 61, 47, 210, 153, 62, 111, 18, 3, 58, 203, 172, 7, 62, 242, 75, 153, 62, 111, 18, 3, 58, 157, 255, 7, 62, 66, 231, 153, 62, 111, 18, 3, 58, 77, 206, 17, 62, 216, 53, 152, 62, 111, 18, 3, 58, 253, 195, 18, 62, 202, 192, 152, 62, 111, 18, 3, 58, 198, 12, 28, 62, 88, 244, 149, 62, 111, 18, 3, 58, 100, 107, 29, 62, 72, 105, 150, 62, 111, 18, 3, 58, 210, 97, 38, 62, 87, 140, 146, 62, 111, 18, 3, 58, 56, 252, 39, 62, 56, 238, 146, 62, 111, 18, 3, 58, 196, 195, 48, 62, 189, 10, 142, 62, 111, 18, 3, 58, 40, 128, 50, 62, 51, 94, 142, 62, 111, 18, 3, 58, 157, 44, 59, 62, 12, 130, 136, 62, 111, 18, 3, 58, 47, 253, 60, 62, 243, 202, 136, 62, 111, 18, 3, 58, 68, 153, 69, 62, 245, 8, 130, 62, 111, 18, 3, 58, 104, 118, 71, 62, 38, 74, 130, 62, 111, 18, 3, 58, 36, 8, 80, 62, 145, 115, 117, 62, 111, 18, 3, 58, 106, 237, 81, 62, 163, 234, 117, 62, 111, 18, 3, 58, 98, 120, 90, 62, 14, 100, 101, 62, 111, 18, 3, 58, 12, 99, 92, 62, 232, 210, 101, 62, 111, 18, 3, 58, 137, 233, 100, 62, 247, 35, 84, 62, 111, 18, 3, 58, 197, 215, 102, 62, 243, 140, 84, 62, 111, 18, 3, 58, 86, 91, 111, 62, 123, 248, 65, 62, 111, 18, 3, 58, 218, 75, 113, 62, 127, 93, 66, 62, 111, 18, 3, 58, 158, 205, 121, 62, 92, 42, 47, 62, 111, 18, 3, 58, 114, 191, 123, 62, 8, 141, 47, 62, 111, 18, 3, 58, 40, 32, 130, 62, 211, 4, 28, 62, 111, 18, 3, 58, 80, 25, 131, 62, 158, 102, 28, 62, 111, 18, 3, 58, 178, 89, 135, 62, 102, 212, 8, 62, 111, 18, 3, 58, 182, 82, 136, 62, 179, 54, 9, 62, 111, 18, 3, 58, 111, 147, 140, 62, 111, 203, 235, 61, 111, 18, 3, 58, 233, 139, 141, 62, 237, 147, 236, 61, 111, 18, 3, 58, 104, 205, 145, 62, 169, 8, 199, 61, 111, 18, 3, 58, 226, 196, 146, 62, 45, 216, 199, 61, 111, 18, 3, 58, 172, 7, 151, 62, 5, 243, 163, 61, 111, 18, 3, 58, 142, 253, 151, 62, 47, 205, 164, 61, 111, 18, 3, 58, 90, 66, 156, 62, 46, 22, 131, 61, 111, 18, 3, 58, 208, 53, 157, 62, 111, 255, 131, 61, 111, 18, 3, 58, 164, 125, 161, 62, 93, 233, 73, 61, 111, 18, 3, 58, 118, 109, 162, 62, 116, 229, 75, 61, 111, 18, 3, 58, 230, 185, 166, 62, 132, 11, 20, 61, 111, 18, 3, 58, 36, 164, 167, 62, 106, 64, 22, 61, 111, 18, 3, 58, 204, 247, 171, 62, 132, 205, 202, 60, 111, 18, 3, 58, 46, 217, 172, 62, 181, 210, 207, 60, 111, 18, 3, 58, 162, 56, 177, 62, 171, 195, 122, 60, 111, 18, 3, 58, 74, 11, 178, 62, 173, 59, 131, 60, 111, 18, 3, 58, 238, 126, 182, 62, 17, 0, 2, 60, 111, 18, 3, 58, 238, 55, 183, 62, 21, 238, 15, 60, 111, 18, 3, 58, 30, 207, 187, 62, 8, 226, 52, 59, 111, 18, 3, 58, 174, 90, 188, 62, 94, 116, 119, 59, 111, 18, 3, 58, 156, 45, 193, 62, 117, 90, 239, 184, 111, 18, 3, 58, 32, 111, 193, 62, 240, 85, 137, 58, 111, 18, 3, 58, 75, 150, 198, 62, 166, 83, 28, 186, 111, 18, 3, 58, 97, 121, 198, 62, 166, 83, 28, 58, 111, 18, 3, 58, 35, 250, 203, 62, 36, 221, 174, 58, 111, 18, 3, 58, 121, 136, 203, 62, 44, 76, 30, 59, 111, 18, 3, 58, 184, 79, 209, 62, 104, 169, 183, 59, 111, 18, 3, 58, 214, 165, 208, 62, 228, 155, 213, 59, 111, 18, 3, 58, 82, 153, 214, 62, 185, 121, 70, 60, 111, 18, 3, 58, 44, 207, 213, 62, 37, 254, 82, 60, 111, 18, 3, 58, 238, 219, 219, 62, 247, 255, 169, 60, 111, 18, 3, 58, 128, 255, 218, 62, 1, 82, 175, 60, 111, 18, 3, 58, 202, 26, 225, 62, 1, 144, 0, 61, 111, 18, 3, 58, 148, 51, 224, 62, 245, 224, 2, 61, 111, 18, 3, 58, 153, 87, 230, 62, 73, 165, 51, 61, 111, 18, 3, 58, 181, 105, 229, 62, 231, 181, 53, 61, 111, 18, 3, 58, 55, 147, 235, 62, 131, 119, 109, 61, 111, 18, 3, 58, 7, 161, 234, 62, 7, 89, 111, 61, 111, 18, 3, 58, 28, 206, 240, 62, 40, 145, 150, 61, 111, 18, 3, 58, 20, 217, 239, 62, 189, 112, 151, 61, 111, 18, 3, 58, 134, 8, 246, 62, 143, 212, 184, 61, 111, 18, 3, 58, 154, 17, 245, 62, 216, 167, 185, 61, 111, 18, 3, 58, 154, 66, 251, 62, 171, 253, 220, 61, 111, 18, 3, 58, 118, 74, 250, 62, 144, 200, 221, 61, 111, 18, 3, 58, 75, 38, 0, 63, 1, 240, 0, 62, 111, 18, 3, 58, 107, 179, 255, 62, 76, 239, 1, 62, 111, 18, 3, 58) +}] + +[sub_resource type="ShaderMaterial" id="ShaderMaterial_pexqy"] +render_priority = 0 +shader = ExtResource("2_ryi4h") +shader_parameter/steps = Vector2(10, 10) +shader_parameter/size = Vector2(0.002, 0.002) +shader_parameter/offset = Vector2(0.001, 0.001) + +[sub_resource type="QuadMesh" id="QuadMesh_b7vce"] +size = Vector2(0.5, 0.3) + +[sub_resource type="ViewportTexture" id="ViewportTexture_1fknx"] +viewport_path = NodePath("XAxis/SubViewport") + +[sub_resource type="ViewportTexture" id="ViewportTexture_xdri6"] +viewport_path = NodePath("YAxis/SubViewport") + +[node name="LineChart" type="Node3D"] +script = ExtResource("1_n7fu8") + +[node name="Line" type="MeshInstance3D" parent="."] +material_override = SubResource("StandardMaterial3D_20gpn") +mesh = SubResource("ArrayMesh_raxtd") + +[node name="Plane" type="MeshInstance3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.25, 0.15, -0.001) +material_override = SubResource("ShaderMaterial_pexqy") +mesh = SubResource("QuadMesh_b7vce") + +[node name="XAxis" type="Sprite3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 0.965926, 0.258819, 0, -0.258819, 0.965926, 0, 0, 0) +centered = false +offset = Vector2(0, -400) +pixel_size = 0.0001 +texture = SubResource("ViewportTexture_1fknx") + +[node name="SubViewport" type="SubViewport" parent="XAxis"] +transparent_bg = true +size = Vector2i(5000, 400) + +[node name="XAxis" parent="XAxis/SubViewport" instance=ExtResource("2_2ow77")] + +[node name="YAxis" type="Sprite3D" parent="."] +centered = false +offset = Vector2(-400, 0) +pixel_size = 0.0001 +texture = SubResource("ViewportTexture_xdri6") + +[node name="SubViewport" type="SubViewport" parent="YAxis"] +transparent_bg = true +size = Vector2i(400, 3000) + +[node name="YAxis" parent="YAxis/SubViewport" instance=ExtResource("3_48ptx")] diff --git a/app/content/ui/components/line_chart/x_axis.gd b/app/content/ui/components/line_chart/x_axis.gd new file mode 100644 index 0000000..ae7288d --- /dev/null +++ b/app/content/ui/components/line_chart/x_axis.gd @@ -0,0 +1,47 @@ +@tool +extends Control + +const axil_label_theme = preload ("./axis_label.tres") + +const WIDTH = 5000 +const HEIGHT = 400 + +@onready var axis_labels = $AxisLabels + +var minmax = Vector2(0, 100) +var axis_divisions = R.state(10.0) +var font_size = 80 +var border_size = 10 +var display_dates = true + +func _draw(): + var base_axis = 20 + var dash_width = 30 + var text_pos = base_axis + dash_width + + for child in axis_labels.get_children(): + child.free() + + draw_line(Vector2(0, base_axis), Vector2(WIDTH, base_axis), Color(1, 1, 1), 10) + + var y_steps = WIDTH / axis_divisions.value + + for i in range(y_steps, WIDTH, y_steps): + draw_line(Vector2(i, base_axis + dash_width), Vector2(i, base_axis), Color(1, 1, 1), 5) + + var font = get_theme_default_font() + + var relative_i = inverse_lerp(0, WIDTH, i) + + var text = "" + if display_dates: + text = Time.get_datetime_string_from_unix_time(lerp(minmax.x, minmax.y, relative_i)).substr(11, 5) + else: + text = str(round(lerp(minmax.x, minmax.y, relative_i) * 100) / 100) + + var label = Label.new() + label.text = text + label.label_settings = axil_label_theme + label.position = Vector2(i, text_pos) + + axis_labels.add_child(label) \ No newline at end of file diff --git a/app/content/ui/components/line_chart/x_axis.tscn b/app/content/ui/components/line_chart/x_axis.tscn new file mode 100644 index 0000000..71ae6c4 --- /dev/null +++ b/app/content/ui/components/line_chart/x_axis.tscn @@ -0,0 +1,17 @@ +[gd_scene load_steps=2 format=3 uid="uid://bb3shmvedk1oh"] + +[ext_resource type="Script" path="res://content/ui/components/line_chart/x_axis.gd" id="1_5fiu5"] + +[node name="XAxis" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_5fiu5") + +[node name="AxisLabels" type="Control" parent="."] +anchors_preset = 0 +offset_right = 169.0 +offset_bottom = 111.0 diff --git a/app/content/ui/components/line_chart/y_axis.gd b/app/content/ui/components/line_chart/y_axis.gd new file mode 100644 index 0000000..0e195fe --- /dev/null +++ b/app/content/ui/components/line_chart/y_axis.gd @@ -0,0 +1,33 @@ +@tool +extends Control + +const WIDTH = 400 +const HEIGHT = 3000 + +var minmax = Vector2(0, 100) +var axis_divisions = R.state(10.0) +var label = "" +var font_size = 80 +var border_size = 10 + +func _draw(): + var base_axis = WIDTH - 20 + var dash_width = -30 + var text_pos = base_axis + dash_width + + draw_line(Vector2(base_axis, 0), Vector2(base_axis, HEIGHT), Color(1, 1, 1), 10) + + var x_steps = HEIGHT / axis_divisions.value + + for i in range(x_steps, HEIGHT, x_steps): + draw_line(Vector2(base_axis + dash_width, i), Vector2(base_axis, i), Color(1, 1, 1), 5) + + var font = get_theme_default_font() + + var relative_i = inverse_lerp(HEIGHT, 0, i) + var text = str(round(lerp(minmax.x, minmax.y, relative_i) * 100) / 100) + (" " + label) if label != "" else "" + + var text_size = font.get_string_size(text, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size + border_size) + + draw_string_outline(get_theme_default_font(), Vector2(text_pos - text_size.x, i + text_size.y / 4), text, HORIZONTAL_ALIGNMENT_LEFT, text_size.x, font_size, border_size, Color(0, 0, 0)) + draw_string(get_theme_default_font(), Vector2(text_pos - text_size.x, i + text_size.y / 4), text, HORIZONTAL_ALIGNMENT_LEFT, text_size.x, font_size, Color(1, 1, 1)) \ No newline at end of file diff --git a/app/content/ui/components/line_chart/y_axis.tscn b/app/content/ui/components/line_chart/y_axis.tscn new file mode 100644 index 0000000..53ca4a2 --- /dev/null +++ b/app/content/ui/components/line_chart/y_axis.tscn @@ -0,0 +1,8 @@ +[gd_scene load_steps=2 format=3 uid="uid://bs5wjs1sf67il"] + +[ext_resource type="Script" path="res://content/ui/components/line_chart/y_axis.gd" id="1_e83gd"] + +[node name="YAxis" type="Control"] +layout_mode = 3 +anchors_preset = 0 +script = ExtResource("1_e83gd") diff --git a/app/content/ui/menu/edit/edit_menu.gd b/app/content/ui/menu/edit/edit_menu.gd index 84fa240..7b46d7f 100644 --- a/app/content/ui/menu/edit/edit_menu.gd +++ b/app/content/ui/menu/edit/edit_menu.gd @@ -90,8 +90,10 @@ func render(): previous_page_button.visible = has_prev_page previous_page_button.disabled = !has_prev_page + previous_page_button.body.get_node("CollisionShape3D").disabled = !has_prev_page next_page_button.visible = has_next_page next_page_button.disabled = !has_next_page + next_page_button.body.get_node("CollisionShape3D").disabled = !has_next_page clear_menu() if selected_device == null: diff --git a/app/content/ui/menu/view/view_menu.tscn b/app/content/ui/menu/view/view_menu.tscn index 03cac72..085c2d4 100644 --- a/app/content/ui/menu/view/view_menu.tscn +++ b/app/content/ui/menu/view/view_menu.tscn @@ -62,6 +62,7 @@ horizontal_alignment = 0 [node name="MinSlider" parent="Content" instance=ExtResource("4_d3xhb")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.06, 0.01, 0.2) +value = 0.0 step = 1.0 show_label = true size = Vector3(10, 0.4, 1) @@ -76,6 +77,7 @@ horizontal_alignment = 0 [node name="MaxSlider" parent="Content" instance=ExtResource("4_d3xhb")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.06, 0.01, 0.24) +value = 0.0 step = 1.0 show_label = true size = Vector3(10, 0.4, 1) diff --git a/app/lib/globals/console.gd b/app/lib/globals/console.gd index bfc3199..1574eff 100644 --- a/app/lib/globals/console.gd +++ b/app/lib/globals/console.gd @@ -2,15 +2,19 @@ extends Node const console_scene = preload ("res://content/ui/console.tscn") var _console: Node3D = null +@onready var main = get_node_or_null("/root/Main") func _ready(): - get_node("/root/Main").tree_entered.connect(func(): + if main == null: + return + + main.tree_entered.connect(func(): init_console() ) func init_console(): _console = console_scene.instantiate() - get_node("/root/Main").add_child(_console) + main.add_child(_console) var camera = get_node("/root/Main/XROrigin3D/XRCamera3D") _console.global_position = camera.global_position + camera.global_transform.basis.z * - 1 diff --git a/app/lib/globals/home_api.gd b/app/lib/globals/home_api.gd index fbaa024..763a3d6 100644 --- a/app/lib/globals/home_api.gd +++ b/app/lib/globals/home_api.gd @@ -120,4 +120,12 @@ func get_voice_assistant() -> VoiceAssistant: if api.has_method("get_voice_assistant") == false: return null - return api.get_voice_assistant() \ No newline at end of file + return api.get_voice_assistant() + +func get_history(entity_id, start, end=null): + assert(has_connected(), "Not connected") + + if api.has_method("get_history") == false: + return null + + return await api.get_history(entity_id, start, end) \ No newline at end of file diff --git a/app/lib/home_apis/hass_ws/handlers/history.gd b/app/lib/home_apis/hass_ws/handlers/history.gd new file mode 100644 index 0000000..eb2f5d1 --- /dev/null +++ b/app/lib/home_apis/hass_ws/handlers/history.gd @@ -0,0 +1,35 @@ +const HASS_API = preload ("../hass.gd") + +var api: HASS_API +var integration_exists: bool = false + +func _init(hass: HASS_API): + self.api = hass + +func get_history(entity_id: String, start: String, end=null): + var meta_response = await api.send_request_packet({ + "type": "recorder/get_statistics_metadata", + "statistic_ids": [ + entity_id + ] + }) + + var data_response = await api.send_request_packet({ + "type": "recorder/statistics_during_period", + "start_time": start, + "statistic_ids": [ + entity_id + ], + "period": "5minute", + "types": [ + "state", + "mean" + ] + }) + + return { + "unit": meta_response.payload.result[0]["display_unit_of_measurement"], + "has_mean": meta_response.payload.result[0]["has_mean"], + "unit_class": meta_response.payload.result[0]["unit_class"], + "data": data_response.payload.result[entity_id] + } \ No newline at end of file diff --git a/app/lib/home_apis/hass_ws/hass.gd b/app/lib/home_apis/hass_ws/hass.gd index 675010f..b3c23d2 100644 --- a/app/lib/home_apis/hass_ws/hass.gd +++ b/app/lib/home_apis/hass_ws/hass.gd @@ -3,6 +3,7 @@ extends Node const AuthHandler = preload ("./handlers/auth.gd") const IntegrationHandler = preload ("./handlers/integration.gd") const AssistHandler = preload ("./handlers/assist.gd") +const HistoryHandler = preload ("./handlers/history.gd") signal on_connect() signal on_disconnect() @@ -27,6 +28,7 @@ var packet_callbacks := CallbackMap.new() var auth_handler: AuthHandler var integration_handler: IntegrationHandler var assist_handler: AssistHandler +var history_handler: HistoryHandler func _init(url:=self.url, token:=self.token): self.url = url @@ -35,6 +37,7 @@ func _init(url:=self.url, token:=self.token): auth_handler = AuthHandler.new(self, url, token) integration_handler = IntegrationHandler.new(self) assist_handler = AssistHandler.new(self) + history_handler = HistoryHandler.new(self) devices_template = devices_template.replace("\n", " ").replace("\t", "").replace("\r", " ") connect_ws() @@ -285,3 +288,6 @@ func update_room(room: String): func get_voice_assistant(): return assist_handler + +func get_history(entity_id, start, end=null): + return await history_handler.get_history(entity_id, start, end) \ No newline at end of file diff --git a/app/lib/stores/house.gd b/app/lib/stores/house.gd index 57de618..2f72c3c 100644 --- a/app/lib/stores/house.gd +++ b/app/lib/stores/house.gd @@ -8,15 +8,16 @@ func _init(): self.state = R.store({ ## Type Room - ## name: String - ## corners: Vec2[] - ## height: float + ## name: String + ## corners: Vec2[] + ## height: float "rooms": [], ## Type Entity ## id: String - ## position: Vec3 - ## rotation: Vec3 - ## room: String + ## position: Vec3 + ## rotation: Vec3 + ## room: String + ## interface: String "entities": [], "align_position1": Vector3(), "align_position2": Vector3() diff --git a/app/lib/utils/entity_factory.gd b/app/lib/utils/entity_factory.gd index 3c34cc5..3608632 100644 --- a/app/lib/utils/entity_factory.gd +++ b/app/lib/utils/entity_factory.gd @@ -9,10 +9,13 @@ const MediaPlayer = preload ("res://content/entities/media_player/media_player.t const Camera = preload ("res://content/entities/camera/camera.tscn") const ButtonEntity = preload ("res://content/entities/button/button.tscn") const NumberEntity = preload ("res://content/entities/number/number.tscn") +const LineGraphEntity = preload ("res://content/entities/line_chart/line_chart.tscn") -static func create_entity(id: String): +static func create_entity(id: String, type=null): var entity = null - var type = id.split(".")[0] + + if type == null: + type = id.split(".")[0] match type: "switch": @@ -29,6 +32,8 @@ static func create_entity(id: String): entity = ButtonEntity.instantiate() "number": entity = NumberEntity.instantiate() + "line_chart": + entity = LineGraphEntity.instantiate() _: return null