return peripheral_data_last_sent = time.time() # Send sensor data for device_id, value in data.items(): ansible.send_message('UPDATE_PERIPHERAL', { 'peripheral': { 'name': 'sensor_{}'.format(device_id), 'peripheralType':'SENSOR_BOOLEAN', 'value': value, 'id': device_id } }) while True: msg = ansible.recv() # Handle any incoming commands from the UI if msg: msg_handling(msg) # Send whether or not robot is executing code ansible.send_message('UPDATE_STATUS', { 'status': {'value': robot_status} }) # Send battery level ansible.send_message('UPDATE_BATTERY', { 'battery': { 'value': 100 # TODO: Make this not a lie } })
'1231': 'MotorB', '1232': 'LimitA', '1233': 'LineA', '1234': 'PotentiometerA', '1235': 'EncoderA', '1236': 'ColorThing', '1237': 'MetalDetectorA', '1238': 'ScalarA', '1239': 'ServoA', '1240': 'TempMotor', '1241': 'TempLine' } while True: batteryLevel = random.uniform(0, 12.0) mc.set('gamepad', {'time': datetime.now()}) # sending arbitary data to API msg = ansible.recv() if msg: msg_type = msg['header']['msg_type'] if msg_type == 'execute' and not robotStatus: with open('student_code.py', 'w+') as f: f.write(msg['content']['code']) student_proc = subprocess.Popen( ['python', '-u', 'student_code.py'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) lines_iter = iter(student_proc.stdout.readline, b'') console_proc = multiprocessing.Process(target=log_output, args=(lines_iter, )) console_proc.start() robotStatus = 1 print 'Running student code'