-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
101 lines (82 loc) · 3.07 KB
/
server.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
from flask import Flask, request, jsonify
from rigol import DP832, ArgumentError, InitError
app = Flask(__name__)
device = DP832()
json_header = {'Content-Type': 'application/json'}
def response(q):
return jsonify(q.to_dict())
@app.route("/")
def index():
return jsonify(device.idn()), 200, json_header
@app.route("/<channel_name>")
def state(channel_name):
channel = device[channel_name]
if not channel:
return jsonify(error="No such channel: " + channel_name), 404, json_header
return jsonify(response=channel.state), 200, json_header
@app.route("/<channel_name>/on")
def on(channel_name):
channel = device[channel_name]
if not channel:
return jsonify(error="No such channel: " + channel_name), 404, json_header
channel.state = "ON"
return jsonify(response=channel.state), 200, json_header
@app.route("/<channel_name>/off")
def off(channel_name):
channel = device[channel_name]
if not channel:
return jsonify(error="No such channel: " + channel_name), 404, json_header
channel.state = "OFF"
return jsonify(response=channel.state), 200, json_header
@app.route("/<channel_name>/voltage")
def voltage(channel_name):
channel = device[channel_name]
if not channel:
return jsonify(error="No such channel: " + channel_name), 404, json_header
value = request.args.get("set")
if value:
channel.voltage = value
return jsonify(response=channel.voltage), 200, json_header
@app.route("/<channel_name>/current_limit")
def current_limit(channel_name):
channel = device[channel_name]
if not channel:
return jsonify(error="No such channel: " + channel_name), 404, json_header
value = request.args.get("set")
if value:
channel.current_limit = value
return jsonify(response=channel.current_limit), 200, json_header
@app.route("/<channel_name>/current")
def current(channel_name):
channel = device[channel_name]
if not channel:
return jsonify(error="No such channel: " + channel_name), 404, json_header
value = request.args.get("set")
if value:
channel.current = value
return jsonify(response=channel.current), 200, json_header
@app.route("/<channel_name>/u")
def u(channel_name):
channel = device[channel_name]
if not channel:
return jsonify(error="No such channel: " + channel_name), 404, json_header
return jsonify(response=channel.u), 200, json_header
@app.route("/<channel_name>/i")
def i(channel_name):
channel = device[channel_name]
if not channel:
return jsonify(error="No such channel: " + channel_name), 404, json_header
return jsonify(response=channel.i), 200, json_header
@app.route("/<channel_name>/p")
def p(channel_name):
channel = device[channel_name]
if not channel:
return jsonify(error="No such channel: " + channel_name), 404, json_header
return jsonify(response=channel.p), 200, json_header
@app.route("/select")
def select():
return response(device.select(request.args.get("channel")))
if __name__ == "__main__":
device.start()
app.run(debug=False)
device.close()