Skip to content

Commit 42843df

Browse files
committed
Print final height to file in JSON format
1 parent 5cf598d commit 42843df

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

idasen_controller/main.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ async def unsubscribe(client, uuid):
193193
except KeyError:
194194
# This happens on windows, I don't know why
195195
pass
196-
except Exception as e:
197-
telegram_send.send(messages=["`Exception: `" + str(e)], parse_mode="markdown")
196+
except Exception as ex:
197+
sendToTelegram("Exception: " + str(ex), "markdown")
198198
print("Exception: ", e)
199199

200200
async def move_to(client, target):
@@ -257,7 +257,6 @@ def _move_to(sender, data):
257257
await asyncio.wait_for(move_done, timeout=config['movement_timeout'])
258258
except asyncio.TimeoutError as e:
259259
print('Timed out while waiting for desk')
260-
telegram_send.send(messages=["`Timed out while waiting for desk`"], parse_mode='markdown')
261260
await unsubscribe(client, UUID_HEIGHT)
262261

263262
def unpickle_desk():
@@ -368,21 +367,31 @@ async def run_command(client, config):
368367
await asyncio.sleep(1)
369368
final_height, speed = struct.unpack("<Hh", await client.read_gatt_char(UUID_HEIGHT))
370369
print("Final height: {:4.0f}mm (Target: {:4.0f}mm)".format(rawToMM(final_height), rawToMM(target)))
370+
printFinalHeightToFile(rawToMM(final_height))
371+
372+
def printFinalHeightToFile(height):
373+
writePath = os.path.dirname(os.path.realpath(__file__)) + "/desk_height.json"
374+
with open(writePath, "w") as file:
375+
height_wo_unit = str(height).split("mm")[0]
376+
file.write('{"final_height": ' + height_wo_unit + '}')
371377

372378
def skip_check():
373-
"""If force flag is provided, do not skip"""
379+
# If force flag is provided, do not skip"""
374380
args, leftovers = parser.parse_known_args()
375381
if args.force is not False:
376-
telegram_send.send(messages=["`Force mode`"], parse_mode="markdown")
382+
sendToTelegram("Force mode", "markdown")
377383
return False
378-
"""Check the script's modification date to see if it was 'touched' today"""
384+
# Check the script's modification date to see if it was 'touched' today"""
379385
t = os.path.getmtime(__file__)
380386
modification_day = datetime.datetime.fromtimestamp(t).day
381387
if (modification_day == datetime.date.today().day):
382-
telegram_send.send(messages=["`Skipping`"], parse_mode="markdown")
388+
sendToTelegram("Skipping", "markdown")
383389
return True
384390
return False
385391

392+
def sendToTelegram(message, parse_mode):
393+
telegram_send.send(messages=["`" + message + "`"], parse_mode=parse_mode)
394+
386395
async def run_server(client, config):
387396
"""Start a tcp server to listen for commands"""
388397
def disconnect_callback(client, _ = None):

0 commit comments

Comments
 (0)