Skip to content
Snippets Groups Projects
Commit f58cb0e2 authored by Rolf Niepraschk's avatar Rolf Niepraschk
Browse files

DevHub examples

parent 7b714036
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
# Rolf Niepraschk, Rolf.Niepraschk@ptb.de, 2023-03-08
import socket, signal, sys, time
import json
CONTROLER_HOST = 'e75463'
CONTROLER_PORT_V1_V2 = '10001'
CONTROLER_PORT_V3_V4 = '10003'
DEVHUB_URL = 'http://localhost:9009/'
HTTP_HEADER = 'Content-Type: application/json'
DATA_TEMPLATE = { 'Action':'TCP', 'Host':CONTROLER_HOST }
VALVE = {
'V1': { 'Port':10001, 'Index':0 },
'V2': { 'Port':10001, 'Index':1 },
'V3': { 'Port':10003, 'Index':0 },
'V4': { 'Port':10003, 'Index':1 }
}
PROMPT = 'J/N ? '
def _find_getch():
# https://stackoverflow.com/questions/510357/how-to-read-a-single-character-from-the-user
try:
import termios
except ImportError:
# Non-POSIX. Return msvcrt's (Windows') getch.
import msvcrt
return msvcrt.getch
# POSIX system. Create and return a getch that manipulates the tty.
import sys, tty
def _getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
return _getch
getch = _find_getch()
def y_n_input(prompt, true_chars):
print (prompt, end='', flush=True)
x = getch()
return x in true_chars
def is_movement_needed(n, pos):
x = False
print('Position des Ventils {}: {}mm'.format(n, pos))
if pos != 0:
corr = -pos
print('Soll Ventil {} um {}mm zur Nullposition gefahren werden?'
.format(n, corr))
x = y_n_input(PROMPT, ['j','J'])
else:
print('{} ist bereits auf Nullposition. Es ist nichts zu tun.'
.format(n))
print()
return x
def get_pos(n):
port = VALVE[n]['Port']
idx = VALVE[n]['Index']
d = DATA_TEMPLATE.copy()
d['Port'] = port
d['Value'] = 'xyz ' + str(idx)
# TODO: devhub-Kommunikation
pos = 8
print('==> get_pos: ' + json.dumps(d))
return pos
def move_rel(n, dist):
port = VALVE[n]['Port']
idx = VALVE[n]['Index']
d = DATA_TEMPLATE.copy()
d['Port'] = port
d['Value'] = 'uvw ' + str(dist)
# TODO: devhub-Kommunikation
print('==> move: ' + json.dumps(d))
pos = 0
return pos
print('\n')
###print(json.dumps(DATA_TEMPLATE))
print('='*64)
for name in VALVE:
pos = get_pos(name)
mov = is_movement_needed(name, pos)
if mov:
print('\nVentil {} wird um {}mm bewegt ...'.format(name, -pos))
res = move_rel(name, -pos)
print('Resultat: {}mm'.format(res))
print('='*64)
print('\nEnde!')
sys.exit(0)
#!/usr/bin/env python3
# Rolf Niepraschk, Rolf.Niepraschk@ptb.de, 2023-03-08
import socket, signal, sys, time
import json
DEVHUB_URL = 'http://localhost:9009/'
HTTP_HEADER = 'Content-Type: application/json'
DATA_TEMPLATE = { 'Action':'TCP', 'Host':CONTROLER_HOST }
print('\nEnde!')
sys.exit(0)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment