Finished work to enable navi sounds.
This commit is contained in:
		@@ -12,18 +12,19 @@ type
 | 
			
		||||
    notificationSecs: int]
 | 
			
		||||
 | 
			
		||||
const VERSION = "0.3.0"
 | 
			
		||||
const NOTE_TITLE = "Daily Notifier v" & VERSION
 | 
			
		||||
const timeFmt = "HH:mm"
 | 
			
		||||
 | 
			
		||||
var args: Table[string, Value]
 | 
			
		||||
var cfg: DPConfig
 | 
			
		||||
let appName = getAppFilename()
 | 
			
		||||
let soundsDir: string = appName[0..(appName.len-15)] & "sounds/navi"
 | 
			
		||||
let soundFiles = toSeq(walkFiles(soundsDir))
 | 
			
		||||
let soundFiles = toSeq(walkFiles(soundsDir & "/*"))
 | 
			
		||||
randomize()
 | 
			
		||||
 | 
			
		||||
proc parseDailyPlan(filename: string): seq[PlanItem] =
 | 
			
		||||
 | 
			
		||||
  debug "Parsing daily plan file: " & filename
 | 
			
		||||
  debug "debug - Parsing daily plan file: " & filename
 | 
			
		||||
 | 
			
		||||
  result = @[]
 | 
			
		||||
 | 
			
		||||
@@ -60,21 +61,27 @@ proc parseDailyPlan(filename: string): seq[PlanItem] =
 | 
			
		||||
    of AfterPlans: break
 | 
			
		||||
    else: break
 | 
			
		||||
 | 
			
		||||
  debug "Found " & $result.len & " items."
 | 
			
		||||
  debug "debug - Found " & $result.len & " items."
 | 
			
		||||
 | 
			
		||||
proc doAndLog(cmd: string): void =
 | 
			
		||||
  debug "debug - Executing '" & cmd & "'"
 | 
			
		||||
  discard execShellCmd(cmd)
 | 
			
		||||
 | 
			
		||||
proc notifyDailyPlanItem(item: PlanItem): void =
 | 
			
		||||
 | 
			
		||||
  let soundFile = appName[0..(appName.len-15)] & "sounds/navi/" & soundFiles[random(soundFiles.len)]
 | 
			
		||||
  let desc = item.time.format(timeFmt) & " - " & item.note
 | 
			
		||||
  debug "debug - Notifying: " & desc
 | 
			
		||||
 | 
			
		||||
  let soundFile = soundFiles[random(soundFiles.len)]
 | 
			
		||||
  case hostOS
 | 
			
		||||
  of "macosx":
 | 
			
		||||
    discard execShellCmd("osascript -e 'display notification \"" &
 | 
			
		||||
      item.time.format(timeFmt) & " - " & item.note &
 | 
			
		||||
      "\" with title \"Daily Notifier v" & VERSION & "\"")
 | 
			
		||||
    discard execShellCmd("ogg123 \"" & soundFile & "\"")
 | 
			
		||||
    doAndLog "osascript -e 'display notification \"" &
 | 
			
		||||
       desc & "\" with title \"" & NOTE_TITLE & "\"'"
 | 
			
		||||
    doAndLog "ogg123 \"" & soundFile & "\""
 | 
			
		||||
 | 
			
		||||
  of "linux":
 | 
			
		||||
    discard execShellCmd("notify-send 'Daily Notifier v" & VERSION & "' '" & item.note & "'")
 | 
			
		||||
    discard execShellCmd("paplay \"" & soundFile & "\"")
 | 
			
		||||
    doAndLog "notify-send '" & NOTE_TITLE & "' '" & desc & "'"
 | 
			
		||||
    doAndLog "paplay \"" & soundFile & "\""
 | 
			
		||||
 | 
			
		||||
  else: quit("Unsupported host OS: '" & hostOS & "'.")
 | 
			
		||||
 | 
			
		||||
@@ -89,7 +96,7 @@ proc loadConfig(s: cint): void {. exportc, noconv .} =
 | 
			
		||||
  var rcFilename: string =
 | 
			
		||||
    foldl(rcLocations, if len(a) > 0: a elif existsFile(b): b else: "")
 | 
			
		||||
 | 
			
		||||
  debug "Reloading config file from " & rcFilename
 | 
			
		||||
  debug "debug - Reloading config file from " & rcFilename
 | 
			
		||||
 | 
			
		||||
  var jsonCfg: JsonNode
 | 
			
		||||
  try: jsonCfg = parseFile(rcFilename)
 | 
			
		||||
@@ -111,6 +118,7 @@ proc loadConfig(s: cint): void {. exportc, noconv .} =
 | 
			
		||||
 | 
			
		||||
proc mainLoop(args: Table[string, Value]): void =
 | 
			
		||||
  
 | 
			
		||||
  debug "debug - Started daemon main loop."
 | 
			
		||||
  loadConfig(0)
 | 
			
		||||
  signal(SIGHUP, loadConfig)
 | 
			
		||||
  var curDay: TimeInfo = getLocalTime(fromSeconds(0))
 | 
			
		||||
@@ -198,19 +206,19 @@ Options:
 | 
			
		||||
 | 
			
		||||
  if args["start"]:
 | 
			
		||||
    # Start our daemon process (if needed)
 | 
			
		||||
    info "Starting daemon."
 | 
			
		||||
    info "daily_notifier: Starting daemon."
 | 
			
		||||
    let childPid = daemonize(cfg.pidfile, "/dev/null", cfg.logfile, cfg.errfile, proc(): void = mainLoop(args))
 | 
			
		||||
    if childPid == 0: quit(QuitSuccess) # We are the child... don't need to do anything else.
 | 
			
		||||
    info "Started, pid: " & $childPid
 | 
			
		||||
    info "daily_notifier: Started, pid: " & $childPid
 | 
			
		||||
 | 
			
		||||
  elif args["stop"] or args["reconfigure"]:
 | 
			
		||||
 | 
			
		||||
    if not fileExists(cfg.pidfile):
 | 
			
		||||
      echo "daily_notifier is not running"
 | 
			
		||||
      info "daily_notifier: not running"
 | 
			
		||||
      quit(QuitSuccess)
 | 
			
		||||
 | 
			
		||||
    let pid = parseInt(readFile(cfg.pidfile).strip)
 | 
			
		||||
    debug "Killing process " & $pid
 | 
			
		||||
    info "daily_notifier: Killing process " & $pid
 | 
			
		||||
 | 
			
		||||
    if args["stop"]: discard kill(pid, SIGTERM)
 | 
			
		||||
    else: discard kill(pid, SIGHUP)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user