Skip to content

Commit

Permalink
#252 pigpiod should only be required if GPIOs are used
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel Müller committed Apr 17, 2022
1 parent de264f2 commit f60bd16
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import de.avanux.smartapplianceenabler.control.*;
import de.avanux.smartapplianceenabler.control.ev.*;
import de.avanux.smartapplianceenabler.gpio.GpioControllable;
import de.avanux.smartapplianceenabler.gpio.GpioAccessProvider;
import de.avanux.smartapplianceenabler.meter.*;
import de.avanux.smartapplianceenabler.modbus.EVModbusControl;
import de.avanux.smartapplianceenabler.modbus.ModbusElectricityMeterDefaults;
Expand All @@ -36,7 +37,6 @@
import de.avanux.smartapplianceenabler.semp.webservice.DeviceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.pigpioj.PigpioInterface;

import javax.xml.bind.annotation.*;
import java.time.LocalDate;
Expand Down Expand Up @@ -181,7 +181,7 @@ public void setTimeframeIntervalHandler(TimeframeIntervalHandler timeframeInterv
this.timeframeIntervalHandler.addTimeframeIntervalChangedListener(this);
}

public void init(PigpioInterface pigpioInterface, Map<String, ModbusTcp> modbusIdWithModbusTcp, String notificationCommand) {
public void init(Map<String, ModbusTcp> modbusIdWithModbusTcp, String notificationCommand) {
logger.debug("{}: Initializing appliance", id);
mqttClient = new MqttClient(id, getClass());
if(getTimeframeIntervalHandler() == null) {
Expand Down Expand Up @@ -266,6 +266,7 @@ public void init(PigpioInterface pigpioInterface, Map<String, ModbusTcp> modbusI
control.init();

if(getGpioControllables().size() > 0) {
var pigpioInterface = GpioAccessProvider.getPigpioInterface();
if(pigpioInterface != null) {
for(GpioControllable gpioControllable : getGpioControllables()) {
logger.info("{}: Configuring GPIO for {}", id, gpioControllable.getClass().getSimpleName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,6 @@ public static ApplianceManager getInstanceWithoutTimer() {
return instance;
}

private PigpioInterface getPigpioInterface() {
if(System.getProperty("os.arch").equals("arm")) {
try {
return PigpioJ.autoDetectedImplementation();
}
catch(Error e) {
// warning will be logged later on only if GPIO access is required
logger.error("Error creating PigpioInterface.", e);
}
}
else {
logger.warn("GPIO access disabled - not running on Raspberry Pi.");
}
return null;
}

public void run() {
try {
MqttClient.start();
Expand Down Expand Up @@ -282,7 +266,7 @@ public void init() {
}
logger.debug("{}: Initializing appliance ...", appliance.getId());
try {
appliance.init(getPigpioInterface(), modbusIdWithModbusTcp,
appliance.init(modbusIdWithModbusTcp,
appliances.getConfigurationValue(ConfigurationParam.NOTIFICATION_COMMAND.getVal()));
}
catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2022 Axel Müller <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package de.avanux.smartapplianceenabler.gpio;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.pigpioj.PigpioInterface;
import uk.pigpioj.PigpioJ;

public class GpioAccessProvider {
private static Logger logger = LoggerFactory.getLogger(GpioAccessProvider.class);

private static PigpioInterface pigpioInterfaceInstance = null;

public static PigpioInterface getPigpioInterface() {
if(System.getProperty("os.arch").equals("arm")) {
try {
if(pigpioInterfaceInstance == null) {
pigpioInterfaceInstance = PigpioJ.autoDetectedImplementation();
}
return pigpioInterfaceInstance;
}
catch(Error e) {
logger.error("Error creating PigpioInterface.", e);
}
}
else {
logger.warn("GPIO access disabled - not running on Raspberry Pi.");
}
return null;
}

}

0 comments on commit f60bd16

Please sign in to comment.