From 4265ec0f6c24dd58bf9d95b71c4d1bc87e3c746c Mon Sep 17 00:00:00 2001 From: ANTO SAGAYA JUSTIN R <7411-justinmass2001@users.noreply.git.selfmade.ninja> Date: Thu, 27 Mar 2025 17:45:51 +0000 Subject: [PATCH] MongoDB --- iotweb/app.py | 29 ++++ iotweb/config.json | 3 + iotweb/examples/mongodb/test.py | 12 ++ iotweb/examples/rabbitmq/simple_consumer.py | 19 +++ iotweb/examples/rabbitmq/simple_consumer2.py | 19 +++ iotweb/examples/rabbitmq/simple_producer.py | 25 +++ iotweb/src/User.py | 4 + iotweb/src/__init__.py | 8 + iotweb/src/__pycache__/User.cpython-312.pyc | Bin 0 -> 163 bytes .../src/__pycache__/__init__.cpython-312.pyc | Bin 0 -> 472 bytes iotweb/templates/helloworld.html | 158 ++++++++++++++++++ iotweb/test.py | 3 + iotweb/viewer.py | 24 +++ 13 files changed, 304 insertions(+) create mode 100644 iotweb/app.py create mode 100644 iotweb/config.json create mode 100644 iotweb/examples/mongodb/test.py create mode 100644 iotweb/examples/rabbitmq/simple_consumer.py create mode 100644 iotweb/examples/rabbitmq/simple_consumer2.py create mode 100644 iotweb/examples/rabbitmq/simple_producer.py create mode 100644 iotweb/src/User.py create mode 100644 iotweb/src/__init__.py create mode 100644 iotweb/src/__pycache__/User.cpython-312.pyc create mode 100644 iotweb/src/__pycache__/__init__.cpython-312.pyc create mode 100644 iotweb/templates/helloworld.html create mode 100644 iotweb/test.py create mode 100644 iotweb/viewer.py diff --git a/iotweb/app.py b/iotweb/app.py new file mode 100644 index 0000000..d70f579 --- /dev/null +++ b/iotweb/app.py @@ -0,0 +1,29 @@ +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 ) + diff --git a/iotweb/config.json b/iotweb/config.json new file mode 100644 index 0000000..b20571b --- /dev/null +++ b/iotweb/config.json @@ -0,0 +1,3 @@ +{ + "mongodb_connection_string": "mongodb://Justin_Mass:Amutha%401981@mongodb.selfmade.ninja:27017/?authSource=users" +} \ No newline at end of file diff --git a/iotweb/examples/mongodb/test.py b/iotweb/examples/mongodb/test.py new file mode 100644 index 0000000..3fa5762 --- /dev/null +++ b/iotweb/examples/mongodb/test.py @@ -0,0 +1,12 @@ +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 diff --git a/iotweb/examples/rabbitmq/simple_consumer.py b/iotweb/examples/rabbitmq/simple_consumer.py new file mode 100644 index 0000000..f736317 --- /dev/null +++ b/iotweb/examples/rabbitmq/simple_consumer.py @@ -0,0 +1,19 @@ + +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 diff --git a/iotweb/examples/rabbitmq/simple_consumer2.py b/iotweb/examples/rabbitmq/simple_consumer2.py new file mode 100644 index 0000000..e533282 --- /dev/null +++ b/iotweb/examples/rabbitmq/simple_consumer2.py @@ -0,0 +1,19 @@ + +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 diff --git a/iotweb/examples/rabbitmq/simple_producer.py b/iotweb/examples/rabbitmq/simple_producer.py new file mode 100644 index 0000000..c21ad0d --- /dev/null +++ b/iotweb/examples/rabbitmq/simple_producer.py @@ -0,0 +1,25 @@ +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 diff --git a/iotweb/src/User.py b/iotweb/src/User.py new file mode 100644 index 0000000..3187077 --- /dev/null +++ b/iotweb/src/User.py @@ -0,0 +1,4 @@ +import pymongo +class User: + def __init__(self, id): + print("Init user with {}".format(id)) \ No newline at end of file diff --git a/iotweb/src/__init__.py b/iotweb/src/__init__.py new file mode 100644 index 0000000..8b8e8ff --- /dev/null +++ b/iotweb/src/__init__.py @@ -0,0 +1,8 @@ + + +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 diff --git a/iotweb/src/__pycache__/User.cpython-312.pyc b/iotweb/src/__pycache__/User.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..94c770eb128268051e9e101cccafe7678172a225 GIT binary patch literal 163 zcmX@j%ge<81hth<(?RrO5P=Rpvj9b=GgLBYGWxA#C}INgK7-W!a?&qLEYi<OOe!ue z$uCMwPu0)J&rQ|ODlIO_%*#zIE;ceSFx1b?FDWR>&q_@$0Wr%{lk|&=lJ!H2Q;YNp iDt~d<<mRW8=A_ycu>y@`1ma>4<0CU8BV!RWkOcrO8YvzC literal 0 HcmV?d00001 diff --git a/iotweb/src/__pycache__/__init__.cpython-312.pyc b/iotweb/src/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..b59fc50101ef6932431fdd7f73ad7b019f6bd1c0 GIT binary patch literal 472 zcma)3F-rq66i)80qP<qcLDz~dhtSl{(n-1q9dvU!u6OB~v%MTis+_dY!9fwxO=mah zPjM7Mf}6Mt=hVsM+Un{D@8!Mseeb=HJeSKwpzwI}88#pQA3B*s<vB7fxd#Eb07oE& zj^WQ4O<T%LW!ZspN(F~d{PF`B7-K^b3PVF^U5<ef73T5ZIi7{%Sw93RaLm?u9Uapi zMp2&&68AjL>$TbrB2*-dM%WiRJi~3|)3`&zYQ$+QA#+S=(FDh`kT4PpX)){ts%W0z zRN7Om+aX;nE$z9!T%}T(G|{QYP}gg7E-3Rt{Lk&2`N(xiOoZ!JlT;Q%EZpfDW~<qH zKHpSs1m0Ggqy6s%P+GaO9}ZvajnUq_wRE|4>s&e4+ppGIrevVyz`XGZu#!rd{)Wlo bZjT22E^e@8mFkD$y8dNC2(yI(T+g*X#13)C literal 0 HcmV?d00001 diff --git a/iotweb/templates/helloworld.html b/iotweb/templates/helloworld.html new file mode 100644 index 0000000..78c027e --- /dev/null +++ b/iotweb/templates/helloworld.html @@ -0,0 +1,158 @@ +<!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 diff --git a/iotweb/test.py b/iotweb/test.py new file mode 100644 index 0000000..890ed9d --- /dev/null +++ b/iotweb/test.py @@ -0,0 +1,3 @@ +from src import get_config + +print(get_config('mongodb_connection_string')) diff --git a/iotweb/viewer.py b/iotweb/viewer.py new file mode 100644 index 0000000..ce5ad04 --- /dev/null +++ b/iotweb/viewer.py @@ -0,0 +1,24 @@ + +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) -- GitLab