Skip to content

Commit ce5748d

Browse files
authored
Merge pull request #18 from jedie/develop
Develop
2 parents 205ab37 + 8f3ca9e commit ce5748d

7 files changed

+51
-21
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Tested devices:
1313
* schedule multiple timers
1414
* OTA updates (currently without directory support)
1515
* turn the switch on/off by the web page or the device button
16-
* checkbox for each day of the week where timers are active (Currently only stored in RTC RAM!)
16+
* checkbox for each day of the week where timers are active
1717
* Reset the device by long pressing the button
1818
* supports multiple WIFI credentials
1919

@@ -29,7 +29,6 @@ The device will do this on every boot:
2929

3030
Things that will be implement in the near feature:
3131

32-
* Store 'active weekdays' in filesystem and not only in RTC RAM
3332
* timer toggle flag to reverse: power is switched off during the specified periods
3433
* display local time to UTC offset via JavaScript
3534
* recalculate UTC timestamp on the page into local time via JavaScript

Screenshot_2019-12-02 Sonoff S20.png

-44.7 KB
Binary file not shown.

helpers/print_timer_files.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
_TIMERS_FILENAME = 'timers.txt'
2+
_ACTIVE_DAYS_FILENAME = 'timer_days.txt'
3+
4+
5+
def print_files(file_name):
6+
print('_' * 79)
7+
print(file_name)
8+
with open(file_name, 'r') as f:
9+
print(f.read())
10+
print('-' * 79)
11+
12+
13+
print_files(_TIMERS_FILENAME)
14+
print_files(_ACTIVE_DAYS_FILENAME)

src/http_set_timer.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ async def get_form(server, reader, writer, querystring, timers=None):
1717
'on_selected': 'selected' if server.power_timer.active else '',
1818
'off_selected': '' if server.power_timer.active else 'selected',
1919
}
20-
from power_timer_schedule import get_active_days
20+
21+
from times_utils import get_active_days
2122
active_days = get_active_days()
2223
del get_active_days
23-
del sys.modules['power_timer_schedule']
24+
del sys.modules['times_utils']
2425
gc.collect()
2526

2627
for day_no in range(7):
@@ -45,22 +46,25 @@ async def get_submit(server, reader, writer, querystring):
4546
del sys.modules['urllib_parse']
4647
gc.collect()
4748

48-
from times_utils import parse_timers, save_timers
49+
from times_utils import parse_timers, save_timers, save_active_days
4950
try:
5051
timers = parse_timers(get_parameters['timers'])
52+
del parse_timers
5153

5254
save_timers(timers)
5355
del save_timers
5456

5557
power_timer_active = get_parameters['active'] == 'on'
5658

59+
save_active_days(tuple(sorted([
60+
no for no in range(7)
61+
if 'd%i' % no in get_parameters
62+
])))
63+
del save_active_days
64+
5765
from rtc import update_rtc_dict
5866
update_rtc_dict(data={
5967
constants.POWER_TIMER_ACTIVE_KEY: power_timer_active,
60-
constants.POWER_TIMER_WEEKDAYS_KEY: [
61-
no for no in range(7)
62-
if 'd%i' % no in get_parameters
63-
]
6468
})
6569
del update_rtc_dict
6670
except ValueError as e:
@@ -84,8 +88,6 @@ async def get_submit(server, reader, writer, querystring):
8488
server.power_timer.today_active = None
8589

8690
server.power_timer.schedule_next_switch()
87-
88-
del sys.modules['times_utils'] # used in schedule_next_switch
8991
gc.collect()
9092

9193
from http_utils import send_redirect

src/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def main():
1616

1717
from wifi import WiFi
1818

19-
__version__ = 'v0.6.0'
19+
__version__ = 'v0.6.1'
2020

2121
# Init device button IRQ:
2222
Pins.button_pin.irq(Button().irq_handler)

src/power_timer_schedule.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,16 @@
66
import utime as time
77

88

9-
def get_active_days():
10-
from rtc import get_rtc_value
11-
active_days = get_rtc_value(key=constants.POWER_TIMER_WEEKDAYS_KEY, default=list(range(7)))
12-
del get_rtc_value
13-
del sys.modules['rtc']
14-
gc.collect()
15-
return active_days
16-
17-
189
def active_today():
1910
"""
2011
Is the timer active this weekday?
2112
"""
13+
from times_utils import get_active_days
2214
active_days = get_active_days()
15+
del get_active_days
16+
del sys.modules['times_utils']
17+
gc.collect()
18+
2319
today = time.localtime()[6]
2420
return today in active_days
2521

src/times_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22

33
_TIMERS_FILENAME = 'timers.txt'
4+
_ACTIVE_DAYS_FILENAME = 'timer_days.txt'
45

56

67
def parse_time(clock_time):
@@ -76,6 +77,24 @@ def restore_timers():
7677
yield ()
7778

7879

80+
def get_active_days():
81+
try:
82+
with open(_ACTIVE_DAYS_FILENAME, 'r') as f:
83+
return tuple([int(d) for d in f.read().split(',')])
84+
except OSError:
85+
print('File not exists: %r' % _ACTIVE_DAYS_FILENAME)
86+
return tuple(range(7))
87+
88+
89+
def save_active_days(active_days):
90+
if tuple(get_active_days()) == active_days:
91+
# Don't save if same active days already exists
92+
return
93+
94+
with open(_ACTIVE_DAYS_FILENAME, 'w') as f:
95+
f.write(','.join([str(d) for d in active_days]))
96+
97+
7998
def get_next_timer(current_time):
8099
"""
81100
return next next switching point

0 commit comments

Comments
 (0)