Skip to content

Commit

Permalink
import mcp-spi-ads module
Browse files Browse the repository at this point in the history
  • Loading branch information
luis-godinez committed Aug 18, 2024
1 parent b6dae63 commit 254c8cd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 275 deletions.
2 changes: 1 addition & 1 deletion src/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
},
{
"name": "STEAM_NTC",
"channel": 1,
"channel": 2,
"system": "steam",
"control": true,
"resistanceAt25C": 10000,
Expand Down
35 changes: 24 additions & 11 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
import { OpenSaunaPlatform } from './platform.js';
import rpio from 'rpio';
import { openMcp3008, McpInterface, McpReading, EightChannels } from 'mcp-spi-adc';
const mcpSpiAdc = require('mcp-spi-adc');
import i2c from 'i2c-bus';
import { OpenSaunaConfig, AuxSensorConfig, SystemType } from './settings.js';

Expand All @@ -13,7 +13,7 @@ export class OpenSaunaAccessory {
private steamTemperatureSensor?: Service;
private lightPowerSwitch?: Service;
private fanPowerSwitch?: Service;
private adc!: McpInterface;
private adc!: any; // Updated to any since we're using require
private i2cBus!: i2c.PromisifiedBus;
private saunaTimer: NodeJS.Timeout | null = null;
private steamTimer: NodeJS.Timeout | null = null;
Expand Down Expand Up @@ -79,19 +79,32 @@ export class OpenSaunaAccessory {

private validateSensorConfiguration() {
const systemCount: { [key: string]: number } = {};
const channelSet = new Set<number>(); // To track used channels

this.config.auxSensors.forEach((sensor) => {
// Check for duplicate system configuration
if (sensor.system) {
if (!systemCount[sensor.system]) {
systemCount[sensor.system] = 0;
}
systemCount[sensor.system]++;
}

// Check for duplicate channels
if (channelSet.has(sensor.channel)) {
throw new Error(
`Duplicate channel detected: Channel ${sensor.channel} is already used by another sensor. Each sensor must have a unique channel.`,

Check warning on line 96 in src/platformAccessory.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

This line has a length of 141. Maximum allowed is 140

Check warning on line 96 in src/platformAccessory.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

This line has a length of 141. Maximum allowed is 140
);
}
channelSet.add(sensor.channel);
});

// Ensure only one NTC sensor per system
for (const system in systemCount) {
if (systemCount[system] > 1) {
throw new Error(`Only one NTC sensor is allowed for the ${system} system.`);
throw new Error(
`Only one NTC sensor is allowed for the ${system} system.`,
);
}
}
}
Expand Down Expand Up @@ -136,7 +149,7 @@ export class OpenSaunaAccessory {
reject(new Error('ADC initialization timeout'));
}, 5000); // 5-second timeout

openMcp3008(0, { speedHz: 1350000 }, (error) => {
this.adc = mcpSpiAdc.openMcp3008(0, { speedHz: 1350000 }, (error: any) => {
clearTimeout(timeout);
if (error) {
this.platform.log.error('Failed to open ADC:', error);
Expand Down Expand Up @@ -555,23 +568,23 @@ export class OpenSaunaAccessory {
// Monitor temperatures using ADC channels
private monitorTemperatures() {
this.config.auxSensors.forEach((sensor) => {
const adcChannel = sensor.channel as EightChannels;
const adcChannel = sensor.channel;

// Open ADC channel for each sensor
this.adc = openMcp3008(adcChannel, { speedHz: 1350000 }, (err: string) => {
this.adc = mcpSpiAdc.openMcp3008(adcChannel, { speedHz: 1350000 }, (err: any) => {
if (err) {
this.platform.log.error(
`Failed to open ADC channel ${adcChannel} for sensor "${sensor.system}": ${err}`,
`Failed to open ADC channel ${adcChannel} for sensor "${sensor.name}": ${err}`,
);
return;
}

// Set up a regular interval to read from the ADC channel
const interval = setInterval(() => {
this.adc.read((err: string | null, reading: McpReading) => {
this.adc.read((err: any, reading: any) => {
if (err) {
this.platform.log.error(
`Failed to read temperature for sensor "${sensor.system}": ${err}`,
`Failed to read temperature for sensor "${sensor.name}": ${err}`,
);
return;
}
Expand All @@ -590,15 +603,15 @@ export class OpenSaunaAccessory {
const isInvalidReading = temperatureCelsius < -20 || temperatureCelsius > 150;
if (isInvalidReading) {
this.platform.log.warn(
`${sensor.system} Invalid Temperature: ${displayTemperature.toFixed(2)} °${
`${sensor.name} Invalid Temperature: ${displayTemperature.toFixed(2)} °${
this.config.temperatureUnitFahrenheit ? 'F' : 'C'
}`,
);
// Reflect the invalid state in the HomeKit UI or log
this.reflectInvalidReadingState(sensor);
return;
} else {
this.platform.log.info(`[Temp] ${sensor.system}:${temperatureCelsius}`);
this.platform.log.info(`[Temp] ${sensor.name}:${temperatureCelsius}`);
}

// Update the HomeKit characteristic with the current temperature
Expand Down
263 changes: 0 additions & 263 deletions src/typings.d.ts

This file was deleted.

0 comments on commit 254c8cd

Please sign in to comment.