WillemVH commited on
Commit
5db2c1e
·
verified ·
1 Parent(s): d399ddb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -1
app.py CHANGED
@@ -11,7 +11,24 @@ def get_servo_list(fileid):
11
  if current_line == fileid:
12
  return line_content.strip()
13
  return None
14
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @app.route('/')
16
  def main():
17
  files = os.listdir('.')
@@ -39,5 +56,53 @@ def apirequests():
39
  if servo == 3:
40
  return three
41
  # ^ identify the servo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  if __name__ == '__main__':
43
  app.run(host='0.0.0.0', port=5000, debug=False)
 
11
  if current_line == fileid:
12
  return line_content.strip()
13
  return None
14
+ def update_servo_in_database(fileid, new_value):
15
+ try:
16
+ fileid = int(fileid)
17
+ #steal the lines
18
+ with open('database.txt', 'r') as file:
19
+ lines = file.readlines()
20
+ # make sure the line is not scamming me and is fake
21
+ if fileid < 1 or fileid > len(lines):
22
+ return False
23
+ #update it
24
+ lines[fileid - 1] = new_value + '\n' # do the minus bc it starts a 0 not 1
25
+ #put 'em all back
26
+ with open('database.txt', 'w') as file:
27
+ file.writelines(lines)
28
+ return True
29
+ except Exception as e:
30
+ print(f"database was not really updated 😟: {e}")
31
+ return False
32
  @app.route('/')
33
  def main():
34
  files = os.listdir('.')
 
56
  if servo == 3:
57
  return three
58
  # ^ identify the servo
59
+
60
+ @app.route('/debugchange')
61
+ def debuggly():
62
+ try:
63
+ # debugchange?=id,servo_number,new_value
64
+ full_query = request.query_string.decode('utf-8')
65
+ if full_query.startswith('='):
66
+ data = full_query[1:]
67
+ # get the parts of the url
68
+ parts = data.split(',')
69
+ if len(parts) < 3:
70
+ return "please use format debugchange?=id,servo_number,new_value lol", 400
71
+
72
+ id, servo_num, new_value = parts
73
+ #create or update lines
74
+ servolist = get_servo_list(id)
75
+ if servolist is None:
76
+ # 0 as default
77
+ servos = ['0', '0', '0']
78
+ else:
79
+ # use current values
80
+ servos = servolist.split(',')
81
+ servo_num = int(servo_num)
82
+ if servo_num < 1 or servo_num > len(servos):
83
+ return f"keep the servo number between 1 and 3", 400
84
+
85
+ # make sure it's between 1 and 180
86
+ new_value = int(new_value)
87
+ if new_value < 0 or new_value > 180:
88
+ return "Error: Servo value must be between 0-180 degrees", 400
89
+
90
+ # update the servo
91
+ servos[servo_num - 1] = str(new_value) # do the minus thing because list starts a 0 not 1 :(
92
+
93
+ # save to big database
94
+ updated_line = ','.join(servos)
95
+ if update_servo_in_database(id, updated_line):
96
+ if servolist is None:
97
+ return f"created new line {id} with no problems ever! servo {servo_num} set to {new_value}°<br>New values: {updated_line}"
98
+ else:
99
+ return f"updated line {id}! servo {servo_num} set to {new_value}°<br>new values: {updated_line}"
100
+ else:
101
+ return "yeah nah that didn't work :(", 500
102
+
103
+ except ValueError as e:
104
+ return f"invalid frogmat - {e}", 400
105
+ except Exception as e:
106
+ return f"oh no big error: {str(e)}", 500
107
  if __name__ == '__main__':
108
  app.run(host='0.0.0.0', port=5000, debug=False)