Added basic file read/write module

This commit is contained in:
Laika 2021-07-08 21:37:03 -04:00
parent 9598a855c4
commit 44ce4b07d3
4 changed files with 65 additions and 5 deletions

View file

@ -1,3 +1,10 @@
2:GET:heartbeat:MTE 2:GET:heartbeat:MTE
4:GET:location:coords 4:GET:location:coords
70:SET:heartbeat:kill:1 5:SET:memory:filename:shark.bmp
6:SET:memory:buffer:Qk1+BgAAAAAAAD4AAAAoAAAAZAAAAGQAAAABAAEAAAAAAEAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP///wD////////////////wAAAA////////////////8AAAAP////////////////AAAAD//////////8/////wAAAA//////////+P////8AAAAP//////////j/////AAAAD//////////4f////wAAAA///////////H////8AAAAP//////////x/////AAAAD//n//////x8f////wAAAA//x//////4fD////8AAAAP/8P/+fgH/H4/////AAAAD//j//HwB/w+H////wAAAA//4//x+Af8Px////8AAAAP/+P/8f/H/j8P////AAAAD//h//D/x/4AD////wAAAA//8f/4/8f+AAf///8AAAAP//H/+P/H/wAH////AAAAD//w//j/x/+Hw////wAAAA//+P/4f4f/x+P///8AAAAP//j4PH+P/8Pj////AAAAD//4ABx/j//h4////wAAAA//+AAcf4//4eP///8AAAAP//gDPD+H8/H3////AAAAD//4//4/g8P7/////wAAAA//+P/+P8AD//////8AAAAP//h//j/gB///////AAAAD//8f/9/8B///////wAAAA///Hx///////////8AAAAP//wAf///////////AAAAD//8AP///////////wAAAA///gH/+P////////8AAAAP//////D/////////AAAAD//////w/////////wAAAA//////4P////////8AAAAP/////+D/////////AAAAD/////+Af////f///wAAAA//////AH////H///8AAAAP/////gx////A////AAAAD////8AAA///AP///wAAAA////+AAAAH/gD///8AAAAP///+AAAAAPgY////AAAAD////Bx/+AAgeP///wAAAA////g+///wAPj///8AAAAP///wf////AP4////AAAAD///4B////AD+P///wAAAA///8AP///gYfD///8AAAAP///Dz///wfBw////AAAAD///h////gP4MP///wAAAA///4////wP/BH///8AAAAP//+P4//wH/4B////AAAAD///j8P/wP//Af///wAAAA///w/D/gH//8H///8AAAAP//8f5/wH///h////AAAAD///H//gB///8f///wAAAA///w//AMf///H///8AAAAP//8P/AGH////////AAAAD///gAAPj////////wAAAA///4AAB4////////8AAAAP///AAAGP////////AAAAD//////Aj////////wAAAA//////8A////////8AAAAP//////gP////////AAAAD//////8D////////wAAAA///////g////////8AAAAP//////+f////////AAAAD////////////////wAAAA////////////////8AAAAP////////////////AAAAD////////////////wAAAA////////////////8AAAAP////////////////AAAAD////////////////wAAAA////////////////8AAAAP////////////////AAAAD////////////////wAAAA////////////////8AAAAP////////////////AAAAD/////gf/n///////wAAAA/n///wH/x///////8AAAAPx/8B8A/8P///////AAAAD8f+APuP/j///////wAAAA/H/gD/j/4///////8AAAAPx/45/4/+H///////AAAAD8f+P/+P/x///////wAAAA/H/j//D/8f//////8AAAAPx/4//x//H///////AAAAD8f+P/4f/w///////wAAAA+H/AH8P/+MH/////8AAAAPj/gB+H/ngB//////AAAAD4/8A/j/wAA//////wAAAA8P/h/4H8AB//////8AAAAPH/4/+APgD///////AAAADx/+AAwD/////////wAAAAAf/gAPx/////////8AAAAAAA8AH///////////AAAAAAAP/////////////wAAAA4AH/////////////8AAAAP////////////////AAAAD////////////////wAAAA
7:EXE:memory
8:GET:memory:freeSpace
8:SET:memory:mode:download
8:SET:memory:filename:badtest
9:EXE:memory
10:SET:heartbeat:kill:1

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

51
satsim/modules/memory.py Normal file
View file

@ -0,0 +1,51 @@
from .module import module
class memory_module(module):
def __init__(self):
super().__init__("memoryBank")
self.fields["files"] = []
self.fields["freeSpace"] = 4000
self.fields["mode"] = "upload"
self.fields["buffer"] = 0
self.fields["filename"] = ""
self.writable = ["buffer","filename", "mode"]
def mod_get(self, field="none"):
if field not in self.fields:
return (-1, "GET FATAL: field '" + field + "'' does not exist in module: " + self.fields["name"])
return (self.fields[field], "GET OK")
def mod_set(self, field="none", value="none"):
if field not in self.fields:
return (-1, "SET FATAL: field '" + field + "'' does not exist in module: " + self.fields["name"])
if field not in self.writable:
return (-1, "SET FATAL: field '" + field + "' is not writable in module: " + self.fields["name"])
if field == "mode" and (value == "upload" or value == "download"):
self.fields[field] = value
return (0, "SET OK")
self.fields[field] = value
return (0, "SET OK")
def mod_exe(self):
if self.fields["mode"] == "upload":
if self.fields["freeSpace"] - len(self.fields["buffer"]) >= 0:
self.fields["freeSpace"] -= len(self.fields["buffer"])
with open("./datastore/"+self.fields["filename"],"wb+") as f:
f.write(bytes(self.fields["buffer"],encoding="UTF-8"))
return (0, "WRITE OK")
else:
return (-1, "WRITE FATAL: OUT OF MEMORY")
if self.fields["mode"] == "download":
try:
with open("./datastore/"+self.fields["filename"],"rb") as f:
return (f.read(), "READ OK")
except:
return (-1, "READ FATAL: FILE NOT FOUND")
def mod_not(self):
pass
def mod_update(self):
pass

View file

@ -2,6 +2,7 @@ import numpy as np
from modules.gamma import gamma_sensor from modules.gamma import gamma_sensor
from modules.heartbeat import heartbeat_module from modules.heartbeat import heartbeat_module
from modules.location import location_module from modules.location import location_module
from modules.memory import memory_module
from time import sleep from time import sleep
from astropy.coordinates import SkyCoord as sc from astropy.coordinates import SkyCoord as sc
from os import system from os import system
@ -22,7 +23,8 @@ class sat:
"heartbeat" : heartbeat_module(), "heartbeat" : heartbeat_module(),
"location" : location_module(), "location" : location_module(),
#"power" : power_manager(), #"power" : power_manager(),
#"attitude" : attitude_module(), #"attitude" : attitude_module(),
"memory" : memory_module(),
"gamma_sensor" : gamma_sensor() "gamma_sensor" : gamma_sensor()
} }
@ -49,8 +51,8 @@ class sat:
res = self.modules[com[2]].mod_get(com[3]) res = self.modules[com[2]].mod_get(com[3])
elif com[1] == "SET" and len(com) == 5: elif com[1] == "SET" and len(com) == 5:
res = self.modules[com[2]].mod_set(com[3],com[4]) res = self.modules[com[2]].mod_set(com[3],com[4])
elif com[1] == "EXE" and len(com) == 5: elif com[1] == "EXE" and len(com) == 3:
res = self.modules[com[2]].mod_exe(com[3],com[3]) res = self.modules[com[2]].mod_exe()
else: else:
res = (-1, "BAD COMMAND: " + c) res = (-1, "BAD COMMAND: " + c)
print(res) print(res)
@ -62,7 +64,7 @@ class sat:
print("heading to bed") print("heading to bed")
return return
with open("status", "r+") as statfile: with open("MTE", "r+") as statfile:
statfile.seek(0) statfile.seek(0)
statfile.truncate() statfile.truncate()
statfile.write(str(MTE)) statfile.write(str(MTE))