forked from andrewhao/shmile.bak
-
Notifications
You must be signed in to change notification settings - Fork 39
/
app.coffee
85 lines (67 loc) · 2.6 KB
/
app.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
express = require "express"
jade = require "jade"
http = require "http"
sys = require "sys"
fs = require "fs"
yaml = require "yaml"
dotenv = require "dotenv"
exec = require("child_process").exec
dotenv.load()
console.log("printer is: #{process.env.PRINTER_ENABLED}")
PhotoFileUtils = require("./lib/photo_file_utils")
StubCameraControl = require("./lib/stub_camera_control")
CameraControl = require("./lib/camera_control")
ImageCompositor = require("./lib/image_compositor")
exp = express()
web = http.createServer(exp)
exp.configure ->
exp.set "views", __dirname + "/views"
exp.set "view engine", "jade"
exp.use express.json()
exp.use express.methodOverride()
exp.use exp.router
exp.use express.static(__dirname + "/public")
exp.get "/", (req, res) ->
res.render "index",
title: "shmile"
extra_css: []
exp.get "/gallery", (req, res) ->
res.render "gallery",
title: "gallery!"
extra_css: [ "photoswipe/photoswipe" ]
image_paths: PhotoFileUtils.composited_images(true)
# FIXME/ahao This global state is no bueno.
State = image_src_list: []
ccKlass = if process.env['STUB_CAMERA'] is "true" then StubCameraControl else CameraControl
camera = new ccKlass().init()
camera.on "photo_saved", (filename, path, web_url) ->
State.image_src_list.push path
io = require("socket.io").listen(web)
web.listen 3000
io.sockets.on "connection", (websocket) ->
sys.puts "Web browser connected"
camera.on "camera_begin_snap", ->
websocket.emit "camera_begin_snap"
camera.on "camera_snapped", ->
websocket.emit "camera_snapped"
camera.on "photo_saved", (filename, path, web_url) ->
websocket.emit "photo_saved",
filename: filename
path: path
web_url: web_url
websocket.on "snap", () ->
camera.emit "snap"
websocket.on "all_images", ->
websocket.on "composite", ->
compositer = new ImageCompositor(State.image_src_list).init()
compositer.emit "composite"
compositer.on "composited", (output_file_path) ->
console.log "Finished compositing image. Output image is at ", output_file_path
State.image_src_list = []
# Control this with PRINTER=true or PRINTER=false
if process.env.PRINTER_ENABLED is "true"
console.log "Printing image at ", output_file_path
exec "lpr -o #{process.env.PRINTER_IMAGE_ORIENTATION} -o media=\"#{process.env.PRINTER_MEDIA}\" #{output_file_path}"
websocket.broadcast.emit "composited_image", PhotoFileUtils.photo_path_to_url(output_file_path)
compositer.on "generated_thumb", (thumb_path) ->
websocket.broadcast.emit "generated_thumb", PhotoFileUtils.photo_path_to_url(thumb_path)