Skip to content

Commit

Permalink
compatibility-with-computer
Browse files Browse the repository at this point in the history
  • Loading branch information
rclarke0 committed Feb 26, 2024
1 parent ea23e72 commit 6561b8f
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 10 deletions.
13 changes: 13 additions & 0 deletions hardware-testing/hardware_testing/abr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"type": "service_account",
"project_id": "angelic-bee-411621",
"private_key_id": "dd685fb9db8522658c4c0f07982a9439d5319115",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVsA1O+bllO6m7\nVWwYTVglXu0hIK27ihxhpsvgIV4eYeVkZj+YaDxeLDTR3mFCLCKDJ8Qjv3S2s1jz\nD/JvHT7nqWUF87VxXX4fgU10uMDIeeSM7KjhIeNPoIvFJm4kYN5PmufGtUK5+tSw\nSys8uDhjpb6cG+CVUeNwUnEFhJbAEgyProjZsW/snO+Y1o3OdN1G0CutPzmTpn+b\nLZ0/Mf/QXFcWZlfU0L9OwnJ8xiIO2aHFWS5S4vO98hzvSxsDh1vhMx4fVwyuld5P\n3NEJV8p/XmraCJFbrGVUZiaYhGv8VHhTYvlPZq3mhR+udBAoIDTzgHU+QjFL4ixa\njzmc3N3VAgMBAAECggEACZSPfX8BLSgA6IMCnJmSqy2kqqqfQ/IGd/+Ha+xmzDYC\nywa6SkRdnL9ikA4WFnihqthINuuBilH3PvNF1nO+O3B9jUB4W+2abWTGstJCUHJw\nvM5GzsXu+QEzAObBROXIRLsK245tW17g7vmYplW6EjKg/PfRy2zGzEESh8JhLaDA\n/pSUOFktaJp3GPAdvLx4Wa0kPRb4Y7+/ARiWH5KQGduEGiFl3gK1wHZhdKO6rPlE\njiwGlKKsHMowSZYltiebLv9e9+W/ACbToLWctXXASNzfYeke7Chp+JbbG5sjcHri\nPIxC2t30B1h9AlQh4O919LNKDxDoQjowCuZHicb9QQKBgQDQdGQWKQ61CJ7oZ3dZ\nBwgBJy3PDoFPnuE71Rcvzrs79CI4Dm1N02gXMvtkCC6Q4X2krGuJagWwK7OeIKDW\nqj5Y1gk/CICk0R8xVmR0Cvt1chlIGZtngs9kNyd78mSR6fdmWKQa9kH+gmeDXZ/0\nHv/T0eCGD82K9ogDsMGkDPjKoQKBgQC31EZ2atn6pve6wXyVIKRNa82T7odh/kiH\nnu4KjqneeacYzAIxrp0g4YOe6uwjfWBKJ4KT/mx93AOIjj/eluiPaV0UYnGGikGu\nvgKTbhUvJNGlzR718+FMnGqJa04FH29tgGgOV6ETSGAdOiV7wrxLBx8ru/3cbipD\nNP+DMBlatQKBgQCQvrEc3ieTN0g4clfiHsrFGjJp21WZNuaJCF9QWAmVCOGV8Rae\n+tQFf+YlwLSs75ax+PO2+EK44K+ZdmiYhqBCSWUkOsAxMfCp7qZUGaggFjlN4lIR\nnzhpCw9Etrx0wXSDwbs56ZNLqbs88Y73EWts5wgzrtd3zdYGUf8z7bn34QKBgDik\nBmX8PFe31ILNlTZZzWYxfjFDgKFX+T8YYB4OoRZ0g2DnkxF9zNpRwf1YQxQYUG/o\nxpoU/Za2ELyln9ZUxylQ/KYIQxSCSwStdRu6Anr5LEOa9RwxXcz+MPFQh8SaT/n8\nY5Nvuqi3Vy4KOXxtnxi7BAa0sN6XbxwwGz5vtpA1AoGATooJ3hY9ECgaq6yuK/YF\nl4R7hiTV6Yr7qx1DMbqBv2SpXy3pL4Onh1y2+rpvgH1B5LDLZ25euHRG3GHSuScE\nGcVN7PJ/7p1S/jUZ/Fx9ypKaG++yfU3KvRxW7trbE/8AS9CP5Uph/8UYQ016ilX4\nV/uSgS5MbZCWouIashyFIsc=\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "107655704552755133565",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/abr-432%40angelic-bee-411621.iam.gserviceaccount.com",
"universe_domain": "googleapis.com"
}
116 changes: 116 additions & 0 deletions hardware-testing/hardware_testing/google_sheets_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import gspread
import socket
import httplib2
from oauth2client.service_account import ServiceAccountCredentials
from typing import (Any, Dict, Iterator, List,
Optional, Sequence, Set, Tuple, Union)
import pprint

"""This module requires a creditials.json file before getting started.
The following link provides a step by step tutorial on how to get startd.
https://towardsdatascience.com/accessing-google-spreadsheet-data-using-python-90a5bc214fd2"""

class google_sheet:
"""
This module was designed for the testing team to automate tests by
ultizing google sheets API to record data. The following attributes that this
class needs is the file_name and the tab_number define.
"""
def __init__(self, creditals, file_name, tab_number):
self.scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
self.creditals = \
ServiceAccountCredentials.from_json_keyfile_name(creditals, self.scope)
self.gc = gspread.authorize(self.creditals)
self.file_name = file_name
self.tab_number = tab_number
self.spread_sheet = self.open_google_sheet()
self.worksheet = self.open_worksheet(self.tab_number)
self.row_index = 1

def open_google_sheet(self):
"""Open Google Spread Sheet"""
sheet = self.gc.open(self.file_name)
return sheet

def open_worksheet(self, tab_number: int )-> classmethod:
"""Open individual worksheet within a googlesheet"""
return self.spread_sheet.get_worksheet(tab_number)

def create_worksheet(self, tab_name: int):
"""Create a worksheet with a title for a tab. This method need
to have an existing spreadsheet for it to work."""
try:
self.spread_sheet.add_worksheet(tab_name, rows="1000", cols="26")
except gspread.exceptions.APIError:
print('Work Sheet already exists')

def write_header(self, header: List):
"""Write Header to first row, this method as determines if the first row
has a header that identical to the header the user is writing too."""
header_list = self.worksheet.row_values(1)
print(header_list)
if header_list != header:
self.worksheet.insert_row(header, self.row_index)

def write_to_row(self, data: List):
"""Write data into a row in a List[] format, this method skips the first
row of the worksheet due to headers. """
try:
self.row_index += 1
self.worksheet.insert_row(data, index= self.row_index)
except socket.gaierror:
pass
except httplib2.ServerNotFoundError:
print('UNABLE TO CONNECT TO SERVER!!, CHECK CONNECTION')
except Exception as error:
print(error.traceback)

def delete_row(row_index: int):
"""Delete Row from google sheet"""
self.worksheet.delete_row(row_index)

def update_cell(self, row: int, column: int, single_data: any):
"""Update an individual cell according to a row and column. Note that
this function only updates one individual cell"""
self.worksheet.update_cell(row, column, data)

def update_cells(self, cells, data):
cell_list = self.worksheet.range(cells)
self.worksheet.update_cells(data)

def get_all_data(self)-> Dict[str, any]:
"""Get all the Data recorded from worksheet."""
return self.worksheet.get_all_records()

def get_column(self, column_number: int)-> int:
return self.worksheet.col_values(column_number)

def get_index_row(self):
"""Check for the next available row to write too."""
row_index = len(self.get_column(1))
print("Row Index: ", row_index)
return row_index

def update_row_index(self):
"""Update self.row_index instance variable"""
self.row_index = self.get_index_row()


def get_all_sheets(self):
"""List all tabs in the spreadsheets"""
worksheets = self.spread_sheet.worksheets()
return worksheets

def get_sheet_by_name(self, title: str):
#worksheet = sh.worksheet("January")
try:
worksheet = self.spread_sheet.worksheet(title)
return worksheet
except gspread.exceptions.WorksheetNotFound:
raise 'Worksheet does not exist!!, create a \
worksheet first using the create_worksheet() function.'

def token_check():
if self.creditals.access_token_expired:
self.gc.login()
35 changes: 25 additions & 10 deletions hardware-testing/hardware_testing/scripts/abr_asair_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
import sys
import time as t
from typing import List
import os



try:
sys.path.insert(0, "/var/lib/jupyter/notebooks")
import google_sheets_tool # type: ignore[import]
except ImportError:
raise ImportError(
"Run on robot. Make sure google_sheets_tool.py is in jupyter notebook."
)


def _get_user_input(lst: List[str], some_string: str) -> str:
Expand All @@ -29,10 +24,28 @@ def _get_user_input(lst: List[str], some_string: str) -> str:

class _ABRAsairSensor:
def __init__(self, robot: str, duration: int, frequency: int) -> None:
var = _get_user_input(["C", "R"], "Running on computer (C) or robot (R)? ")
if var == "R":
try:
sys.path.insert(0, "/var/lib/jupyter/notebooks")
import google_sheets_tool # type: ignore[import]
credentials_path = "/var/lib/jupyter/notebooks/abr.json"
except ImportError:
from hardware_testing import google_sheets_tool
raise ImportError(
"Run on robot. Make sure google_sheets_tool.py is in jupyter notebook."
)
else:
from hardware_testing import google_sheets_tool
#credentials_path = "/hardware-testing/hardware_testing/abr.json"
credentials_path = "C:/Users/Rhyann Clarke/opentrons/hardware-testing/hardware_testing/abr.json"

print(os.path.exists(credentials_path))
test_name = "ABR-Environment-Monitoring"
run_id = data.create_run_id()
file_name = data.create_file_name(test_name, run_id, robot)
sensor = asair_sensor.BuildAsairSensor(False, True)
sensor = asair_sensor.BuildAsairSensor(False, False)
print(sensor)
env_data = sensor.get_reading()
header = [
"Robot",
Expand All @@ -45,7 +58,7 @@ def __init__(self, robot: str, duration: int, frequency: int) -> None:
header_str = ",".join(header) + "\n"
data.append_data_to_file(test_name, run_id, file_name, header_str)
# Upload to google has passed
credentials_path = "/var/lib/jupyter/notebooks/abr.json"

try:
google_sheet = google_sheets_tool.google_sheet(
credentials_path, "ABR Ambient Conditions", tab_number=0
Expand Down Expand Up @@ -119,8 +132,10 @@ def __init__(self, robot: str, duration: int, frequency: int) -> None:
"PVT1ABR10",
"PVT1ABR11",
"PVT1ABR12",
"ROOM_339",
"Room_340",
] # type: List
robot = _get_user_input(robot_list, "Robot: ")
robot = _get_user_input(robot_list, "Robot/Room: ")
duration = int(input("Duration (min): "))
frequency = int(input("Frequency (min): "))
_ABRAsairSensor(robot, duration, frequency)

0 comments on commit 6561b8f

Please sign in to comment.