config_loader.rb 4.26 KB
Newer Older
1
require 'yaml'
2
require 'peak/tnc_port'
3
require 'apex/aprs_kiss'
4
require 'kiss'
5

6
module Peak
7
    def self.find_config(verbose, config_paths=[])
8
9
        config_file = 'peak.conf'
        rc_file = '.peakrc'
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
        cur_path = config_file
        home_path = File.join(Dir.home, rc_file)
        etc_path = File.join('', 'etc', config_file)
        config_paths = [cur_path, home_path, etc_path] + config_paths
        
        if verbose
            puts 'Searching for configuration file in the following locations: ' + config_paths.inspect
        end
        
        config_paths.each do |config_path|
            if File.file?(config_path)
                return YAML::load_file(config_path)
            end
        end
        
        return nil
    end
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    
    def self.config_lookup_enforce(config_map, key)
        unless config_map.key?(key)
            echo_colorized_error('Invalid configuration, could not find an ' + key + ' attribute in section')
            return false
        end
        return true
    end
    
    def self.init_port_map(config)
        port_map = {}
        
        config.each do |section_name, section_content|
            if section_name.start_with?('TNC ')
                tnc_name = section_name.strip.split(' ')[1].strip
                if tnc_name == 'IGATE'
                    echo_colorized_error('IGATE was used as the name for a TNC in the configuration, this name is reserved')
44
                    return nil
45
46
47
48
49
50
                end
            
                kiss_tnc = nil
                if config_lookup_enforce(section_content, 'com_port') and config_lookup_enforce(section_content, 'baud')
                    com_port = section_content['com_port']
                    baud = section_content['baud']
51
                    kiss_tnc = Apex::AprsKiss.new(Kiss::KissSerial.new(com_port, baud))
52
                else
53
                    return nil
54
55
56
57
58
59
60
61
62
63
64
65
                end
                
                if section_content.key?('kiss_init')
                    kiss_init_string = section_content['kiss_init']
                    if kiss_init_string == 'MODE_INIT_W8DED'
                        kiss_tnc.connect(Kiss::MODE_INIT_W8DED)
                    elsif kiss_init_string == 'MODE_INIT_KENWOOD_D710'
                        kiss_tnc.connect(Kiss::MODE_INIT_KENWOOD_D710)
                    elsif kiss_init_string == 'NONE'
                        kiss_tnc.connect
                    else
                        echo_colorized_error('Invalid configuration, value assigned to kiss_init was not recognized: ' + kiss_init_string)
66
                        return nil
67
68
69
70
71
72
                    end
                else
                    kiss_tnc.connect
                end
    
                unless config_lookup_enforce(section_content, 'port_count')
73
                    return nil
74
75
76
77
78
79
80
81
82
                end
                
                port_count = section_content['port_count']
                
                (1..port_count).each do |port|
                    port_name = tnc_name + '-' + port.to_s
                    port_section_name = 'PORT ' + port_name
                    
                    unless config_lookup_enforce(config, port_section_name)
83
                        return nil
84
85
86
87
                    end
                    port_section = config[port_section_name]
                    
                    unless config_lookup_enforce(port_section, 'identifier')
88
                        return nil
89
90
91
92
                    end
                    port_identifier = port_section['identifier']

                    unless config_lookup_enforce(port_section, 'net')
93
                        return nil
94
95
96
97
                    end
                    port_net = port_section['net']

                    unless config_lookup_enforce(port_section, 'tnc_port')
98
                        return nil
99
100
101
                    end
                    tnc_port = port_section['tnc_port']
                    
102
103
104
105
106
107
                    unless config_lookup_enforce(port_section, 'echo')
                        return nil
                    end
                    echo_state = port_section['echo']
                    
                    port_map[port_name] = TncPort.new(kiss_tnc, port_name, port_identifier, port_net, echo_state, tnc_port)
108
109
110
111
112
113
                end
            end
        end
        
        return port_map
    end
114
end