This repository has been archived on 2023-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
ha_client/lib/entities/media_player/media_player_entity.class.dart

110 lines
4.2 KiB
Dart
Raw Normal View History

2019-08-24 21:22:32 +03:00
part of '../../main.dart';
2018-11-04 18:20:06 +02:00
class MediaPlayerEntity extends Entity {
static const SUPPORT_PAUSE = 1;
static const SUPPORT_SEEK = 2;
static const SUPPORT_VOLUME_SET = 4;
static const SUPPORT_VOLUME_MUTE = 8;
static const SUPPORT_PREVIOUS_TRACK = 16;
static const SUPPORT_NEXT_TRACK = 32;
static const SUPPORT_TURN_ON = 128;
static const SUPPORT_TURN_OFF = 256;
static const SUPPORT_PLAY_MEDIA = 512;
static const SUPPORT_VOLUME_STEP = 1024;
static const SUPPORT_SELECT_SOURCE = 2048;
static const SUPPORT_STOP = 4096;
static const SUPPORT_CLEAR_PLAYLIST = 8192;
static const SUPPORT_PLAY = 16384;
static const SUPPORT_SHUFFLE_SET = 32768;
static const SUPPORT_SELECT_SOUND_MODE = 65536;
2019-03-19 23:07:40 +02:00
MediaPlayerEntity(Map rawData, String webHost) : super(rawData, webHost);
2018-11-04 18:20:06 +02:00
bool get supportPause => ((supportedFeatures &
2018-11-04 18:20:06 +02:00
MediaPlayerEntity.SUPPORT_PAUSE) ==
MediaPlayerEntity.SUPPORT_PAUSE);
bool get supportSeek => ((supportedFeatures &
2018-11-04 18:20:06 +02:00
MediaPlayerEntity.SUPPORT_SEEK) ==
MediaPlayerEntity.SUPPORT_SEEK);
bool get supportVolumeSet => ((supportedFeatures &
2018-11-04 18:20:06 +02:00
MediaPlayerEntity.SUPPORT_VOLUME_SET) ==
MediaPlayerEntity.SUPPORT_VOLUME_SET);
bool get supportVolumeMute => ((supportedFeatures &
2018-11-04 18:20:06 +02:00
MediaPlayerEntity.SUPPORT_VOLUME_MUTE) ==
MediaPlayerEntity.SUPPORT_VOLUME_MUTE);
bool get supportPreviousTrack => ((supportedFeatures &
2018-11-04 18:20:06 +02:00
MediaPlayerEntity.SUPPORT_PREVIOUS_TRACK) ==
MediaPlayerEntity.SUPPORT_PREVIOUS_TRACK);
bool get supportNextTrack => ((supportedFeatures &
2018-11-04 18:20:06 +02:00
MediaPlayerEntity.SUPPORT_NEXT_TRACK) ==
MediaPlayerEntity.SUPPORT_NEXT_TRACK);
bool get supportTurnOn => ((supportedFeatures &
2018-11-04 18:20:06 +02:00
MediaPlayerEntity.SUPPORT_TURN_ON) ==
MediaPlayerEntity.SUPPORT_TURN_ON);
bool get supportTurnOff => ((supportedFeatures &
2018-11-04 18:20:06 +02:00
MediaPlayerEntity.SUPPORT_TURN_OFF) ==
MediaPlayerEntity.SUPPORT_TURN_OFF);
bool get supportPlayMedia => ((supportedFeatures &
2018-11-11 18:36:49 +02:00
MediaPlayerEntity.SUPPORT_PLAY_MEDIA) ==
MediaPlayerEntity.SUPPORT_PLAY_MEDIA);
bool get supportVolumeStep => ((supportedFeatures &
2018-11-11 18:36:49 +02:00
MediaPlayerEntity.SUPPORT_VOLUME_STEP) ==
MediaPlayerEntity.SUPPORT_VOLUME_STEP);
bool get supportSelectSource => ((supportedFeatures &
2018-11-11 18:36:49 +02:00
MediaPlayerEntity.SUPPORT_SELECT_SOURCE) ==
MediaPlayerEntity.SUPPORT_SELECT_SOURCE);
bool get supportStop => ((supportedFeatures &
2018-11-11 18:36:49 +02:00
MediaPlayerEntity.SUPPORT_STOP) ==
MediaPlayerEntity.SUPPORT_STOP);
bool get supportClearPlaylist => ((supportedFeatures &
2018-11-11 18:36:49 +02:00
MediaPlayerEntity.SUPPORT_CLEAR_PLAYLIST) ==
MediaPlayerEntity.SUPPORT_CLEAR_PLAYLIST);
bool get supportPlay => ((supportedFeatures &
2018-11-11 18:36:49 +02:00
MediaPlayerEntity.SUPPORT_PLAY) ==
MediaPlayerEntity.SUPPORT_PLAY);
bool get supportShuffleSet => ((supportedFeatures &
2018-11-11 18:36:49 +02:00
MediaPlayerEntity.SUPPORT_SHUFFLE_SET) ==
MediaPlayerEntity.SUPPORT_SHUFFLE_SET);
bool get supportSelectSoundMode => ((supportedFeatures &
2018-11-11 18:36:49 +02:00
MediaPlayerEntity.SUPPORT_SELECT_SOUND_MODE) ==
MediaPlayerEntity.SUPPORT_SELECT_SOUND_MODE);
2018-11-04 18:20:06 +02:00
2018-11-23 14:11:34 +02:00
List<String> get soundModeList => getStringListAttributeValue("sound_mode_list");
List<String> get sourceList => getStringListAttributeValue("source_list");
2019-09-15 17:29:49 +03:00
DateTime get positionLastUpdated => DateTime.tryParse("${attributes["media_position_updated_at"]}")?.toLocal();
int get durationSeconds => _getIntAttributeValue("media_duration");
int get positionSeconds => _getIntAttributeValue("media_position");
2018-11-23 14:11:34 +02:00
2018-11-17 22:40:33 +02:00
@override
Widget _buildAdditionalControlsForPage(BuildContext context) {
return MediaPlayerControls();
}
2019-09-15 17:29:49 +03:00
bool canCalculateActualPosition() {
return positionLastUpdated != null && durationSeconds != null && positionSeconds != null;
}
double getActualPosition() {
double result = 0;
if (canCalculateActualPosition()) {
Duration durationD;
Duration positionD;
durationD = Duration(seconds: durationSeconds);
positionD = Duration(
seconds: positionSeconds);
result = positionD.inSeconds.toDouble();
int differenceInSeconds = DateTime
.now()
.difference(positionLastUpdated)
.inSeconds;
result = ((result + differenceInSeconds) <= durationD.inSeconds) ? (result + differenceInSeconds) : durationD.inSeconds.toDouble();
}
return result;
}
2018-11-04 18:20:06 +02:00
}