Skip to content

Instantly share code, notes, and snippets.

@HipsterBrown
Last active June 16, 2017 13:45
Show Gist options
  • Save HipsterBrown/3811de6f541cba35f32524c222452e07 to your computer and use it in GitHub Desktop.
Save HipsterBrown/3811de6f541cba35f32524c222452e07 to your computer and use it in GitHub Desktop.
Quick Tessel Webcam Server
<!doctype html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Tessel-Cam</title>
<style>
body {
font-family: Helvetica, sans-serif;
text-align: center;
}
.link-reset {
color: inherit;
text-decoration: none;
}
.button {
border: 3px solid magenta;
border-radius: 3px;
color: magenta;
padding: 0.5rem 1rem;
}
.button:hover {
cursor: pointer;
}
.absolute {
position: absolute;
}
.relative {
position: relative;
}
.overlay-image {
bottom: 1rem;
right: 1rem;
}
.debug {
border: 1px solid red;
}
.max-content {
width: -moz-max-content;
width: -webkit-max-content;
width: max-content;
}
.mx-auto {
margin: 0 auto;
}
</style>
</head>
<body>
<h1>Welcome to Tessel Cam!</h1>
<div class="relative max-content mx-auto">
<img src="{{cameraURL}}" alt="Web Cam Stream" width="800" height="600" >
<a href="/capture" class="button link-reset absolute overlay-image" download>Snap a Photo!</a>
</div>
<h3>The current node version is {{nodeVersion}}</h3>
</body>
</html>
'use strict';
const av = require('tessel-av');
const os = require('os');
const http = require('http');
const fs = require('fs');
const path = require('path');
const templateStream = require('./stream-template');
const port = 9001;
const camera = new av.Camera({
fps: 30,
height: 600,
width: 800,
});
const server = http.createServer((request, response) => {
if (/capture/.test(request.url)) {
console.log('capturing photo...');
response.writeHead(200, { "Content-Type": "image/jpeg" });
camera.capture().pipe(response);
return;
}
console.log('getting the main page');
const indexPageStream = fs.createReadStream(path.join(__dirname, 'index.html'));
response.writeHead(200, { "Content-Type": "text/html" });
indexPageStream
.pipe(templateStream({
cameraURL: camera.url,
nodeVersion: process.versions.node,
}))
.pipe(response);
}).listen(port, () => {
console.log(`Now serving at http://${os.hostname()}:${port}`);
console.log(camera.url);
});
{
"name": "tessel-cam",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "t2 run index.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"tessel-av": "^0.19.0"
}
}
'use strict';
const Transform = require('stream').Transform;
module.exports = (data = {}) => (
new Transform({
writableObjectMode: true,
transform(chunk, encoding, callback) {
let html = chunk.toString();
for (const prop in data) {
const finder = new RegExp(`{{\\s*(${prop})\\s*}}`, 'g');
html = html.replace(finder, data[prop]);
}
callback(null, html);
},
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment