Skip to content

Commit

Permalink
save config, runs flawless
Browse files Browse the repository at this point in the history
  • Loading branch information
hmmh-sven-scheil committed Dec 8, 2024
1 parent 55fb687 commit 982878e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 55 deletions.
64 changes: 32 additions & 32 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
{
"favorites": [
{
"name": "Tanja",
"file": "fav_tanja.json"
},
{
"name": "Sven",
"file": "fav_sven.json"
},
{
"name": "Nachrichten",
"file": "fav_news.json"
},
{
"name": "60,70,80,90er",
"file": "fav_80er.json"
},
{
"name": "International",
"file": "fav_international.json"
}
],
"favorites_dir": "favorites",
"last_favlist": 1,
"last_favlist_name": "Sven",
"last_station": 2,
"last_station_name": "Radio Bremen 2",
"last_station_url": "http://icecast.radiobremen.de/rb/bremenzwei/live/mp3/128/stream.mp3",
"last_station_logo": "rb2.jpg",
"last_volume": 36,
"screensaver_after_s": 9,
"auto_save_config": true
"favorites": [
{
"name": "Tanja",
"file": "fav_tanja.json"
},
{
"name": "Sven",
"file": "fav_sven.json"
},
{
"name": "Nachrichten",
"file": "fav_news.json"
},
{
"name": "60,70,80,90er",
"file": "fav_80er.json"
},
{
"name": "International",
"file": "fav_international.json"
}
],
"favorites_dir": "favorites",
"last_favlist": 1,
"last_favlist_name": "60,70,80,90er",
"last_station": 3,
"last_station_name": "FluxFM 70er",
"last_station_url": "http://streams.fluxfm.de/70er/mp3-320/streams.fluxfm.de/",
"last_station_logo": "dummy.jpg",
"last_volume": 80,
"screensaver_after_s": 9,
"auto_save_config": true
}
25 changes: 17 additions & 8 deletions radio_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ class Config:
last_volume: int
screensaver_after_s: int
auto_save_config: bool
is_dirty = False
is_dirty: bool = False
filepath: str = None
all_stations_dict = {}

@staticmethod
def from_json(filepath: str) -> 'Config':
with open(filepath, 'r') as file:
def from_json(config_filepath: str) -> 'Config':
with open(config_filepath, 'r') as file:
data = json.load(file)
favorites_dict = {}
for favorite in data['favorites']:
Expand All @@ -60,7 +62,8 @@ def from_json(filepath: str) -> 'Config':
last_station_logo=data['last_station_logo'],
last_volume=data['last_volume'],
screensaver_after_s=data['screensaver_after_s'],
auto_save_config=data['auto_save_config']
auto_save_config=data['auto_save_config'],
filepath = config_filepath
)

def to_json(self):
Expand All @@ -77,13 +80,11 @@ def to_json(self):
'screensaver_after_s': self.screensaver_after_s,
'auto_save_config': self.auto_save_config
}
with open('test_config.json', 'w') as file:
with open(self.filepath, 'w') as file:
json.dump(data, file, indent=4)

def get_last_station_data(self):
return RadioStation(name=self.last_station_name,
url=self.last_station_url,
logo=self.last_station_logo)
return RadioStation(name=self.last_station_name, url=self.last_station_url, logo=self.last_station_logo)

def get_favlist_by_idx(self, idx):
for i, name in enumerate(self.favorites_dict.keys()):
Expand All @@ -100,6 +101,11 @@ def change_property(self, prop_name, prop_value):
else:
print(f"Error: Config property '{prop_name}' does not exist.")


def __post_init__(self):
self.all_stations_dict = self.get_all_stations_dict()
print(f"all {len(self.all_stations_dict)} stations: {self.all_stations_dict} ")

def get_all_stations_dict(self):
all_stations = {}
for i, name in enumerate(self.favorites_dict.keys()):
Expand All @@ -111,6 +117,9 @@ def get_all_stations_dict(self):

return all_stations

def get_station_data_by_name(self, name):
return self.all_stations_dict.get(name)


#_favorites_path = 'favorites'
# = Config.from_json('config.json')
Expand Down
34 changes: 19 additions & 15 deletions radio_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def __init__(self, parent, controller):
def setup_station_slots(self, favlist_name):
"""create station slot buttons"""

for btn_station_slot in self.btn_station_slot:
btn_station_slot.destroy()

self.btn_station_slot = []

for i, station in enumerate(self.controller.config.favorites_dict[favlist_name].stations):
logo_fn = station.logo
logo_img = self.controller.logos.get_image_as_ctk(logo_fn, _logo_size)
Expand Down Expand Up @@ -181,7 +186,7 @@ def __init__(self, parent, controller):

def select_fav(self):
new_favlist_name = self.favorite_btns[self.radio_var.get()].cget('text')
print(f"Click on favorite list item {self.radio_var.get()}, {new_favlist_name}")
print(f"Click on favorite list item {self.radio_var.get()} '{new_favlist_name}'")
self.controller.change_to_favlist(new_favlist_name)
#self.master.master.reset_timestamp()

Expand All @@ -197,19 +202,16 @@ def __init__(self, parent, controller):
self.grid_columnconfigure((0,1,2), weight=1)
self.grid_rowconfigure(0, weight=1)

# create mute button
self.btn_mute = ctk.CTkButton(master=self, text=_str_mute, width=_station_btn_size, height=40,
command=self.btn_mute_cb, state='disabled',
font=ctk.CTkFont(size=18, weight="bold"))
self.btn_mute.grid(row=0, column=0, padx=5, pady=5, sticky="w")

# create volume slider
self.sl_volume = (ctk.CTkSlider(master=self, from_=0, to=100, width=250, command=self.volume_slider_event_cb))
self.sl_volume.configure(number_of_steps=25)
self.sl_volume.set(self.controller.config.last_volume)
self.sl_volume.grid(row=0, column=1, padx=0, pady=0, sticky="ew")

# create stop button
self.btn_play_stop = ctk.CTkButton(master=self, text=_str_play, width=_station_btn_size, height=40,
command=self.btn_play_stop_cb, font=ctk.CTkFont(size=18, weight="bold"))
self.btn_play_stop.grid(row=0, column=3, padx=5, pady=5, sticky="e")
Expand Down Expand Up @@ -272,10 +274,10 @@ def stopped(self):
def playing(self):
self.update_media(self.station_name, _str_no_info)

def update_media(self, station_name, title):
self.station_name = station_name
self.title = title
self.la_title.configure(text=f"{station_name}: {title}")
def update_media(self, new_station_name, new_title):
self.station_name = new_station_name
self.title = new_title
self.la_title.configure(text=f"{new_station_name}: {new_title}")

def update_datetime(self):
self.datetime_tid = self.la_datetime.configure(text=time.asctime())
Expand All @@ -294,15 +296,15 @@ def __init__(self, config):
self.config = config
self.logos = LogoDist(img_ext=_img_ext, directory=_stations_logos_path, default_logo_name=_dummy_logo_fn)
self.config = config
print(self.config.get_all_stations_dict())

self.vlc_instance = vlc.Instance()
self.media_player = self.vlc_instance.media_player_new()
self.Media = None
self.is_muted = False
self.is_playing = False
self.prev = ""
self.current_station_idx = self.config.last_station
self.current_station_idx = self.config.last_station #TODO get rid of idx
self.current_station_name = self.config.last_station_name
self.current_favlist_name = self.config.last_favlist_name

self.update_timer = Interval(_update_meta_info_s, self.update_meta_info)
Expand All @@ -318,17 +320,17 @@ def toggle_playing(self ):
if self.is_playing:
self.stop_playing()
else:
self.play_last_station()
self.play_current_station()

def stop_playing(self):
self.media_player.stop()
self.is_playing=False
self.fr_volume.stopped()
self.fr_info.stopped()

def play_last_station(self):
station_data = self.config.get_last_station_data()
self.play_station(station_data, self.config.last_station)
def play_current_station(self):
station_data = self.config.get_station_data_by_name(self.current_station_name)
self.play_station(station_data, self.current_station_idx)

#TODO get rid of idx
def play_station(self, new_station_data, new_station_idx):
Expand All @@ -342,15 +344,17 @@ def play_station(self, new_station_data, new_station_idx):
self.is_playing=True
self.fr_volume.playing()
self.config.change_property("last_station", self.current_station_idx)
self.config.change_property("last_favlist_name", self.config.last_favlist_name)
self.config.change_property("last_favlist_name", self.current_favlist_name)
self.config.change_property("last_station_url", new_station_data.url)
self.config.change_property("last_station_logo", new_station_data.logo)
self.config.change_property("last_station_name", new_station_data.name)
self.current_station_idx = new_station_idx
self.current_station_name = new_station_data.name
self.fr_stations.playing()
self.fr_info.playing()
self.prev = ""
self.update_meta_info()
self.config.to_json()
else:
print(f"new_station_data = {new_station_data} is empty!")

Expand Down

0 comments on commit 982878e

Please sign in to comment.