-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'jmashon/working-branch' of github.com:FarmBot-Labs/side…
…car-starter-pack into jmashon/working-branch
- Loading branch information
Showing
10 changed files
with
281 additions
and
74 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from fbapi import FarmbotAPI | ||
from datetime import datetime | ||
import paho.mqtt.client as mqtt | ||
|
||
import json | ||
|
||
class BrokerConnect(): | ||
def __init__(self): | ||
self.api = FarmbotAPI() | ||
|
||
self.token = self.api.token | ||
self.client = None | ||
|
||
# connect() --> establish connection to message broker | ||
# disconnect() --> disconnect from message broker | ||
|
||
# publish() | ||
|
||
# on_connect() --> subscribe to channel | ||
# on_message() --> print channel/message | ||
|
||
# def on_connect(self, client, *_args): | ||
# # subscribe to all channels | ||
# self.client.subscribe(f"bot/{self.token['token']['unencoded']['bot']}/#") | ||
# print('connected') | ||
|
||
# def on_message(self, _client, _userdata, msg): | ||
# print('-' * 100) | ||
# # print channel | ||
# print(f'{msg.topic} ({datetime.now().strftime("%Y-%m-%d %H:%M:%S")})\n') | ||
# # print message | ||
# print(json.dumps(json.loads(msg.payload), indent=4)) | ||
|
||
# def on_connect(client, *_args): | ||
# # subscribe to all channels | ||
# client.subscribe(f"bot/{TOKEN['token']['unencoded']['bot']}/#") | ||
# print('connected') | ||
|
||
def status_connect(self, client, *_args): | ||
# Subscribe to specific channel | ||
device_info = self.api.get('device') | ||
device_id = device_info['id'] | ||
|
||
client.subscribe("bot/device_4652/status") | ||
print('connected via status_connect()') | ||
|
||
def on_message(self, _client, _userdata, msg): | ||
print('-' * 100) | ||
# print channel | ||
print("Channel:") | ||
print(f'{msg.topic} ({datetime.now().strftime("%Y-%m-%d %H:%M:%S")})\n') | ||
# print message | ||
print("Message:") | ||
print(json.dumps(json.loads(msg.payload), indent=4)) | ||
|
||
def connect(self): | ||
print(self.api.token) | ||
|
||
self.api.check_token() | ||
|
||
self.client = mqtt.Client() | ||
self.client.username_pw_set( | ||
username=self.token['token']['unencoded']['bot'], | ||
password=self.token['token']['encoded'] | ||
) | ||
|
||
self.client.connect( | ||
self.token['token']['unencoded']['mqtt'], | ||
port=1883, | ||
keepalive=60 | ||
) | ||
|
||
# self.client.on_connect = status_connect | ||
# self.client.on_message = on_message | ||
|
||
self.client.loop_start() | ||
|
||
def disconnect(self): | ||
if self.client is not None: | ||
self.client.loop_stop() | ||
self.client.disconnect() | ||
|
||
def publish(self, message): | ||
if self.client is None: | ||
self.connect() | ||
|
||
self.client.publish( | ||
f'bot/{self.token["token"]["unencoded"]["bot"]}/from_clients', | ||
payload=json.dumps(message) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
from farmbot_util_PORT import Farmbot | ||
|
||
class TestFarmbot(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.farmbot = Farmbot() | ||
|
||
@patch('farmbot_util_PORT.FarmbotAPI.get_token') | ||
def test_get_token(self, mock_get_token): | ||
mock_get_token.return_value = 'fake_token' | ||
self.farmbot.get_token('[email protected]', 'password123') | ||
self.assertEqual(self.farmbot.token, 'fake_token') | ||
mock_get_token.assert_called_once_with('[email protected]', 'password123', 'https://my.farm.bot') | ||
|
||
@patch('farmbot_util_PORT.FarmbotAPI.get_info') | ||
def test_get_info(self, mock_get_info): | ||
mock_get_info.return_value = {'info': 'fake_info'} | ||
result = self.farmbot.get_info() | ||
self.assertEqual(result, {'info': 'fake_info'}) | ||
mock_get_info.assert_called_once() | ||
|
||
@patch('farmbot_util_PORT.FarmbotAPI.set_info') | ||
def test_set_info(self, mock_set_info): | ||
self.farmbot.set_info('label', 'value') | ||
mock_set_info.assert_called_once_with('label', 'value') | ||
|
||
@patch('farmbot_util_PORT.FarmbotAPI.log') | ||
def test_log(self, mock_log): | ||
self.farmbot.log('message', 'info') | ||
mock_log.assert_called_once_with('message', 'info') | ||
|
||
@patch('farmbot_util_PORT.FarmbotAPI.safe_z') | ||
def test_safe_z(self, mock_safe_z): | ||
mock_safe_z.return_value = 10 | ||
result = self.farmbot.safe_z() | ||
self.assertEqual(result, 10) | ||
mock_safe_z.assert_called_once() | ||
|
||
@patch('farmbot_util_PORT.FarmbotAPI.garden_size') | ||
def test_garden_size(self, mock_garden_size): | ||
mock_garden_size.return_value = {'x': 1000, 'y': 2000} | ||
result = self.farmbot.garden_size() | ||
self.assertEqual(result, {'x': 1000, 'y': 2000}) | ||
mock_garden_size.assert_called_once() | ||
|
||
@patch('farmbot_util_PORT.FarmbotAPI.group') | ||
def test_group(self, mock_group): | ||
sequences = ['seq1', 'seq2'] | ||
mock_group.return_value = {'grouped': True} | ||
result = self.farmbot.group(sequences) | ||
self.assertEqual(result, {'grouped': True}) | ||
mock_group.assert_called_once_with(sequences) | ||
|
||
@patch('farmbot_util_PORT.FarmbotAPI.curve') | ||
def test_curve(self, mock_curve): | ||
mock_curve.return_value = {'curve': True} | ||
result = self.farmbot.curve('seq', 0, 0, 10, 10, 5, 5) | ||
self.assertEqual(result, {'curve': True}) | ||
mock_curve.assert_called_once_with('seq', 0, 0, 10, 10, 5, 5) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |