immersive-home/app/content/entities/camera/camera.gd

62 lines
1.3 KiB
GDScript3
Raw Normal View History

2024-01-27 16:13:43 +02:00
extends Entity
const Entity = preload ("../entity.gd")
2023-12-10 00:47:17 +02:00
@export var view_width = 0.15
@onready var view = $View
@onready var http_request = $HTTPRequest
2023-12-10 02:48:39 +02:00
@onready var mesh = $MeshInstance3D
2023-12-10 00:47:17 +02:00
# Called when the node enters the scene tree for the first time.
func _ready():
2024-01-27 16:13:43 +02:00
super()
icon.value = "photo_camera"
2023-12-10 00:47:17 +02:00
var stateInfo = await HomeApi.get_state(entity_id)
set_state(stateInfo)
await HomeApi.watch_state(entity_id, func(new_state):
set_state(new_state)
)
func set_state(stateInfo):
if stateInfo == null:
view.texture = null
2023-12-10 02:48:39 +02:00
mesh.visible = true
2023-12-10 00:47:17 +02:00
return
var state = stateInfo["state"]
if state == "unavailable":
view.texture = null
2023-12-10 02:48:39 +02:00
mesh.visible = true
2023-12-10 00:47:17 +02:00
return
if stateInfo["attributes"].has("entity_picture"):
load_image(stateInfo["attributes"]["entity_picture"])
func load_image(url: String):
http_request.request("http://192.168.33.33:8123" + url)
var result = await http_request.request_completed
if result[0] != HTTPRequest.RESULT_SUCCESS:
print("Error loading image: ", result[0], " ", result[1])
return
var image = Image.new()
var error = image.load_png_from_buffer(result[3])
var pixel_size = view_width / image.get_size().x
if error != OK:
print("Error loading image: ", error)
return
var texture = ImageTexture.create_from_image(image)
view.texture = texture
view.pixel_size = pixel_size
2024-01-25 17:29:33 +02:00
mesh.visible = false