config_loader.rb 4.03 KB
Newer Older
1
require 'yaml'
2
require 'peak/tnc_port'
3

4
module Peak
5
    def self.find_config(verbose, config_paths=[])
6
7
        config_file = 'peak.conf'
        rc_file = '.peakrc'
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
        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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    
    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')
                    return false
                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']
49
                    kiss_tnc = Apex::AprsKiss.new(Kiss::KissSerial.new(com_port, baud))
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                else
                    return false
                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)
                        return false
                    end
                else
                    kiss_tnc.connect
                end
    
                unless config_lookup_enforce(section_content, 'port_count')
                    return false
                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)
                        return false
                    end
                    port_section = config[port_section_name]
                    
                    unless config_lookup_enforce(port_section, 'identifier')
                        return false
                    end
                    port_identifier = port_section['identifier']

                    unless config_lookup_enforce(port_section, 'net')
                        return false
                    end
                    port_net = port_section['net']

                    unless config_lookup_enforce(port_section, 'tnc_port')
                        return false
                    end
                    tnc_port = port_section['tnc_port']
                    
                    port_map[port_name] = TncPort.new(kiss_tnc, port_name, port_identifier, port_net, true, tnc_port)
                end
            end
        end
        
        return port_map
    end
107
end