/*
to set the clock
sudo date -s '2023-06-21 05:50:30 '

This program restores the ramdisk/ [].db database from SSD

---ATTENTION !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
---!@#$%^&*  run '.read sweep_ramdisk.sql' at the sqlite3 CLI  before shutting down pi or you WILL lose ramdisk data !@#$%^&*
--sometimes you need to reboot the RPi
--This means that the mounted ramdisk/ ?.db  will not exist upon startup.
--The ramdisk then needs to be built from scratch
--This is the companion to sweep_ramdisk.sql 
STEPS:
1. sweep - copies ramdisk/irx into ssd.irx, etc.
2. halt
3. power off
4. maintenance
5. power on
6. modbus - optional if changing charge parameters, etc.
7. build - reloads ramdisk table irx


#The ramdisk mountpoint directory is created beforehand on the ssd as 
mkdir /var/www/html/[your project directory]/ramdisk

pi@raspberrypi:~ $ cat /etc/fstab
...
tmpfs /var/www/html/AIO/ramdisk tmpfs nodev,nosuid,size=16M 0 0 <------ how to create the ramdisk.  
cd /var/www/html/AIO/; sqlite3 -init initx.sql
*/
.system date; ls -1 /dev/ttyUSB*; lsusb
.open 'ramdisk/aiox.db'
PRAGMA busy_timeout=12000;

.read cr_tables.sql
.read cr_ids.sql
.read cr_views.sql

--reload ramdisk database from ssd
attach 'aiox.db' as ssd;
.databases
.tables

.echo on
begin transaction;
-- keep last 5 days in ramdisk
select date(max(dt), '-7 days') from ssd.irx ;
insert or ignore into irx  select * from ssd.irx where dt >  (select date(max(dt), '-8 days') from ssd.irx) ;
insert or ignore into ir   select * from ssd.ir  where dt >  (select date(max(dt), '-8 days') from ssd.ir) ;
insert or ignore into hrx  select * from ssd.hrx where dt >  (select date(max(dt), '-8 days') from ssd.hrx) ;
insert or ignore into hr   select * from ssd.hr  where dt >  (select date(max(dt), '-8 days') from ssd.hr) ;
end transaction;

