OpenECHOのサンプルを探していたが、大抵がProcessingを含む形式だった。一番シンプルな、Processingを含まないサンプルがarmadillo社様サイトで公開されていたので、それをベースに少し修正した。(オリジナルは2つのjavaだったが、それをうまくビルドする方法が分からず。antとか使うのだろうか)
実行環境は、Ubuntuです。
file: Ellight.java ( EL Light)
sumi@uburp4:~/lang/java$ cat Ellight.java import com.sonycsl.echo.Echo; import com.sonycsl.echo.eoj.device.DeviceObject; import com.sonycsl.echo.processing.defaults.DefaultNodeProfile; import com.sonycsl.echo.eoj.device.housingfacilities.GeneralLighting; import java.io.IOException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.logging.Level; import java.util.logging.Logger; class MyLighting extends GeneralLighting { byte[] mStatus = {0x31}; byte[] mMode = {0x41}; byte[] mLocation = {0x39}; byte[] mVersion = {0x01, 0x01, 0x61, 0x00}; byte[] mFaultStatus = {0x42}; byte[] mManufacturerCode = {0x12, 0x34, 0xFE}; @Override protected boolean setOperationStatus(byte[] edt) { Runtime rt = Runtime.getRuntime(); Process pr = null; try { /* LEDをON/OFF */ if (edt[0] == 0x30) pr = rt.exec(new String[] { "/bin/sh", "-c", "echo 255 > /sys/class/leds/green/brightness" }); else if (edt[0] == 0x31) pr = rt.exec(new String[] { "/bin/sh", "-c", "echo 0 > /sys/class/leds/green/brightness" }); if (pr != null) pr.waitFor(); inform().reqInformOperationStatus().send(); } catch (InterruptedException | IOException ex) { Logger.getLogger(MyLighting.class.getName()).log(Level.SEVERE,null, ex); } return true; } @Override protected byte[] getOperationStatus() { try { Runtime rt = Runtime.getRuntime(); Process pr = rt.exec("cat /sys/class/leds/green/brightness"); InputStream is = pr.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = br.readLine(); if (line != null && line.equals("0")) mStatus[0] = 0x31; else mStatus[0] = 0x30; } catch (IOException ex) { Logger.getLogger(MyLighting.class.getName()).log(Level.SEVERE, null, ex); } return mStatus; } @Override protected boolean setLightingModeSetting(byte[] edt) { mMode[0] = edt[0]; try { inform().reqInformLightingModeSetting().send(); } catch (IOException ex) { Logger.getLogger(MyLighting.class.getName()).log(Level.SEVERE, null, ex); } return true; } @Override protected byte[] getLightingModeSetting() { return mMode; } @Override protected boolean setInstallationLocation(byte[] edt) { mLocation[0] = edt[0]; try { inform().reqInformInstallationLocation().send(); } catch (IOException ex) { Logger.getLogger(MyLighting.class.getName()).log(Level.SEVERE, null, ex); } return true; } @Override protected byte[] getInstallationLocation() { return mLocation; } @Override protected byte[] getStandardVersionInformation() { return mVersion; } @Override protected byte[] getFaultStatus() { return mFaultStatus; } @Override protected byte[] getManufacturerCode() { return mManufacturerCode; } } public class Ellight { public static void main(String[] args) throws InterruptedException, IOException { Echo.start(new DefaultNodeProfile(), new DeviceObject[]{new MyLighting()}); } }
同一ディレクトリに、OpenECHO.jarがあるとして、以下でビルドと実行
$ javac -cp OpenECHO.jar Ellight.java $ java -cp /<path>/OpenECHO.jar: Ellight
上記で照明デバイスとしてECHONET Liteプロトコルで待ち受けてくれる
別のPCから動作テストを実施. ECHONET Liteプロトコルで、上記デバイスに
Location(機器設置場所)を問い合わせ(自作のEL通信ツール)
Javaソースで設定したLocation: 0x39が返却されるのを確認
$ ./elp.py get 029001@192.168.10.120 Location GET(0x62) EPC... Location 108100010EF00102900162018100 |10|81|0001|0EF001|029001|62(Get)|01|81(Location)_00_| waiting... ---------------------------------------- ------------------------------ from ('192.168.10.120', 3610) ------------------------------ msg: 108100010290010ef0017201810139 |10|81|0001|029001|0ef001|72(Get_Res)|01|81(Location)_01_39|
■ソースを参照したサイト
https://armadillo.atmark-techno.com/howto/a400-openecho-howto
「Armadillo-400シリーズでECHONET Lite対応一般照明機器」
上記Javaソースは、上記armadillo社様サイトより引用、一部改修してテストしました。
シンプルなサンプルソースなので分かりやすい!!]
■メモ
kaden emulatorをソースから動かす方法
(1)Processingを落とす (2)echonetフォルダ配下にechonet.pdeを置く (3)走らせると、ライブラリがないと怒られる 3-1controlP5がない・・・ライブラリのマネージ機能でContorlP5を入れる documents\Processing\libraries\contorolP5\library\controlP5.jar が入る 3-2sonyのライブラリ(OpenECHO)はGitから落とす。 Githubの SonyCSL/OpenECHOの processing/libraryにOpenECHOのjarがあるのでそれを落として入れる documents\Processing\libraries\OpenECHOlibrary\OpenECHO.jar