forked from eurotech/RedHat-CodeStarter-2017
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: nicolatimeus <[email protected]>
- Loading branch information
1 parent
83be5f8
commit f770d7a
Showing
2 changed files
with
81 additions
and
1 deletion.
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
78 changes: 78 additions & 0 deletions
78
....demo.opcua.server/src/org/eclipse/kura/demo/opcua/server/i2c/GroveTemperatureSensor.java
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,78 @@ | ||
package org.eclipse.kura.demo.opcua.server.i2c; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
import org.eclipse.kura.core.util.ProcessUtil; | ||
import org.eclipse.kura.core.util.SafeProcess; | ||
import org.eclipse.kura.demo.opcua.server.Sensor; | ||
import org.eclipse.milo.opcua.sdk.core.AccessLevel; | ||
import org.eclipse.milo.opcua.sdk.server.api.nodes.VariableNode; | ||
import org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext; | ||
import org.eclipse.milo.opcua.stack.core.Identifiers; | ||
import org.eclipse.milo.opcua.stack.core.UaException; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.DataValue; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.Variant; | ||
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.base.Charsets; | ||
import com.google.common.io.Files; | ||
|
||
public class GroveTemperatureSensor extends Sensor { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(GroveTemperatureSensor.class); | ||
|
||
private static final String PYTHON_CODE = "import smbus\n" + "import time\n" + "bus = smbus.SMBus(1)\n" | ||
+ "bus.write_i2c_block_data(0x44, 0x2C, [0x06])\n" + "time.sleep(0.5)\n" | ||
+ "data = bus.read_i2c_block_data(0x44, 0x00, 6)\n" + "temp = data[0] * 256 + data[1]\n" | ||
+ "cTemp = -45 + (175 * temp / 65535.0)\n" + "fTemp = -49 + (315 * temp / 65535.0)\n" | ||
+ "humidity = 100 * (data[3] * 256 + data[4]) / 65535.0\n" + "print \"%.2f\" %cTemp\n"; | ||
|
||
public GroveTemperatureSensor(String name) { | ||
super(name, Identifiers.Double); | ||
} | ||
|
||
@Override | ||
public void init() throws IOException { | ||
Files.write(PYTHON_CODE, new File("/tmp/temperature.py"), Charsets.UTF_8); | ||
} | ||
|
||
@Override | ||
public UByte getAccessLevel(AttributeContext context, VariableNode node) throws UaException { | ||
return UByte.valueOf(AccessLevel.getMask(AccessLevel.READ_ONLY)); | ||
} | ||
|
||
@Override | ||
public UByte getUserAccessLevel(AttributeContext context, VariableNode node) throws UaException { | ||
return UByte.valueOf(AccessLevel.getMask(AccessLevel.READ_ONLY)); | ||
} | ||
|
||
@Override | ||
public DataValue getValue(AttributeContext context, VariableNode node) throws UaException { | ||
SafeProcess proc = null; | ||
try { | ||
proc = ProcessUtil.exec("python /tmp/temperature.py"); | ||
proc.waitFor(); | ||
|
||
try (BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()))) { | ||
String line = br.readLine(); | ||
return new DataValue(new Variant(Double.parseDouble(line))); | ||
} | ||
} catch (Exception e) { | ||
logger.warn("got exception", e); | ||
throw new UaException(e); | ||
} finally { | ||
if (proc != null) { | ||
ProcessUtil.destroy(proc); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
} | ||
} |