Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Graphs #124

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions app/addons/debug_draw_3d/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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)
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)
```

```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);

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);
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);
}
```

Expand All @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions app/content/entities/line_chart/line_chart.gd
Original file line number Diff line number Diff line change
@@ -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"
33 changes: 33 additions & 0 deletions app/content/entities/line_chart/line_chart.tscn
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/content/entities/number/number.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
20 changes: 20 additions & 0 deletions app/content/entities/sensor/sensor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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

Expand Down
10 changes: 9 additions & 1 deletion app/content/entities/sensor/sensor.tscn
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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="."]
2 changes: 1 addition & 1 deletion app/content/main.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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="."]
Expand Down
15 changes: 9 additions & 6 deletions app/content/system/house/house.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
2 changes: 1 addition & 1 deletion app/content/ui/components/button/button.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions app/content/ui/components/line_chart/axis_label.tres
Original file line number Diff line number Diff line change
@@ -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)
Loading