]> git.basschouten.com Git - openhab-addons.git/blob
5b77fa7a8f8dd82de46be3be28f6eff4da770a71
[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.plex.internal.dto;
14
15 import java.math.BigDecimal;
16 import java.math.MathContext;
17 import java.math.RoundingMode;
18 import java.util.Date;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link PlexSession} is the class used internally by the PlexPlayer things
25  * to keep state of updates from the Bridge.
26  *
27  * @author Brian Homeyer - Initial contribution
28  * @author Aron Beurskens - Binding development
29  */
30 public class PlexSession {
31     private String title;
32     private String thumb;
33     private String art;
34     private long viewOffset;
35     private String type;
36     private BigDecimal progress;
37     private String machineIdentifier;
38     private PlexPlayerState state;
39     private String local;
40     private long duration;
41     private Date endTime;
42     private String sessionKey = "";
43     private Integer userId;
44     private String userTitle = "";
45
46     private final Logger logger = LoggerFactory.getLogger(PlexSession.class);
47
48     public String getSessionKey() {
49         return sessionKey;
50     }
51
52     public void setSessionKey(String sessionKey) {
53         this.sessionKey = sessionKey;
54     }
55
56     public String getTitle() {
57         return title;
58     }
59
60     public Date getEndTime() {
61         return endTime;
62     }
63
64     public void setEndTime(Date endTime) {
65         this.endTime = endTime;
66     }
67
68     public void setTitle(String title) {
69         this.title = title;
70     }
71
72     public String getThumb() {
73         return thumb;
74     }
75
76     public void setThumb(String thumb) {
77         this.thumb = thumb;
78     }
79
80     public long getViewOffset() {
81         return viewOffset;
82     }
83
84     public void setViewOffset(long viewOffset) {
85         this.viewOffset = viewOffset;
86         updateProgress();
87     }
88
89     public String getType() {
90         return type;
91     }
92
93     public void setType(String type) {
94         this.type = type;
95     }
96
97     public BigDecimal getProgress() {
98         return progress;
99     }
100
101     public void setProgress(BigDecimal progress) {
102         this.progress = progress;
103     }
104
105     public String getMachineIdentifier() {
106         return machineIdentifier;
107     }
108
109     public void setMachineIdentifier(String machineIdentifier) {
110         this.machineIdentifier = machineIdentifier;
111     }
112
113     public PlexPlayerState getState() {
114         return state;
115     }
116
117     public void setState(PlexPlayerState state) {
118         this.state = state;
119     }
120
121     public String getLocal() {
122         return local;
123     }
124
125     public void setLocal(String local) {
126         this.local = local;
127     }
128
129     public long getDuration() {
130         return duration;
131     }
132
133     public void setDuration(long duration) {
134         this.duration = duration;
135     }
136
137     public String getArt() {
138         return art;
139     }
140
141     public void setArt(String art) {
142         this.art = art;
143     }
144
145     public Integer getUserId() {
146         return userId;
147     }
148
149     public void setUserId(Integer userId) {
150         this.userId = userId;
151     }
152
153     public String getUserTitle() {
154         return userTitle;
155     }
156
157     public void setUserTitle(String userTitle) {
158         this.userTitle = userTitle;
159     }
160
161     private void updateProgress() {
162         try {
163             if (this.duration > 0) {
164                 BigDecimal progress = new BigDecimal("100")
165                         .divide(new BigDecimal(this.duration), new MathContext(100, RoundingMode.HALF_UP))
166                         .multiply(new BigDecimal(this.viewOffset)).setScale(2, RoundingMode.HALF_UP);
167                 progress = BigDecimal.ZERO.max(progress);
168                 progress = new BigDecimal("100").min(progress);
169
170                 this.endTime = new Date(System.currentTimeMillis() + (this.duration - this.viewOffset));
171                 this.progress = progress;
172             }
173         } catch (Exception e) {
174             logger.debug("An exception occurred while polling the updating Progress: '{}'", e.getMessage());
175         }
176     }
177 }