Removed Storage, refactored Configuration data structure(s)

This commit is contained in:
2020-06-08 21:16:15 -05:00
parent f04dfb01e7
commit 287f8db2d8
8 changed files with 200 additions and 99 deletions

View File

@@ -9,7 +9,7 @@ module TAC
end end
def config_changed! def config_changed!
@config[:config][:updated_at] = Time.now @config.config.updated_at = Time.now
@config_changed = true @config_changed = true
end end
@@ -19,7 +19,7 @@ module TAC
def load_config def load_config
if File.exist?(TAC::CONFIG_PATH) if File.exist?(TAC::CONFIG_PATH)
return JSON.parse(File.read( TAC::CONFIG_PATH ), symbolize_names: true) return TAC::Config.new
else else
write_default_config write_default_config
load_config load_config
@@ -32,7 +32,7 @@ module TAC
end end
def save_config def save_config
json = JSON.dump(@config) json = @config.to_json
File.open(TAC::CONFIG_PATH, "w") { |f| f.write json } File.open(TAC::CONFIG_PATH, "w") { |f| f.write json }
@@ -41,7 +41,7 @@ module TAC
def upload_config def upload_config
if @tacnet.connected? if @tacnet.connected?
json = JSON.dump(@config) json = @config.to_json
@tacnet.puts(TAC::TACNET::PacketHandler.packet_upload_config(json)) @tacnet.puts(TAC::TACNET::PacketHandler.packet_upload_config(json))
end end
end end
@@ -66,8 +66,6 @@ module TAC
}, },
data: { data: {
groups: [], groups: [],
actions: [],
values: [],
}, },
} }
) )

141
lib/config.rb Normal file
View File

@@ -0,0 +1,141 @@
module TAC
class Config
attr_reader :config, :groups
def initialize
@config = nil
@groups = nil
parse(File.read(TAC::CONFIG_PATH))
end
def parse(json)
data = JSON.parse(json, symbolize_names: true)
if data.is_a?(Array)
parse_original_config(data)
elsif data.is_a?(Hash) && data.dig(:config, :spec_version) == TAC::CONFIG_SPEC_VERSION
parse_spec_current(data)
else
raise "Unable to load config."
end
end
def parse_original_config(data)
end
def parse_spec_current(data)
@config = Configuration.from_json(data[:config])
@groups = data.dig(:data, :groups).map { |g| Group.from_json(g) }
end
def to_json(*args)
{
config: @config,
data: {
groups: @groups
}
}.to_json(*args)
end
class Configuration
attr_accessor :created_at, :updated_at, :spec_version, :hostname, :port
attr_reader :presets
def initialize(created_at:, updated_at:, spec_version:, hostname:, port:, presets:)
@created_at, @updated_at = created_at, updated_at
@spec_version = spec_version
@hostname, @port = hostname, port
@presets = presets
end
def to_json(*args)
{
created_at: @created_at,
updated_at: @updated_at,
spec_version: @spec_version,
hostname: @hostname,
port: @port,
presets: @presets
}.to_json(*args)
end
def self.from_json(hash)
Configuration.new(
created_at: hash[:created_at], updated_at: hash[:updated_at],
spec_version: hash[:spec_version], hostname: hash[:hostname],
port: hash[:port], presets: hash[:presets].map { |ps| Preset.from_json(ps) }
)
end
end
class Preset
def initialize()
end
def to_json(*args)
end
def self.from_json(hash)
end
end
class Group
attr_accessor :name
attr_reader :actions
def initialize(name:, actions:)
@name = name
@actions = actions
end
def to_json(*args)
{
name: @name,
actions: @actions
}.to_json(*args)
end
def self.from_json(hash)
Group.new(name: hash[:name], actions: hash[:actions].map { |a| Action.from_json(a) })
end
end
class Action
attr_accessor :name, :enabled
attr_reader :variables
def initialize(name:, enabled:, variables:)
@name, @enabled = name, enabled
@variables = variables
end
def to_json(*args)
{
name: @name,
enabled: @enabled,
variables: @variables
}.to_json(*args)
end
def self.from_json(hash)
Action.new(name: hash[:name], enabled: hash[:enabled], variables: hash[:variables].map { |h| Variable.from_json(h) })
end
end
class Variable
attr_accessor :name, :type, :value
def initialize(name:, type:, value:)
@name, @type, @value = name, type, value
end
def to_json(*args)
{
name: @name,
type: @type,
value: @value
}.to_json(*args)
end
def self.from_json(hash)
Variable.new(name: hash[:name], type: hash[:type], value: hash[:value])
end
end
end
end

View File

@@ -4,12 +4,12 @@ module TAC
def build def build
background Gosu::Color::GRAY background Gosu::Color::GRAY
@type = @options[:value].type if @options[:value] @type = @options[:variable].type if @options[:variable]
label "Name" label "Name"
@name_error = label "Error", text_size: 18, color: TAC::Palette::TACNET_CONNECTION_ERROR @name_error = label "Error", text_size: 18, color: TAC::Palette::TACNET_CONNECTION_ERROR
@name_error.hide @name_error.hide
@name = edit_line @options[:value] ? @options[:value].name : "", text_size: 18 @name = edit_line @options[:variable] ? @options[:variable].name : "", text_size: 18
label "Type" label "Type"
@type_error = label "Error", text_size: 18, color: TAC::Palette::TACNET_CONNECTION_ERROR @type_error = label "Error", text_size: 18, color: TAC::Palette::TACNET_CONNECTION_ERROR
@@ -28,7 +28,7 @@ module TAC
label "Value" label "Value"
@value_error = label "Error", text_size: 18, color: TAC::Palette::TACNET_CONNECTION_ERROR @value_error = label "Error", text_size: 18, color: TAC::Palette::TACNET_CONNECTION_ERROR
@value_error.hide @value_error.hide
@value = edit_line @options[:value] ? @options[:value].value : "", text_size: 18 @value = edit_line @options[:variable] ? @options[:variable].value : "", text_size: 18
end end
flow width: 1.0 do flow width: 1.0 do
@@ -36,10 +36,10 @@ module TAC
close close
end end
button @options[:value] ? "Update" : "Add", width: 0.475, text_size: 18 do |b| button @options[:variable] ? "Update" : "Add", width: 0.475, text_size: 18 do |b|
if valid? if valid?
if @options[:value] if @options[:variable]
@options[:callback_method].call(@options[:value], @name.value.strip, @type, @value.value.strip) @options[:callback_method].call(@options[:variable], @name.value.strip, @type, @value.value.strip)
else else
@options[:callback_method].call(@name.value.strip, @type, @value.value.strip) @options[:callback_method].call(@name.value.strip, @type, @value.value.strip)
end end

View File

@@ -18,8 +18,8 @@ module TAC
ACTIONS_PRIMARY = Gosu::Color.new(0xff_4444aa) ACTIONS_PRIMARY = Gosu::Color.new(0xff_4444aa)
ACTIONS_SECONDARY = Gosu::Color.new(0xff_040404) ACTIONS_SECONDARY = Gosu::Color.new(0xff_040404)
VALUES_PRIMARY = Gosu::Color.new(0xff_660066) VARIABLES_PRIMARY = Gosu::Color.new(0xff_660066)
VALUES_SECONDARY = Gosu::Color.new(0xff_440044) VARIABLES_SECONDARY = Gosu::Color.new(0xff_440044)
EDITOR_PRIMARY = Gosu::Color.new(0xff_446688) EDITOR_PRIMARY = Gosu::Color.new(0xff_446688)
EDITOR_SECONDARY = Gosu::Color.new(0xff_224466) EDITOR_SECONDARY = Gosu::Color.new(0xff_224466)

View File

@@ -103,19 +103,19 @@ module TAC
end end
end end
stack width: 0.333, height: 1.0 do stack width: 0.333, height: 1.0 do
background TAC::Palette::VALUES_PRIMARY background TAC::Palette::VARIABLES_PRIMARY
flow do flow do
label "Values" label "Values"
button "+", text_size: 18 do button "+", text_size: 18 do
if @active_action if @active_action
push_state(TAC::Dialog::VariableDialog, title: "Create Value", callback_method: method(:create_value)) push_state(TAC::Dialog::VariableDialog, title: "Create Value", callback_method: method(:create_variable))
else else
push_state(TAC::Dialog::AlertDialog, title: "Error", message: "Unable to create value,\nno action selected.") push_state(TAC::Dialog::AlertDialog, title: "Error", message: "Unable to create variable,\nno action selected.")
end end
end end
end end
@values_list = stack width: 1.0 do @variables_list = stack width: 1.0 do
end end
end end
end end
@@ -156,29 +156,21 @@ module TAC
end end
def create_group(name) def create_group(name)
window.backend.config[:data][:groups] << {id: rand(100), name: name} window.backend.config.groups << TAC::Config::Group.new(name: name, actions: [])
window.backend.config_changed! window.backend.config_changed!
populate_groups_list populate_groups_list
end end
def update_group(group_struct, name) def update_group(group, name)
group = window.backend.config[:data][:groups].find { |g| g[:id] == group_struct.id } group.name = name
group[:name] = name
window.backend.config_changed! window.backend.config_changed!
populate_groups_list populate_groups_list
end end
def delete_group(group_struct) def delete_group(group)
group = window.backend.config[:data][:groups].find { |a| a[:id] == group_struct.id } window.backend.config.groups.delete(group)
window.backend.config[:data][:groups].delete(group)
window.backend.config[:data][:actions].select { |a| a[:group_id] == group[:id] }.each do |action|
window.backend.config[:data][:actions].delete(action)
window.backend.config[:data][:values].delete_if { |v| v[:action_id] == action[:id] }
end
window.backend.config_changed! window.backend.config_changed!
@active_group = nil @active_group = nil
@@ -186,68 +178,62 @@ module TAC
@active_action = nil @active_action = nil
@active_action_label.value = "" @active_action_label.value = ""
@actions_list.clear @actions_list.clear
@values_list.clear @variables_list.clear
populate_groups_list populate_groups_list
end end
def create_action(name) def create_action(name)
window.backend.config[:data][:actions] << {id: rand(100), group_id: @active_group.id, name: name, enabled: true} @active_group.actions << TAC::Config::Action.new(name: name, enabled: true, variables: [])
window.backend.config_changed! window.backend.config_changed!
populate_actions_list(@active_group.id) populate_actions_list(@active_group)
end end
def update_action(action_struct, name) def update_action(action, name)
action = window.backend.config[:data][:actions].find { |a| a[:id] == action_struct.id } action.name = name
action[:name] = name
window.backend.config_changed! window.backend.config_changed!
populate_actions_list(@active_group.id) populate_actions_list(@active_group)
end end
def delete_action(action_struct) def delete_action(action)
action = window.backend.config[:data][:actions].find { |a| a[:id] == actions_struct.id } @active_group.actions.delete(action)
window.backend.config[:data][:actions].delete(action)
window.backend.config[:data][:values].delete_if { |v| v[:action_id] == action[:id] }
window.backend.config_changed! window.backend.config_changed!
@active_action = nil @active_action = nil
@active_action_label.value = "" @active_action_label.value = ""
@values_list.clear @variables_list.clear
populate_actions_list(@active_group.id) populate_actions_list(@active_group)
end end
def create_value(name, type, value) def create_variable(name, type, value)
window.backend.config[:data][:values] << {id: rand(100), action_id: @active_action.id, name: name, type: type, value: value} @active_action.variables << TAC::Config::Variable.new(name: name, type: type, value: value)
window.backend.config_changed! window.backend.config_changed!
populate_values_list(@active_action.id) populate_variables_list(@active_action)
end end
def update_value(value_struct, name, type, value) def update_variable(variable, name, type, value)
_v = window.backend.config[:data][:values].find { |v| v[:id] == value_struct.id } variable.name = name
_v[:name] = name variable.type = type
_v[:type] = type variable.value = value
_v[:value] = value
window.backend.config_changed! window.backend.config_changed!
populate_values_list(@active_action.id) populate_variables_list(@active_action)
end end
def delete_value(value_struct) def delete_variable(variable)
_v = window.backend.config[:data][:values].find { |v| v[:id] == value_struct.id } @active_action.variables.delete(variable)
window.backend.config[:data][:values].delete(_v)
window.backend.config_changed! window.backend.config_changed!
populate_values_list(@active_action.id) populate_variables_list(@active_action)
end end
def populate_groups_list def populate_groups_list
groups = TAC::Storage.groups groups = window.backend.config.groups
@groups_list.clear do @groups_list.clear do
groups.each do |group| groups.each do |group|
@@ -258,23 +244,23 @@ module TAC
@active_action = nil @active_action = nil
@active_action_label.value = "" @active_action_label.value = ""
populate_actions_list(group.id) populate_actions_list(group)
@values_list.clear @variables_list.clear
end end
button "E", text_size: 18 do button "E", text_size: 18 do
push_state(Dialog::NamePromptDialog, title: "Rename Group", renaming: group, callback_method: method(:update_group)) push_state(Dialog::NamePromptDialog, title: "Rename Group", renaming: group, callback_method: method(:update_group))
end end
button "D", text_size: 18 do button "D", text_size: 18 do
push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete group and all\nof its actions and values?", callback_method: proc { delete_group(group) }) push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete group and all\nof its actions and variables?", callback_method: proc { delete_group(group) })
end end
end end
end end
end end
end end
def populate_actions_list(group_id) def populate_actions_list(group)
actions = TAC::Storage.actions(group_id) actions = group.actions
@actions_list.clear do @actions_list.clear do
actions.each do |action| actions.each do |action|
@@ -283,35 +269,35 @@ module TAC
@active_action = action @active_action = action
@active_action_label.value = action.name @active_action_label.value = action.name
populate_values_list(action.id) populate_variables_list(action)
end end
button "E", text_size: 18 do button "E", text_size: 18 do
push_state(Dialog::NamePromptDialog, title: "Rename Action", renaming: action, callback_method: method(:update_action)) push_state(Dialog::NamePromptDialog, title: "Rename Action", renaming: action, callback_method: method(:update_action))
end end
button "D", text_size: 18 do button "D", text_size: 18 do
push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete action and all\nof its values?", callback_method: proc { delete_action(action) }) push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete action and all\nof its variables?", callback_method: proc { delete_action(action) })
end end
end end
end end
end end
end end
def populate_values_list(action_id) def populate_variables_list(action)
values = TAC::Storage.values(action_id) variables = action.variables
@values_list.clear do @variables_list.clear do
values.each_with_index do |value, i| variables.each_with_index do |variable, i|
flow width: 1.0 do flow width: 1.0 do
background TAC::Palette::VALUES_SECONDARY if i.odd? background TAC::Palette::VARIABLES_SECONDARY if i.odd?
label value.name, text_size: 18, width: 0.855 label variable.name, text_size: 18, width: 0.855
button "E", text_size: 18 do button "E", text_size: 18 do
push_state(Dialog::VariableDialog, title: "Edit Variable", value: value, callback_method: method(:update_value)) push_state(Dialog::VariableDialog, title: "Edit Variable", variable: variable, callback_method: method(:update_variable))
end end
button "D", text_size: 18 do button "D", text_size: 18 do
push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete value?", callback_method: proc { delete_value(value) }) push_state(Dialog::ConfirmDialog, title: "Are you sure?", message: "Delete variable?", callback_method: proc { delete_variable(variable) })
end end
end end
end end

View File

@@ -1,23 +0,0 @@
module TAC
module Storage
Group = Struct.new(:id, :name)
Action = Struct.new(:id, :group_id, :name, :enabled)
Value = Struct.new(:id, :action_id, :name, :type, :value)
def self.groups
$window.backend.config[:data][:groups].map do |g|
Group.new(g[:id], g[:name])
end
end
def self.actions(group_id)
$window.backend.config[:data][:actions].map{ |a| Action.new(a[:id], a[:group_id], a[:name], a[:enabled]) }.select { |a| a.group_id == group_id }
end
def self.values(action_id)
types = [:double, :float, :string, :boolean, :integer]
$window.backend.config[:data][:values].map { |v| Value.new(v[:id], v[:action_id], v[:name], v[:type].to_sym, v[:value]) }.select { |a| a.action_id == action_id }
end
end
end

View File

@@ -44,7 +44,6 @@ module TAC
elsif @connection && @connection.client && @connection.client.socket_error? elsif @connection && @connection.client && @connection.client.socket_error?
"<b>Status:</b> #{_status}\n\n#{@connection.client.last_socket_error.to_s.chars.each_slice(32).to_a.map { |c| c.join }.join("\n")}" "<b>Status:</b> #{_status}\n\n#{@connection.client.last_socket_error.to_s.chars.each_slice(32).to_a.map { |c| c.join }.join("\n")}"
else else
pp client
"<b>Status:</b> #{_status}" "<b>Status:</b> #{_status}"
end end
end end

View File

@@ -7,8 +7,8 @@ require_relative "lib/tac"
require_relative "lib/palette" require_relative "lib/palette"
require_relative "lib/window" require_relative "lib/window"
require_relative "lib/version" require_relative "lib/version"
require_relative "lib/storage"
require_relative "lib/backend" require_relative "lib/backend"
require_relative "lib/config"
require_relative "lib/states/boot" require_relative "lib/states/boot"
require_relative "lib/states/editor" require_relative "lib/states/editor"
require_relative "lib/states/manage_presets" require_relative "lib/states/manage_presets"