Skip to content

Commit bfa3522

Browse files
committed
Now has the tables and methods for sebastians power boards
1 parent 880c2e0 commit bfa3522

File tree

3 files changed

+185
-1
lines changed

3 files changed

+185
-1
lines changed

database/feshie-structure.sql

+108
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,114 @@ ALTER TABLE `unprocessed_smart_data`
958958
ALTER TABLE `wind_readings`
959959
ADD CONSTRAINT `wind_readings_ibfk_1` FOREIGN KEY (`device`) REFERENCES `devices` (`id`);
960960

961+
962+
--
963+
-- Tables for Sebastians power board and required constraints
964+
--
965+
966+
967+
--
968+
-- Table structure for table `solar_current_readings`
969+
--
970+
971+
DROP TABLE IF EXISTS `solar_current_readings`;
972+
CREATE TABLE `solar_current_readings` (
973+
`id` int(11) NOT NULL,
974+
`device_id` varchar(40) NOT NULL,
975+
`timestamp` datetime NOT NULL,
976+
`value` float NOT NULL
977+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
978+
979+
-- --------------------------------------------------------
980+
981+
--
982+
-- Table structure for table `mppt_readings`
983+
--
984+
985+
DROP TABLE IF EXISTS `mppt_readings`;
986+
CREATE TABLE `mppt_readings` (
987+
`id` int(11) NOT NULL,
988+
`device_id` varchar(40) NOT NULL,
989+
`timestamp` datetime NOT NULL,
990+
`value` float NOT NULL
991+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
992+
993+
-- --------------------------------------------------------
994+
995+
--
996+
-- Table structure for table `soc_readings`
997+
--
998+
999+
DROP TABLE IF EXISTS `soc_readings`;
1000+
CREATE TABLE `soc_readings` (
1001+
`id` int(11) NOT NULL,
1002+
`device_id` varchar(40) NOT NULL,
1003+
`timestamp` datetime NOT NULL,
1004+
`value` int(3) NOT NULL COMMENT 'State of Charge (Percentage)'
1005+
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
1006+
1007+
--
1008+
-- Indexes for table `solar_current_readings`
1009+
--
1010+
ALTER TABLE `solar_current_readings`
1011+
ADD PRIMARY KEY (`id`),
1012+
ADD UNIQUE KEY `device_id_2` (`device_id`,`timestamp`),
1013+
ADD KEY `device_id` (`device_id`),
1014+
ADD KEY `timestamp` (`timestamp`);
1015+
1016+
--
1017+
-- Indexes for table `mppt_readings`
1018+
--
1019+
ALTER TABLE `mppt_readings`
1020+
ADD PRIMARY KEY (`id`),
1021+
ADD KEY `device_id` (`device_id`),
1022+
ADD KEY `timestamp` (`timestamp`);
1023+
1024+
--
1025+
-- Indexes for table `soc_readings`
1026+
--
1027+
ALTER TABLE `soc_readings`
1028+
ADD PRIMARY KEY (`id`),
1029+
ADD UNIQUE KEY `device_id_2` (`device_id`,`timestamp`),
1030+
ADD KEY `device_id` (`device_id`),
1031+
ADD KEY `timestamp` (`timestamp`);
1032+
1033+
1034+
--
1035+
-- AUTO_INCREMENT for table `solar_current_readings`
1036+
--
1037+
ALTER TABLE `solar_current_readings`
1038+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
1039+
--
1040+
-- AUTO_INCREMENT for table `mppt_readings`
1041+
--
1042+
ALTER TABLE `mppt_readings`
1043+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
1044+
--
1045+
-- AUTO_INCREMENT for table `soc_readings`
1046+
--
1047+
ALTER TABLE `soc_readings`
1048+
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
1049+
--
1050+
-- Constraints for table `solar_current_readings`
1051+
--
1052+
ALTER TABLE `solar_current_readings`
1053+
ADD CONSTRAINT `solar_current_readings_ibfk_1` FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`) ON UPDATE CASCADE;
1054+
1055+
--
1056+
-- Constraints for table `mppt_readings`
1057+
--
1058+
ALTER TABLE `mppt_readings`
1059+
ADD CONSTRAINT `mppt_readings_ibfk_1` FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`) ON UPDATE CASCADE;
1060+
1061+
--
1062+
-- Constraints for table `soc_readings`
1063+
--
1064+
ALTER TABLE `soc_readings`
1065+
ADD CONSTRAINT `soc_readings_ibfk_1` FOREIGN KEY (`device_id`) REFERENCES `devices` (`id`) ON UPDATE CASCADE;
1066+
1067+
1068+
9611069
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
9621070
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
9631071
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

feshiedb.py

+64
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,49 @@ def save_voltage(self, device_id, timestamp, value):
257257
except MySQLdb.Error as e:
258258
self.logger.error(e)
259259

260+
261+
def save_mppt(self, device_id, timestamp, value):
262+
self.logger.debug("Saving MPPT")
263+
if self.db is None:
264+
raise FeshieDbError()
265+
try:
266+
cursor = self.db.cursor()
267+
cursor.execute(
268+
"INSERT INTO mppt_readings (device_id, timestamp, value) VALUES (%s, %s, %s)",
269+
(device_id, timestamp, value))
270+
cursor.close()
271+
self.db.commit()
272+
except MySQLdb.Error as e:
273+
self.logger.error(e)
274+
275+
def save_soc(self, device_id, timestamp, value):
276+
self.logger.debug("Saving SOC")
277+
if self.db is None:
278+
raise FeshieDbError()
279+
try:
280+
cursor = self.db.cursor()
281+
cursor.execute(
282+
"INSERT INTO soc_readings (device_id, timestamp, value) VALUES (%s, %s, %s)",
283+
(device_id, timestamp, value))
284+
cursor.close()
285+
self.db.commit()
286+
except MySQLdb.Error as e:
287+
self.logger.error(e)
288+
289+
def save_solar_current(self, device_id, timestamp, value):
290+
self.logger.debug("Saving solar current")
291+
if self.db is None:
292+
raise FeshieDbError()
293+
try:
294+
cursor = self.db.cursor()
295+
cursor.execute(
296+
"INSERT INTO solar_current_readings (device_id, timestamp, value) VALUES (%s, %s, %s)",
297+
(device_id, timestamp, value))
298+
cursor.close()
299+
self.db.commit()
300+
except MySQLdb.Error as e:
301+
self.logger.error(e)
302+
260303
def save_adc(self, device_id, timestamp, adc_id, value):
261304
self.logger.debug("Saving adc")
262305
if self.db is None:
@@ -373,6 +416,27 @@ def get_battery_readings(self, node):
373416
raw = self.db.store_result().fetch_row(0)
374417
return raw
375418

419+
def get_mppt_readings(self, node):
420+
if self.db is None:
421+
raise FeshieDbError()
422+
self.db.query("SELECT DATE_FORMAT( timestamp, \"%%Y-%%m-%%d %%H:%%i:00\"), value FROM mppt_readings WHERE device_id = \"%s\" AND timestamp > \"%s\" AND timestamp <= NOW();" % (node, DATE_LIMIT))
423+
raw = self.db.store_result().fetch_row(0)
424+
return raw
425+
426+
def get_soc_readings(self, node):
427+
if self.db is None:
428+
raise FeshieDbError()
429+
self.db.query("SELECT DATE_FORMAT( timestamp, \"%%Y-%%m-%%d %%H:%%i:00\"), value FROM soc_readings WHERE device_id = \"%s\" AND timestamp > \"%s\" AND timestamp <= NOW();" % (node, DATE_LIMIT))
430+
raw = self.db.store_result().fetch_row(0)
431+
return raw
432+
433+
def get_solor_current_readings(self, node):
434+
if self.db is None:
435+
raise FeshieDbError()
436+
self.db.query("SELECT DATE_FORMAT( timestamp, \"%%Y-%%m-%%d %%H:%%i:00\"), value FROM solar_current_readings WHERE device_id = \"%s\" AND timestamp > \"%s\" AND timestamp <= NOW();" % (node, DATE_LIMIT))
437+
raw = self.db.store_result().fetch_row(0)
438+
return raw
439+
376440
def get_acceleromter_readings(self, node):
377441
if self.db is None:
378442
raise FeshieDbError()

feshieunpacker.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ def unpackall(self):
4343
if temperature < -100:
4444
temperature = float((((int(temperature *4) ) ^ 0x1FF) +1))/4
4545
self.database.save_temperature(node, timestamp, temperature)
46-
self.database.save_voltage(node, timestamp, sample.batt)
46+
power_mode = sample.WhichOneof("battery")
47+
if "power" == power_mode:
48+
self.logger.info("Unpacking Power board")
49+
self.database.save_soc(node, timestamp, sample.power.soc)
50+
self.database.save_mppt(node, timestamp, float(sample.power.mppt)/10)
51+
self.database.save_solar_current(node, timestamp, float(sample.power.current)/1000)
52+
self.database.save_voltage(node, timestamp, float(sample.power.batt)/1000)
53+
elif "batt" == power_mode:
54+
self.logger.info("Using onboard ADC value for battery reading")
55+
self.database.save_voltage(node, timestamp, sample.batt)
56+
else:
57+
self.logger.warn("No voltage measurement type set")
58+
4759
self.database.save_accelerometer(
4860
node, timestamp,
4961
sample.accX, sample.accY, sample.accZ)

0 commit comments

Comments
 (0)