]> git.basschouten.com Git - openhab-addons.git/blob
c233b0b35469ca1de8aecc1b31366e9ce2105f72
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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     public static final String CALL_TYPE_CALL = "CALL";
30     public static final String CALL_TYPE_CONNECT = "CONNECT";
31     public static final String CALL_TYPE_RING = "RING";
32     public static final String CALL_TYPE_DISCONNECT = "DISCONNECT";
33
34     private final String rawEvent;
35     private final String timestamp;
36     private final String callType;
37     private final String id;
38     private @Nullable String externalNo;
39     private @Nullable String internalNo;
40     private @Nullable String connectionType;
41     private @Nullable String line;
42
43     public CallEvent(String rawEvent) {
44         this.rawEvent = rawEvent;
45
46         String[] fields = rawEvent.split(";");
47         if (fields.length < 4) {
48             throw new IllegalArgumentException("Cannot parse call event: " + rawEvent);
49         }
50
51         timestamp = fields[0];
52         callType = fields[1];
53         id = fields[2];
54
55         switch (callType) {
56             case CALL_TYPE_RING:
57                 externalNo = fields[3];
58                 internalNo = fields[4];
59                 connectionType = fields[5];
60                 break;
61             case CALL_TYPE_CONNECT:
62                 line = fields[3];
63                 if (fields.length > 4) {
64                     externalNo = fields[4];
65                 } else {
66                     externalNo = "Unknown";
67                 }
68                 break;
69             case CALL_TYPE_CALL:
70                 line = fields[3];
71                 internalNo = fields[4];
72                 externalNo = fields[5];
73                 connectionType = fields[6];
74                 break;
75             case CALL_TYPE_DISCONNECT:
76                 // no fields to set
77                 break;
78             default:
79                 throw new IllegalArgumentException("Invalid call type: " + callType);
80         }
81     }
82
83     public @Nullable String getLine() {
84         return line;
85     }
86
87     public String getTimestamp() {
88         return timestamp;
89     }
90
91     public String getCallType() {
92         return callType;
93     }
94
95     public String getId() {
96         return id;
97     }
98
99     public @Nullable String getExternalNo() {
100         return externalNo;
101     }
102
103     public @Nullable String getInternalNo() {
104         return internalNo;
105     }
106
107     public @Nullable String getConnectionType() {
108         return connectionType;
109     }
110
111     public String getRaw() {
112         return rawEvent;
113     }
114
115     @Override
116     public String toString() {
117         return "CallEvent [timestamp=" + timestamp + ", callType=" + callType + ", id=" + id + ", externalNo="
118                 + externalNo + ", internalNo=" + internalNo + ", connectionType=" + connectionType + ", line=" + line
119                 + "]";
120     }
121 }