TypeScriptはじめました
GitHubリポジトリ
動かし方
npm install npm start
動くようになるまでにしたこと
初期化
npm init typings init
最初にcrypto-jsとその型定義
npm install crypto-js --save typings install dt~cryptojs --save --global
tsconfig.jsonを記述
{ "compilerOptions": { "target": "es5", "module": "commonjs", "moduleResolution": "node", "removeComments": false }, "files": [ "typings/index.d.ts" ] }
app/main.tsを書く前に
これだけだと、requireファンクションを使うことができなかった。 Angular2のtypings.jsonをみて、少し追加。
typings install dt~node --save --global
これで、requireが使えるようになる。 だけど、とりあえず、Angular2を参考にcore-jsとjasmineも追加しておくことにした。
typings install dt~core-js --save --global typings install dt~jasmine --save --global
app/main.tsを書く
Visual Studio Codeを使ってさくさくっと記述。型があるのは補完とかがきいて便利。
Seleniumメモ
Azure IoT HubのMQTT(s)
環境
MQTTに接続するユーザ名とパスワード
ユーザ名
{iothubhostname}/{device_id}
パスワード
SharedAccessSignature sig={signature-string}&se={expiry}&sr={URL-encoded-resourceURI}
String password = String.format( "SharedAccessSignature sig=%s&se=%s&sr=%s", URLEncoder.encode(sig, StandardCharsets.UTF_8.name()), expiry, scope);
expiry
有効期限でUNIXタイムスタンプ(秒)のlong値です
URL-encoded-resourceURI(scope)
{iothubhostname}/devices/{device_id}
signature-string(sig)
デバイスのキーをBase64でデコードしたバイナリをHMAC-SHA256のキーとして使用します。
String deviceKey = properties.getDeviceKey(); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(Base64.getMimeDecoder().decode(deviceKey), "HmacSHA256"));
URL-encoded-resourceURIとexpiryを改行で分けたメッセージを署名します
String scope = String.format("%s/devices/%s", properties.getHostname(), URLEncoder.encode(properties.getDeviceId(), StandardCharsets.UTF_8.name())); String message = String.format("%s\n%s", scope, expiry); String sig = Base64.getMimeEncoder().encodeToString(mac.doFinal(message.getBytes()));
IoT Hubへのメッセージの送信
devices/{device_id}/messages/events/ または devices/{device_id}/messages/events/{property_bag} をトピック名としてメッセージを送信できます。
IoT Hubからのメッセージの受信
devices/{device_id}/messages/devicebound/# をトピックフィルターとして使用してサブスクライブできます。
GitHub
Jackson
Jacksonを使って、Java Objectをjsonに変換する場合のコードです。Java Objectはpublicのフィールドがあればgetterはなくてもよいです。
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(bean);
jsonには、beanのプロパティ名をキーとするイメージが出力されます。プロパティ名と異なるキーにする場合には、@JsonPropertyを使用することができます。
public class Bean { @JsonProperty("user_id") public Integer id; public String name; }
このBeanクラスからjsonに変換したイメージは下のようになります。
{"user_id":null,"name":null}
特定のクラスや特定のフィールドのnull値の項目のキーが生成されないようにしたい場合は、クラスまたはフィールドにアノテーションを記述します。
@JsonInclude(JsonInclude.Include.NON_NULL) public class Bean {
@JsonInclude(JsonInclude.Include.NON_NULL) public String name;
または、ObjectMapperを設定してnull値の項目のキーが生成されないようにできます。
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
String json = mapper.writeValueAsString(bean);
Spring Bootアプリケーション
ObjectMapperにInclude.NON_NULLを設定したい場合は、application.propertiesに記述します。
spring.jackson.serializationInclusion=NON_NULL
Javaアプリケーションのリモートデバッグ
Spring Bootアプリケーションをリモートデバッグしたときのメモ
$ java -agentlib:jdwp=transport=dt_socket,server=y,address=8000 -jar application.jar
Gradleでのプロパティファイルの拡張(expand)
Spring Bootアプリケーションで、検証環境と本番環境とでプロパティの値を変更するためのビルドスクリプトを書いてみました。
GradleのドキュメントとSpring Bootのリファレンスを参考にしています。
ここでは、例としてspring.profiles.activeの値を設定してみます。application.propertiesには次のように書きます。
spring.profiles.active=${ext.profiles}
次に、build.gradleにprocessResourcesを記述します。
processResources { filesMatching('**/application.properties') { expand(project.properties) } }
-Pprofiles=fooを指定してbuildを実行します。 build/resourcesにコピーされたapplication.propertiesは下のようになります。
spring.profiles.active=foo
置換してほしくないプロパティについては、$の前に(バックスラッシュ)を付加します。
spring.datasource.url=\${url}
build/resourcesにコピーされたプロパティファイルは下のようになります。
spring.datasource.url=${url}
Azure IoT Hubにデバイスを登録する
Azure IoT Hubにデバイスを登録する時、WindowsだとDevice Explorerを使用します、Windows以外の場合はiothub-explorerを使用します。
ここでは、LinuxやMac OS Xのような非Windowsを対象とします。
前提
- Node.JSとnpmがインストールされていることが必要です。
iothub-explorerのインストール
- GitHubのiothub-explorerを参考にインストールします。
$ sudo npm install -g iothub-explorer@latest
$ iothub-explorer help
- Azure PortalのIoT Hubの設定から、共有アクセスポリシーを選択し、iothubownerの接続文字列をクリップボードにコピーして、つぎのコマンドを実行します。
$ iothub-explorer login "HostName=<my-hub>.azure-devices.net;SharedAccessKeyName=<my-policy>;SharedAccessKey=<my-policy-key>"
- new-device-idの部分に登録するデバイスIDを設定します。
$ iothub-explorer create <new-device-id> --connection-string