Доработка Для отправки УКП сообщений

This commit is contained in:
TotMaxim
2025-11-14 16:35:33 +03:00
parent c678b4a22a
commit f8fbb3bc4e
5 changed files with 1076 additions and 9 deletions

139
main.gd
View File

@@ -1,12 +1,27 @@
extends Control
const BUFFER_SIZE = 32
var dictionary_yau07: Dictionary
var udp_broadcast: UDPBroadcast
var dictionary_sockets_bit: Dictionary ## Байт 8 для отправки о состоянии ячеек
func _ready() -> void:
var device_yau07: Array = get_tree().get_nodes_in_group('device-addr')
for device in device_yau07:
device.connect('toggled', on_modify_device.bind(device.get_meta('device')))
var sockets: Array = get_tree().get_nodes_in_group('socket')
for soc in sockets:
soc.connect('toggled', _on_modify_sockets.bind(soc.get_meta('bit'), soc.get_meta('device'))) ## Мета bit от 0 до 3, мета ПРД
var power_ukp: Array = get_tree().get_nodes_in_group('power_ukp')
for socket_power in power_ukp:
socket_power.connect('text_submitted', _on_ukp_submitted.bind(socket_power.get_meta('device')))
var temperature_ukp: Array = get_tree().get_nodes_in_group('temperature_ukp')
for socket_temp in temperature_ukp:
socket_temp.connect('text_submitted', _on_ukp_submitted.bind(socket_temp.get_meta('device')))
func _exit_tree():
for device in dictionary_yau07:
@@ -16,9 +31,129 @@ func _exit_tree():
func on_modify_device(toggled: bool, name_device: String) -> void:
if toggled:
var udp_broadcast := UDPBroadcast.new(name_device)
udp_broadcast = UDPBroadcast.new(name_device)
udp_broadcast.add_timer_to_scene(self)
udp_broadcast.start_broadcast()
dictionary_yau07[name_device] = udp_broadcast
else:
dictionary_yau07[name_device].close_udp_unit()
func _process(_delta: float) -> void:
for device in dictionary_yau07:
if dictionary_yau07[device]:
dictionary_yau07[device].poll_receive()
static func _set_bit(bits: int, index: int, val: bool) -> int:
return bits | (1 << index) if val else bits & ~(1 << index)
## Функция для установки статусов ячеек на связи (УГ, ЭМС-Г, УКП)
func _on_modify_sockets(toggled: bool, bit: int, device: String)->void:
if dictionary_yau07.has(device):
var udp_unit = dictionary_yau07[device]
udp_unit.sockets_status_bit = _set_bit(udp_unit.sockets_status_bit, bit, toggled)
## Функция для установки значений в ячейках УКП
func _on_ukp_submitted(_new_text: String, device: String) -> void:
var prep_temperature_ukp1_array: Array = _on_text_temperature_changed(device, 1)
var prep_temperature_ukp2_array: Array = _on_text_temperature_changed(device, 2)
var prep_power_ukp1_array: Array = _on_text_power_changed(device, 1)
var prep_power_ukp2_array: Array = _on_text_power_changed(device, 2)
var combine_data_ukp1: Array
combine_data_ukp1 = _combine_arrays(combine_data_ukp1, prep_temperature_ukp1_array, prep_power_ukp1_array)
var combine_data_ukp2: Array
combine_data_ukp2 = _combine_arrays(combine_data_ukp2, prep_temperature_ukp2_array, prep_power_ukp2_array)
_form_buffers(device,combine_data_ukp1, 1, dictionary_yau07)
_form_buffers(device,combine_data_ukp2, 2, dictionary_yau07)
func _on_text_temperature_changed(device: String, number_device: int) -> Array:
const CONST_MIN_TEMP: int = -25
const MAXIMUM_CODE_ADC: int = 3796
const MINIMUM_CODE_AD: int = 2660
const TEMP: float = 115.0 / (MAXIMUM_CODE_ADC - MINIMUM_CODE_AD)
var current_change: Array
for prd in get_tree().get_nodes_in_group('prd'):
if device == prd.get_meta('yau07'):
for temperature_node in prd.get_tree().get_nodes_in_group('temperature_ukp'):
if prd.is_ancestor_of(temperature_node):
current_change.append(temperature_node)
break
var temp_arr: Array = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
if number_device == 1:
for i in 6:
if not current_change[i].text == null:
var value = _safe_convert_to_int(current_change[i].text)
var normal_temp: int = MAXIMUM_CODE_ADC - (value - CONST_MIN_TEMP - 3) / TEMP
temp_arr[i] = normal_temp
elif number_device == 2:
for i in 6:
if not current_change[i+6].text == null:
var value = _safe_convert_to_int(current_change[i+6].text)
var normal_temp: int = MAXIMUM_CODE_ADC - (value - CONST_MIN_TEMP - 3) / TEMP
temp_arr[i] = normal_temp
return temp_arr
func _on_text_power_changed(device: String, number_device: int) -> Array:
var current_change: Array
for prd in get_tree().get_nodes_in_group('prd'):
if device == prd.get_meta('yau07'):
for power_node in prd.get_tree().get_nodes_in_group('power_ukp'):
if prd.is_ancestor_of(power_node):
current_change.append(power_node)
break
var power_arr: Array = [0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]
if number_device == 1:
for i in 6:
if not current_change[i].text == null:
var value = _safe_convert_to_int(current_change[i].text)
power_arr[i] = value
elif number_device == 2:
for i in 6:
if not current_change[i+6].text == null:
var value = _safe_convert_to_int(current_change[i+6].text)
power_arr[i] = value
return power_arr
#_update_buffers(power_arr_1, device)
static func _safe_convert_to_int(text: String) -> int:
text = text.strip_edges()
if text.is_valid_int():
return int(text)
elif text.begins_with("0x") and text.substr(2).is_valid_hex_number():
return text.hex_to_int()
else:
return 0
static func _combine_arrays(ukp_data: Array, power_arr: Array, temp_arr: Array)-> Array:
for i in temp_arr.size():
ukp_data.append(temp_arr[i])
for i in power_arr.size():
ukp_data.append(power_arr[i])
return ukp_data
static func _form_buffers(device: String, ukp_data: Array, number_device: int, dic_yau07: Dictionary):
var buffer: PackedByteArray = PackedByteArray()
buffer.resize(BUFFER_SIZE)
for i in ukp_data.size():
buffer.encode_u16(2 * i, ukp_data[i])
if dic_yau07.has(device):
var udp_unit = dic_yau07[device]
if number_device == 1:
udp_unit.ukp0_data = buffer
elif number_device == 2:
udp_unit.ukp1_data = buffer