Bunch of changes
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
extends Node
|
||||
class_name DataRecorderNode
|
||||
|
||||
var MAX_SENSOR_DATA_SIZE = 524288000
|
||||
|
||||
var cached_data = []
|
||||
|
||||
var thread: Thread
|
||||
var mutex: Mutex
|
||||
var exit_thread = false
|
||||
|
||||
var timer = 0
|
||||
var timer_target = 15
|
||||
|
||||
var target_dir = "user://sensor_data/"
|
||||
|
||||
# Called when the node enters the scene tree for the first time.
|
||||
func _ready() -> void:
|
||||
DataSignals.raw_data_received.connect(_on_raw_data_received)
|
||||
thread = Thread.new()
|
||||
mutex = Mutex.new()
|
||||
thread.start(_thread_func)
|
||||
DirAccess.make_dir_absolute(target_dir)
|
||||
|
||||
func _exit_tree() -> void:
|
||||
mutex.lock()
|
||||
exit_thread = true
|
||||
mutex.unlock()
|
||||
thread.wait_to_finish()
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
mutex.lock()
|
||||
timer += delta
|
||||
mutex.unlock()
|
||||
|
||||
func _on_raw_data_received(source_ip: String, data_type: String, timestamp_list: Array, data_dict: Dictionary):
|
||||
cached_data.append([source_ip, data_type, timestamp_list, data_dict])
|
||||
|
||||
func _thread_func():
|
||||
while true:
|
||||
mutex.lock()
|
||||
var should_exit = exit_thread
|
||||
mutex.unlock()
|
||||
if should_exit:
|
||||
break
|
||||
mutex.lock()
|
||||
var should_write_data = (timer >= timer_target)
|
||||
mutex.unlock()
|
||||
if not should_write_data:
|
||||
continue
|
||||
mutex.lock()
|
||||
timer = 0
|
||||
var data_copy = null
|
||||
if cached_data.size() > 0:
|
||||
data_copy = cached_data.duplicate(true)
|
||||
cached_data.clear()
|
||||
mutex.unlock()
|
||||
if data_copy != null:
|
||||
_write_data(data_copy)
|
||||
_delete_old_data()
|
||||
|
||||
func _write_data(data_to_write: Array):
|
||||
var current_file_path = target_dir + str(Time.get_unix_time_from_system()) + "_sensor_data.csv"
|
||||
var file = FileAccess.open(current_file_path, FileAccess.WRITE)
|
||||
for data in data_to_write:
|
||||
var line = str(data[0]) + ',' + str(data[1]) + ','
|
||||
for time_data in data[2]:
|
||||
line += str(time_data) + ','
|
||||
for value in data[3]:
|
||||
line += str(value) + ','
|
||||
file.store_line(line)
|
||||
file.close()
|
||||
|
||||
func _delete_old_data():
|
||||
var files: Array = []
|
||||
var dir = DirAccess.open(target_dir)
|
||||
dir.list_dir_begin()
|
||||
while true:
|
||||
var file_name = dir.get_next()
|
||||
if file_name == "":
|
||||
break
|
||||
elif file_name.begins_with("."):
|
||||
continue
|
||||
files.append(file_name)
|
||||
dir.list_dir_end()
|
||||
files.sort()
|
||||
var cutoff_idx = 0
|
||||
var file_size_sum = 0
|
||||
for idx in range(files.size(), 0, -1):
|
||||
file_size_sum += FileAccess.get_size(target_dir + files[idx-1])
|
||||
if file_size_sum >= MAX_SENSOR_DATA_SIZE:
|
||||
cutoff_idx = idx
|
||||
for i in range(cutoff_idx):
|
||||
DirAccess.remove_absolute(target_dir+files[i])
|
||||
|
||||
Reference in New Issue
Block a user