extends BaseSensorNode
class_name AccelSensorNode


var accel_vector: Vector3 = Vector3.ZERO
var linear_accel_vector: Vector3 = Vector3.ZERO
var g_accel_vector: Vector3 = Vector3.ZERO

var DOWN = Vector2(0, -1.)


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	super._ready()
	DataSignals.accelerometer_data_received.connect(_on_data_received)

func _on_data_received(source_ip: String, timestamp_list: Array, accel: Array, linear_accel: Array, g_accel: Array):
	if source_ip != ip_address:
		return
	var msg_timestamp = timestamp_list[0] * 3600 + timestamp_list[1] * 60 + timestamp_list[2] + timestamp_list[3]
	if not Constants.is_timestamp_newer(timestamp, msg_timestamp):
		return
	timestamp = msg_timestamp
	Signals.linear_acceleration_updated.emit(ip_address, linear_accel)
	var x_z = Vector2(g_accel[0], g_accel[2]).normalized()
	var y_z = Vector2(g_accel[1], g_accel[2]).normalized()
	var pitch = DOWN.angle_to(x_z)
	var roll = y_z.angle_to(DOWN)
	SensorSignals.pitch_updated.emit(ip_address, pitch)
	SensorSignals.roll_updated.emit(ip_address, roll)
