Skip to content
Snippets Groups Projects
Commit 4265ec0f authored by ANTO SAGAYA JUSTIN R's avatar ANTO SAGAYA JUSTIN R :speech_balloon:
Browse files

MongoDB

parent c14c8a84
No related branches found
No related tags found
No related merge requests found
from flask import Flask , render_template
import os
import math
from src import User
app = Flask(__name__)
basename = '/iotcloud'
@app.route(basename+'/hello')
def hello_world():
d = {
"username": "justin",
"env": "labs",
"avatar": "https://media.licdn.com/dms/image/D5603AQGqSYfbrnkjcw/profile-displayphoto-shrink_200_200/0/1694890248719?e=1720051200&v=beta&t=M6ZsyUoBIQ8fpaswZe6aCld0oOmrala4dKPRt2m4v98"
}
return render_template('helloworld.html', data = d)
@app.route(basename+'/')
def home():
return "<h1> Welcome to IoT Cloud </h1>"
@app.route(basename + '/whoami')
def whoami():
return os.popen('whoami').read()
if __name__ == '__main__':
app.run(host = '0.0.0.0' , port = '7000' , debug = True )
{
"mongodb_connection_string": "mongodb://Justin_Mass:Amutha%401981@mongodb.selfmade.ninja:27017/?authSource=users"
}
\ No newline at end of file
from pymongo import MongoClient
client = MongoClient('mongodb://Justin_Mass:Amutha%401981@mongodb.selfmade.ninja:27017/?authSource=users')
db = client.Justin_Mass_iotcloud
# db = client['Justin_Mass_iotcloud'] alternate way
result = db.test.find_one({
"username": "Justin"
})
print(result)
\ No newline at end of file
import pika
def callback(ch, method, properties, body):
print(" [ X ] Received % r " % body )
credentials = pika.PlainCredentials('Justin', 'Justin@Saran2001')
parameters = pika.ConnectionParameters('rabbitmq.selfmade.ninja',
5672,
'justinmass2001_Amutha',
credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue='my_first_queue')
channel.basic_consume(queue='my_first_queue',
auto_ack=True,
on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
\ No newline at end of file
import pika
def callback(ch, method, properties, body):
print(" [ X ] Received % r " % body )
credinsials = pika.PlainCredentials('Justin', 'Justin@Saran2001')
parameters = pika.ConnectionParameters('rabbitmq.selfmade.ninja',
5672,
'justinmass2001_Amutha',
credinsials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue='my_first_queue')
channel.basic_consume(queue='my_first_queue',
auto_ack=True,
on_message_callback=callback)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
\ No newline at end of file
import pika
import random
from time import sleep
# Uses AMQP protocol to communicate with RabbitMQ server.
credentials = pika.PlainCredentials('Justin', 'Justin@Saran2001') # username and password for pubuser.
parameters = pika.ConnectionParameters('rabbitmq.selfmade.ninja', # host name of the RabbitMQ server.
5672, # port number of the RabbitMQ server.
'justinmass2001_Amutha', # virtual host name of the RabbitMQ server.
credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue='my_first_queue') # declare a first consumer queue.
channel.queue_declare(queue='my_second_queue') # declare a second consumer queue.
while True:
message = str(random.random())
channel.basic_publish(exchange='',
routing_key='my_first_queue',
body='Hello World!')
print(" [x] Sent '{}'".format(message))
sleep(1)
# connection.close()
\ No newline at end of file
import pymongo
class User:
def __init__(self, id):
print("Init user with {}".format(id))
\ No newline at end of file
def get_config(key):
config_file = "/home/justinmass2001/iotproject/iotweb/config.json" # always use absolute path example use full path call pwd,
# not use relative path example ../
file = open(config_file, "r")
print(file.read())
\ No newline at end of file
File added
File added
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>viewer JS Template </title>
<!-- <link rel="image" href="image/python.png"> -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/viewerjs/1.11.7/viewer.css" integrity="sha512-9NawOLzuLE2GD22PJ6IPWXEjPalb/FpdH1qMpgXdaDM+0OfxEV75ZCle6KhZi0vM6ZWvMrNnIZv6YnsL+keVmA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="icon" href="image/logo.png">
<style>
img {
width: 150px;
height: 150px;
}
</style>
</head>
<body>
<div class="container">
<div class="alert alert-success" role="alert">
Welcome {{ data.username }} , you are in {{ data.env }}
</div>
<!-- class called row and collum -->
<div class="container">
<div class="row">
<div class="col-auto">
<button class="btn btn-primary" style="margin-bottom: 20px;">Add Devices</button>
</div>
</div>
<div class="row">
<div class="col">
<!-- class for card-1 -->
<div class="card text-center">
<div class="card-header">
Raspberry Camera 2
</div>
<div class="card-body">
<h5 class="card-title">Special title treatment</h5>
<p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
<button id="raspi-cam-1" class="btn btn-primary raspi-cm-viewer">View Captures</a>
</div>
<div class="card-footer text-muted">
2 days ago
</div>
</div>
</div>
<div class="col">
<!-- class for card-2 -->
<div class="card text-center">
<div class="card-header">
Selfmade Ninja Academy Lab
</div>
<div class="card-body">
<h5 class="card-title">Special title treatment</h5>
<p class="card-text">Click To the Open button to View live Site. Access detailed captures and ensure the.</p>
<button id="raspi-cam-2" class="btn btn-primary raspi-cm-viewer">Open</a>
</div>
<div class="card-footer text-muted">
2 days ago
</div>
</div>
</div>
<div class="col">
<!-- class for card-3 -->
<div class="card text-center">
<div class="card-header">
Raspberry Camera 3
</div>
<div class="card-body">
<h5 class="card-title">Special title treatment</h5>
<p class="card-text">This is a simple card with some text content. You can add more details here</p>
<a href="raspi-cam-3" class="btn btn-primary raspi-cam-viewer">View Captures</a>
</div>
<div class="card-footer text-muted">
2 days ago
</div>
</div>
</div>
</div>
</div>
<ul id="images" style="display: none;">
<li><img src="https://th.bing.com/th/id/OIP.d7XoYXNNdLcexb9E1xehCAHaEK?w=278&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7" alt="Picture 1"></li> <br>
<li><img src="https://th.bing.com/th/id/OIP.TldVfnF-trAnn4FNSJ42RAHaEK?w=324&h=182&c=7&r=0&o=5&dpr=1.3&pid=1.7" alt="Picture 2"></li> <br>
<li><img src="https://th.bing.com/th/id/OIP.ysjV0sgOseoA-KELS-qr_AHaEK?w=320&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7" alt="Picture 3"></li> <br>
<li><img src="{{ data.avater }}" alt="Picture 1"></li> <!-- my github profile picture. -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js" integrity="sha384-I7E8VVD/ismYTF4hNIPjVp/Zjvgyol6VFvRkX/vR+Vc4jQkC+hVqc2pM8ODewa9r" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/viewerjs/1.11.7/viewer.min.js" integrity="sha512-lZD0JiwhtP4UkFD1mc96NiTZ14L7MjyX5Khk8PMxJszXMLvu7kjq1sp4bb0tcL6MY+/4sIuiUxubOqoueHrW4w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/viewerjs/1.11.7/viewer.common.js" integrity="sha512-LlQl0sFUcI1vIDM7mJLlc6a8ED/7OodBOExKGh93wuOhftpuR+LqrihUhDERtnX526QoQqqKWxg6BpMjEibyMw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
// View an image.
// const viewer = new Viewer(document.getElementById('images'), {
// loop: false,
// interval: 500,
// shown: function(event){
// console.log(event);
// console.log("shown")
// },
// show: function(event){
// console.log(event);
// console.log("began to show");
// }
// });
function apiCall(){
// Do network calls and fetch more images.
return `
<li><img src="https://th.bing.com/th/id/OIP.d7XoYXNNdLcexb9E1xehCAHaEK?w=278&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7" alt="Picture 1" /></li>
<li><img src="https://th.bing.com/th/id/OIP.TldVfnF-trAnn4FNSJ42RAHaEK?w=324&h=182&c=7&r=0&o=5&dpr=1.3&pid=1.7" alt="Picture 2" /></li>
<li><img src="https://th.bing.com/th/id/OIP.ysjV0sgOseoA-KELS-qr_AHaEK?w=320&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7" alt="Picture 3" /></li>
<li><img src="https://th.bing.com/th/id/OIP.d7XoYXNNdLcexb9E1xehCAHaEK?w=278&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7" alt="Picture 1" /></li>
<li><img src="https://th.bing.com/th/id/OIP.TldVfnF-trAnn4FNSJ42RAHaEK?w=324&h=182&c=7&r=0&o=5&dpr=1.3&pid=1.7" alt="Picture 2" /></li>
<li><img src="https://th.bing.com/th/id/OIP.ysjV0sgOseoA-KELS-qr_AHaEK?w=320&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7" alt="Picture 3" /></li>
`
}
const images = document.getElementById('images');
const viewer = new Viewer(images, {
loop: true,
interval: 500,
view: function(event){
console.log((event.detail.index + 1 ) + " / " + viewer.length);
var cur_image = event.detail.index + 1;
var length = viewer.length;
var leftover = 1;
if(length - cur_image <= leftover){
console.log("now we can add more images");
$(images).append(apiCall());
viewer.update();
}
}
});
$('#raspi-cam-1').on('click', function(e){
viewer.show();
});
// click to go this website page.
function openSite(){
window.open('http://labs.selfmade.ninja', '_blank');
}
$('#raspi-cam-2').on('click', openSite);
</script>
</body>
</html>
\ No newline at end of file
from src import get_config
print(get_config('mongodb_connection_string'))
from flask import Flask, render_template
import os
import subprocess # for whoami
app = Flask(__name__, template_folder='templates')
basename = "/helloworld"
def whoami():
return subprocess.check_output('whoami').decode('utf-8')
@app.route(basename+'/')
def helloworld():
d = {
"username": whoami().strip(),
"env": "Development",
"avater": "https://git.selfmade.ninja/uploads/-/system/user/avatar/7411/avatar.png?width=800"
}
return render_template('helloworld.html', data=d )
if __name__ == '__main__':
app.run(debug=True)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment