都内で働くSEの技術的なひとりごと / Technical soliloquy of System Engineer working in Tokyo

都内でサラリーマンやってます。SQL Server を中心とした (2023年からは Azure も。) マイクロソフト系(たまに、OSS系などマイクロソフト以外の技術も...)の技術的なことについて書いています。日々の仕事の中で、気になったことを技術要素関係なく気まぐれに選んでいるので記事内容は開発言語、インフラ等ばらばらです。なお、当ブログで発信、発言は私個人のものであり、所属する組織、企業、団体等とは何のかかわりもございません。ブログの内容もきちんと検証して使用してください。英語の勉強のため、英語の

I wrote a code to control Nature Remo in C# because I saw the article about Sony DSC-QX10 that I wrote in the past.

I bought Nature Remo mini on impulse.

Nature Remo mini 家電コントロ-ラ- REMO2W1

Nature Remo mini 家電コントロ-ラ- REMO2W1

 

 

Simple.

f:id:koogucc11:20190505202344j:plain

Nature Remo mini

 

Nature Remo is in simple box.

f:id:koogucc11:20190505202400j:plain

Simple Box

Nature Remo mini is a bit bigger than Airpods.

f:id:koogucc11:20190505202444j:plain

Nature Remo mini and AirPods

 

The cause of impulse buying is the following article. I wanted to control Nature Remo mini like Sony DSC-QX10.

ryuchan.hatenablog.com

 

I would like to operate Nature Remo mini with C # referring to the blog above and the Nature Remo API. Based on the following sources used in the operation of Sony DSC-QX10, I will change it to Nature Remo mini version.

static async void DoPost()
{
    string jsonparams = "{\"method\": \"actTakePicture\"," +
                        "\"params\": []," +
                        "\"id\": 1," +
                        "\"version\": \"1.0\"}";
string url = "http://10.0.0.1:10000/sony/camera"; var httpclient = new HttpClient(); var jsoncontent = new StringContent(jsonparams, Encoding.UTF8, "application/json"); httpclient.MaxResponseContentBufferSize = int.MaxValue; var response = await httpclient.PostAsync(url, jsoncontent); String text = await response.Content.ReadAsStringAsync(); }

 

It is a DVD deck to control. The remote controller is as shown below.

f:id:koogucc11:20190506082720j:plain

Remote Controller

 

In order to control the device, you need to get the device id. It can be obtained by sending a request to Nature Remo mini using the code below. Please get the access token here.

static async void DoGet()
{
    string token = "Bearer " + "{access token}";
    string url = "https://api.nature.global/1/appliances";
    var httpclient = new HttpClient();
    
    httpclient.DefaultRequestHeaders.Add("Authorization", token);

    var response = await httpclient.GetAsync(url);
}

 

Since you can get the device id, use that the device id to control the power of a DVD deck.

f:id:koogucc11:20190506084126p:plain

device id

The acquired id ( e1ac8815-1480-4914-bd77-a42c5037659b) is specified as follows, and the source is rewritten for POST request. A status code is returned in 200, it will succeed. I think It is able to write the code of the GET code and the POST code beautifully by using HttpRequestMessage and SendAsync.

static async void DoPost()
{
    string token = "Bearer " + "{access token}";
    string url = "https://api.nature.global/1/signals/e1ac8815-1480-4914-bd77-a42c5037659b/send";
    var httpclient = new HttpClient();
    var httpreqmsg = new HttpRequestMessage(HttpMethod.Post, url);

    httpreqmsg.Headers.Add("Authorization", token);

    var response = await httpclient.SendAsync(httpreqmsg);
}

 

Because I want to make the sample source code as simple as possible, I do not specify HEADER information and other necessary information neatly. For full-fledged use, please refer carefully to the Nature Remo API specification.

A programming of device is fun. I noticed after Nature Remo mini arrived at my house, but the second generation of Nature Remo has humidity, human feeling, and light sensor. I should have bought the second generation of Nature Remo!

Nature Remo 第2世代モデル 家電コントロ-ラ- REMO1W2

Nature Remo 第2世代モデル 家電コントロ-ラ- REMO1W2

 

It looks fun to control Philips Hue.


DSC - QX10 の制御するのを C# で書いたことを思い出したので Nature Remo でも同じことをしてみる

 Nature Remo mini を衝動買いました。

Nature Remo mini 家電コントロ-ラ- REMO2W1

Nature Remo mini 家電コントロ-ラ- REMO2W1

 シンプルです。

f:id:koogucc11:20190505202344j:plain
カバー

 シンプルな箱に入っています。

f:id:koogucc11:20190505202400j:plain

 大きさは以下のような感じです。右は AirPods です。

f:id:koogucc11:20190505202444j:plain
AirPods との大きさ比較

 今回の衝動買いの発端は、下記の記事です。DSC - QX10 みたいに Nature Remo を操作してみたいと思ってしまいました。
ryuchan.hatenablog.com

 上記のブログと Nature Remo API を参照しながら C# で Nature Remo を操作したいと思います。DSC - QX10 の操作で使用した下記のソースを参考に、Nature Remo 版へ改変していきます。

static async void DoPost()
{
    // 撮影する場合の JSON フォーマット
    string jsonparams = "{\"method\": \"actTakePicture\"," +
                        "\"params\": []," +
                        "\"id\": 1," +
                        "\"version\": \"1.0\"}";
    // サービスのURL
    string url = "http://10.0.0.1:10000/sony/camera";

    var httpclient = new HttpClient();
    // content の作成
    var jsoncontent = new StringContent(jsonparams, Encoding.UTF8, "application/json");
    httpclient.MaxResponseContentBufferSize = int.MaxValue;
    // POSTする
    var response = await httpclient.PostAsync(url, jsoncontent);
    // 結果の読み込み
    String text = await response.Content.ReadAsStringAsync();
}

 今回制御するのは、DVDデッキです。リモコンは下図の通りです。

f:id:koogucc11:20190506082720j:plain
使用したリモコン

 デバイスを制御するため、device id を取得するために、下記のコードを使用し、Nature Remo にリクエストを送る必要があります。アクセストークンは、ここで取得してください。

static async void DoGet()
{
    string token = "Bearer " + "{アクセストークン}";
    string url = "https://api.nature.global/1/appliances";
    var httpclient = new HttpClient();
    
    httpclient.DefaultRequestHeaders.Add("Authorization", token);

    var response = await httpclient.GetAsync(url);
}

 id が取得できますので、その id を DVD デッキの電源制御に使用します。

f:id:koogucc11:20190506084126p:plain
id の取得結果

 取得した id ( 今回は、e1ac8815-1480-4914-bd77-a42c5037659b )を下記のように指定し、POST リクエスト用にソースを書き換えます。実行し、status code が 200 で返却されたら成功です。HttpRequestMessage, SendAsync を使った方が、GET と POST のコードが綺麗に書けそうですね。

static async void DoPost()
{
    string token = "Bearer " + "{アクセストークン}";
    string url = "https://api.nature.global/1/signals/e1ac8815-1480-4914-bd77-a42c5037659b/send";
    var httpclient = new HttpClient();
    var httpreqmsg = new HttpRequestMessage(HttpMethod.Post, url);

    httpreqmsg.Headers.Add("Authorization", token);

    var response = await httpclient.SendAsync(httpreqmsg);
}

サンプルソースコードをなるべくシンプルにしたいため、ヘッダ情報、その他必要な情報とかきちんと指定していません。本格的に使用する場合は API 仕様をじっくり参照の上、コーディングしてください。私はほとんど見ていないので笑

 制御系のプログラミングは楽しいですね。Nature Remo が家に到着した後に気づいたのですが、第二世代の Nature Remo は、湿度、人感、光度センサーなどが付いているんですね。第二世代を買えばよかった泣。

Nature Remo 第2世代モデル 家電コントロ-ラ- REMO1W2

Nature Remo 第2世代モデル 家電コントロ-ラ- REMO1W2

 Philips Hue の制御とかも楽しそうですね。

Great feature additions about automatic synchronous statistics update

I was seeing "SQL Database Engine Blog", I found the following article.
blogs.msdn.microsoft.com

Following scenarios are assumed.

Consider the following query execution scenario:

You execute a SELECT query that triggers an automatic synchronous statistics update.
The synchronous statistics update begins execution and your query waits (is essentially blocked) until the fresh statistics are generated.
The query compilation and execution does not resume until the synchronous statistics update operation completes.

There was a time when the query suddenly became slow one day. but you do not know the cause unknown well. You were told by a customer, so you try to confirm without reproducibility. do you have experience like? Lol

Add wait type "WAIT_ON_SYNC_STATISTICS_REFRESH" for automatic statistics updates. Let's try it. Create non-clustered index on the AdventureWorks2017.Sales.SalesOrderHeader TaxAmt column.

f:id:koogucc11:20190505085846p:plain
non-clustered index of TaxAmt

Let's execute the following query in SQL Server Management Studio.

UPDATE
    TOP(10) PERCENT Sales.SalesOrderHeader
SET
    TaxAmt = (TaxAmt * 1)

f:id:koogucc11:20190505090101p:plain
Execution Result of UPDATE statement

Let's execute the following query in SQL Server Management Studio.

SELECT
    *
FROM
    Sales.SalesOrderHeader2
WHERE
    TaxAmt > 3000.00

f:id:koogucc11:20190505090525p:plain
Execution Result of SELECT statement

Let's execute the following query in SQL Server Management Studio.

SELECT
    *
FROM
    sys.dm_os_wait_stats
WHERE
    wait_type = 'WAIT_ON_SYNC_STATISTICS_REFRESH'

f:id:koogucc11:20190505090238p:plain
Execution Result of SELECT statement

"WAIT_ON_SYNC_STATISTICS_REFRESH" value has been updated. You can also determine that "STATMAN" works by checking with SQL Server Profiler.

f:id:koogucc11:20190505090958p:plain
Execution Result of SQL Server Profiler

Very Helpful. Thanx!

We believe these two small changes will help address a significant diagnostic gap. For feedback or questions, please reach out to us at IntelligentQP@microsoft.com.

いい機能追加を見つけたので、説明してみる

 ひさしぶりに SQL Database Engine Blog を眺めていて、下記の記事を見つけました。
blogs.msdn.microsoft.com

 こんなシナリオを想定しています。

Consider the following query execution scenario:

You execute a SELECT query that triggers an automatic synchronous statistics update.
The synchronous statistics update begins execution and your query waits (is essentially blocked) until the fresh statistics are generated.
The query compilation and execution does not resume until the synchronous statistics update operation completes.

 ある日突然クエリが遅くなった時があったけど、原因不明でよくわからないとかありますよね。お客さんから言われたので、確認してみたら再現性なし、みたいな経験ある人いますよね? 笑

 統計自動更新時の待ちの種類 WAIT_ON_SYNC_STATISTICS_REFRESH の追加です。それでは早速試してみましょう。AdventureWorks2017 の Sales.SalesOrderHeader の TaxAmt 列に非クラスタ化インデックスを作成します。

f:id:koogucc11:20190505085846p:plain
TaxAmt列を非クラスタ化インデックス

 下記のクエリを SQL Server Management Studio で実行します。

UPDATE
    TOP(10) PERCENT Sales.SalesOrderHeader
SET
    TaxAmt = (TaxAmt * 1)

f:id:koogucc11:20190505090101p:plain
Update の実行結果

 下記のクエリを SQL Server Management Studio で実行します。

SELECT
    *
FROM
    Sales.SalesOrderHeader2
WHERE
    TaxAmt > 3000.00

f:id:koogucc11:20190505090525p:plain
SELECT の実行結果

 下記のクエリを SQL Server Management Studio で実行します。

SELECT
    *
FROM
    sys.dm_os_wait_stats
WHERE
    wait_type = 'WAIT_ON_SYNC_STATISTICS_REFRESH'

f:id:koogucc11:20190505090238p:plain
WAIT_ON_SYNC_STATISTICS_REFRESH の実行結果

 WAIT_ON_SYNC_STATISTICS_REFRESH の値が更新されています。また、プロファイラーで確認することで STATMAN が動作したことも判断できますね。

f:id:koogucc11:20190505090958p:plain
プロファイラーの結果

 役立ちますね。助かります。

We believe these two small changes will help address a significant diagnostic gap. For feedback or questions, please reach out to us at IntelligentQP@microsoft.com.

 Apple Pencil (第二世代) を買うか悩み中。そうしたら、ケースも買わないと。

Apple Pencil(第2世代)

Apple Pencil(第2世代)

英文書くのに便利だなぁと思って紹介してみる

 今年から英語がかなり必要、というか生きていけない環境になりそうなので急遽勉強をはじめました。英単語の総復習には、milkan を使っています。

英単語アプリ mikan

英単語アプリ mikan

  • mikan Co.,Ltd.
  • 教育
  • 無料

 TOEIC も受験していきたいと思ったので、トレーニンTOEIC ® test も使用しています。

トレーニング TOEIC ® test

トレーニング TOEIC ® test

  • Flipout LLC
  • 教育
  • 無料

 上記以外には、自分のブログに投稿するのも日本語と英語の記事を投稿します。投稿時に英文法をチェックするのに使用しているのが、Ginger Page です。
www.getginger.jp

 使い方は非常に簡単です。下記の記事から、一文間違った状態でチェックにかけけてみます。
ryuchan.hatenablog.com

 赤字の間違いを仕込んだ文章です。

The end of HEISEI ( 平成 ), REIWA ( 令和 ) has begin. The last year of HEISEI was turning point in my life. However, I still want to evolve, so I want to move forward without fear of change.
I will explain about TRANSLATE (Transact-SQL) - SQL Server | Microsoft Docs. Complex processing in REPLACE function can be describe very simply by TRANSLATE function. Let's compare the description contents of REPLACE function and TRANSLATE function.

 まず、チェックする文章を貼り付け、Ginger it! をクリックします。

f:id:koogucc11:20190503131005p:plain
チェックする文章を貼り付け

 begin → begun に変更されています。

f:id:koogucc11:20190503131159p:plain
begin → begun

 describe → described に変更されています。

f:id:koogucc11:20190503131319p:plain
describe → described

 この機能を使い、投稿前に英文のチェックをしています。また、Sentence Rephaser という機能もあり、イケてる言い回し候補を提案する機能もあります。コツコツ勉強していこう。

Some useful SQL functions, syntax, and more - Part 18 ( REIWA GANNEN Memorial Post )

The end of HEISEI ( 平成 ), REIWA ( 令和 ) has begun. The last year of HEISEI was turning point in my life. However, I still want to evolve, so I want to move forward without fear of change.
I will explain about TRANSLATE (Transact-SQL) - SQL Server | Microsoft Docs. Complex processing in REPLACE function can be described very simply by TRANSLATE function. Let's compare the description contents of REPLACE function and TRANSLATE function.

Let's execute the following query in SQL Server Management Studio.

DECLARE @str NVARCHAR(255) = 'aaa,aaa,aaa|bbb,bbb,bbb'

SELECT
    REPLACE(REPLACE(REPLACE(@str,'|','*'),',','|'),'*',',') [REPLACE],
    TRANSLATE(@str, ',|', '|,') [TRANSLATE]

f:id:koogucc11:20190429083951p:plain
Execution result of REPLACE and TRANSLATE

The features of TRANSLATE function are as follows.

The behavior of the TRANSLATE function is similar to using multiple REPLACE functions. TRANSLATE does not, however, replace a character more than once. This is dissimilar to multiple REPLACE functions, as each use would replace all relevant characters.

In stored procedures, I often find that the RELACE function is nested in multiple layers. Although not available in all cases, use TRANSLATE function to refactor complex stored procedures.

I bought this magazine. I will do my best to study English!!