Skip to content

Commit

Permalink
Add logging system and fix logic in media play (#29)
Browse files Browse the repository at this point in the history
* Rewrite logging and logic

* Fix bug in IS_FILE_SET variable

* Fix media logic and add more logging info

* Add log file output

* Fix typo and add log section in README
  • Loading branch information
KaneBetter authored Oct 31, 2022
1 parent 3414441 commit 63aefad
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

log
PVM
jellyfish720p.mp4
.DS_Store
Expand Down
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ Pi Video Machine - a scalable, synchronized, and networked-controlled, raspberry

### { This is a work in progress }

- TODO: rename the variable
- TODO: rewrite logging
- TODO: rewrite logic between commands
- TODO: Create another python file to control two display
- TODO: create a isFileSet flag


## Requirements
Expand Down Expand Up @@ -174,6 +170,21 @@ cd PVM && git pull
sudo reboot
```
## Logs
All logs for each time each RPI is stored in the log folder.
Name convention for each file is `{:%Y-%m-%d %H:%M:%S}.log`
All output from the console is synchronized to the file in real-time.
```bash
2022-10-31 13:34:21;INFO;Received command: file
2022-10-31 13:34:21;INFO;Received value: jellyfish720p.mp4
2022-10-31 13:34:21;INFO;File set: /home/pi/Videos/jellyfish720p.mp4
```
If you close the program and then reopen it, a new log file will be created.
## Examples
Expand Down
64 changes: 42 additions & 22 deletions pvm.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,88 @@
from datetime import datetime
import os
from pythonosc import dispatcher, osc_server
import argparse
from omxplayer.player import OMXPlayer
import logging
from logging.handlers import TimedRotatingFileHandler
import sys

def _init_logger():
logger = logging.getLogger('PVM')
logger = logging.getLogger("PVM")
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stderr)
fileHandler = TimedRotatingFileHandler('./log/{:%Y-%m-%d %H:%M:%S}.log'.format(datetime.now()), when='midnight')
handler.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s",
"%Y-%m-%d %H:%M:%S")
fileHandler.setFormatter(formatter)
fileHandler.suffix = '%Y_%m_%d.log'
logger.addHandler(fileHandler)
handler.setFormatter(formatter)
logger.addHandler(handler)

_init_logger()
_logger = logging.getLogger('PVM')
_logger.info("Logging system initilized!")
# logging example:
# _logger.info('App started in %s', os.getcwd())
# _logger.debug('App started in %s', os.getcwd())
_logger = logging.getLogger("PVM")
_logger.info("Logging system initilized in %s", os.getcwd())

# Place your videos in this folder for autostart
PEFIX_PATH = "/home/pi/Videos/"
VIDEO_PATH = "jellyfish720p.mp4"
# TODO: rename the variable
media = ""
IS_FILE_SET = False

# TODO: rewrite logging
# TODO: rewrite logic between commands
def parse_commands(*args):
global media
global VIDEO_PATH
global IS_FILE_SET
command = args[1]
_logger.info("Command: %s", command)
_logger.info("Received command: %s", command)
if len(args)>2:
value = args[2]
_logger.info("Value: %s", str(value))
_logger.info("Received command: %s", str(value))
pass
# TODO: Create another python file to control two display
if command=="file":
# TODO: create a isFileSet flag
_logger.info("File set: %s", PEFIX_PATH + value)
IS_FILE_SET = True
media = OMXPlayer(PEFIX_PATH + value, dbus_name='org.mpris.MediaPlayer2.omxplayer', args=['--loop'])
media.pause()
VIDEO_PATH = value
elif command=="start":
# TODO: if media.can_play()
media.play()
return

if not IS_FILE_SET:
_logger.info("Command %s failed because of the file is unset.", command)
return

if command=="start":
if media.can_play():
media.play()
_logger.info("%s command success.", command)
else:
_logger.info("%s command failed.", command)
elif command=="stop":
# TODO: if media.can_stop()
media.stop()
if media.can_quit():
media.stop()
IS_FILE_SET = False
_logger.info("%s command success and file has been unset.", command)
else:
_logger.info("%s command failed.", command)
elif command=="set_position":
media.set_position(float(value))
_logger.info("%s command success.", command)
elif command=="set_rate":
# TODO: check isFileSet flag
fps = str(30 * float(value))
media = OMXPlayer(PEFIX_PATH + VIDEO_PATH, dbus_name='org.mpris.MediaPlayer2.omxplayer', args=['--loop','--force-fps', fps])
media.pause()
_logger.info("%s command success.", command)
elif command=="pause":
# TODO: if media.can_pause()
media.pause()
if media.can_pause():
media.pause()
_logger.info("%s command success.", command)
else:
_logger.info("%s command failed.", command)
else:
# TODO: change loggind
print("I received command \"%s\" but I don't know what to do with it, yet." % command)
_logger.info("%s unknown.", command)


def main(RECEIVE_PORT):
Expand Down

0 comments on commit 63aefad

Please sign in to comment.