first commit

This commit is contained in:
kicap1992
2023-03-14 17:34:21 +08:00
commit 0a1035fd8d
8 changed files with 3207 additions and 0 deletions

2
.env Normal file
View File

@ -0,0 +1,2 @@
PORT = 3001
URL = "http://20.20.20.25"

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
**node_modules

1
Procfile Normal file
View File

@ -0,0 +1 @@
web: node index.js

101
index.js Normal file
View File

@ -0,0 +1,101 @@
const express = require('express');
const app = express();
const mongoose = require('mongoose');
require('dotenv/config');
const formData = require('express-form-data');
const cors = require('cors')
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
// const io = new Server(server);
const io = require("socket.io")(server, {
cors: {
origin: ["http://localhost", "http://127.0.0.1","http://20.20.20.25"],
methods: ["GET", "POST"],
// allowedHeaders: ["my-custom-header"],
// credentials: true
}
});
let users = {};
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(formData.parse());
app.options('*', cors())
app.use(cors())
// mongoose.connect(process.env.DB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true, family: 4, })
// let db = mongoose.connection;
// db.on('error', console.error.bind(console, 'connection error:'));
// db.once('open', function () {
// console.log("Connected to db")
// })
// import routes
const from_esp32_routes = require('./routes/from_esp32_routes');
// routes
app.use('/api/from_esp32', from_esp32_routes)
app.get('/', (req, res) => {
console.log("ada pengambilan data")
res.send('ii dia pyan fdssfsd ');
})
// app.get('/ambil_data_1', (req, res) => {
// res.send('ini ambil data 1');
// })
const port = process.env.PORT || 3001;
io.on('connection', (socket) => {
let userId = socket.id;
if (!users[userId]) users[userId] = [];
users[userId].push(socket.id);
console.log('socket connected', userId);
socket.on('angin', (_) => {
console.log(" ini sebelum broadcast angin")
socket.broadcast.emit('angin', {
kecepatan_per_detik : _.kecepatan_per_detik,
kecepatan_per_jam : _.kecepatan_per_jam,
rps : _.rps,
rpm : _.rpm
});
})
socket.on('curah_hujan', (_) => {
console.log(" ini sebelum broadcast hujan")
socket.broadcast.emit('curah_hujan', {
jumlah_tip : _.jumlah_tip,
curah_hujan_per_menit : _.curah_hujan_per_menit,
curah_hujan_per_jam : _.curah_hujan_per_jam,
curah_hujan_per_hari : _.curah_hujan_per_hari
});
})
socket.on('disconnect', (_) => {
console.log('user disconnected');
console.log(_)
});
});
server.listen(port, () => {
console.log(`Server running on port ${port}`);
})

68
models/device_model.js Normal file
View File

@ -0,0 +1,68 @@
const mongoose = require('mongoose');
const deviceSchema = new mongoose.Schema(
{
_id : {
type : String,
required : true,
maxLength : 12,
},
lat:{
type : String
},
lng:{
type : String
},
status : {
type : String,
// required : true,
},
created_at : {
type : Date,
default : Date.now,
},
updated_at : {
type : Date,
default : Date.now,
}
}
)
const newDeviceSchema = new mongoose.Schema(
{
_id : {
type : String,
required : true,
},
lat:{
type : String
},
lng:{
type : String
},
status: {
type : String,
default : 'Pending',
}
}
)
const notificationSchema = new mongoose.Schema(
{
_id : {
type : String,
required : true,
},
status: {
type : String,
required : true,
}
}
)
const deviceModel = mongoose.model('device', deviceSchema, 'device');
const newDeviceModel = mongoose.model('new_device', newDeviceSchema, 'new_device');
const notificationModel = mongoose.model('notification', notificationSchema, 'notification');
module.exports = {deviceModel , newDeviceModel, notificationModel};

2965
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

22
package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "tanah_longsor_server",
"version": "1.0.0",
"description": "Tanah Longsor Server",
"main": "index.js",
"scripts": {
"dev": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "kicap karan",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.0.1",
"express": "^4.18.1",
"express-form-data": "^2.0.18",
"mongoose": "^6.4.6",
"nodemon": "^2.0.19",
"socket.io": "^4.4.1",
"socket.io-client": "^4.4.1"
}
}

View File

@ -0,0 +1,47 @@
const express = require('express');
const router = express.Router();
require('dotenv/config');
// const { deviceModel, newDeviceModel, notificationModel } = require('../models/device_model');
const io_sock = require("socket.io-client");
// const socket = io_sock(`https://tanah-longosor-be.herokuapp.com/`);
const socket = io_sock(process.env.URL+":"+process.env.PORT);
router.post('/', async (req, res) => {
const { kecepatan_per_detik , kecepatan_per_jam , rps,rpm} = req.body;
console.log("ini kecepatan per detik",kecepatan_per_detik)
console.log("ini kecepatan per jam",kecepatan_per_jam)
console.log("ini rps",rps)
console.log("ini rpm",rpm)
socket.emit('angin', {
kecepatan_per_detik : kecepatan_per_detik,
kecepatan_per_jam : kecepatan_per_jam,
rps : rps,
rpm : rpm
});
res.status(200).send({ status: true, message: "success" });
})
router.post('/curah_hujan', async (req, res) => {
// const device_list = await newDeviceModel.find({});
const { jumlah_tip , curah_hujan_per_menit , curah_hujan_per_jam , curah_hujan_per_hari } = req.body;
console.log("ini jumlah tip",jumlah_tip)
console.log("ini curah hujan per menit",curah_hujan_per_menit)
console.log("ini curah hujan per jam",curah_hujan_per_jam)
console.log("ini curah hujan per hari",curah_hujan_per_hari)
socket.emit('curah_hujan', {
jumlah_tip : jumlah_tip,
curah_hujan_per_menit : curah_hujan_per_menit,
curah_hujan_per_jam : curah_hujan_per_jam,
curah_hujan_per_hari : curah_hujan_per_hari
})
res.status(200).send({ status: true, message: "success" });
})
module.exports = router;