Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make things shorter #2

Open
wants to merge 3 commits into
base: 2025
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 50 additions & 240 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,16 @@
import time
from google.oauth2.service_account import Credentials
from google.auth.transport.requests import AuthorizedSession
import gspread
from gpiozero import Button
from datetime import datetime, date
from datetime import datetime
import requests # Typo corrected from "urequets"

print("Loaded modules")


# Replace with your Google Sheet ID and JSON key file path
sheet_id = '1u2M_qIo_XLVv5HPKYmyX_ehruxVFECf8vmXNKnErk4U'
json_key_file = '/creds/service_account.json'
API_URL = "http://api.henriserverack.com/get_sheet_data"

# Specify GPIO pins for buttons and LEDs

##set 1 (left?)
button1_pin = 2 # Adjust pin numbers as needed
button2_pin = 3
button3_pin = 4
button4_pin = 17
button5_pin = 27
button6_pin = 22
button7_pin = 10
button8_pin = 9
button9_pin = 11
button10_pin = 5
button11_pin = 6
button12_pin = 13
button13_pin = 19
button14_pin = 26

##set 2 (right?)
button15_pin = 14
button16_pin = 15
button17_pin = 18
button18_pin = 23
button19_pin = 24
button20_pin = 25
button21_pin = 8
button22_pin = 7




# Create Button objects
button1 = Button(button1_pin)
button2 = Button(button2_pin)
button3 = Button(button3_pin)
button4 = Button(button4_pin)
button5 = Button(button5_pin)
button6 = Button(button6_pin)
button7 = Button(button7_pin)
button8 = Button(button8_pin)
button9 = Button(button9_pin)
button10 = Button(button10_pin)
button11 = Button(button11_pin)
button12 = Button(button12_pin)
button13 = Button(button13_pin)
button14 = Button(button14_pin)
button15 = Button(button15_pin)
button16 = Button(button16_pin)
button17 = Button(button17_pin)
button18 = Button(button18_pin)
button19 = Button(button19_pin)
button20 = Button(button20_pin)
button21 = Button(button21_pin)
button22 = Button(button22_pin)


# Authenticate with Google Sheets API using google-auth
SCOPES = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
Expand All @@ -76,193 +19,60 @@
worksheet = gc.open_by_key(sheet_id).sheet1
print("Loaded credentials")


def make_api_request():
response = requests.get(API_URL) # Use authorized session
data = response.json()
return data

# Function to add a row to the spreadsheet
def add_row(timestamp, action, name):
worksheet.append_row([timestamp, action, name], value_input_option='USER_ENTERED')

# Function to check if a person is currently clocked in
def is_clocked_in(name):
api_data = make_api_request()
if name == "Mason":
if api_data["data"][0] == "Clocked In":
return True
if api_data["data"][0] == "Not Clocked In":
return False
if name == "Cameron":
if api_data["data"][1] == "Clocked In":
return True
if api_data["data"][1] == "Not Clocked In":
return False
if name == "Jonah":
if api_data["data"][2] == "Clocked In":
return True
if api_data["data"][2] == "Not Clocked In":
return False
if name == "Jerry":
if api_data["data"][3] == "Clocked In":
return True
if api_data["data"][3] == "Not Clocked In":
return False
if name == "Henri K":
if api_data["data"][4] == "Clocked In":
return True
if api_data["data"][4] == "Not Clocked In":
return False
if name == "Liam":
if api_data["data"][5] == "Clocked In":
return True
if api_data["data"][5] == "Not Clocked In":
return False
if name == "Ki Bae":
if api_data["data"][6] == "Clocked In":
return True
if api_data["data"][6] == "Not Clocked In":
return False
if name == "Oliver":
if api_data["data"][7] == "Clocked In":
return True
if api_data["data"][7] == "Not Clocked In":
return False
if name == "Clark":
if api_data["data"][8] == "Clocked In":
return True
if api_data["data"][8] == "Not Clocked In":
return False
if name == "Rain":
if api_data["data"][9] == "Clocked In":
return True
if api_data["data"][9] == "Not Clocked In":
return False
if name == "Malcom":
if api_data["data"][10] == "Clocked In":
return True
if api_data["data"][10] == "Not Clocked In":
return False
if name == "Henry N":
if api_data["data"][11] == "Clocked In":
return True
if api_data["data"][11] == "Not Clocked In":
return False
if name == "Auggie":
if api_data["data"][12] == "Clocked In":
return True
if api_data["data"][12] == "Not Clocked In":
return False
if name == "Anders":
if api_data["data"][13] == "Clocked In":
return True
if api_data["data"][13] == "Not Clocked In":
return False
if name == "Victor":
if api_data["data"][14] == "Clocked In":
return True
if api_data["data"][14] == "Not Clocked In":
return False
if name == "Finn":
if api_data["data"][15] == "Clocked In":
return True
if api_data["data"][15] == "Not Clocked In":
return False
if name == "Sam":
if api_data["data"][16] == "Clocked In":
return True
if api_data["data"][16] == "Not Clocked In":
return False
if name == "Neah":
if api_data["data"][17] == "Clocked In":
return True
if api_data["data"][17] == "Not Clocked In":
return False
if name == "Sophia":
if api_data["data"][18] == "Clocked In":
return True
if api_data["data"][18] == "Not Clocked In":
return False
if name == "Eddie":
if api_data["data"][19] == "Clocked In":
return True
if api_data["data"][19] == "Not Clocked In":
return False
if name == "Sadie":
if api_data["data"][20] == "Clocked In":
return True
if api_data["data"][20] == "Not Clocked In":
return False
if name == "Alena":
if api_data["data"][21] == "Clocked In":
return True
if api_data["data"][21] == "Not Clocked In":
return False
def button_pressed(api_id, name):
global api_data
clocked_in = (api_data["data"][api_id] == "Clocked In")

action = "Clock Out" if clocked_in else "Clock In"
timestamp = datetime.now().strftime("%m/%d/%Y %H:%M:%S")
add_row(timestamp, action, name)

# Logging
print(f"[{timestamp}] {action}: {name}")

def define_button(api_id, pin, name):
# TODO: Do these need debouncing? Default is to not debounce at all
button = Button(pin)
button.when_pressed = lambda: button_pressed(api_id, name)


# Get initial data
api_data = make_api_request()

# api_id, pin, name
define_button( 0, 2, "Mason")
define_button( 1, 3, "Cameron")
define_button( 2, 4, "Jonah")
define_button( 3, 17, "Jerry")
define_button( 4, 27, "Henri K")
define_button( 5, 22, "Liam")
define_button( 6, 10, "Ki Bae")
define_button( 7, 9, "Oliver")
define_button( 8, 11, "Clark")
define_button( 9, 5, "Rain")
define_button(10, 6, "Malcom")
define_button(11, 13, "Henry N")
define_button(12, 19, "Auggie")
define_button(13, 26, "Anders")
define_button(14, 14, "Victor")
define_button(15, 15, "Finn")
define_button(16, 18, "Sam")
define_button(17, 23, "Neah")
define_button(18, 24, "Sophia")
define_button(19, 25, "Eddie")
define_button(20, 8, "Sadie")
define_button(21, 7, "Alena")

# Continuously update data from API
while True:
if button1.is_pressed:
action = "Clock Out" if is_clocked_in("Mason") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Mason")
if button2.is_pressed:
action = "Clock Out" if is_clocked_in("Cameron") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Cameron")
if button3.is_pressed:
action = "Clock Out" if is_clocked_in("Jonah") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Jonah")
if button4.is_pressed:
action = "Clock Out" if is_clocked_in("Jerry") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Jerry")
if button5.is_pressed:
action = "Clock Out" if is_clocked_in("Henri K") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Henri K")
if button6.is_pressed:
action = "Clock Out" if is_clocked_in("Liam") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Liam")
if button7.is_pressed:
action = "Clock Out" if is_clocked_in("Ki Bae") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Ki Bae")
if button8.is_pressed:
action = "Clock Out" if is_clocked_in("Oliver") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Oliver")
if button9.is_pressed:
action = "Clock Out" if is_clocked_in("Clark") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Clark")
if button10.is_pressed:
action = "Clock Out" if is_clocked_in("Rain") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Rain")
if button11.is_pressed:
action = "Clock Out" if is_clocked_in("Malcom") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Malcom")
if button12.is_pressed:
action = "Clock Out" if is_clocked_in("Henry N") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Henry N")
if button13.is_pressed:
action = "Clock Out" if is_clocked_in("Auggie") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Auggie")
if button14.is_pressed:
action = "Clock Out" if is_clocked_in("Anders") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Anders")
if button15.is_pressed:
action = "Clock Out" if is_clocked_in("Victor") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Victor")
if button16.is_pressed:
action = "Clock Out" if is_clocked_in("Finn") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Finn")
if button17.is_pressed:
action = "Clock Out" if is_clocked_in("Sam") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Sam")
if button18.is_pressed:
action = "Clock Out" if is_clocked_in("Neah") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Neah")
if button19.is_pressed:
action = "Clock Out" if is_clocked_in("Sophia") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Sophia")
if button20.is_pressed:
action = "Clock Out" if is_clocked_in("Eddie") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Eddie")
if button21.is_pressed:
action = "Clock Out" if is_clocked_in("Sadie") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Sadie")
if button22.is_pressed:
action = "Clock Out" if is_clocked_in("Alena") else "Clock In"
add_row(datetime.now().strftime("%m/%d/%Y %H:%M:%S"), action, "Alena")
api_data = make_api_request()
# TODO: Do we want to sleep for some time to not be annoying to the server?
Loading