master
Björn 3 years ago
parent bfa6c9e5ed
commit d815226046

@ -0,0 +1,10 @@
NAME=
DEBUG=false
IP=
PORT=3000
JWT_SECRET=
USER_TOKEN=
USER_PASSWORD=

@ -0,0 +1,3 @@
{
"/public/index.js": "/public/index.js"
}

11740
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -14,10 +14,20 @@
"author": "Björn Hase",
"license": "MIT",
"dependencies": {
"axios": "^0.21.1",
"dotenv": "^8.2.0",
"fastify": "^3.9.2",
"fastify-bearer-auth": "^5.0.2",
"fastify-auth": "^1.0.1",
"fastify-jwt": "^2.3.0",
"fastify-static": "^3.3.1"
"fastify-static": "^3.3.1",
"riot": "^5.1.4",
"tinkerforge": "^2.1.32"
},
"devDependencies": {
"@riotjs/compiler": "^5.1.3",
"@riotjs/webpack-loader": "^5.0.0",
"cross-env": "^7.0.3",
"laravel-mix": "^6.0.10",
"postcss": "^8.2.4"
}
}

@ -9,8 +9,8 @@
</head>
<body>
<h1>test<h1>
<script src="/js/index.js"></script>
<tinkerforge-power-button></tinkerforge-power-button>
<script type="text/javascript" src="/index.js"></script>
</body>
</html>

File diff suppressed because one or more lines are too long

@ -0,0 +1,48 @@
const schemas = require('../schemas/state.js')
/**
*
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://github.com/tentakelfabrik/fastify-lowdb-riotjs-lessons-learned
*
*/
module.exports = async function (fastify, opts) {
/**
* getting current status of switch
*
*
* @param {object} request
* @param {object} reply
* @return {object}
*/
fastify.post('/login', function(request, reply) {
reply
.code(200)
.header('Content-Type', 'application/json; charset=utf-8')
.send({
'name': process.env.NAME
'power': power
})
})
/**
* turn on
*
* @param {object} request
* @param {object} reply
* @return {object}
*/
fastify.post('/logout', schemas.putSchema, function(request, reply) {
reply
.code(200)
.header('Content-Type', 'application/json; charset=utf-8')
.send({
'name': process.env.NAME
'power': power
})
})
}

@ -1,5 +1,5 @@
const schemas = require('../schemas/state.js')
const bearerAuthPlugin = require('fastify-bearer-auth')
const TinkerforgePower = require('../tinkerforge/tinkerforgePower.js')
/**
*
@ -11,9 +11,6 @@ const bearerAuthPlugin = require('fastify-bearer-auth')
*/
module.exports = async function (fastify, opts) {
fastify.register(bearerAuthPlugin, process.env.TOKEN.split(','))
/**
* getting current status of switch
*
@ -27,7 +24,8 @@ module.exports = async function (fastify, opts) {
.code(200)
.header('Content-Type', 'application/json; charset=utf-8')
.send({
'power': power
'name': process.env.NAME,
'power': request.body.power
})
})
@ -39,11 +37,15 @@ module.exports = async function (fastify, opts) {
* @return {object}
*/
fastify.put('/state', schemas.putSchema, function(request, reply) {
tinkerforgePower = new TinkerforgePower(request.body.power)
reply
.code(200)
.header('Content-Type', 'application/json; charset=utf-8')
.send({
'power': power
'name': process.env.NAME,
'power': request.body.power
})
})
}

8
src/bootstrap.js vendored

@ -2,11 +2,11 @@
const config = require('dotenv').config()
// logging
const fastify = require('fastify')({
logger: true
const fastify = require('fastify') ({
logger: process.env.DEBUG
})
// path for public
// path for public
const path = require('path')
// adding files and plugins
@ -21,7 +21,7 @@ fastify
// let it rain
const start = async () => {
try {
await fastify.listen(process.env.PORT)
await fastify.listen(process.env.SERVER_PORT, process.env.SERVER_IP)
fastify.log.info(`server listening on ${fastify.server.address().port}`)
} catch (err) {
fastify.log.error(err)

@ -0,0 +1,6 @@
import * as riot from 'riot'
import TinkerforgePowerButton from './tinkerforge-power-button.riot'
// register components
riot.register('tinkerforge-power-button', TinkerforgePowerButton)
riot.mount('tinkerforge-power-button')

@ -0,0 +1,40 @@
<tinkerforge-power-button>
<div class="tinkerforge-power-button">
<button type="button" onclick={ handleClick }>
<span if={ state.power }>On</span>
<span if={ !state.power }>Off</span>
</button>
</div>
<script>
import axios from 'axios'
/**
*
*
* @author Björn Hase
*
*/
export default {
state: {
power: true
},
handleClick(event) {
if (this.state.power) {
this.state.power = false
} else {
this.state.power = true
}
axios.put('/api/state', {
'power': this.state.power
}).then((response) => {
this.update()
})
}
}
</script>
</tinkerforge-power-button>

@ -11,4 +11,8 @@ module.exports = async function (fastify, opts) {
fastify.get('/', function (req, reply) {
return reply.sendFile('index.html')
})
fastify.get('/index.js', function (req, reply) {
return reply.sendFile('index.js')
})
}

@ -0,0 +1,26 @@
/**
* schemas for state routes
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
* @link https://github.com/tentakelfabrik/fastify-lowdb-riotjs-lessons-learned
*/
const putSchema = {
schema: {
params: {
type: 'object',
required: ['user_token', 'user_secret'],
properties: {
'user_token': {
type: 'boolean'
}
},
additionalProperties: false
}
}
}
module.exports = {
putSchema: putSchema
}

@ -1,5 +1,5 @@
/**
* schemas for state routes
* schemas for state routes
*
* @author Björn Hase, Tentakelfabrik
* @license http://opensource.org/licenses/MIT The MIT License
@ -8,7 +8,7 @@
const putSchema = {
schema: {
params: {
data: {
type: 'object',
required: ['power'],
properties: {

@ -0,0 +1,40 @@
const Tinkerforge = require('tinkerforge')
/**
*
*
*
*
*/
class TinkerforgePower {
constructor(power) {
this.host = 'localhost'
this.port = 4223
this.UID = 'xxx'
this.power = power
this.ipcon = new Tinkerforge.IPConnection()
this.idr = new Tinkerforge.BrickletIndustrialDualRelay(this.UID, this.ipcon);
this.ipcon.connect(this.host, this.port, (error) => {
this._handleError(error)
})
this.ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED, (connectReason) => {
this._handlePower(this.power)
})
}
_handleError(error) {
console.log('Error: ' + error)
}
_handlePower(connectReason) {
this.idr.setValue(this.power, this.power)
}
}
module.exports = TinkerforgePower

@ -0,0 +1,32 @@
const mix = require('laravel-mix')
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel applications. By default, we are compiling the CSS
| file for the application as well as bundling up all the JS files.
|
*/
mix.webpackConfig({
module: {
rules: [{
test: /\.riot$/,
use: [{
loader: '@riotjs/webpack-loader'
}]
}
]}
})
mix
.js('src/client/index.js', 'public')
.options({
terser: {
extractComments: false,
}
})
Loading…
Cancel
Save