Skip to content
Snippets Groups Projects
Commit 0ec98f07 authored by Anton Tetov Johansson's avatar Anton Tetov Johansson
Browse files

store token in ignored json file. move python files to directory named same as package

parent 4a5b0b71
Branches
No related tags found
No related merge requests found
*.pyc
# Created by https://www.toptal.com/developers/gitignore/api/python
# Edit at https://www.toptal.com/developers/gitignore?templates=python
weights/
### Python ### ### Python ###
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
...@@ -143,5 +138,7 @@ dmypy.json ...@@ -143,5 +138,7 @@ dmypy.json
# Cython debug symbols # Cython debug symbols
cython_debug/ cython_debug/
weights/yolov3-veges_best.weights
weights/yolov3-vattenhallen_best.weights # custom
weights/
creds.json
{
"device_id": "device_NNNNN",
"token": ""
}
from pathlib import Path
HERE = Path(__file__).parent
REPO = HERE.parent
CREDS_PATH = REPO / "creds.json"
File moved
File moved
File moved
File moved
...@@ -13,9 +13,9 @@ from numpy import sqrt ...@@ -13,9 +13,9 @@ from numpy import sqrt
from pandas import DataFrame from pandas import DataFrame
from gripper import gripper_close, gripper_open from gripper import gripper_close, gripper_open
from move import * from farmbot_yolo.move import *
from detect import * from farmbot_yolo.detect import *
from location import * from farmbot_yolo.location import *
_LOG = getLogger(__name__) _LOG = getLogger(__name__)
......
...@@ -20,7 +20,7 @@ from datetime import timezone, datetime ...@@ -20,7 +20,7 @@ from datetime import timezone, datetime
from dateutil.parser import parse from dateutil.parser import parse
from requests import get, delete from requests import get, delete
import creds from creds import read_credentials
from client import FarmbotClient from client import FarmbotClient
...@@ -55,6 +55,7 @@ def scan(img_path: Path, location_path: Path, # smaller delta ...@@ -55,6 +55,7 @@ def scan(img_path: Path, location_path: Path, # smaller delta
Output: none Output: none
''' '''
opts = Opts(min_x, max_x, min_y, max_y, delta, offset, flag) opts = Opts(min_x, max_x, min_y, max_y, delta, offset, flag)
creds = read_credentials()
pts = [] pts = []
sweep_y_negative = False sweep_y_negative = False
...@@ -72,7 +73,7 @@ def scan(img_path: Path, location_path: Path, # smaller delta ...@@ -72,7 +73,7 @@ def scan(img_path: Path, location_path: Path, # smaller delta
Logger.info('Run without sweep') Logger.info('Run without sweep')
exit() exit()
client = FarmbotClient(creds.device_id, creds.token) client = FarmbotClient(creds["device_id"], creds["token"])
client.move(0, 0, _SWEEEP_HEIGHT) # ensure moving from original client.move(0, 0, _SWEEEP_HEIGHT) # ensure moving from original
for x, y in pts: for x, y in pts:
client.move(x, y, _SWEEEP_HEIGHT) # move camera client.move(x, y, _SWEEEP_HEIGHT) # move camera
...@@ -101,7 +102,8 @@ def simple_move(x: int, y: int, z: int) -> None: ...@@ -101,7 +102,8 @@ def simple_move(x: int, y: int, z: int) -> None:
Input: x, y,z: destination point Input: x, y,z: destination point
photo: take a pic or not photo: take a pic or not
''' '''
client = FarmbotClient(creds.device_id, creds.token) creds = read_credentials()
client = FarmbotClient(creds["device_id"], creds["token"])
client.move(x, y, z) client.move(x, y, z)
client.shutdown() client.shutdown()
return None return None
......
import json
from typing import Dict
from farmbot_yolo import CREDS_PATH
def read_credentials() -> Dict[str]:
if not CREDS_PATH.exists():
raise RuntimeError("No creds.json found in project root. Run python -m farmbot_yolo.get_credentials.")
with CREDS_PATH.open(mode="r") as fp:
creds_dict = json.load(fp)
return creds_dict
"""Request a token to store in creds.json."""
import argparse
import json
from urllib import request
from farmbot_yolo import CREDS_PATH
def request_token(email: str, password: str):
auth_info = {'user': {'email': email, 'password': password}}
req = request.Request('https://my.farmbot.io/api/tokens')
req.add_header('Content-Type', 'application/json')
response = request.urlopen(req, data=json.dumps(auth_info).encode('utf-8'))
token_info = json.loads(response.read().decode())
print("mqtt host [%s]" % token_info['token']['unencoded']['mqtt'])
creds_dict = {"device_id": token_info["token"]["unencoded"]["bot"],
"token": token_info['token']['encoded']}
print("rewriting creds.json")
with CREDS_PATH.open(mode="w") as fp:
json.dump(creds_dict, fp)
if __name__ == "__main__":
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--email', type=str, help="user email for token request")
parser.add_argument('--password', type=str, help="user password for token request")
opts = parser.parse_args()
print("opts %s" % opts)
request_token(opts.email, opts.password)
File moved
device_id="***REMOVED***"
token="***REMOVED***"
#!/usr/bin/env python3
# request a token (for creds.py)
import argparse
import json
from urllib import request
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--email', type=str, help="user email for token request")
parser.add_argument('--password', type=str, help="user password for token request")
opts = parser.parse_args()
print("opts %s" % opts)
auth_info = {'user': {'email': opts.email, 'password': opts.password }}
req = request.Request('https://my.farmbot.io/api/tokens')
req.add_header('Content-Type', 'application/json')
response = request.urlopen(req, data=json.dumps(auth_info).encode('utf-8'))
token_info = json.loads(response.read().decode())
print("mqtt host [%s]" % token_info['token']['unencoded']['mqtt'])
print("rewriting creds.py")
with open("creds.py", "w") as f:
f.write("device_id=\"%s\"\n" % token_info['token']['unencoded']['bot'])
f.write("token=\"%s\"\n" % token_info['token']['encoded'])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment