2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.plex.internal.dto;
15 import java.math.BigDecimal;
16 import java.math.MathContext;
17 import java.math.RoundingMode;
18 import java.util.Date;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * The {@link PlexSession} is the class used internally by the PlexPlayer things
25 * to keep state of updates from the Bridge.
27 * @author Brian Homeyer - Initial contribution
28 * @author Aron Beurskens - Binding development
30 public class PlexSession {
34 private long viewOffset;
36 private BigDecimal progress;
37 private String machineIdentifier;
38 private PlexPlayerState state;
40 private long duration;
42 private String sessionKey = "";
43 private Integer userId;
44 private String userTitle = "";
46 private final Logger logger = LoggerFactory.getLogger(PlexSession.class);
48 public String getSessionKey() {
52 public void setSessionKey(String sessionKey) {
53 this.sessionKey = sessionKey;
56 public String getTitle() {
60 public Date getEndTime() {
64 public void setEndTime(Date endTime) {
65 this.endTime = endTime;
68 public void setTitle(String title) {
72 public String getThumb() {
76 public void setThumb(String thumb) {
80 public long getViewOffset() {
84 public void setViewOffset(long viewOffset) {
85 this.viewOffset = viewOffset;
89 public String getType() {
93 public void setType(String type) {
97 public BigDecimal getProgress() {
101 public void setProgress(BigDecimal progress) {
102 this.progress = progress;
105 public String getMachineIdentifier() {
106 return machineIdentifier;
109 public void setMachineIdentifier(String machineIdentifier) {
110 this.machineIdentifier = machineIdentifier;
113 public PlexPlayerState getState() {
117 public void setState(PlexPlayerState state) {
121 public String getLocal() {
125 public void setLocal(String local) {
129 public long getDuration() {
133 public void setDuration(long duration) {
134 this.duration = duration;
137 public String getArt() {
141 public void setArt(String art) {
145 public Integer getUserId() {
149 public void setUserId(Integer userId) {
150 this.userId = userId;
153 public String getUserTitle() {
157 public void setUserTitle(String userTitle) {
158 this.userTitle = userTitle;
161 private void updateProgress() {
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);
170 this.endTime = new Date(System.currentTimeMillis() + (this.duration - this.viewOffset));
171 this.progress = progress;
173 } catch (Exception e) {
174 logger.debug("An exception occurred while polling the updating Progress: '{}'", e.getMessage());