<commit_before>#!/usr/bin/env python

import serial, sys, time

def heartbeat():
	send_heartbeat()

def shutdown():
	command = "/sbin/shutdown -h -t sec 30"
	import subprocess
	process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
	output = process.communicate()[0]
	print(output)
	ser.flushOutput()
	send_ack()

def abort_shutdown():
	command = "/sbin/shutdown -c"
	import subprocess
	process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
	output = process.communicate()[0]
	print(output)
	ser.flushOutput()
	send_abort()

def mode2heartbeat():
	mode = "heartbeat"

def send_heartbeat():
	ser.write(B'\x42')

def send_ack():
	ser.write(B'\x108')

def send_abort():
	ser.write(B'\x109')

try:
	serialPort = sys.argv[1]
except:
	serialPort = "/dev/ttyACM0"

ser = serial.Serial(
    port=serialPort,
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)

mode = "heartbeat"

commands = {	"heartbeat":      heartbeat,
		"shutdown":       shutdown,
		"abort shutdown": abort_shutdown,
		"ok":             mode2heartbeat,
}

heartbeats = {	"heartbeat":      send_heartbeat,
		"shutdown":       send_ack,
		"abort shutdown": send_abort,
}

ser.isOpen()
while True:
	print(mode)
	heartbeats[mode]()
	end_wait = int(round(time.time() * 1000)) + 2900
	while (int(round(time.time() * 1000)) < end_wait):
		bytesToRead = ser.inWaiting()
		if (bytesToRead > 0):
			cmd = str(ser.readline(), "utf-8")[:-2]<commit_msg>heartbeat.py<commit_after>#!/usr/bin/env python

import serial, sys, time

def heartbeat():
	send_heartbeat()

def shutdown():
	command = "/sbin/shutdown -h -t sec 30"
	import subprocess
	process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
	output = process.communicate()[0]
	print(output)
	ser.flushOutput()
	send_ack()

def abort_shutdown():
	command = "/sbin/shutdown -c"
	import subprocess
	process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
	output = process.communicate()[0]
	print(output)
	ser.flushOutput()
	send_abort()

def mode2heartbeat():
	mode = "heartbeat"

def send_heartbeat():
	ser.write(B'\x42')

def send_ack():
	ser.write(B'\x108')

def send_abort():
	ser.write(B'\x109')

try:
	serialPort = sys.argv[1]
except:
	serialPort = "/dev/ttyACM0"

ser = serial.Serial(
    port=serialPort,
    baudrate=115200,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS
)

mode = "heartbeat"

commands = {	"heartbeat":      heartbeat,
		"shutdown":       shutdown,
		"abort shutdown": abort_shutdown,
		"ok":             mode2heartbeat,
}

heartbeats = {	"heartbeat":      send_heartbeat,
		"shutdown":       send_ack,
		"abort shutdown": send_abort,
}

ser.isOpen()
while True:
	print(mode)
	heartbeats[mode]()
	end_wait = int(round(time.time() * 1000)) + 2950
	while (int(round(time.time() * 1000)) < end_wait):
		bytesToRead = ser.inWaiting()
		if (bytesToRead > 0):
			cmd = str(ser.readline(), "utf-8")[:-2]
{'fraction_non_alphanumeric': 0.1546942866688941, 'fraction_numerical': 0.024724356832609422, 'mean_word_length': 4.252631578947368, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2298951', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1136, 'n_words': 217}