Skip to content

Commit

Permalink
add backup config for hat in case of file corruption (reset while wri…
Browse files Browse the repository at this point in the history
…ting etc)
  • Loading branch information
seandepagnier committed Mar 25, 2024
1 parent 623c0b6 commit 8445c9a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions arduino/rfremote/rfremote.ino
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ void loop() {
if(count <= 2 && count > key_count) {
// construct output using PC0-PC3 to give unique codes for different remote types
uint32_t pinc = (PINC & 0xf);
//pinc = 5;
pinc ^= ((uid_code&7) << 2);
if(wd)
pinc ^= 0xac; // 1011 1111
Expand Down
40 changes: 28 additions & 12 deletions hat/hat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#!/usr/bin/env python
#
# Copyright (C) 2023 Sean D'Epagnier
Expand Down Expand Up @@ -318,15 +319,11 @@ def __init__(self):
self.configfilename = os.getenv('HOME') + '/.pypilot/hat.conf'

# read config
print('loading config file:', self.configfilename)
try:
file = open(self.configfilename)
config = pyjson.loads(file.read())
file.close()
for name in config:
self.config[name] = config[name]
except Exception as e:
print('config failed:', e)
if self.read_config(self.configfilename):
self.write_config('.bak')
else:
print('failed to read config file, trying backup config')
self.read_config(self.configfilename + '.bak')

host = self.config['host']
print('host', host, time.monotonic())
Expand Down Expand Up @@ -498,7 +495,26 @@ def cleanup(signal_number, frame=None):
signal.signal(s, cleanup)
signal.signal(signal.SIGCHLD, cleanup)

def write_config(self):
def read_config(self, filename):
print('loading config file:', filename)
try:
file = open(filename)
config = pyjson.loads(file.read())
file.close()
for name in config:
self.config[name] = config[name]
return True
except Exception as e:
print('config failed:', e)
try:
file = open(filename)
print('bad config: ', file.read())
file.close()
except Exception as e:
print('config read exception printing exception', e)
return False

def write_config(self, suffix=''):
actions = self.config['actions']
for name in list(actions):
if not actions[name] and name[:6] != 'pilot ':
Expand All @@ -510,11 +526,11 @@ def write_config(self):
self.config['modes'] = values['ap.mode']['choices']

try:
f = open(self.configfilename, 'w')
f = open(self.configfilename+suffix, 'w')
f.write(pyjson.dumps(self.config) + '\n')
f.close()
except IOError:
print('failed to save config file:', self.configfilename)
print('failed to save config file:', self.configfilename+suffix)

def update_config(self, name, value):
if name in self.config and self.config[name] == value:
Expand Down
2 changes: 1 addition & 1 deletion pypilot/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
strversion = '0.51'
strversion = '0.52'

0 comments on commit 8445c9a

Please sign in to comment.