-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffbf556
commit 1bcdc8a
Showing
6 changed files
with
299 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"lastDocking": "---- Docking 0 completed at 0.0 v after 0.0 h playtime", "chargeCycles": 0, "lastDismount": "---- Dismount 0 at 0.0 v after 0.0 h recharge", "dockingState": 0, "chargingState": 0, "lastDismountTime": "YYYY-MM-DD HH:MM:SS", "lastRechargeDuration": "0.0", "newBatteryDate": "2021-06-21", "newBatteryAtDocking": "0", "newBatteryAtLifeHours": "0.0", "newBatteryDesc": "Original GoPiGo3/TalentCell 12v 3000mAh", "lastDockingTime": "YYYY-MM-DD HH:MM:SS", "lastPlaytimeDuration": "0.0"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# file: daveDataJson.py | ||
# | ||
# Serialize data values to /home/pi/GoPi5Go/daveData.json | ||
# (running ./daveDataJson.py will create the file) | ||
# | ||
# Methods: | ||
# saveData(dataname, datavalue, logit=False) # adds datanaem:datavalue to rosbotData.json file | ||
# getData(dataname=None) # either returns dictionary with all values, or just value of passed name | ||
# delData(dataname) # delete item from rosbotData.json | ||
# printData() # prints contents of rosbotData.json | ||
# | ||
|
||
""" | ||
Example from Carl: | ||
lastDocking : ---- Docking 4573 completed at 8.1 v after 2.6 h playtime | ||
chargeCycles : 4573 | ||
lastDismount : ---- Dismount 4573 at 10.9 v after 2.5 h recharge | ||
dockingState : 1 | ||
chargingState : 1 | ||
chargeConditioning : 0 | ||
lastDismountTime : 2024-04-15 06:47:03 | ||
lastRechargeDuration : 2.5 | ||
newBatterySetDate : 2023-03-21 | ||
newBatterySetAtDocking : 3652 | ||
newBatterySetAtLifeHours : 36810.9 | ||
newBatterySetDesc : 8x Eneloop White 2000 mAh NiMH AA cells | ||
lastDockingTime : 2024-04-15 04:15:21 | ||
lastPlaytimeDuration : 2.6 | ||
""" | ||
import sys | ||
sys.path.insert(1,'/home/pi/GoPi5Go/plib') | ||
|
||
import json | ||
import threading | ||
# import runLog | ||
DATA_FILE = '/home/pi/GoPi5Go/daveData.json' | ||
|
||
DataLock = threading.Lock() # with DataLock: any operation to make syncronous | ||
|
||
def saveData(dataname, datavalue, logit=False): | ||
|
||
|
||
# print("-- saveData({},{}) called".format(dataname, datavalue)) | ||
with DataLock: # prevents two different saveData() at same time | ||
lData = {} | ||
# print("got lock") | ||
try: | ||
|
||
lData = getData() # lock assures no one changes this till we are done | ||
if lData == None: | ||
lData = {} | ||
# print(" Data:", lData) | ||
lData[dataname] = datavalue | ||
# print(" lData:",lData) | ||
|
||
with open(DATA_FILE, 'w') as outfile: | ||
json.dump( lData, outfile ) | ||
# print(" Data.json updated") | ||
if logit: runLog.logger.info("** Data '{}' = {} updated **".format(dataname, datavalue)) | ||
except: | ||
# print(" saveData failed") | ||
return False | ||
|
||
return True | ||
|
||
def delData(dataname): | ||
# print("-- delData({}) called".format(dataname)) | ||
|
||
with DataLock: | ||
lData = {} | ||
try: | ||
|
||
lData = getData() | ||
if lData == None: | ||
lData = () | ||
# print(" Data:", lData) | ||
if dataname in lData: | ||
del lData[dataname] | ||
# print(" lData:", lData) | ||
|
||
with open(DATA_FILE, 'w') as outfile: | ||
json.dump( lData, outfile ) | ||
# print(" Data.json updated") | ||
# else: print(" {} not found in Data".format(dataname)) | ||
except: | ||
# print(" delData{} failed".dataname) | ||
return False | ||
|
||
return True | ||
|
||
|
||
def getData(dataname=None): | ||
|
||
|
||
# print("-- getData({}) called".format(dataname)) | ||
|
||
try: | ||
with open(DATA_FILE, 'r') as infile: | ||
lData = json.load(infile) | ||
if (dataname == None): | ||
return lData | ||
else: | ||
return lData[dataname] | ||
except: | ||
# print(" getData() exception") | ||
return None | ||
|
||
def printData(): | ||
print("daveData.json contents:") | ||
lData = getData() | ||
if lData != None: | ||
for i in lData: | ||
print(" ",i," : ",lData[i]) | ||
else: | ||
print("daveData.json contains no data") | ||
|
||
def main(): | ||
|
||
print("** Starting main()") | ||
|
||
printData() | ||
|
||
lData = getData() | ||
print(" daveData: ",lData) | ||
|
||
chargeCycles = 1 | ||
|
||
if (saveData('chargeCycles', chargeCycles) == True): | ||
print(' Saved chargeCycles: {}'.format(chargeCycles)) | ||
else: | ||
print(" saveData('chargeCycles') failed") | ||
|
||
lData = getData() | ||
print(" daveData: ",lData) | ||
|
||
|
||
chargeCycles = int(getData('chargeCycles')) | ||
chargeCycles += 1 | ||
|
||
if (saveData('chargeCycles', chargeCycles) == True): | ||
print(' Saved chargeCycles: {}'.format(chargeCycles)) | ||
else: | ||
print(" saveData('chargeCycles') failed") | ||
|
||
|
||
if 'chargeCycles' in lData: | ||
print("removing chargeCycles from daveData.json") | ||
delData('chargeCycles') | ||
|
||
if (saveData('nothing',"not important" ) == True): | ||
print(' Saved nothing: {}'.format("not important")) | ||
else: | ||
print(" saveData('nothing') failed") | ||
|
||
lData = getData() | ||
print(" Data: ",lData) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
sys.path.insert(1,'/home/pi/GoPi5Go/plib') | ||
import daveDataJson | ||
|
||
|
||
|
||
try: | ||
chargeCycles = int(daveDataJson.getData('chargeCycles')) | ||
chargeCycles += 1 | ||
except: | ||
chargeCycles = 0 | ||
|
||
|
||
if (daveDataJson.saveData('chargeCycles', chargeCycles) == True): | ||
print(' Saved chargeCycles: {}'.format(chargeCycles)) | ||
else: | ||
print(" saveData('chargeCycles') failed") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
sys.path.insert(1,"/home/pi/GoPi5Go/plib") | ||
|
||
import daveDataJson | ||
|
||
# print("daveDataJson contents:") | ||
# lcarlData = carlDataJson.getCarlData() | ||
# for i in lcarlData: | ||
# print(" ",i," : ",lcarlData[i]) | ||
|
||
daveDataJson.printData() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters