config_loader.rb 5.48 KB
Newer Older
1
require 'yaml'
2
require 'peak/tnc_port'
3
require 'apex/aprs_kiss'
4
require 'apex/igate_tcp'
5
require 'kiss'
6
require 'aprs_is'
7

8
module Peak
9
    def self.find_config(verbose, config_paths=[])
10
11
        config_file = 'peak.conf'
        rc_file = '.peakrc'
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
        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
29
30
31
32
33
34
35
36
37
38
    
    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)
39
40
41
42
        igate_server = nil
        igate_callsign = nil
        igate_server_port = 14580
        igate_password = nil
43
        igate_echo = 'none'
44
45
46
47
48
49
50
51
52
53
        config.each do |section_name, section_content|
            if section_name.eql?('IGATE')
                return nil unless config_lookup_enforce(section_content, 'server')
                igate_server = section_content['server']

                return nil unless config_lookup_enforce(section_content, 'callsign')
                igate_callsign = section_content['callsign']

                igate_server_port = section_content['server_port'] if section_content.key? 'server_port'

54
55
                igate_echo = section_content['echo'] if section_content.key? 'echo'

56
                return nil unless config_lookup_enforce(section_content, 'password')                
57
                igate_password = section_content['password']
58
59
60
61
62

            end
        end

        igate = nil
63
        port_map = {}
64
        unless igate_server.nil?
65
66
67
            igate = Apex::IGateTcp.new(igate_callsign, igate_password)
            igate.connect(igate_server, igate_server_port)
            port_map['IGATE'] = TncPort.new(igate: igate, echo_frames: igate_echo)
68
69
        end

70
71
72
73
74
        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')
75
                    return nil
76
77
78
79
80
81
                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']
82
                    kiss_tnc = Apex::AprsKiss.new(Kiss::KissSerial.new(com_port, baud))
83
                else
84
                    return nil
85
86
87
88
89
90
91
92
93
94
95
96
                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)
97
                        return nil
98
99
100
101
102
103
                    end
                else
                    kiss_tnc.connect
                end
    
                unless config_lookup_enforce(section_content, 'port_count')
104
                    return nil
105
106
107
108
109
110
111
112
113
                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)
114
                        return nil
115
116
117
118
                    end
                    port_section = config[port_section_name]
                    
                    unless config_lookup_enforce(port_section, 'identifier')
119
                        return nil
120
121
122
123
                    end
                    port_identifier = port_section['identifier']

                    unless config_lookup_enforce(port_section, 'net')
124
                        return nil
125
126
127
128
                    end
                    port_net = port_section['net']

                    unless config_lookup_enforce(port_section, 'tnc_port')
129
                        return nil
130
131
132
                    end
                    tnc_port = port_section['tnc_port']
                    
133
134
135
136
137
                    unless config_lookup_enforce(port_section, 'echo')
                        return nil
                    end
                    echo_state = port_section['echo']
                    
138
                    port_map[port_name] = TncPort.new(kiss_tnc, port_name, port_identifier, port_net, port = tnc_port, echo_frames: echo_state)
139
140
                end
            end
141

142
143
144
145
        end
        
        return port_map
    end
146
end