Browse Source

adding

master
Björn 3 years ago
parent
commit
d815226046
16 changed files with 11426 additions and 570 deletions
  1. +10
    -0
      .env.example
  2. +3
    -0
      mix-manifest.json
  3. +11186
    -554
      package-lock.json
  4. +12
    -2
      package.json
  5. +2
    -2
      public/index.html
  6. +3
    -0
      public/index.js
  7. +48
    -0
      src/api/auth.js
  8. +8
    -6
      src/api/state.js
  9. +4
    -4
      src/bootstrap.js
  10. +6
    -0
      src/client/index.js
  11. +40
    -0
      src/client/tinkerforge-power-button.riot
  12. +4
    -0
      src/http/web.js
  13. +26
    -0
      src/schemas/auth.js
  14. +2
    -2
      src/schemas/state.js
  15. +40
    -0
      src/tinkerforge/tinkerforgePower.js
  16. +32
    -0
      webpack.mix.js

+ 10
- 0
.env.example View File

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

+ 3
- 0
mix-manifest.json View File

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

+ 11186
- 554
package-lock.json
File diff suppressed because it is too large
View File


+ 12
- 2
package.json View File

@ -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"
}
}

+ 2
- 2
public/index.html View File

@ -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>

+ 3
- 0
public/index.js
File diff suppressed because it is too large
View File


+ 48
- 0
src/api/auth.js View File

@ -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
})
})
}

+ 8
- 6
src/api/state.js View File

@ -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
})
})
}

+ 4
- 4
src/bootstrap.js View File

@ -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)


+ 6
- 0
src/client/index.js View File

@ -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')

+ 40
- 0
src/client/tinkerforge-power-button.riot View File

@ -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>

+ 4
- 0
src/http/web.js View File

@ -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')
})
}

+ 26
- 0
src/schemas/auth.js View File

@ -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
}

+ 2
- 2
src/schemas/state.js View File

@ -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: {


+ 40
- 0
src/tinkerforge/tinkerforgePower.js View File

@ -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

+ 32
- 0
webpack.mix.js View File

@ -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