FRP: Functional reactive programming

  • プログラミング・パラダイムのひとつ。
  • ネットワークのI/Oやユーザ・インタラクションの処理に向いている。
  • FRPには、behaviorとeventという二つの重要な概念がある。
    • behavior: 例) 時間の経過にともなう状態の変化。振る舞い。
    • event: 例) ある時間の状態
  • behaviorとeventを組み合わせて計算することで、逐次的に書くと記述が煩雑になる処理を簡潔に書ける(場合がある)。

参考:

BLEでペリフェラルを操作する

とりあえずAndroid DeveloperにあるBLEのサンプル(BluetoothLeGatt | Android Developers)をいじって、ペリフェラルのスキャン => 接続 => Characteristic の取得 => HRPで心拍数を取得する処理の流れはひととおりわかった(気がする)。

HRPは心拍数が変わったら通知するだけの単純なプロファイルらしく、あんまり応用がない。


というわけで Characteristic で read/writeしたいのだが、下記のサイトが参考になった。


BLEの拡張プロファイルについては下記が参考になりそう。


【BLEを使う】GATT(Generic Attribute Profile)概要 - 叶鋼は午前1時に計算をする



Android と BLESerial をつなげた例。サンプルコードあり。


浅草ギ研 ArduinoとAndroid端末をBLEでつないでみる

任意のcharacteristicのnotificationをenable/disableする疑似コード

    BluetoothGattCharacteristic characteristic; // 任意のcharacteristic
    boolean enable;
    
    int charaProp = characteristic.getProperties();
    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
        // このcharacteristicは read に対応している
        enable = false;

        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        mBluetoothGatt.readCharacteristic(characteristic);
        // このcharacteristicに対してreadのリクエストを送る
        // 非同期に BluetoothGattCallback#onCharacteristicRead(BluetoothGatt, BluetoothGattCharacteristic, int) がコールバックされる
    }

    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
        // このcharacteristicは notification に対応している
        enable = true;

        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
            UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        // characteristicの設定を行うときに指定するUUIDは "00002902-0000-1000-8000-00805f9b34fb" の固定値
        // 対向の BLE peripheral の notification 機能を有効化するため、この characteristic の descriptor に対して BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE を設定する

        mBluetoothGatt.writeDescriptor(descriptor);
        // 設定した descriptor を書き込む
        // 非同期に BluetoothGattCallback#onCharacteristicChanged(BluetoothGatt, BluetoothGattCharacteristic) がコールバックされる
    }

使っているAPI:

  • boolean BluetoothGatt#setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enable)
  • BluetoothGattDescriptor BluetoothGattCharacteristic#getDescriptor(UUID uuid)
    • characteristicの設定を行うときに指定するUUIDは "00002902-0000-1000-8000-00805f9b34fb" の固定値
  • BluetoothGattDescriptor#setValue(byte[] bytes)
    • BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE を設定
  • BluetoothGatt#writeDescriptor(BluetoothGattDescriptor descriptor)

BLE peripheral へバイト列を書き込む模擬コード

    byte[] bytes = { … }; // 書き込むバイト列
    
    BluetoothGattService myService = mBluetoothGatt.getService(UUID.fromString("<Service UUID>"));
    BluetoothGattCharacteristic characteristic = myService.getCharacteristic(UUID.fromString("<Characteristic UUID>"));
    characteristic.setValue(bytes);
    mBluetoothGatt.writeCharacteristic(characteristic);

使っているAPI:

  • BluetoothGattService BluetoothGatt#getService(UUID uuid)
    • 引数は対向 BLE peripheral の Service UUID
  • BluetoothGattCharacteristic BluetoothGattService#getCharacteristic(UUID uuid)
    • 引数は対向 BLE peripheral の Service の Characteristic UUID
  • BluetoothGattCharacteristic#setValue(byte[] bytes);
    • 引数は書き込むバイト列
  • boolean BluetoothGatt#writeCharacteristic(BluetoothGattCharacteristic characteristic)
    • 引数は対向 BLE peripheral へ送信する Characteristic

Amazon EC2メモ

BLEで何かつくる

BLEを使って何かつくれないか考えてみる。

ただBLEといっても漠然としすぎてるので、アイデア出しの下地として以下の構成を考えてみた。

  • 各機器の構成
    • スマホとRaspberry PiをBLEで通信させる
    • Raspberry Pi はネットにつながってて、JSONとかでWebサーバと通信させる
    • Raspberry Piはコンセントから電源を供給、Wi-Fiで常時ネットにつながっている
    • Raspberry PiのGPIOにはデバイス(センサーなど)をつなげることができる

f:id:yamataka548:20150212180258p:plain



Raspberry Pi ではなく Arduino とBLE通信させる構成だとこんな感じ。

  • 各機器の構成
    • スマホArduinoをBLEで通信させる
    • Arduino は電池で電源を供給
    • ArduinoのGPIOにはデバイス(センサーなど)をつなげることができる

f:id:yamataka548:20150212193514p:plain



ちなみに、よくあるBeaconの構成はこんな感じで、スマホのアプリになにかアクションを起こさせるトリガとしてBLE(アドバタイズメント・チャネル)を使っている。

Beacon はアドバタイズメント・チャネルでID情報(UUID:会社ID, major:店舗)を一定時間ごとに送信する。スマホはID情報を受信すると、アプリを起動してアクションを起こす。
このアクションは、例えばWebサーバからメッセージを取得すること (いわゆるプッシュ通知) や、アプリ内ダイアログやビルトイン機能を表示することなど。

Beacon 自体は単にID情報をブロードキャストするだけなので、ボタン電池で数年稼動させることができる。

f:id:yamataka548:20150212182658p:plain

BLE: Bluetooth Low Energy

技術的な特徴はこんな感じ。

  • BLE機器を低消費電力でつくれるため、ボタン電池で数年稼動
  • 免許不要の2.4GHz帯を利用して、最大1Mbpsの通信が可能
  • 通信距離は2.5mから50m

通信は2種類ある。

  • 1対1通信
    • Central (スマホなど) と Peripheral (BLE機器)の通信
    • アドバタイズメント・チャネルによる通信
      • Peripheral の発見と接続に使う
    • データ・チャネルによる通信
      • データ通信に使う
    • 暗号化通信可
  • N対1通信
    • Broadcaster (BLE機器) と Observer (スマホなど) の通信
    • アドバタイズメント・チャネルによる通信
      • Broadcaster の発見と接続に使う
    • 暗号化通信はできない

スマホ/タブレットの対応状況は、Android/iOS端末で下記の通り。

  • Android端末
    • OS: Android4.3(APIレベル18)以上
    • デバイス: 端末にBluetooth4.0が搭載されていること。端末メーカーの仕様を確認。
  • iOS端末
    • OS: iOS7以上
    • デバイス: iPhone 4S以降、iPad (第3世代)以降

用途は下記がある。

  • Beacon
    • スマホの位置情報を特定し、ロケーションに合わせて必要な情報を配信する
    • スマホを測位する距離モードは10メートル程度/数メートル程度/数センチ程度と3段階を判別できる

Cloudera Manager / CDH のメモ

Cloudera Manager 5

Cloudera Manager 5 を消して 4 を入れ直すときの罠

Cloudera Manager 5 をアンインストールした後、Cloudera Manager 4を入れる場合は /var/cache/yum/x86_64/6/cloudera-manager 配下を消す必要があるみたいだ。
残っていると、Cloudera Manager 4のインストーラが5のパッケージを参照してエラーになるため。

sudo rm -rf /var/cache/yum/x86_64/6/cloudera-manager 


また、各Agentホスト(Cloudera Manager Serverホストを除く)の /etc/yum.repos.d/cloudera-manager.repo が Cloudera Manager 5向けのものに変わっているので消す。

sudo rm /etc/yum.repos.d/cloudera-manager.repo
sudo rm /etc/yum.repos.d/cloudera-cdh.repo

なんか、ほかにも消さなきゃならんかもしれん。

http://[cloudera manager host]:7180/ へアクセスした後、インストールウィザードの「クラスタインストール」の画面で真っ白になって操作不能になって困っていたが、原因はブラウザのキャッシュだった。
クリアしたら治った。。

罠大杉だろう。