scheduler: improve all the things
This commit is contained in:
		
							parent
							
								
									3a4a368038
								
							
						
					
					
						commit
						3f8f174a07
					
				
					 1 changed files with 132 additions and 130 deletions
				
			
		
							
								
								
									
										262
									
								
								boot.py
									
										
									
									
									
								
							
							
						
						
									
										262
									
								
								boot.py
									
										
									
									
									
								
							|  | @ -1,14 +1,15 @@ | ||||||
| # ssid, password |  | ||||||
| import wifi |  | ||||||
| import usocket as socket | import usocket as socket | ||||||
| from utime import sleep | from utime import sleep | ||||||
| 
 |  | ||||||
| from machine import Pin, RTC, I2C | from machine import Pin, RTC, I2C | ||||||
| import network | import network | ||||||
| from DS3231 import DS3231 | from DS3231 import DS3231 | ||||||
| import web | import web | ||||||
| 
 |  | ||||||
| import esp | import esp | ||||||
|  | ## wifi.py | ||||||
|  | # ssid = xxx | ||||||
|  | # password = yyy | ||||||
|  | import wifi | ||||||
|  | 
 | ||||||
| esp.osdebug(None) | esp.osdebug(None) | ||||||
| 
 | 
 | ||||||
| import ure | import ure | ||||||
|  | @ -16,6 +17,7 @@ import uos | ||||||
| import ujson | import ujson | ||||||
| 
 | 
 | ||||||
| import gc | import gc | ||||||
|  | 
 | ||||||
| gc.collect() | gc.collect() | ||||||
| 
 | 
 | ||||||
| # global variables | # global variables | ||||||
|  | @ -32,22 +34,22 @@ pin_cfg = Pin(12, Pin.IN) | ||||||
| pin_led = Pin(2, Pin.OUT) | pin_led = Pin(2, Pin.OUT) | ||||||
| 
 | 
 | ||||||
| ## CONFIG | ## CONFIG | ||||||
| CONFIG_FILE="schedule.json" | CONFIG_FILE = "schedule.json" | ||||||
| config = {} | config = {} | ||||||
| # try loading config from filesystem | # try loading config from filesystem | ||||||
| files = uos.listdir() | files = uos.listdir() | ||||||
| if CONFIG_FILE in files: | if CONFIG_FILE in files: | ||||||
|   f = open(CONFIG_FILE, "r") |     f = open(CONFIG_FILE, "r") | ||||||
|   config_raw=f.read() |     config_raw = f.read() | ||||||
|   config = ujson.loads(config_raw) |     config = ujson.loads(config_raw) | ||||||
|   f.close() |     f.close() | ||||||
| for i in range(7): | for i in range(7): | ||||||
|   if not config.get(i): |     if not config.get(i): | ||||||
|     config[i] = { |         config[i] = { | ||||||
|       1: {"on": {"h": 0, "m": 0}, "off": {"h": 0, "m": 0}}, |             1: {"on": {"h": 0, "m": 0}, "off": {"h": 0, "m": 0}}, | ||||||
|       2: {"on": {"h": 0, "m": 0}, "off": {"h": 0, "m": 0}} |             2: {"on": {"h": 0, "m": 0}, "off": {"h": 0, "m": 0}} | ||||||
|     } |         } | ||||||
|     print("initialized day: ", i) |         print("initialized day: ", i) | ||||||
| # print("CONFIG: %s" % config) | # print("CONFIG: %s" % config) | ||||||
| 
 | 
 | ||||||
| ## RTC | ## RTC | ||||||
|  | @ -63,133 +65,133 @@ hf = "header.html" | ||||||
| ff = "footer.html" | ff = "footer.html" | ||||||
| 
 | 
 | ||||||
| if (pin_cfg.value() == 1): | if (pin_cfg.value() == 1): | ||||||
|   pin_led.value(0) |     pin_led.value(0) | ||||||
| 
 | 
 | ||||||
|   header = open(hf, "r").read() |     header = open(hf, "r").read() | ||||||
|   footer = open(ff, "r").read() |     footer = open(ff, "r").read() | ||||||
| 
 | 
 | ||||||
|   station = network.WLAN(network.STA_IF) |     station = network.WLAN(network.STA_IF) | ||||||
|   station.active(True) |     station.active(True) | ||||||
|   station.connect(wifi.ssid, wifi.password) |     station.connect(wifi.ssid, wifi.password) | ||||||
|   while station.isconnected() == False: |     while station.isconnected() == False: | ||||||
|     pass |         pass | ||||||
|   # print("Connection successful", station.ifconfig()) |     # print("Connection successful", station.ifconfig()) | ||||||
| 
 | 
 | ||||||
|   s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||||||
|   s.bind(("", 80)) |     s.bind(("", 80)) | ||||||
|   s.listen(5) |     s.listen(5) | ||||||
| 
 | 
 | ||||||
|   print("Configuration now...") |     print("Configuration now...") | ||||||
|   while True: |     while True: | ||||||
|     conn, addr = s.accept() |         conn, addr = s.accept() | ||||||
|     req_recv = conn.recv(512) |         req_recv = conn.recv(512) | ||||||
|     req_decode = req_recv.decode() |         req_decode = req_recv.decode() | ||||||
|     req_split = req_decode.split("\n") |         req_split = req_decode.split("\n") | ||||||
|     request = req_split[0] |         request = req_split[0] | ||||||
|     print("request: ", request) |         print("request: ", request) | ||||||
| 
 | 
 | ||||||
|     response = "" |         response = "" | ||||||
| 
 | 
 | ||||||
|     mode = "default" |         mode = "default" | ||||||
|     if "/config_dt" in request: |         if "/config_dt" in request: | ||||||
|       mode = "config_dt" |             mode = "config_dt" | ||||||
|     elif "/config_day" in request: |         elif "/config_day" in request: | ||||||
|       mode = "config_day" |             mode = "config_day" | ||||||
|     elif "/favicon" in request: |         elif "/favicon" in request: | ||||||
|       mode = "none" |             mode = "none" | ||||||
| 
 | 
 | ||||||
|     ## config_* --> save |         ## config_* --> save | ||||||
|     if ("config" in mode) and ("save=1" in request): |         if ("config" in mode) and ("save=1" in request): | ||||||
|       update = False |             update = False | ||||||
|       ## DATE |             ## DATE | ||||||
|       reg = ure.search("date=[0-9-]+", request) |             reg = ure.search("date=[0-9-]+", request) | ||||||
|       if reg: |             if reg: | ||||||
|         tmp = reg.group(0).replace("date=", "").split("-") |                 tmp = reg.group(0).replace("date=", "").split("-") | ||||||
|         if len(tmp) == 3: |                 if len(tmp) == 3: | ||||||
|           update = True |                     update = True | ||||||
|           new_date = [int(tmp[0]), int(tmp[1]), int(tmp[2])] |                     new_date = [int(tmp[0]), int(tmp[1]), int(tmp[2])] | ||||||
|           ds.Date(new_date) |                     ds.Date(new_date) | ||||||
| 
 | 
 | ||||||
|       ## TIME |             ## TIME | ||||||
|       reg = ure.search("time=[0-9-]+", request) |             reg = ure.search("time=[0-9-]+", request) | ||||||
|       if reg: |             if reg: | ||||||
|         tmp = reg.group(0).replace("time=", "").split("-") |                 tmp = reg.group(0).replace("time=", "").split("-") | ||||||
|         if len(tmp) >= 2: |                 if len(tmp) >= 2: | ||||||
|           update = True |                     update = True | ||||||
|           new_time = [int(tmp[0]), int(tmp[1]), 0] |                     new_time = [int(tmp[0]), int(tmp[1]), 0] | ||||||
|           if len(tmp) == 3: |                     if len(tmp) == 3: | ||||||
|             new_time[2] = int(tmp[2]) |                         new_time[2] = int(tmp[2]) | ||||||
|           ds.Time(new_time) |                     ds.Time(new_time) | ||||||
| 
 | 
 | ||||||
|       if update: |             if update: | ||||||
|         update = False |                 update = False | ||||||
|         tmp_dt = ds.DateTime() |                 tmp_dt = ds.DateTime() | ||||||
|         tmp_dt.append(0) |                 tmp_dt.append(0) | ||||||
|         rtc.datetime(tmp_dt) |                 rtc.datetime(tmp_dt) | ||||||
|         print("Updated datetime", rtc.datetime()) |                 print("Updated datetime", rtc.datetime()) | ||||||
| 
 | 
 | ||||||
|       ## DAY |             ## DAY | ||||||
|       reg = ure.search("day=[0-6]+", request) |             reg = ure.search("day=[0-6]+", request) | ||||||
|       if reg: |             if reg: | ||||||
|         update = True |                 update = True | ||||||
|         tmp = reg.group(0).replace("day=", "") |                 tmp = reg.group(0).replace("day=", "") | ||||||
|         day = int(tmp) |                 day = int(tmp) | ||||||
|         for p in [1, 2]: |                 for p in [1, 2]: | ||||||
|           # on |                     # on | ||||||
|           reg = ure.search(("p%d_on=[0-9-]+" % p), request) |                     reg = ure.search(("p%d_on=[0-9-]+" % p), request) | ||||||
|           if reg: |                     if reg: | ||||||
|             tmp = reg.group(0).replace(("p%d_on=" % p), "").split("-") |                         tmp = reg.group(0).replace(("p%d_on=" % p), "").split("-") | ||||||
|             if len(tmp) == 2: |                         if len(tmp) == 2: | ||||||
|               h = int(tmp[0]) |                             h = int(tmp[0]) | ||||||
|               m = int(tmp[1]) |                             m = int(tmp[1]) | ||||||
|               config[day][p]["on"] = {"h": h, "m": m} |                             config[day][p]["on"] = {"h": h, "m": m} | ||||||
|           # off |                     # off | ||||||
|           reg = ure.search(("p%d_off=[0-9-]+" % p), request) |                     reg = ure.search(("p%d_off=[0-9-]+" % p), request) | ||||||
|           if reg: |                     if reg: | ||||||
|             tmp = reg.group(0).replace(("p%d_off=" % p), "").split("-") |                         tmp = reg.group(0).replace(("p%d_off=" % p), "").split("-") | ||||||
|             if len(tmp) == 2: |                         if len(tmp) == 2: | ||||||
|               h = int(tmp[0]) |                             h = int(tmp[0]) | ||||||
|               m = int(tmp[1]) |                             m = int(tmp[1]) | ||||||
|               config[day][p]["off"] = {"h": h, "m": m} |                             config[day][p]["off"] = {"h": h, "m": m} | ||||||
| 
 | 
 | ||||||
|       if update: |             if update: | ||||||
|         f = open(CONFIG_FILE, "w") |                 f = open(CONFIG_FILE, "w") | ||||||
|         f.write(ujson.dumps(config)) |                 f.write(ujson.dumps(config)) | ||||||
|         f.close() |                 f.close() | ||||||
|         print("updated config:\n %s" % (ujson.dumps(config))) |                 print("updated config:\n %s" % (ujson.dumps(config))) | ||||||
|         # print("updated config") |                 # print("updated config") | ||||||
| 
 | 
 | ||||||
|     ## default |         ## default | ||||||
|     if mode == "default": |         if mode == "default": | ||||||
|       print("web_default") |             print("web_default") | ||||||
|       response = header + web.default() + footer |             response = header + web.default() + footer | ||||||
|     ## config_dt |         ## config_dt | ||||||
|     elif mode == "config_dt": |         elif mode == "config_dt": | ||||||
|       response = header + web.config_datetime(rtc) + footer |             response = header + web.config_datetime(rtc) + footer | ||||||
|     ## config_day |         ## config_day | ||||||
|     elif mode == "config_day": |         elif mode == "config_day": | ||||||
|       reg = ure.search("day=[0-6-]+", request) |             reg = ure.search("day=[0-6-]+", request) | ||||||
|       if reg: |             if reg: | ||||||
|         tmp = reg.group(0).replace("day=", "") |                 tmp = reg.group(0).replace("day=", "") | ||||||
|         day = int(tmp) |                 day = int(tmp) | ||||||
|         print("web_config, day ", day) |                 print("web_config, day ", day) | ||||||
|         response = header + web.config_day(config, day) + footer |                 response = header + web.config_day(config, day) + footer | ||||||
|     ## favicon |         ## favicon | ||||||
|     elif mode == "favicon": |         elif mode == "favicon": | ||||||
|       print("favicon") |             print("favicon") | ||||||
|       response = "" |             response = "" | ||||||
| 
 | 
 | ||||||
|     ## send response |         ## send response | ||||||
|     try: |         try: | ||||||
|         conn.send("HTTP/1.1 200 OK\n") |             conn.send("HTTP/1.1 200 OK\n") | ||||||
|         conn.send("Content-Type: text/html\n") |             conn.send("Content-Type: text/html\n") | ||||||
|         conn.send("Connection: close\n\n") |             conn.send("Connection: close\n\n") | ||||||
|         conn.sendall(response) |             conn.sendall(response) | ||||||
|         sleep(1) |             sleep(1) | ||||||
|         conn.close() |             conn.close() | ||||||
|         # print("connection closed successfully") |             # print("connection closed successfully") | ||||||
|     except: |         except: | ||||||
|       print("connection closed with error") |             print("connection closed with error") | ||||||
| 
 | 
 | ||||||
| else: | else: | ||||||
|   print("running scheduler...") |     print("running scheduler...") | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue