Skip to content

Commit 300d5e5

Browse files
authored
This is the ussd bit
With this bit one can be able to report sitings of places where trash has been dumped wrecklessly.
1 parent ff7f39e commit 300d5e5

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

Diff for: mqtt_client.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#my code
2+
from flask import Flask, request
3+
import threading
4+
import paho.mqtt.client as mqtt
5+
import requests
6+
import json
7+
8+
import os
9+
10+
response = ""
11+
######added now........unique = ['1','mars', 'elvis', 'Kori', 'brian','205', 'keith']
12+
mqtt_data = {}
13+
14+
def start_mqtt_subscriber():
15+
#####i doubt whether this 2 lines of code are functional
16+
clientid="14"
17+
client = mqtt.Client(client_id=clientid)
18+
19+
def on_connect(client, userdata, flags, rc):
20+
if rc == 0:
21+
# data = request.get_json()
22+
# Perform an HTTP request
23+
# url = data.get('url')
24+
# response = requests.get(url)
25+
client.subscribe("keith",0) # Subscribe to all topics under the client's ID
26+
27+
28+
print("Connected to MQTT broker")
29+
30+
else:
31+
print(f"Connection failed with code {rc}")
32+
33+
def on_message(client, userdata, message):
34+
35+
topic = message.topic
36+
answer = message.payload.decode()
37+
#payload = message.payload.decode()
38+
print(f"Received message on topic: {topic}, payload: {answer}")
39+
if topic == "P1":
40+
mqtt_data["P1"]=answer
41+
if topic == "P2":
42+
mqtt_data["P2"]=answer
43+
if topic == "P3":
44+
mqtt_data["P3"]=answer
45+
if topic == "P4":
46+
mqtt_data["P4"]=answer
47+
if topic == "P5":
48+
mqtt_data["P5"]=answer
49+
if topic == "P6":
50+
mqtt_data["P6"]=answer
51+
52+
53+
# Process the incoming MQTT message here above
54+
55+
url = 'http://localhost:5000/post_data'
56+
# payload = {'temp': 10}
57+
r = requests.post(url, json=mqtt_data)
58+
print(r.text)
59+
60+
61+
client.on_connect = on_connect
62+
client.on_message = on_message
63+
broker_address = "mqtt.eclipseprojects.io" # Replace with your broker's address
64+
client.connect(broker_address, 1883, 30)
65+
66+
67+
68+
69+
# Start the MQTT subscriber loop
70+
client.loop_forever()
71+
72+
if __name__ == '__main__':
73+
start_mqtt_subscriber()
74+
75+
76+
77+

Diff for: server.py

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#my code
2+
from flask import Flask, request
3+
import threading
4+
import paho.mqtt.client as mqtt
5+
import requests
6+
import json
7+
from mqtt_client import start_mqtt_subscriber
8+
import random
9+
10+
11+
12+
app = Flask(__name__)
13+
14+
import os
15+
16+
response = ""
17+
#identity = data_queue.get()
18+
mqtt_data = {}
19+
20+
21+
22+
23+
def calculating(amount):
24+
try:
25+
price = float(amount) * 1
26+
except Exception as e:
27+
raise Exception("Amount cannot be converted to valid number. Check and try again")
28+
return price
29+
30+
#///creating the methods of communiction
31+
32+
33+
34+
@app.route('/', methods=['POST', 'GET'])
35+
def ussd_callback():
36+
37+
print
38+
global response
39+
global mqtt_data
40+
session_id = request.values.get("sessionId", None)#/////getting the session id
41+
service_code = request.values.get("serviceCode", None)#//////////getting the service code
42+
phone_number = request.values.get("phoneNumber", None)#getting the phone number that requested
43+
text =request.values.get("text", "default")#getting the request
44+
session_state = text.split('*')
45+
46+
current_level = len(session_state)
47+
48+
49+
50+
51+
52+
53+
if current_level == 1:
54+
response = "CON Hello and welcome team Zuri Bin.We work with you to deliver your domestic waste prompty; when you need it.\n"
55+
response += "1. Report someone 's trash\n"
56+
response += "2. Report my own trash"
57+
58+
elif current_level == 2 and session_state[1] == '1':
59+
response = "CON Hello and thank you for reporting trash. Kindly choose location\n"
60+
response += "1:Nairobi\n"
61+
response += "2:We are currently not in other regions"
62+
63+
64+
65+
elif current_level ==2 and session_state[1]== '2':
66+
response = "END Thanks we will try our best to get into your region."
67+
68+
elif current_level ==3 and session_state[2]== '1':
69+
response = "CON Kindly specify your region\n"
70+
response += "1:CBD\n"
71+
response += "2:Pipeline\n"
72+
response += "3:Kileleshwa\n"
73+
response += "4:Langata"
74+
75+
elif current_level == 4 and session_state[3] == '1':
76+
response = "CON How big is your trash\n"
77+
response += "1:Big\n"
78+
response += "2:Medium\n"
79+
response += "3:Small"
80+
81+
elif current_level == 5 and session_state[4] == '1':
82+
response = 'END Thank you. Your concern is noted.'
83+
84+
elif current_level == 5 and session_state[4] == '2':
85+
response = 'END Thank you. Your concern is noted.'
86+
87+
elif current_level == 5 and session_state[4] == '3':
88+
response = 'END Thank you. Your concern is noted.'
89+
90+
elif current_level == 5 and session_state[3] == '2':
91+
response = 'END Apparently we have not reached this region\n.'
92+
elif current_level == 5 and session_state[3] == '3':
93+
response = 'END Apparently we have not reached this region\n.'
94+
elif current_level == 5 and session_state[3] == '4':
95+
response = 'END Apparently we have not reached this region\n.'
96+
97+
return response
98+
99+
100+
101+
#Receive response from africas talking
102+
@app.route('/call', methods=['POST'])
103+
def call_back_client():
104+
return '<Response> <Dial phoneNumbers="" maxDuration="5"/></Response>'
105+
106+
107+
if __name__ == '__main__':
108+
mqtt_thread = threading.Thread(target=start_mqtt_subscriber)
109+
110+
mqtt_thread.daemon = True
111+
# Daemonize the thread so it exits when the main thread exits
112+
mqtt_thread.start()
113+
app.run(host="0.0.0.0", port=os.environ.get('PORT'))
114+
115+
116+
117+

0 commit comments

Comments
 (0)