Add mode switching.

This commit is contained in:
2020-05-23 13:55:16 +02:00
parent 2907cf6574
commit 87bfce1a46
6 changed files with 75 additions and 39 deletions

View File

@@ -1,4 +1,4 @@
window {
window {
background: black;
font-size: 30px;
}

View File

@@ -8,10 +8,17 @@ type
success*: bool ## Indicating if the opration was successfull
errorMsg*: string ## Error meassge in case the operation failed
Mode* {.pure.} = enum ## Options for the display mode
None = "none" ## No images will be displayed
Foxes = "foxes" ## Some nice foxes
Inspiro = "inspiro" ## Inspiring nonsense
File = "file" ## Images from a local path
Command* = enum
cClose = "close" ## Closes the control server and exists the applicaiton
cRefresh = "refresh" ## Force refresh of the image now
cTimeout = "timeout" ## Set image timeout to a new value
cMode = "mode" ## Change the servers display mode
CommandMessage* = object of RootObj
command*: Command ## Command that the application should execute
@@ -27,4 +34,8 @@ proc newCommand*(c: Command, p: string = ""): CommandMessage =
CommandMessage(command: c, parameter: p)
proc wrap*(msg: CommandMessage): string =
$(%msg) & "\r\L"
$(%msg) & "\r\L"
proc enumToStrings*(en: typedesc): seq[string] =
for x in en:
result.add $x

View File

@@ -1,29 +1,50 @@
import net
import strutils, net
import argparse
import commands
import common
var socket = newSocket()
proc sendCommand(server, port: string, msg: CommandMessage) =
proc sendCommand*(server, port: string, msg: CommandMessage) =
socket.connect(server, Port(port.parseInt))
if not socket.trySend(msg.wrap):
echo "Cannot send command: ", msg
socket.close()
var p = newParser("pixctrl"):
help("Control utilitiy for randopix")
option("-s", "--server", help="Host running the randopix server", default="127.0.0.1")
option("-p", "--port", help="Port to connect to the randopix server", default = $defaultPort)
command("refresh"):
help("Force image refresh now")
run:
let c = newCommand(cRefresh)
sendCommand(opts.parentOpts.server, opts.parentOpts.port, c)
command("timeout"):
help("Set timeout in seconds before a new image is displayed")
arg("seconds", default = "300")
run:
let c = newCommand(cTimeout, opts.seconds)
sendCommand(opts.parentOpts.server, opts.parentOpts.port, c)
p.run(commandLineParams())
proc switchMode*(server, port: string, mode: string) =
try:
discard parseEnum[Mode](mode)
except ValueError:
echo "Invalid mode: ", mode
echo "Accepted modes: ", enumToStrings(Mode).join(", ")
return
let c = newCommand(cMode, mode)
sendCommand(server, port, c)
when isMainModule:
var p = newParser("pixctrl"):
help("Control utilitiy for randopix")
option("-s", "--server", help="Host running the randopix server", default="127.0.0.1")
option("-p", "--port", help="Port to connect to the randopix server", default = $defaultPort)
command($cRefresh):
help("Force image refresh now")
run:
let c = newCommand(cRefresh)
sendCommand(opts.parentOpts.server, opts.parentOpts.port, c)
command($cTimeout):
help("Set timeout in seconds before a new image is displayed")
arg("seconds", default = "300")
run:
let c = newCommand(cTimeout, opts.seconds)
sendCommand(opts.parentOpts.server, opts.parentOpts.port, c)
command($cMode):
help("Change the display mode of the server")
arg("mode")
run:
switchMode(opts.parentOpts.server, opts.parentOpts.port, opts.mode)
try:
p.run(commandLineParams())
except:
echo p.help

View File

@@ -1,6 +1,6 @@
import os, sets, random, httpClient, json, strformat, options
import gintro/[gdkpixbuf, gobject]
import commands
import common
const
supportedExts = @[".png", ".jpg", ".jpeg"]
@@ -12,12 +12,6 @@ type
FileOpResult* = object of OpResult
file*: string
Mode* {.pure.} = enum ## Options for the display mode
None = "none" ## No images will be displayed
Foxes = "foxes" ## Some nice foxes
Inspiro = "inspiro" ## Inspiring nonsense
File = "file" ## Images from a local path
ImageProvider* = ref object of RootObj
## Manages images that should be displayed
verbose: bool ## Additional logging for the image provider

View File

@@ -2,7 +2,7 @@ import os, options, strformat
import gintro/[glib, gobject, gtk, gio]
import gintro/gdk except Window
import argparse except run
import providers, server, commands
import providers, server, common
const
css = slurp("app.css")
@@ -24,10 +24,6 @@ var
# Server vor recieving commands from external tools
serverWorker: system.Thread[ServerArgs]
proc enumToStrings(en: typedesc): seq[string] =
for x in en:
result.add $x
proc log(things: varargs[string, `$`]) =
if args.verbose:
echo things.join()
@@ -53,6 +49,10 @@ proc newArgs(): Option[Args] =
try:
let opts = p.parse(commandLineParams())
# Catch the help option. Do nothing more
if opts.help:
return
# Parse the starting mode
var startMode: Mode
try:
@@ -127,17 +127,27 @@ proc checkServerChannel(image: Image): bool =
if tried.dataAvailable:
let msg: CommandMessage = tried.msg
log "Main app got message: ", msg.command
log "Recieved command: ", msg.command
case msg.command
of cRefresh:
forceUpdate(nil, nil, image)
of cTimeout:
let val = msg.parameter.parseInt * 1000
log "Setting timeout to ", val
args.timeout = val
discard updateTimeout.remove
updateTimeout = int(timeoutAdd(uint32(args.timeout), timedUpdate, image))
of cMode:
try:
let mode = parseEnum[Mode](msg.parameter)
log "Switching mode: ", mode
imageProvider.mode = mode
except ValueError:
log "Invalid mode: ", msg.parameter
else:
log "Command ignored: ", msg.command

View File

@@ -1,11 +1,11 @@
import net, json, strutils
import commands
import common
type
ServerArgs* = object of RootObj
verbose: bool
var
var
chan*: Channel[CommandMessage]
verbose: bool
@@ -31,7 +31,7 @@ proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} =
server.listen()
log "Control server is listening"
while true:
while true:
# Process client requests
var client = net.newSocket()
server.accept(client)
@@ -41,7 +41,7 @@ proc runServer*[ServerArgs](arg: ServerArgs) {.thread, nimcall.} =
if line == "":
log "No data from client"
continue
var jsonData = parseJson(line)
let msg = jsonData.to(CommandMessage)
case msg.command