first commit
This commit is contained in:
commit
c90336efbb
|
@ -0,0 +1,178 @@
|
|||
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
|
||||
|
||||
# Logs
|
||||
|
||||
logs
|
||||
_.log
|
||||
npm-debug.log_
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
.pnpm-debug.log*
|
||||
|
||||
# Caches
|
||||
|
||||
.cache
|
||||
|
||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
||||
|
||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
||||
|
||||
# Runtime data
|
||||
|
||||
pids
|
||||
_.pid
|
||||
_.seed
|
||||
*.pid.lock
|
||||
|
||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
||||
|
||||
lib-cov
|
||||
|
||||
# Coverage directory used by tools like istanbul
|
||||
|
||||
coverage
|
||||
*.lcov
|
||||
|
||||
# nyc test coverage
|
||||
|
||||
.nyc_output
|
||||
|
||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
||||
|
||||
.grunt
|
||||
|
||||
# Bower dependency directory (https://bower.io/)
|
||||
|
||||
bower_components
|
||||
|
||||
# node-waf configuration
|
||||
|
||||
.lock-wscript
|
||||
|
||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
||||
|
||||
build/Release
|
||||
|
||||
# Dependency directories
|
||||
|
||||
node_modules/
|
||||
jspm_packages/
|
||||
|
||||
# Snowpack dependency directory (https://snowpack.dev/)
|
||||
|
||||
web_modules/
|
||||
|
||||
# TypeScript cache
|
||||
|
||||
*.tsbuildinfo
|
||||
|
||||
# Optional npm cache directory
|
||||
|
||||
.npm
|
||||
|
||||
# Optional eslint cache
|
||||
|
||||
.eslintcache
|
||||
|
||||
# Optional stylelint cache
|
||||
|
||||
.stylelintcache
|
||||
|
||||
# Microbundle cache
|
||||
|
||||
.rpt2_cache/
|
||||
.rts2_cache_cjs/
|
||||
.rts2_cache_es/
|
||||
.rts2_cache_umd/
|
||||
|
||||
# Optional REPL history
|
||||
|
||||
.node_repl_history
|
||||
|
||||
# Output of 'npm pack'
|
||||
|
||||
*.tgz
|
||||
|
||||
# Yarn Integrity file
|
||||
|
||||
.yarn-integrity
|
||||
|
||||
# dotenv environment variable files
|
||||
|
||||
.env
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
.env.local
|
||||
|
||||
# parcel-bundler cache (https://parceljs.org/)
|
||||
|
||||
.parcel-cache
|
||||
|
||||
# Next.js build output
|
||||
|
||||
.next
|
||||
out
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
|
||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
||||
|
||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
||||
|
||||
# public
|
||||
|
||||
# vuepress build output
|
||||
|
||||
.vuepress/dist
|
||||
|
||||
# vuepress v2.x temp and cache directory
|
||||
|
||||
.temp
|
||||
|
||||
# Docusaurus cache and generated files
|
||||
|
||||
.docusaurus
|
||||
|
||||
# Serverless directories
|
||||
|
||||
.serverless/
|
||||
|
||||
# FuseBox cache
|
||||
|
||||
.fusebox/
|
||||
|
||||
# DynamoDB Local files
|
||||
|
||||
.dynamodb/
|
||||
|
||||
# TernJS port file
|
||||
|
||||
.tern-port
|
||||
|
||||
# Stores VSCode versions used for testing VSCode extensions
|
||||
|
||||
.vscode-test
|
||||
|
||||
# yarn v2
|
||||
|
||||
.yarn/cache
|
||||
.yarn/unplugged
|
||||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
|
||||
# IntelliJ based IDEs
|
||||
.idea
|
||||
|
||||
# Finder (MacOS) folder config
|
||||
.DS_Store
|
||||
|
||||
|
||||
.env
|
|
@ -0,0 +1,11 @@
|
|||
# Flood Notification App (Node js)
|
||||
|
||||
#### this is the script for esp8266 connected to 2 water level sensor and a ultrasonic sensor.
|
||||
#### If the water reach the first water level sensor then "warning",
|
||||
#### If reach second water level then danger and the water is measure by ultrasonic
|
||||
#### the value is send to node js server (run it using "bun --hot index.js")
|
||||
#### then it will be display to a flutter app.
|
||||
|
||||
![alt text](image.png)
|
||||
|
||||
![alt text](image-1.png)
|
|
@ -0,0 +1,22 @@
|
|||
const mysql = require('mysql');
|
||||
const dotenv = require('dotenv');
|
||||
|
||||
dotenv.config();
|
||||
|
||||
// Connect to the MySQL database
|
||||
const connection = mysql.createConnection({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_NAME
|
||||
});
|
||||
|
||||
// connection.connect((err) => {
|
||||
// if (err) {
|
||||
// console.error('Error connecting to MySQL database: ' + err.stack);
|
||||
// return;
|
||||
// }
|
||||
// console.log('Connected to MySQL database as id ' + connection.threadId);
|
||||
// })
|
||||
|
||||
module.exports = {connection}
|
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
|
@ -0,0 +1,145 @@
|
|||
const express = require('express');
|
||||
const http = require('http');
|
||||
const cors = require('cors');
|
||||
const dotenv = require('dotenv');
|
||||
const socket = require('./socket');
|
||||
const app = express();
|
||||
const server = http.createServer(app);
|
||||
const io = socket.init(server);
|
||||
const iosend = socket.getIO();
|
||||
const conn = require('./conn.js');
|
||||
const connection = conn.connection;
|
||||
|
||||
dotenv.config();
|
||||
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
// app.use(fileUpload());
|
||||
app.options('*', cors());
|
||||
app.use(cors());
|
||||
|
||||
|
||||
async function insert_data(water_height, status) {
|
||||
const query_insert = 'INSERT INTO tb_data (water_height, status) VALUES (?, ?)';
|
||||
const result_insert = await new Promise((resolve, reject) => {
|
||||
connection.query(query_insert, [water_height, status], (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
app.post('/', async (req, res) => {
|
||||
var data = req.body;
|
||||
try {
|
||||
const query_search = 'Select * from tb_data';
|
||||
const result_search = await new Promise((resolve, reject) => {
|
||||
connection.query(query_search, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
})
|
||||
var now = new Date();
|
||||
|
||||
const status = data.danger_level == 1 ? 2 : data.warning_level == 1 ? 1 : 0;
|
||||
if (result_search.length > 0) {
|
||||
const last_data = result_search[result_search.length - 1];
|
||||
|
||||
var converted_time_last = new Date(last_data.created_at);
|
||||
now = now.getTime();
|
||||
converted_time_last = converted_time_last.getTime();
|
||||
var differenceInSeconds = (now - converted_time_last) / 1000;
|
||||
console.log(differenceInSeconds, "second");
|
||||
|
||||
|
||||
const thresholds = {
|
||||
2: 30,
|
||||
1: 60,
|
||||
default: 120
|
||||
};
|
||||
|
||||
const threshold = thresholds[status] || thresholds.default;
|
||||
|
||||
if (differenceInSeconds > threshold) {
|
||||
insert_data(data.water_height, status);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
insert_data(data.water_height, status);
|
||||
}
|
||||
|
||||
console.log(data);
|
||||
io.emit('data', data);
|
||||
res.status(200).json({ message: 'success', data: data });
|
||||
} catch (error) {
|
||||
console.log("ini error post", error);
|
||||
res.status(500).json({ message: 'Internal server error' });
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
app.get('/', async (req, res) => {
|
||||
const date = req.query.date;
|
||||
console.log(date, "ini di get");
|
||||
|
||||
const query_search = date == null
|
||||
? 'SELECT * FROM tb_data ORDER BY created_at DESC LIMIT 20'
|
||||
: 'SELECT * FROM tb_data WHERE created_at LIKE "%' + date + '%" ORDER BY created_at DESC';
|
||||
|
||||
const result_search = await new Promise((resolve, reject) => {
|
||||
connection.query(query_search, (err, result) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
});
|
||||
})
|
||||
res.status(200).json({ message: 'success', data: result_search });
|
||||
|
||||
})
|
||||
|
||||
// app error handler
|
||||
app.use((err, req, res, next) => {
|
||||
console.log(err);
|
||||
res.status(500).send('Something broke!');
|
||||
});
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
let userID = socket.id;
|
||||
console.log('A user connected: ' + userID);
|
||||
|
||||
socket.on('scan_dia', (data) => {
|
||||
console.log('Received scan_dia event: ' + data);
|
||||
});
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
console.log('User disconnected: ' + userID);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
app,
|
||||
server,
|
||||
io
|
||||
};
|
||||
|
||||
const port = process.env.PORT || 3001;
|
||||
|
||||
// Start the server
|
||||
server.listen(port, () => {
|
||||
console.log(`Server is running on http://localhost:${port}`);
|
||||
});
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
// Enable latest features
|
||||
"lib": ["ESNext", "DOM"],
|
||||
"target": "ESNext",
|
||||
"module": "ESNext",
|
||||
"moduleDetection": "force",
|
||||
"jsx": "react-jsx",
|
||||
"allowJs": true,
|
||||
|
||||
// Bundler mode
|
||||
"moduleResolution": "bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"noEmit": true,
|
||||
|
||||
// Best practices
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
|
||||
// Some stricter flags (disabled by default)
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"noPropertyAccessFromIndexSignature": false
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"name": "flood",
|
||||
"version": "1.0.0",
|
||||
"description": "flood backend",
|
||||
"main": "index.js",
|
||||
"author": "kicap",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"dotenv": "^16.4.5",
|
||||
"express": "^4.19.2",
|
||||
"express-form-data": "^2.0.23",
|
||||
"mysql": "^2.18.1",
|
||||
"socket.io": "^4.7.5"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
const socketio = require('socket.io');
|
||||
// const socketio_client = require('socket.io-client');
|
||||
const dotenv = require('dotenv');
|
||||
|
||||
dotenv.config();
|
||||
|
||||
// const socket_client = socketio_client("http://localhost:"+process.env.PORT);
|
||||
|
||||
let io;
|
||||
|
||||
function init(server) {
|
||||
io = socketio(server);
|
||||
return io;
|
||||
}
|
||||
|
||||
function getIO() {
|
||||
if (!io) {
|
||||
throw new Error('Socket.io not initialized');
|
||||
}
|
||||
return io;
|
||||
}
|
||||
|
||||
|
||||
|
||||
module.exports = {
|
||||
init,
|
||||
getIO,
|
||||
// socket_client
|
||||
};
|
Loading…
Reference in New Issue