-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·229 lines (181 loc) · 6.76 KB
/
Copy pathutils.py
File metadata and controls
executable file
·229 lines (181 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# -*- coding: utf-8 -*-
import logging
import configparser
from pathlib import Path
from typing import List, Any, Callable
import serial
from time import sleep
from struct import unpack_from
import bisect
# Logging
logging.basicConfig()
logger = logging.getLogger("BluetoothOutbackInverter")
logger.setLevel(logging.INFO)
config = configparser.ConfigParser()
path = Path(__file__).parents[0]
default_config_file_path = path.joinpath("default_config.ini").absolute().__str__()
custom_config_file_path = path.joinpath("config.ini").absolute().__str__()
config.read([default_config_file_path, custom_config_file_path])
def _get_list_from_config(
group: str, option: str, mapper: Callable[[Any], Any] = lambda v: v
) -> List[Any]:
rawList = config[group][option].split(",")
return list(
map(mapper, [item for item in rawList if item != "" and item is not None])
)
# Constants - Need to dynamically get them in future
DRIVER_VERSION = 0.1
DRIVER_SUBVERSION = ".3"
zero_char = chr(48)
degree_sign = "\N{DEGREE SIGN}"
#LINEAR_LIMITATION_ENABLE = "True" == config["DEFAULT"]["LINEAR_LIMITATION_ENABLE"]
#MAX_BATTERY_CHARGE_CURRENT = float(config["DEFAULT"]["MAX_BATTERY_CHARGE_CURRENT"])
#CELL_VOLTAGES_WHILE_CHARGING = _get_list_from_config("DEFAULT", "CELL_VOLTAGES_WHILE_CHARGING", lambda v: float(v))
#PUBLISH_CONFIG_VALUES = int(config["DEFAULT"]["PUBLISH_CONFIG_VALUES"])
OUTBACK_ADDRESS = config["DEFAULT"]["OUTBACK_ADDRESS"]
DEBUG_MODE = config.getboolean('DEFAULT', 'DEBUG_MODE')
def constrain(val, min_val, max_val):
if min_val > max_val:
min_val, max_val = max_val, min_val
return min(max_val, max(min_val, val))
def mapRange(inValue, inMin, inMax, outMin, outMax):
return outMin + (((inValue - inMin) / (inMax - inMin)) * (outMax - outMin))
def mapRangeConstrain(inValue, inMin, inMax, outMin, outMax):
return constrain(mapRange(inValue, inMin, inMax, outMin, outMax), outMin, outMax)
def calcLinearRelationship(inValue, inArray, outArray):
if inArray[0] > inArray[-1]: # change compare-direction in array
return calcLinearRelationship(inValue, inArray[::-1], outArray[::-1])
else:
# Handle out of bounds
if inValue <= inArray[0]:
return outArray[0]
if inValue >= inArray[-1]:
return outArray[-1]
# else calculate linear current between the setpoints
idx = bisect.bisect(inArray, inValue)
upperIN = inArray[idx - 1] # begin with idx 0 as max value
upperOUT = outArray[idx - 1]
lowerIN = inArray[idx]
lowerOUT = outArray[idx]
return mapRangeConstrain(inValue, lowerIN, upperIN, lowerOUT, upperOUT)
def calcStepRelationship(inValue, inArray, outArray, returnLower):
if inArray[0] > inArray[-1]: # change compare-direction in array
return calcStepRelationship(inValue, inArray[::-1], outArray[::-1], returnLower)
# Handle out of bounds
if inValue <= inArray[0]:
return outArray[0]
if inValue >= inArray[-1]:
return outArray[-1]
# else get index between the setpoints
idx = bisect.bisect(inArray, inValue)
return outArray[idx] if returnLower else outArray[idx - 1]
def is_bit_set(tmp):
return False if tmp == zero_char else True
def kelvin_to_celsius(kelvin_temp):
return kelvin_temp - 273.1
def format_value(value, prefix, suffix):
return (
None
if value is None
else ("" if prefix is None else prefix)
+ str(value)
+ ("" if suffix is None else suffix)
)
def read_serial_data(
command, port, baud, length_pos, length_check, length_fixed=None, length_size=None
):
try:
with serial.Serial(port, baudrate=baud, timeout=0.1) as ser:
return read_serialport_data(
ser, command, length_pos, length_check, length_fixed, length_size
)
except serial.SerialException as e:
logger.error(e)
return False
# Open the serial port
# Return variable for the openned port
def open_serial_port(port, baud):
ser = None
tries = 3
while tries > 0:
try:
ser = serial.Serial(port, baudrate=baud, timeout=0.1)
tries = 0
except serial.SerialException as e:
logger.error(e)
tries -= 1
return ser
# Read data from previously openned serial port
def read_serialport_data(
ser: serial.Serial,
command,
length_pos,
length_check,
length_fixed=None,
length_size=None,
):
try:
ser.flushOutput()
ser.flushInput()
ser.write(command)
length_byte_size = 1
if length_size is not None:
if length_size.upper() == "H":
length_byte_size = 2
elif length_size.upper() == "I" or length_size.upper() == "L":
length_byte_size = 4
count = 0
toread = ser.inWaiting()
while toread < (length_pos + length_byte_size):
sleep(0.005)
toread = ser.inWaiting()
count += 1
if count > 50:
logger.error(">>> ERROR: No reply - returning")
return False
# logger.info('serial data toread ' + str(toread))
res = ser.read(toread)
if length_fixed is not None:
length = length_fixed
else:
if len(res) < (length_pos + length_byte_size):
logger.error(
">>> ERROR: No reply - returning [len:" + str(len(res)) + "]"
)
return False
length_size = length_size if length_size is not None else "B"
length = unpack_from(">" + length_size, res, length_pos)[0]
# logger.info('serial data length ' + str(length))
count = 0
data = bytearray(res)
while len(data) <= length + length_check:
res = ser.read(length + length_check)
data.extend(res)
# logger.info('serial data length ' + str(len(data)))
sleep(0.005)
count += 1
if count > 150:
logger.error(
">>> ERROR: No reply - returning [len:"
+ str(len(data))
+ "/"
+ str(length + length_check)
+ "]"
)
return False
return data
except serial.SerialException as e:
logger.error(e)
return False
locals_copy = locals().copy()
def publish_config_variables(dbusservice):
for variable, value in locals_copy.items():
if variable.startswith("__"):
continue
if (
isinstance(value, float)
or isinstance(value, int)
or isinstance(value, str)
or isinstance(value, List)
):
dbusservice.add_path(f"/Info/Config/{variable}", value)