2018年12月24日 星期一

Alexa 架構



Alexa 是什麼?

亞馬遜公司所發售Amazon Echo(智慧喇叭)內搭載的智慧型語音助理 - Alexa


Alexa 能做些什麼?

在於「操作顛覆」,其他已上市的 IoT 系統幾乎以「觸控」操作來完成,語音助理 Siri、Google Now 只能做為輔助工具,在 Amazon Echo,以語音為主要操控方式,使用者只靠說話就能與之互動,透過語音下達指令,Alexa 可以為你閱讀新聞、查看天氣與交通狀況,撥流行音樂、確認行事曆、訂Pizza、叫車、買東西等,可說是一款語音助理與生活相互融合的絕佳範例。


開發者可用Alexa Skills Kit 開發自己的Alexa Skill。例如美國最大的餐點外送公司Grubhub在Alexa 上的開頭語是:「Alexa, tell Grubhub I’m hungry」;Uber 則是用:「Alexa, ask Uber to request a ride!」。許多企業都在思考該如何以最直覺的方式讓人們完成點餐、搭車等等的服務,讓消費者更快下決定,有效提高下單率!

架構

  • Framework: alexa-app-server (nodejs) 
  • 溝通需使用 https (self-signed certificate 需參考官方文件) 
  • Backend 主要的對象是 Intent event,依不同的event進行回應 
  • 依需求,溝通過程不主動關閉 session 需在8秒內回應 response,否則將被強制關閉 session


Intent 範例



2018年12月23日 星期日

什麼是 RS485?

什麼是 RS-422?

RS-422 (EIA RS-422-A 標準) 為過去用於 Apple Macintosh 電腦的序列連結。 與 RS-232 參照至接地的非平衡式訊號相反,RS-422 為使用差動電子訊號, 差動傳輸使用 2 個通道,分別用於傳輸與接收訊號,因此相較於 RS-232,其抗擾性更佳,通訊距離也更長。 這些優勢讓 RS-422 更適合用於工業級應用。


什麼是 RS485?

RS-485 (EIA-485 標準) 為 RS-422 的改良版本,從原本的 10 組裝置提高至 32 組裝置,並定義必要的電子特性,以在最大負載下保持適當的訊號電壓。

RS232 連線長度只能50~100 呎(17~33m),一個 Comm Port只能接一台,為因應工廠控制需要,於1993年訂定 RS485

  • 抗雜訊能力提高
  • 距離可達4000 呎 (1333 米) 
  • 同時可控制32個設備
  • 優點 
    1. 比RS232便宜,只使用一個+5V電源, 比RS232用的 ±5V方便 
    2. 網路功能:可長距離、多儀器連結,使用高阻抗的接收器,可接達256 台
    3. 長距離: 1333m
    4. 快速:可達10Mbits/s
      (RS232只能達 ~20Kbits/s)
  • RS485接線法,基本上只有三條線 TRA, TRB, SG 可只使用2線, 須加終端電阻, 每台機器須設不同的位址
  • 終端電阻加在電腦端及最遠一台機器的一端,需搭配導線的阻抗值 (必須大於75ohm),一般而言AWG24線使用(100~150ohm即可)
  • 由於RS232還是PC現有的介面,所以常使用RS232到RS485的轉接器,以便利用RS232控制RS485儀器

什麼是 RS232C?


什麼是 RS232C

RS-232 (ANSI/EIA-232 標準) 為過去 IBM 相容電腦的序列連結功能, 可用以連接滑鼠、印表機或數據機,或用於工業級儀器控制等許多功能。
  • 電腦中最常用的介面之一。 
  • RS-232 : Recommend Standard number 232. 
  • C 表示最新的版本. 
  • 大多電腦都是符合RS-232C一部份標準. 
  • 連線長度只能50~100 呎(17~33m)
  • 一個 Comm Port只能接一台
  • 全功能的 RS-232C 規定使用25-pin D 接頭,使用其中的 22 pins. 
  • 對電腦通訊,大多是不必要的,因此大多數PC只使用一個9pin的公接頭。 
  • 其實,只要使用其中3根pin便可達到全雙工 (full duplex)通訊的目的,一根Send,一根 receive,一根ground
    (Full duplex:送資料與接收資料同時進行,亦即是 雙向的溝通bi-directional)

RS232定義兩種接頭形式,接腳訊號定義不同
  • DCE (data communication Equipment) 
    • 母接頭
    • 資料通訊設備→遠端儀器
  • DTE (data terminal equipment) 
    • 公接頭
    • 資料終端設備→電腦
  • DTE 與DCE可直接一對一連接,
  • 同類間連接需要用Null modem Cable,交換 send 與 receive腳位 

資料如何傳輸 - start bit
  • RS232是屬於序列式的(serial)傳輸資料,資料是一個bit一個bit傳
  • 一般狀態,TD 與RD是在高電位,一端的TD 是接到另端的RD,當有資料要傳時,一 端的TD會先被拉低電位,而另一端的 RD接收到低電位開始接收資料,這個動作叫做送起始位元( Start bit)

接收資料-data bits
  • Start bit後,兩邊的設備開始準備接收資料 
  • 為了溝通,兩邊必須有相同的傳輸速度(baud rate),及有多少 data bits要送
    (Baud rate: 每秒多少bit的傳輸動作)
  • 確定好傳輸速度,接著規定要傳多少bit, 一般是傳8或7 bit,以8 bit最常用。LSB最小位元先送。

資料檢查 Parity bit 
  • 檢查序列資料是否傳得正確?
  • 奇偶檢查 
  • Data bit送完,會再送一個parity bit,讓接收端檢查資料是否正確
  • 有以下幾種: 
    • none:不送parity bit (最常用) 
    • Odd:當data bit有偶數個1時 → 送1補成奇數個 
    • Even :當data bit有奇數個1時 → 送1補成偶數個 
    • Mark:永遠送1做parity bit 
    • Space :永遠送0做parity bit

Stop Bit
  • 停止位元: 將電位拉高,宣告資料已經傳 輸完畢。 
  • 最常用的傳輸格式常記為 8-N-1 8: 
    • 8 data bit 
    • N: No parity bit 
    • 1: 1 start/stop bit
      (設備較慢時用 2bits)

防止資料遺失
  • 使用Handshaking(握手 ) 
    • 可不用 
    • 很多方式
      • 利用某一根線的高低電位 ( 如DTR),當一端準備收資料時先拉高某一線電位,另一端接收到訊息後,才開始送資料
      • 利用TD, RD線,傳送特殊的byte 當成準備接收資料的代號
  • 使用中斷(interrupt)或詢問(polling) 
    • Interrupt:有事件發生時執行 
    • Polling:以軟體不斷檢查資料
  • 使用Acknowledgement(確認 ) 
    • 收到資料時,送回一個特殊byte確認資料收到
  • 錯誤檢查(error checking) 送checksum byte給接收端檢查資料正確性,有問題,通知重送資料

2018年12月10日 星期一

ASP.NET Core 2.0 - JwtBearerEvents


想在 .Net Core 2.0 裡接 jwt 驗證的事件,
比如拿到 token 驗證失敗的 exception,
該怎麼做呢?

建立 CustomJwtBearerEvents 類別,實作 JwtBearerEvents
public class CustomJwtBearerEvents : JwtBearerEvents
{
    private static Logger _logger = LogManager.GetCurrentClassLogger();

    public CustomJwtBearerEvents() : base()
    {}

    public override Task AuthenticationFailed
            (AuthenticationFailedContext context)
    {
        _logger.Error(context.Exception.Message);
        return this.OnAuthenticationFailed.Invoke(context);
    }

    public override Task TokenValidated(TokenValidatedContext context)
    {
        _logger.Trace("OnTokenValidated: " + context.SecurityToken);

        if (something)
        {
            var failMsg = $"Invalid API Token : {jwtToken.RawData}";
            context.Fail(failMsg);
            _logger.Error(failMsg);
                    
        }
        return this.OnTokenValidated.Invoke(context);
    }

    public override Task Challenge(JwtBearerChallengeContext context)
    {
        return this.OnChallenge.Invoke(context);
    }

    public override Task MessageReceived(MessageReceivedContext context)
    {
        return this.OnMessageReceived.Invoke(context);
    }
}

reference: https://www.codeproject.com/Articles/1205160/ASP-NET-Core-Bearer-Authentication

2018年3月28日 星期三

Runtime + Compiler on Vue-Cli 3.0


有需求動態載入 component,
在 jsbin 做簡單的測試是沒問題,
但使用 Vue-Cli 3.0 的開發環境,
卻拋出以下錯誤

You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

經一番奮鬥後,發現 Vue-Cli 3.0 的 webpack 設定,
預設載入的 Vue 是 vue.runtime.esm.js,
所以在 runtime 的時候會發生此問題,
需將  vue.runtime.esm.js 換成  vue.esm.js
在 run time 期間也有 compiler 可以用

那該怎麼做呢?

在根目錄下設定 vue.config.js 如下即可
 (若沒有此檔,請自行新增!)
module.exports = {
  configureWebpack: {
    resolve: {
      alias: {
        'vue$': 'vue/dist/vue.esm.js'
      }
    }
  }
}


參考來源:

  1. https://github.com/vuejs/vue/tree/dev/dist#explanation-of-build-files
  2. https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md

2018年3月22日 星期四

關閉 Visual Studio 2017 自動編譯 ts 檔


在專案檔加入 <typescriptenabled>true</typescriptenabled>.

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
  </PropertyGroup>


reference:

TypeScriptCompileBlocked

If you are using a different build tool to build your project (e.g. gulp, grunt , etc.) and VS for the development and debugging experience, set true in your project. This should give you all the editing support, but not the build when you hit F5.

2017年12月14日 星期四

Startup pm2 command for Windows Server 2012 R2


需在 windows server 重新啟動的時候,
自動執行 pm2 start someapp.js,
pm2 有 startup command 提供此機制,但不支援 windows,
以下是 worked solution


The following is my latest solution which works really well:
  • create a specific Windows user for running node scripts
  • login as that user and npm install pm2 -g
  • pm2 start all the apps I would like to have startup
  • pm2 save to save the current process list
  • create a .bat file and within it paste the following commands:
@echo off
set HOMEDRIVE=C:
set HOMEPATH=\Users\%USERNAME%
set path=C:\Users\%USERNAME%\AppData\Roaming\npm;%path%
pm2 delete all & pm2 resurrect

Use Windows Task Scheduler to create a task that:
  • runs "whether user is logged on or not"
  • is Triggered "At startup"
  • is configured to run the .bat file that we created