#!/usr/bin/env python3 ''' 用RESTful API控制电机运动 ''' import subprocess from flask import Flask import RPi.GPIO as GPIO from worker import * from time import sleep APP = Flask(__name__) @APP.route('/carousel/clockwise') def carousel_clockwise(): carousel.start(GPIO.HIGH) return 'Done' @APP.route('/carousel/counter-clockwise') def carousel_counter_clockwise(): carousel.start(GPIO.LOW) return 'Done' @APP.route('/carousel/stop') def carousel_stop(): carousel.stop() return 'Done' @APP.route('/carousel/presets/clockwise-90') def carousel_preset_clockwise_90(): carousel.forward_degree(90) return 'Done' @APP.route('/carousel/presets/counter-clockwise-90') def carousel_preset_counter_clockwise_90(): carousel.backward_degree(90) return 'Done' @APP.route('/elevator/down') def elevator_down(): elevator.start(GPIO.HIGH) return 'Done' @APP.route('/elevator/up') def elevator_up(): elevator.start(GPIO.LOW) return 'Done' @APP.route('/elevator/stop') def elevator_stop(): elevator.stop() return 'Done' @APP.route('/elevator/presets/down-2-7') def elevator_preset_down_2_7(): elevator.forward_degree(7200 * 2.7) return 'Done' @APP.route('/elevator/presets/up-2-7') def elevator_preset_up_2_7(): elevator.backward_degree(7200 * 2.7) return 'Done' @APP.route('/conveyor/output') def conveyor_output(): conveyor.start(GPIO.LOW) return 'Done' @APP.route('/conveyor/recycle') def conveyor_recycle(): conveyor.start(GPIO.HIGH) return 'Done' @APP.route('/conveyor/stop') def conveyor_stop(): conveyor.stop() return 'Done' @APP.route('/conveyor/presets/output') def conveyor_preset_output(): conveyor.backward_degree(360 * 2) return 'Done' @APP.route('/conveyor/presets/recycle') def conveyor_preset_recycle(): conveyor.forward_degree(360 * 2) return 'Done' @APP.route('/watchdog/open') def watchdog_open(): watchdog.start(GPIO.HIGH) return 'Done' @APP.route('/watchdog/close') def watchdog_close(): watchdog.start(GPIO.LOW) return 'Done' @APP.route('/watchdog/stop') def watchdog_stop(): watchdog.stop() return 'Done' @APP.route('/watchdog/presets/open') def watchdog_preset_open(): watchdog.backward_degree(360 * 2) return 'Done' @APP.route('/watchdog/presets/close') def watchdog_preset_close(): watchdog.forward_degree(360 * 2) return 'Done' @APP.route('/recycleh/moveout') def recycleh_moveout(): recycleh.start(GPIO.LOW) return 'Done' @APP.route('/recycleh/movein') def recycleh_movein(): recycleh.start(GPIO.HIGH) return 'Done' @APP.route('/recycleh/stop') def recycleh_stop(): recycleh.stop() return 'Done' @APP.route('/recycleh/presets/moveout') def recycleh_preset_moveout(): recycleh.forward_degree(360 * 2) return 'Done' @APP.route('/recycleh/presets/movein') def recycleh_preset_movein(): recycleh.backward_degree(360 * 2) return 'Done' @APP.route('/recyclev/rise') def recyclev_rise(): recyclev.start(GPIO.HIGH) return 'Done' @APP.route('/recyclev/fall') def recyclev_fall(): recyclev.start(GPIO.LOW) return 'Done' @APP.route('/recyclev/stop') def recyclev_stop(): recyclev.stop() return 'Done' @APP.route('/recyclev/presets/rise') def recyclev_preset_rise(): recyclev.backward_degree(360 * 2) return 'Done' @APP.route('/recyclev/presets/fall') def recyclev_preset_fall(): recyclev.forward_degree(360 * 2) return 'Done' @APP.route('/bag-pusher/push') def bag_pusher_push(): bag_pusher.start(GPIO.HIGH) return 'Done' @APP.route('/bag-pusher/loose') def bag_pusher_loose(): bag_pusher.start(GPIO.LOW) return 'Done' @APP.route('/bag-pusher/stop') def bag_pusher_stop(): bag_pusher.stop() return 'Done' @APP.route('/bag-pusher/presets/push') def bag_pusher_preset_push(): bag_pusher.backward_degree(360) return 'Done' @APP.route('/bag-pusher/presets/loose') def bag_pusher_preset_loose(): bag_pusher.forward_degree(360) return 'Done' @APP.route('/scissors-pusher/close') def scissors_pusher_close(): scissors_pusher.start(GPIO.HIGH) return 'Done' @APP.route('/scissors-pusher/far') def scissors_pusher_far(): scissors_pusher.start(GPIO.LOW) return 'Done' @APP.route('/scissors-pusher/stop') def scissors_pusher_stop(): scissors_pusher.stop() return 'Done' @APP.route('/scissors-pusher/presets/close') def scissors_pusher_preset_close(): scissors_pusher.backward_degree(360) return 'Done' @APP.route('/scissors-pusher/presets/far') def scissors_pusher_preset_far(): scissors_pusher.forward_degree(360) return 'Done' @APP.route('/scissors/cut') def scissors_cut(): scissors.degree(150) return 'Done' @APP.route('/scissors/loose') def scissors_loose(): scissors.degree(10) return 'Done' @APP.route('/spigot/open') def spigot_open(): spigot.enable() return 'Done' @APP.route('/spigot/close') def spigot_close(): spigot.disable() return 'Done'