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