]> git.basschouten.com Git - openhab-addons.git/blob
3c82b67908ff4e171f185a9199b31987db2fcee5
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.avmfritz.internal.callmonitor;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 /**
19  * Call Events received from a fritzbox.
20  *
21  * 12.07.20 09:11:30;RING;0;0171123456;888888;SIP2;
22  * 12.07.20 09:13:40;DISCONNECT;0;0;
23  *
24  * @author Kai Kreuzer - Initial contribution
25  */
26 @NonNullByDefault
27 public class CallEvent {
28
29     private final String rawEvent;
30     private final String timestamp;
31     private final String callType;
32     private final String id;
33     private @Nullable String externalNo;
34     private @Nullable String internalNo;
35     private @Nullable String connectionType;
36     private @Nullable String line;
37
38     public CallEvent(String rawEvent) {
39         this.rawEvent = rawEvent;
40
41         String[] fields = rawEvent.split(";");
42         if (fields.length < 4) {
43             throw new IllegalArgumentException("Cannot parse call event: " + rawEvent);
44         }
45
46         timestamp = fields[0];
47         callType = fields[1];
48         id = fields[2];
49
50         if (callType.equals("RING")) {
51             externalNo = fields[3];
52             internalNo = fields[4];
53             connectionType = fields[5];
54         } else if (callType.equals("CONNECT")) {
55             line = fields[3];
56             if (fields.length > 4) {
57                 externalNo = fields[4];
58             } else {
59                 externalNo = "Unknown";
60             }
61         } else if (callType.equals("CALL")) {
62             line = fields[3];
63             internalNo = fields[4];
64             externalNo = fields[5];
65             connectionType = fields[6];
66         } else if (callType.equals("DISCONNECT")) {
67             // no fields to set
68         } else {
69             throw new IllegalArgumentException("Invalid call type: " + callType);
70         }
71     }
72
73     public @Nullable String getLine() {
74         return line;
75     }
76
77     public String getTimestamp() {
78         return timestamp;
79     }
80
81     public String getCallType() {
82         return callType;
83     }
84
85     public String getId() {
86         return id;
87     }
88
89     public @Nullable String getExternalNo() {
90         return externalNo;
91     }
92
93     public @Nullable String getInternalNo() {
94         return internalNo;
95     }
96
97     public @Nullable String getConnectionType() {
98         return connectionType;
99     }
100
101     public String getRaw() {
102         return rawEvent;
103     }
104
105     @Override
106     public String toString() {
107         return "CallEvent [timestamp=" + timestamp + ", callType=" + callType + ", id=" + id + ", externalNo="
108                 + externalNo + ", internalNo=" + internalNo + ", connectionType=" + connectionType + ", line=" + line
109                 + "]";
110     }
111 }