-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
166 lines (132 loc) · 6.23 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os
import shutil
from flask import Flask, render_template, request, json, send_file
from flask.json import jsonify
from engineio.payload import Payload
from flask_socketio import SocketIO, emit
from flask_mqtt import Mqtt
from flask_cors import CORS
import firmwareGenerator
from Models.RobotModel import RoboticArm, Servo, ServoRange, Wifi, MqttConfig
app = Flask(__name__)
CORS(app)
app.config['SECRET'] = 'mysecretkeydkjasndjkasndkjoejigtoinfijsdfubyievyibdw'
app.config['MQTT_BROKER_URL'] = 'mqtt.iot.ideamart.io'
app.config['MQTT_BROKER_PORT'] = 1883
app.config['MQTT_USERNAME'] = 'tester2-tesingdev2-v3_3220'
app.config['MQTT_PASSWORD'] = '1572435365_3220'
app.config['MQTT_KEEPALIVE'] = 5
mqtt = Mqtt(app)
Payload.max_decode_packets = 50
socketio = SocketIO(app, cors_allowed_origins="*")
pubTopicMqtt = "tester2/tesingdev2/v3/common" # Subscribe topic for server
subTopicMqtt = "sub/1119/tester2/tesingdev2/v3/pub" # Publish topic for server
d_pin = [12, 26, 32, 14, 18, 22] # Default digital pins for esp32
a_pin = [13, 27, 33, 25, 19, 23] # Default analog pins for esp32
@app.route('/rovoSpec', methods=['POST'])
def specification():
# select firmware based on microController type & connectivity module
src = 'firmwareTemplates/ServoReadHardwareTrial_ESP32_multiservo_SimModule'
dest = 'output_generated_firmware/firmware_v1.0/main'
print(f'Raw data: {request.get_json()}')
robot_model = RoboticArm.json_to_obj(request.get_json())
if robot_model.micro_c != "Arduino UNO R3" and robot_model.com_module == "WiFi":
print("ESP-WiFi Selected.")
src = 'firmwareTemplates/ServoReadHardwareTrial_ESP32_multiservo_Wi-Fi'
elif robot_model.micro_c == "Arduino UNO R3" and robot_model.com_module == "SIMCOM 7000":
print("UNO-SIM7000C Selected.")
src = 'firmwareTemplates/ServoReadHardwareTrial_uno_multiservo_SimModule'
firmwareGenerator.select_template(src, dest)
print(f'DOF: {robot_model.dof}')
print("***************************")
servo = Servo.json_to_obj(robot_model.dof_row_obj)
print(f'servoType: {servo.selected_type}')
print("***************************")
servo_range = ServoRange.json_to_obj(servo.servo_range)
print(f'servo_range_format: {servo_range}')
print("***************************")
wifi = Wifi.json_to_obj(robot_model.wifi)
print(f'Wifi username:{wifi.username}')
print("***************************")
mqtt_config = MqttConfig.json_to_obj(robot_model.mqtt_config)
# Fetch data to template
firmwareGenerator.map_of_spec_val["servo_count_dof"] = robot_model.dof
# "Dpin_list": "{12, 26, 32, 14}", # default
# "Apin_list": "{13, 27, 33, 25}", # default
print(f'Selected D_pins: {d_pin[:robot_model.dof]}')
print(f'Selected A_pins: {a_pin[:robot_model.dof]}')
firmwareGenerator.map_of_spec_val["Dpin_list"] = f'{{{str(d_pin[:robot_model.dof])[1:-1]}}}'
firmwareGenerator.map_of_spec_val["Apin_list"] = f'{{{str(a_pin[:robot_model.dof])[1:-1]}}}'
firmwareGenerator.map_of_spec_val["servo_range_list"] = servo_range
if robot_model.mqtt_default:
firmwareGenerator.map_of_spec_val["mqtt_setting"] = firmwareGenerator.map_of_spec_val["mqtt_setting"]
else:
firmwareGenerator.map_of_spec_val["mqtt_setting"]["mqtt_host"] = str(mqtt_config.host)
firmwareGenerator.map_of_spec_val["mqtt_setting"]["mqtt_port"] = str(mqtt_config.port)
firmwareGenerator.map_of_spec_val["mqtt_setting"]["mqtt_username"] = str(mqtt_config.username)
firmwareGenerator.map_of_spec_val["mqtt_setting"]["mqtt_password"] = str(mqtt_config.password)
firmwareGenerator.map_of_spec_val["mqtt_setting"]["pub_topic"] = str(mqtt_config.pub_topic)
firmwareGenerator.map_of_spec_val["mqtt_setting"]["sub_topic"] = str(mqtt_config.sub_topic)
print(firmwareGenerator.map_of_spec_val["mqtt_setting"])
if wifi.username is not None:
firmwareGenerator.map_of_spec_val["wifi_setting"]["username"] = str(wifi.username)
firmwareGenerator.map_of_spec_val["wifi_setting"]["password"] = str(wifi.password)
# print("robot:" + str(robot_model.dof))
# print("dict val:" + str(firmwareGenerator.map_of_spec_val["servo_count_dof"]))
# Generate firmware
firmwareGenerator.generate_firmware()
# Generate zip
generated_package = "output_generated_firmware/firmware_v1.0"
shutil.make_archive("output_zip/firmware_v1.0", 'zip', generated_package)
f = open('output_zip/firmware_v1.0.zip', 'rb')
remove_dest()
return f.read()
def remove_dest():
rmv_dir = 'output_generated_firmware/firmware_v1.0/main'
for f in os.listdir(rmv_dir):
os.remove(os.path.join(rmv_dir, f))
@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
# mqtt.subscribe('#')
mqtt.subscribe('tester2/tesingdev2/v3/common')
print("MQTT Connected.")
# @socketio.on('socket')
# def test_message(message):
# print('connect response EVENT >>>>')
# # emit('my response', {'data': message['data']}, broadcast=True)
# print("connect response from client: " + message)
@socketio.on('connect')
def init_connect():
emit('connect_socket', "Msg From server, Socket Connected")
print('IN CONNECT >>>>')
@socketio.on('my event')
def test_message(message):
print('IN MY EVENT >>>>')
print("received msg from client !!!:" + message['data'])
mqtt.publish(subTopicMqtt, message['data'])
# emit('my response', {'data': message})
@socketio.on('unSubscribe_all')
def sub_disconnect():
print('IN DISCONNECT >>>>')
print('Client disconnected')
mqtt.unsubscribe_all()
@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
data = dict(
topic=message.topic,
payload=json.loads(message.payload.decode())
)
print("my MSG : " + message.payload.decode())
# emit a mqtt_message event to the socket containing the message data
# if data['topic'] == pubTopicMqtt:
socketio.emit('mqtt_message', data=data)
# socketIo.sleep(0)
# elif data['topic'] == subTopicMqtt:
# socketIo.emit('mqtt_message', data=data)
# else:
# print("unauthorized data:" + data[''])
@mqtt.on_log()
def handle_logging(client, userdata, level, buf):
print(level, buf)
if __name__ == '__main__':
socketio.run(app)