Skip to content

Commit 64ed283

Browse files
authored
Merge pull request #3 from yeyeto2788/master
Latest change pulled.
2 parents e06a25c + 73ef192 commit 64ed283

39 files changed

+361
-223
lines changed

action.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import redis
66

7-
sys.path.append('..')
7+
88

99

1010
class Action():

config_load.py

-10
This file was deleted.

constants.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PREVIOUS_LINE = "\x1b[1F"
2+
RED_BACK = "\x1b[41;37m"
3+
GREEN_BACK = "\x1b[42;30m"
4+
YELLOW_BACK = "\x1b[43;30m"
5+
RESET = "\x1b[0m"

controls/arduino/control.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import redis
44
from nanpy import (ArduinoApi, SerialManager)
55
import sys
6-
sys.path.append('..')
6+
77

88
default_connection = SerialManager()
99

controls/linux/control.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from adafruit_debouncer import Debouncer
88

99

10-
sys.path.append('..')
10+
1111

1212

1313
# Base sensor class to extend all other arduino sensors from.

mudpi.py

+33-23
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,45 @@
77
using redis.
88
"""
99

10-
import RPi.GPIO as GPIO
11-
import threading
1210
import datetime
11+
import json
1312
import socket
14-
import redis
13+
# import RPi.GPIO as GPIO
14+
import threading
1515
import time
16-
import json
17-
import sys
1816

19-
# TODO: This might not be needed and could be deleted
20-
sys.path.append('..')
17+
import redis
18+
19+
import constants
2120
from action import Action
22-
from config_load import load_config_json
2321
from server.mudpi_server import MudpiServer
24-
from workers.linux.lcd_worker import LcdWorker
22+
from utils import load_config_json
23+
from workers.linux.camera_worker import CameraWorker
24+
from workers.linux.control_worker import PiControlWorker
2525
from workers.linux.i2c_worker import PiI2CWorker
26+
from workers.linux.lcd_worker import LcdWorker
2627
from workers.linux.relay_worker import RelayWorker
27-
from workers.linux.camera_worker import CameraWorker
2828
from workers.linux.sensor_worker import PiSensorWorker
29-
from workers.linux.control_worker import PiControlWorker
30-
from workers.trigger_worker import TriggerWorker
3129
from workers.sequence_worker import SequenceWorker
30+
from workers.trigger_worker import TriggerWorker
31+
3232
try:
3333
from workers.arduino.arduino_worker import ArduinoWorker
34+
3435
NANPY_ENABLED = True
3536
except ImportError:
3637
NANPY_ENABLED = False
38+
3739
try:
3840
from workers.adc_worker import ADCMCP3008Worker
41+
3942
MCP_ENABLED = True
40-
except ImportError:
43+
except (ImportError, AttributeError):
4144
MCP_ENABLED = False
4245

4346
from logger.Logger import Logger, LOG_LEVEL
44-
import variables
4547

46-
CONFIGS = {}
48+
4749
PROGRAM_RUNNING = True
4850
threads = []
4951
actions = {}
@@ -58,7 +60,16 @@
5860

5961
print(chr(27) + "[2J")
6062
print('Loading MudPi Configs...\r', end="", flush=True)
61-
CONFIGS = load_config_json()
63+
64+
try:
65+
CONFIGS = load_config_json()
66+
except FileNotFoundError:
67+
print(
68+
f'{constants.RED_BACK}There is no configuration file present on the '
69+
f'filesystem{constants.RESET}\n\r'
70+
)
71+
exit(1)
72+
6273
# Singleton redis to prevent connection conflicts
6374
try:
6475
r = redis.Redis(host=CONFIGS['redis'].get('host', '127.0.0.1'),
@@ -112,10 +123,6 @@
112123

113124
try:
114125
print('Initializing Garden Control \r', end="", flush=True)
115-
GPIO.setwarnings(False)
116-
GPIO.setmode(GPIO.BCM)
117-
GPIO.cleanup()
118-
# Pause for GPIO to finish
119126
time.sleep(0.1)
120127
Logger.log(
121128
LOG_LEVEL["info"],
@@ -278,10 +285,13 @@
278285
Logger.log(
279286
LOG_LEVEL["info"],
280287
'{0} Actions...\t\t\t\t\033[1;32m Initializing\033[0;0m'.format(
281-
len(CONFIGS['actions'])))
288+
len(CONFIGS['actions']))
289+
)
282290
except KeyError:
283-
Logger.log(LOG_LEVEL["info"],
284-
'Actions...\t\t\t\t\033[1;31m Disabled\033[0;0m')
291+
Logger.log(
292+
LOG_LEVEL["info"],
293+
'Actions...\t\t\t\t\033[1;31m Disabled\033[0;0m'
294+
)
285295

286296
# Worker for Sequences
287297
try:

sensors/arduino/humidity_sensor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from nanpy import (ArduinoApi, SerialManager, DHT)
77
import sys
88

9-
sys.path.append('..')
109

11-
import variables
10+
11+
import constants
1212

1313
default_connection = SerialManager(device='/dev/ttyUSB0')
1414

sensors/arduino/light_sensor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from nanpy import (ArduinoApi, SerialManager)
77
import sys
88

9-
sys.path.append('..')
109

11-
import variables
10+
11+
import constants
1212

1313
default_connection = SerialManager(device='/dev/ttyUSB0')
1414

sensors/arduino/rain_sensor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from nanpy import (ArduinoApi, SerialManager)
77
import sys
88

9-
sys.path.append('..')
109

11-
import variables
10+
11+
import constants
1212

1313
default_connection = SerialManager(device='/dev/ttyUSB0')
1414
# r = redis.Redis(host='127.0.0.1', port=6379)

sensors/arduino/soil_sensor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from nanpy import (ArduinoApi, SerialManager)
77
import sys
88

9-
sys.path.append('..')
109

11-
import variables
10+
11+
import constants
1212

1313
default_connection = SerialManager(device='/dev/ttyUSB0')
1414
# r = redis.Redis(host='127.0.0.1', port=6379)

sensors/arduino/temperature_sensor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
from logger.Logger import Logger, LOG_LEVEL
1010

11-
sys.path.append('..')
1211

13-
import variables
12+
13+
import constants
1414

1515
default_connection = SerialManager(device='/dev/ttyUSB0')
1616

sensors/linux/float_sensor.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import time
2-
import json
3-
import redis
4-
from .sensor import Sensor
5-
import digitalio
61
import board
2+
import digitalio
3+
4+
from .sensor import Sensor
75

86

97
# PIN MODE : OUT | IN
@@ -16,12 +14,16 @@ def __init__(self, pin, name=None, key=None, redis_conn=None):
1614
return
1715

1816
def init_sensor(self):
19-
"""Initialize the sensor here (i.e. set pin mode, get addresses, etc) this gets called by the worker"""
20-
self.gpio_pin = digitalio.DigitalInOut(self.pin_obj) # Default to input : https://github.com/adafruit/Adafruit_Blinka/blob/master/src/digitalio.py#L111
17+
"""Initialize the sensor here (i.e. set pin mode, get addresses, etc)
18+
this gets called by the worker"""
19+
# Default to input :
20+
# https://github.com/adafruit/Adafruit_Blinka/blob/master/src/digitalio.py#L111
21+
self.gpio_pin = digitalio.DigitalInOut(self.pin_obj)
2122
return
2223

2324
def read(self):
24-
"""Read the sensor(s), parse the data and store it in redis if redis is configured"""
25+
"""Read the sensor(s), parse the data and store it in redis if redis
26+
is configured"""
2527
value = self.gpio_pin.value
2628
return value
2729

sensors/linux/humidity_sensor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from sensors.linux.sensor import Sensor
88

9-
sys.path.append('..')
9+
1010

1111
from logger.Logger import Logger, LOG_LEVEL
1212

sensors/linux/i2c/bme680_sensor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from logger.Logger import Logger, LOG_LEVEL
77
from sensors.linux.i2c.sensor import Sensor
88

9-
sys.path.append('..')
9+
1010

1111

1212
class Bme680Sensor(Sensor):

sensors/linux/i2c/t9602_sensor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from logger.Logger import Logger, LOG_LEVEL
77
from sensors.linux.i2c.sensor import Sensor
88

9-
sys.path.append('..')
9+
1010

1111

1212
class T9602Sensor(Sensor):

sensors/linux/sensor.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def __init__(self, pin, name=None, key=None, redis_conn=None):
2828
else:
2929
Logger.log(
3030
LOG_LEVEL["error"],
31-
"Cannot detect pin type (Digital or analog), should be Dxx or Axx for digital or analog. "
31+
"Cannot detect pin type (Digital or analog), "
32+
"should be Dxx or Axx for digital or analog. "
3233
"Please refer to "
3334
"https://github.com/adafruit/Adafruit_Blinka/tree/master/src/adafruit_blinka/board"
3435
)

sensors/mcp3xxx/soil_sensor.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from logger.Logger import Logger, LOG_LEVEL
66
from sensors.mcp3xxx.sensor import Sensor
77

8-
sys.path.append('..')
8+
99

1010
# Tested using Sun3Drucker Model SX239
1111
# Wet Water = 287
@@ -27,8 +27,10 @@ def init_sensor(self):
2727

2828
def read(self):
2929
resistance = self.read_pin()
30-
moistpercent = ((resistance - WaterBounds) / (
31-
AirBounds - WaterBounds)) * 100
30+
moistpercent = (
31+
(resistance - WaterBounds) / (
32+
AirBounds - WaterBounds)
33+
) * 100
3234
if moistpercent > 80:
3335
moisture = 'Very Dry - ' + str(int(moistpercent))
3436
elif 80 >= moistpercent > 45:

triggers/control_trigger.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
from .trigger import Trigger
66

7-
sys.path.append('..')
7+
88

99
from logger.Logger import Logger, LOG_LEVEL
1010

triggers/sensor_trigger.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import sys
55
from .trigger import Trigger
66

7-
sys.path.append('..')
7+
88

99
from logger.Logger import Logger, LOG_LEVEL
1010

triggers/time_trigger.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
CRON_ENABLED = True
1111
except ImportError:
1212
CRON_ENABLED = False
13-
sys.path.append('..')
13+
1414

1515
from logger.Logger import Logger, LOG_LEVEL
1616

triggers/trigger.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import threading
55
import sys
66

7-
sys.path.append('..')
7+
88

99
from logger.Logger import Logger, LOG_LEVEL
1010

triggers/trigger_group.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import threading
55
import sys
66

7-
sys.path.append('..')
7+
88

99
from logger.Logger import Logger, LOG_LEVEL
1010

utils.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import json
2+
3+
4+
def load_config_json():
5+
"""
6+
7+
Returns:
8+
Dictionary from json file.
9+
"""
10+
11+
with open('mudpi.config') as loadedfile:
12+
configs = json.load(loadedfile)
13+
loadedfile.close()
14+
return configs
15+
16+
17+
def get_config_item(config, item, default=None, replace_char='_'):
18+
"""
19+
20+
Args:
21+
config:
22+
item:
23+
default:
24+
replace_char:
25+
26+
Returns:
27+
Configuration item
28+
"""
29+
30+
value = config.get(item, default)
31+
32+
if type(value) == str:
33+
value = value.replace(" ", replace_char).lower()
34+
35+
return value

variables.py

-5
This file was deleted.

0 commit comments

Comments
 (0)