def flashWriteData(trame) : #on écrit trame dans fichier data ofi=open('fichier_data', 'a') dispo=os.getfree('/flash')# vérifie si y a de la place sur flash if dispo > 100: ofi.write(trame) ofi.close() return
def flashWriteMeasure(mesure,) : #on écrit les mesures dernière trame dans fichier mesure ofi=open('fichier_derniere_mesure', 'w') dispo=os.getfree('/flash')#en cours d'ajout, non testé, vérifie si y a de la place sur flash if dispo > 100: ofi.write(mesure) ofi.close() return
def __init__(self, max_file_size_kb: int = 10, log_to_sd_card: bool = False): # set up error logging to log file self.rtc = machine.RTC() self.MAX_FILE_SIZE = max_file_size_kb * 1000 # in bytes self.logfile = ('/sd/' if log_to_sd_card else '') + 'log.txt' with open(self.logfile, 'a') as f: self.file_position = f.tell() print("++ file logging enabled") print("\tfile: \"{}\"".format(self.logfile)) print("\tcurrent size: {: 9.2f} KB".format(self.file_position / 1000.0)) print("\tmaximal size: {: 9.2f} KB".format(self.MAX_FILE_SIZE / 1000.0)) print("\tfree flash memory:{: 6d} KB".format(os.getfree('/flash'))) if log_to_sd_card: print("\tfree SD memory: {: 6d} MB".format( int(os.getfree('/sd') / 1000))) print("")
def house_keeping(targetDir,targetSpace,match_str): # targetSpace in GiB # os.chdir(targetDir) files_ = os.listdir() files = sorted([file for file in files_ if match_str in file]) nfiles = len(files) for file in files: logger.debug(file) logger.debug('nfiles={}'.format(nfiles)) cur = 0 logger.info('Before: Free = {}GiB, Target={}GiB'.format(os.getfree(targetDir)/(1024*1024),targetSpace)) while os.getfree(targetDir) < targetSpace*1024*1024: if match_str in files[cur]: os.remove(targetDir + '/' + files[cur]) logger.info('{} deleted.'.format(files[cur])) cur += 1 if cur == nfiles: logger.error('no matching file found!') logger.info('After : Free = {}GiB, Target={}GiB'.format(os.getfree(targetDir)/(1024*1024),targetSpace)) return
def send_file(): logger.info("FTP to CTLR ...") logger.info("Remote={} ...".format('/sd/data/{}'.format( date_string(T_start)))) os.chdir('/sd') # out = dropfile.dropfile(CTLR_IPADDRESS, 21, '/sd/data/{}'.format(date_string(T_start)), 'micro', 'python', filename) out = dropfile.dropfile(CTLR_IPADDRESS, 21, '/sd/data', 'micro', 'python', filename) if out == 0: logger.info("FTP Successful.") else: logger.info("FTP Failed.") diskutil.house_keeping('/sd/data', 20, 'acc') logger.info('available space = {}GiB'.format(os.getfree('/sd')))
def flashWriteData(trame): #on écrit trame dans fichier data print("ecriture de trame") ofi = open('fichier_data', 'a') dispo = os.getfree('/flash') # vérifie si y a de la place sur flash if dispo > 100: ofi.write(trame) else: print("memoire saturee") pycom.rgbled( c.jaune_pale ) #si pas de mémoire, on arrête le programme qui va rebooter à l'infini à cause du chien de garde time.sleep(c.timeout / 1000) ofi.close() return
def _df(part, verbose): free_kib = os.getfree(part) free_b = free_kib * 1024 free_mib = free_kib / 1024 if verbose: used_b = du(part, do_return=True) used_kib = used_b / 1024 total_b = used_b + free_b total_kib = total_b / 1024 error_b = 4 * 1024 * 1024 - total_b print(part, ' ', free_b, ' B free (', free_kib, " KiB, ", round(free_kib / 1024,2), " MiB), ", used_b, ' B used (', round(used_kib,2), ' KiB, ', round(used_kib / 1024,2), ' MiB), ', total_b, ' B total (', total_kib, ' KiB, ', round(total_kib / 1024,2), ' MiB) [error_b=', error_b, ']', sep='') # /flash 4087808 B free (3992 KiB, 3.9 MiB), 41247 B used (40.28 KiB, 0.04 MiB), 4129055 B total (4032.28 KiB, 3.94 MiB) [error_b=65249] # FIXME: why do we have an error of 64K? # I would expect something below 1K due to rounding since os.getfree() reports in KiB # probably something like inodes, ie managing directories and filenames else: print(part, kib * 1024, 'B free (', kib, "KiB)", sep='')
def _stress(): s = "All work and no play makes Jack a dull boy" * 10 d = '/flash/stress' try: mkdir(d) except: pass if False: print(os.listdir('/flash/stress')) ct = 0 while True: f = d + '/jack{:05}.txt'.format(ct) print(ct) for x in range(1): tee(s, f, True) _spin(x) print('\b', end='') print(ct, os.getfree('/flash'), len(os.listdir(d))) ct += 1
def mem_free(self): import os try: self.write_int16(os.getfree('/flash')) except AttributeError: self.write_int16(350000)
def send_file(): _files = os.listdir('/sd/data/uploading') logger.info('{} file(s) to upload from /sd/data/uploading...'.format( len(_files))) time.sleep(15 * (iACC - 1)) for _file in _files: _file_fullpath = '/sd/data/uploading/' + _file # _file='gcam1-acc1-2020-0507-201110' # 0 0 0 _dateStr = _file[11:20] # yyyy-mmdd _timestamp = _file[11:] # yyyy-mmdd-HHMMSS _dest_path = '/sd/data/' + _dateStr logger.info("FTP to CTLR: Remote={} ...".format(_dest_path)) os.chdir('/sd/data/uploading') iter = 0 out = None while iter < 3: try: out = dropfile.dropfile(CTLR_IPADDRESS, 21, _dest_path, 'micro', 'python', _file) logger.debug('out={}'.format(out)) if out == 0: break # Successful if out == -1: logger.debug( '*************************************************') logger.debug( 'retry dropfile, current_iter={}, connection timeout; svr busy' .format(iter)) logger.debug( '*************************************************') iter += 1 time.sleep( 5) # waiting until the connection is closed by the svr continue else: logger.debug( '*************************************************') logger.debug( 'retry dropfile, current_iter={}, svr response out={}'. format(iter, out)) logger.debug( '*************************************************') iter += 1 time.sleep( 5) # waiting until the connection is closed by the svr continue except OSError as exc: # if exc.args[0] == uerrno.ECONNRESET or exc.args[0] == uerrno.ECONNABORTED or \ # exc.args[0] == uerrno.ETIMEDOUT: logger.debug( '*************************************************') logger.exception( 'retry dropfile, current_iter={}, errorcode={}'.format( iter, exc.args[0])) logger.debug( '*************************************************') time.sleep(30) iter += 1 continue except: logger.debug( '*************************************************') logger.exception( 'retry dropfile, current_iter={}, non-OSError'.format( iter)) logger.debug( '*************************************************') time.sleep(30) iter += 1 continue if out == 0: logger.info("FTP Successful.") _file_fullpath_new = _file_fullpath.replace( 'uploading', 'uploaded') os.rename(_file_fullpath, _file_fullpath_new) logger.info('file moved to uploaded folder.') request = 'set,file{},{};'.format(iACC, _timestamp) logger.info('sending request to the sever: "{}"'.format(request)) response = socket_client.request(request) else: logger.info("****** FTP Failed after all *******") # filename='/sd/data/uploading/gcam1-acc1-2020-0507-201110' # 0 0 0 0 0 timestamp = filename[30:] # yyyy-mmdd-HHMMSS request = 'set,state{},3;set,rms{},{},{:.6f};'.format( iACC, iACC, timestamp, rmsACC) logger.info('sending request to the sever: "{}"'.format(request)) response = socket_client.request(request) diskutil.house_keeping('/sd/data/uploaded', 25, 'acc') diskutil.house_keeping('/sd/log', 3, 'log') logger.info('available space = {:,} bytes'.format(os.getfree('/sd'))) gc.collect() return
print("ULP_WAKE") print("===== os =========================================") print("sysname", os.uname().sysname) # e.g., GPy print("release", os.uname().release) # e.g., 1.20.1.r1 print("release", os.uname().version) print("uname", os.uname()) import pycom print("===== pycom ======================================") try: print("free_heap", pycom.get_free_heap()) # print("bootmgr", pycom.bootmgr()) print("partition", pycom.bootmgr()[0]) print("fs_type", pycom.bootmgr()[1]) print("free", os.getfree('/flash')) print(pycom.bootmgr()[2]) # safeboot print(pycom.bootmgr()[3]) # status print("ota_slot", hex(pycom.ota_slot())) if (pycom.ota_slot() == 0x210000): print("ota_slot is", "'ota_0' in 'new' 8MB layout") elif (pycom.ota_slot() == 0x1A0000): print("ota_slot is", "'ota_0' in 'old' 4MB layout") elif (pycom.ota_slot() == 0x10000): print("ota_slot is", "'Factory'") else: raise Exception("Unkown ota_slot"+ str(pycom.ota_slot())) except: pass try:
def disk_status(): flash_used = 100 - os.getfree('/flash') / (4 * 1024) * 100 # in (%) sd_used = 100 - os.getfree('/sd') / (32 * 1024 * 1024) * 100 # in (%) return [int(flash_used), int(sd_used)]
targetDir = '/sd' targetSpace = 1024 * 1024 # 1 GiB sd = SD() logger.info('mounting SD...') os.mount(sd, '/sd') logger.info('mounting success') def date_string(time_sec): t_ = time.localtime(time_sec) return '{}-{:0>2d}{:0>2d}-{:0>2d}{:0>2d}{:0>2d}'.format( t_[0], t_[1], t_[2], t_[3], t_[4], t_[5]) # sys.exit('') logger.info('free-space ={}'.format(os.getfree('/sd'))) while True: logger.info('creating a file ...') diskutil.create_file('/sd/acc{}'.format(date_string(time.time())), 1) logger.info('creating a file Done.') free_space = os.getfree('/sd') logger.info('free-space ={}'.format(free_space)) diskutil.house_keeping('/sd', 15, 'acc') logger.info('Disk space available after house-keeping: {}'.format( os.getfree(targetDir))) time.sleep(1)
date_string(time_start)))) os.chdir('/sd') out = dropfile.dropfile(CTLR_IPADDRESS, 21, '/sd/data/{}'.format(date_string(time_start)), 'micro', 'python', filename) if out == 0: logger.debug("success") else: logger.debug('Failed.') logger.debug("Transferring file to CTLR via FTP Done.") # logger.info('creating a file ...') # diskutil.create_file('/sd/gcam{}-acc{}-{}'.format(iCAM,iACC,datetime_string(time.time())),1) # logger.info('creating a file Done.') diskutil.house_keeping('/sd/', 20, 'acc') logger.info('available space = {}GiB'.format(os.getfree('/sd'))) # # DEEP SLEEP (state=5) # response = socket_client.request('set,state{},5'.format(iACC)) t_now = time.time() res = (t_now + 43) % T_exec if res != 0: t_next = t_now + 43 - res + T_exec else: t_next = t_now + 43 logger.debug('time_now = {}'.format(datetime_string(t_now))) logger.debug('time_next = {}'.format(datetime_string(t_next))) logger.debug("Going to Sleep for {} seconds...".format(t_next - 40 - (t_now))) time.sleep(3)