|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import websocket |
| 4 | +import json |
| 5 | +import time |
| 6 | +import csv |
| 7 | +import argparse |
| 8 | +import sys |
| 9 | + |
| 10 | + |
| 11 | +STD_HEADER = [ |
| 12 | + 'stamp', |
| 13 | + 'runtime', |
| 14 | + 'temperature', |
| 15 | + 'target', |
| 16 | + 'state', |
| 17 | + 'heat', |
| 18 | + 'totaltime', |
| 19 | + 'profile', |
| 20 | +] |
| 21 | + |
| 22 | + |
| 23 | +PID_HEADER = [ |
| 24 | + 'pid_time', |
| 25 | + 'pid_timeDelta', |
| 26 | + 'pid_setpoint', |
| 27 | + 'pid_ispoint', |
| 28 | + 'pid_err', |
| 29 | + 'pid_errDelta', |
| 30 | + 'pid_p', |
| 31 | + 'pid_i', |
| 32 | + 'pid_d', |
| 33 | + 'pid_kp', |
| 34 | + 'pid_ki', |
| 35 | + 'pid_kd', |
| 36 | + 'pid_pid', |
| 37 | + 'pid_out', |
| 38 | +] |
| 39 | + |
| 40 | + |
| 41 | +def logger(hostname, csvfile, noprofilestats, pidstats, stdout): |
| 42 | + status_ws = websocket.WebSocket() |
| 43 | + |
| 44 | + csv_fields = [] |
| 45 | + if not noprofilestats: |
| 46 | + csv_fields += STD_HEADER |
| 47 | + if pidstats: |
| 48 | + csv_fields += PID_HEADER |
| 49 | + |
| 50 | + out = open(csvfile, 'w') |
| 51 | + csv_out = csv.DictWriter(out, csv_fields, extrasaction='ignore') |
| 52 | + csv_out.writeheader() |
| 53 | + |
| 54 | + if stdout: |
| 55 | + csv_stdout = csv.DictWriter(sys.stdout, csv_fields, extrasaction='ignore', delimiter='\t') |
| 56 | + csv_stdout.writeheader() |
| 57 | + else: |
| 58 | + csv_stdout = None |
| 59 | + |
| 60 | + while True: |
| 61 | + try: |
| 62 | + msg = json.loads(status_ws.recv()) |
| 63 | + |
| 64 | + except websocket.WebSocketException: |
| 65 | + try: |
| 66 | + status_ws.connect(f'ws://{hostname}/status') |
| 67 | + except Exception: |
| 68 | + time.sleep(5) |
| 69 | + |
| 70 | + continue |
| 71 | + |
| 72 | + if msg.get('type') == 'backlog': |
| 73 | + continue |
| 74 | + |
| 75 | + if not noprofilestats: |
| 76 | + msg['stamp'] = time.time() |
| 77 | + if pidstats and 'pidstats' in msg: |
| 78 | + for k, v in msg.get('pidstats', {}).items(): |
| 79 | + msg[f"pid_{k}"] = v |
| 80 | + |
| 81 | + csv_out.writerow(msg) |
| 82 | + out.flush() |
| 83 | + |
| 84 | + if stdout: |
| 85 | + for k in list(msg.keys()): |
| 86 | + v = msg[k] |
| 87 | + if isinstance(v, float): |
| 88 | + msg[k] = '{:5.3f}'.format(v) |
| 89 | + csv_stdout.writerow(msg) |
| 90 | + sys.stdout.flush() |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + parser = argparse.ArgumentParser(description='Log kiln data for analysis.') |
| 95 | + parser.add_argument('--hostname', type=str, default="localhost:8081", help="The kiln-controller hostname:port") |
| 96 | + parser.add_argument('--csvfile', type=str, default="/tmp/kilnstats.csv", help="Where to write the kiln stats to") |
| 97 | + parser.add_argument('--pidstats', action='store_true', help="Include PID stats") |
| 98 | + parser.add_argument('--noprofilestats', action='store_true', help="Do not store profile stats (default is to store them)") |
| 99 | + parser.add_argument('--stdout', action='store_true', help="Also print to stdout") |
| 100 | + args = parser.parse_args() |
| 101 | + |
| 102 | + logger(args.hostname, args.csvfile, args.noprofilestats, args.pidstats, args.stdout) |
0 commit comments