1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
107
108
// ------------------------------------------------------------------------------
// Copyright 2018 Uwe Arzt, mail@uwe-arzt.de
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------------

use knx_rs::address::Address;

use std::str::FromStr;

use std::collections::HashMap;

use std::io::{BufRead, BufReader};
use std::fs::File;

pub struct Ets {
    project: String,
    maingroup_name: HashMap<u8, String>,
    middlegroup_name: HashMap<(u8, u8), String>,
    address_name: HashMap<Address, String>,
}

// ------------------------------------------------------------------------------
impl Ets {
    pub fn new(opc_file: &str) -> Ets {
        let mut ets = Ets {
            project: String::new(),
            maingroup_name: HashMap::new(),
            middlegroup_name: HashMap::new(),
            address_name: HashMap::new(),
        };
        ets.ets_read(opc_file);
        ets
    }
    fn ets_read(&mut self, opc_file: &str) {
        let file = File::open(opc_file).unwrap();
        let mut buf = BufReader::new(file);

        let _ = buf.read_line(&mut self.project);
        self.project = self.project.trim().to_string();

        for line in buf.lines() {
            Ets::parse_ets_opc_line(self, &line.unwrap());
        }
    }
    fn parse_ets_opc_line(&mut self, line: &str) {
        let (main, middle, address, name, _eis) = parse_line(line).unwrap().1;
        let address = Address::from_str(address).unwrap();
        // println!("{}, {}", address, name);
        self.maingroup_name.insert(address.main(), main.to_string());
        self.middlegroup_name
            .insert(address.middle(), middle.to_string());
        self.address_name.insert(address, name.to_string());
    }

    pub fn project_string(&self) -> &str {
        &self.project
    }
    pub fn main_string(&self, main: u8) -> &str {
        self.maingroup_name.get(&main).unwrap()
    }
    pub fn middle_string(&self, middle: (u8, u8)) -> &str {
        self.middlegroup_name.get(&middle).unwrap()
    }
    pub fn address_string(&self, address: &Address) -> &str {
        self.address_name.get(&address).unwrap()
    }

    pub fn print(&self) {
        println!("-----------------------------");
        println!("project: {}", self.project);
        println!("-----------------------------");
        for (maingroup, ref maingroup_string) in self.maingroup_name.iter() {
            println!("{}:\t{}", maingroup, maingroup_string); 
        }
        println!("-----------------------------");
        for (&(maingroup, middlegroup), ref middlegroup_string) in self.middlegroup_name.iter() {
            println!("{}/{}:\t{}", maingroup, middlegroup, middlegroup_string); 
        }
        println!("-----------------------------");
        for (address, ref address_string) in self.address_name.iter() {
            println!("{}:\t{}", address, address_string); 
        }
        println!("-----------------------------");
    }
}

// ------------------------------------------------------------------------------
named!(parse_line<&str, (&str, &str, &str, &str, &str)>,
    do_parse!(
        main: take_until_and_consume!(".") >>
        middle: take_until_and_consume!(".") >>
        address: take_until_and_consume!("\t") >>
        name: take_until_and_consume!("\t") >>
        eis: take_until_and_consume!("\t") >>
        (main, middle, address, name, eis)
    )
);