Kadecot WebSocket APIを使用して家電(エミュレータ)の状態を表示&操作する方法①-エミュレータ作成編
KadecotのWebSocket APIを使ってエミュレータや実機の家電の状態を表示したり、操作したりできるWebリモコンを作成する手順です。 今回はエミュレータを使用する場合をご紹介します。 長くなりますので2回に分けます。まずエミュレータの作成方法です。 ちなみに開発環境はeclipseを使用しています。 用意するものは以下の通りです。
- Xamppが使える開発用PC(リモコンのWebサーバとして使用するため)
- Android端末(Kadecotサーバ用)
- Android端末(リモコンとして使用)
- OpenECHO
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.IOException; import com.sonycsl.echo.Echo; import com.sonycsl.echo.eoj.device.DeviceObject; import com.sonycsl.echo.processing.defaults.DefaultNodeProfile; public class Main { // 各エミュレータの宣言 static private ThermometerEmulator thermometer; // 温度計 static private ElectricFanEmulator electricfan; // 扇風機 public static void main(String[] args) { Echo.addEventListener( new Echo.Logger(System.out)); // ログ出力 try { thermometer = new ThermometerEmulator(); electricfan = new ElectricFanEmulator(); Echo.start(new DefaultNodeProfile(), new DeviceObject[]{thermometer,electricfan}); } catch (IOException e) { e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.io.IOException; import com.sonycsl.echo.eoj.device.sensor.TemperatureSensor; public class ThermometerEmulator extends TemperatureSensor { byte[] mActualTempl = {0,(byte)250}; // 計測温度を格納 // ←この場合25℃ public ThermometerEmulator(){ } @Override protected byte[] getMeasuredTemperatureValue() { System.out.println("getMeasuredTemperatureValue() is called."); System.out.println("mActualTempl = "+ mActualTempl); return mActualTempl; } @Override protected byte[] getOperationStatus() { // TODO 自動生成されたメソッド・スタブ return null; } @Override protected byte[] getFaultStatus() { // TODO 自動生成されたメソッド・スタブ return null; } @Override protected byte[] getInstallationLocation() { // TODO 自動生成されたメソッド・スタブ return null; } @Override protected byte[] getManufacturerCode() { // TODO 自動生成されたメソッド・スタブ return null; } @Override protected boolean setInstallationLocation(byte[] arg0) { // TODO 自動生成されたメソッド・スタブ return false; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import java.io.IOException; import com.sonycsl.echo.eoj.device.airconditioner.VentilationFan; public class ElectricFanEmulator extends VentilationFan { byte[] mStatus = {0x31}; // 電源状態を格納する変数です。デフォルトは OFF と仮定します。 byte[] mLocation = {0x00}; // 機器の置き場所を格納する変数です。 byte[] mFaultStatus = {0x42}; // 機器に問題が発生した時に、そのコードを格納します。 byte[] mManufacturerCode = {0, 0, 0}; // ベンダー固有のメーカーコードです。 @Override protected boolean setOperationStatus(byte[] edt) { mStatus[0] = edt[0] ; //電源状態が変化したことを他のノードに通知します try{ // ------------------------------------------------------------------ // この部分に、実際に扇風機の稼動を制御するためのコードを書く。 // (Raspberry PiのGPIOを操作するためのコード) // とりあえずは、ログだけ出すようにする if(mStatus[0] == 0x30){ System.out.println("Fan ON!"); inform().reqInformOperationStatus().send(); }else if(mStatus[0] == 0x31){ System.out.println("Fan OFF!"); inform().reqInformOperationStatus().send(); }else{ System.out.println("Parameter Error: Can NOT operate status"); } // ------------------------------------------------------------------ }catch(IOException e){ e.printStackTrace(); } return true; } @Override protected byte[] getOperationStatus() { return mStatus; } @Override protected byte[] getFaultStatus() { // TODO 自動生成されたメソッド・スタブ return mFaultStatus; } @Override protected byte[] getInstallationLocation() { // TODO 自動生成されたメソッド・スタブ return mLocation; } @Override protected byte[] getManufacturerCode() { // TODO 自動生成されたメソッド・スタブ return mManufacturerCode; } @Override protected boolean setInstallationLocation(byte[] edt) { mLocation[0] = edt[0]; try{ inform().reqInformInstallationLocation().send(); }catch (IOException e) { e.printStackTrace(); } return true; } } |