2018年8月26日日曜日

mbedでMQTTのパブリッシュとサブスクライブ

今回はmbedをつかっていきまぁす。
mbedの記事は今回がお初かと。

今回は備忘録臭が強めです(笑)
ethernetケーブルは100均のケーブルを剥いで使うもよし、
RJ45買うもよし、(当方ではHR911105Aを使用)
そんなかんじで配線してください。(適当)

こちらのコードを大体パクった感じですが、一部RTOS仕様にして、
パブリッシャーを関数にくるめただけです。




いままで、マイコンとはUSB経由のシリアル通信でパソコンと
通信していたのですが、ポートが変わったり、
Ubuntuだとパーミッションなども気にしないといけなく、
結構うんざりしてたため、MQTTを用いた通信に踏み切ったわけです。
お察しだとは思いますが、MQTT-bridgeのROSパッケージのstd_msgs::Int32MultiArrayに
準拠したjsonの記述でパブリッシュのメッセージを記述しています(笑)
イーサネット特有の面倒くささなども出てきますが、
USBシリアル通信の癖の強さよりはマシかと。
んまぁ、おいおい使ってみてですかねぇ

2018年3月28日水曜日

FlutterでAndroidとiOSそれぞれに実機で実行させてみた

最近β版が出たばっかりのFlutter。
まだまだBluetoothなどのサポートがされていないことから
Flutterのみですべてのニーズに答えることはまだ難しそうですが、
実際ロードマップに対応予定となっておりますし、
面白そうな試みではあったわけで実際試してみました!

Flutter自体はマルチプラットフォームで開発できるわけですが、
iOSアプリケーションはXcodeを経由する都合上、
macOSが必要となるため、hackintosh Macが必要となります。

    環境

  • MacBook Air (11-inch, Mid 2011)
    • 1.6 GHz Intel Core i5
    • 4 GB 1333 MHz DDR3
    • Intel HD Graphics 3000 384 MB
    • macOS Sierra 10.12.6
    • Xcode 9.2
    • Android Studio 3.0.1
    • Visual Studio Code 1.21.1
  • Galaxy S8 (SM-F950FD)
    • 2.3 GHz Exynos Octa 8895
    • 4 GB RAM (詳細不明)
    • Mail-G71
    • Android 7.0
  • iPhone 6 plus(A1524)
    • 1.4 GHz Apple A8 Processor
    • 1GB(LPDDR3)
    • iOS 11.2.6 (15D100)
  • ※iPhoneにおいてはWikipediaを参照

XcodeAndroid Studioをいれておいてください。
(これは他のわかりやすいサイトをググってくださいm(_ _)m)

brew も入ってない人は入れてください。

Flutter自体の導入は公式を参考にしてください。

今回はVSCodeを使って環境構築してみました。

まず、例の如くDart CodeFlutter Snippets for vscodeを導入していきましょう。
それぞれ導入して再読み込みしたら、つぎにCommandキーと,を同時押ししてVSCodeの設定画面を
開きます。

画像の右側のエディタに注目してください。
私の場合はFlutterをhomeディレクトリの下にインストールしたのでこのように記述しています。

VSCodeの設定はこれで以上です。

あとはflutter doctorなりの依存関係などを解決すれば準備完了のはずです。

ではVSCodeのコマンドパレットをひらいて、New Projectしてみましょう!!


ここでプロジェクト名を指定しますが
Androidアプリのみ作成予定の方はいいですが、
iOSアプリを作成する方はできるだけオリジナリティのある方がいいです

...まぁいくらでも修正できますが(笑)


そしたらこんな感じで生成されます。

ここでAndroid端末をつないでみましょう!

そしたら右下に端末の名前が表示させると思われます。

Controlとshiftと@を同時押しするとVSCode内蔵のterminalが開くので
flutter run
と入力してください。
そしたら後は待つだけ!!


では、
つぎは問題のiOS編です。

まずXcode開きましょう!!


そこからメニューバーのfileタブからOpenをクリックすると
Xcodeのプロジェクトフォルダの選択を求められるので、
先程、Flutterにて自動生成されたiosという名前のフォルダーを選択します。


ここで急に思い出したので書き出しますが、
Xcode初めての方はAppleアカウントをXcodeに紐付ける必要があります。

Xcodeの環境設定(Preferences...)をクリック


Accountsタブをひらいてログインしてごにょごにょ…

ではここでFlutterの設定の方に戻りましょう!!



ここで先程設定したアカウントを設定しましょう。

あとBundle identifierですが、これは多分世界中でかぶらせては行けない感じなので、
完全にオリジナルのものを設定しないと書き込み時にエラーがでるので
気をつけてください!!


これでXcodeの設定はオッケーです。

あとはVSCodeのほうのterminalで
flutter run
をするとiOS端末に書き込まれます。

※iOS端末のほうは設定画面からデベロッパー認証しないと起動しないので注意してくださいね(^_^;)


こんな感じでどうでしょう??

hackintosh Macユーザーの方々、どうです?
一度お試しあれ〜(*^_^*)


いや〜やっぱり長くなってしまいましたねぇ
でも一つのソースでiOS端末とAndroid端末を同時に開発できるのは
非常に魅力的です。
ただ、Dartに関する情報が少ないのが玉に瑕ってところです。
GoogleはAndroidの次期OSにもFlutterを採用するとも言っている
ようなので、触っておいても無駄にはならないと思います。

あと、DartはJavaScriptの代わりとしても使えるので
実は結構優秀だったりして…?

|д゚)チラッ<ROSDartはよ

2018年2月12日月曜日

PSoC CreatorのUSBMIDIのデモについての考察

今日は建国記念の日が日曜日に入ったためか
振替休日ということで月曜日なのにおやすみというわけですが、
なんといいますか...
振替休日ってなんか都合がいいですね(何が言いたい)

というわけで、今回も短めにさっくり行きたいと思います。

PSoC5LPではUSBホスト機能が内蔵されていて、
その中でも少数ですがAPI(詳細はデータシートをご覧ください)
が存在するものがあります。

USBMIDIはその中の一つに当たるわけです。

ここで当方のPSoC周りの環境を記載しておきますと、
  • PSoC Creator 4.1 Update 1 (4.1.0.3210)
  • PSoC 5LP Development kit
    (CY8C5868AXI-LP035)
という環境です。

ではまずPSoCCreatorを開いて、Fileタブをクリックして、
そこの3行目あたりにあるCode Example...を開いてください。


USBあたりでフィルターかけてあげると出てくると思われます。

このコンテンツはどうやらダウンロードしないといけないらしいので
初回に開くときのみ左側に地球マークがあると思われるので
そこをクリックしてダウンロードしてください。


当方の環境の場合はその後にProjectタブのなかの
Device Selector...でPSoCを変更しています。

PSoCを変更したあとはピンアサインの確認を忘れずに(;´∀`)

今回は回路エディターの方はいじらないです。

あくまでソースコードの考察ということで悪しからず...

/*******************************************************************************
* Copyright 2012-2015, Cypress Semiconductor Corporation. All rights reserved.
* This software is owned by Cypress Semiconductor Corporation and is protected
* by and subject to worldwide patent and copyright laws and treaties.
* Therefore, you may use this software only as provided in the license agreement
* accompanying the software package from which you obtained this software.
* CYPRESS AND ITS SUPPLIERS MAKE NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* WITH REGARD TO THIS SOFTWARE, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT,
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*******************************************************************************/

#include <project.h>

#define BUTT1                 (0x01u)
#define BUTT2                 (0x02u)

#define DEVICE                  (0u)
#define MIDI_MSG_SIZE           (4u)

/*MIDI Message Fields */
#define MIDI_MSG_TYPE           (0u)
#define MIDI_NOTE_NUMBER        (1u)
#define MIDI_NOTE_VELOCITY      (2u)

/* MIDI Notes*/
#define NOTE_72                 (72u)
#define NOTE_76                 (76u)

/* MIDI Notes Velocity*/
#define VOLUME_OFF              (0u)
#define VOLUME_ON               (100u)

#define USB_SUSPEND_TIMEOUT     (2u)

/* Identity Reply message */
const uint8 CYCODE MIDI_IDENTITY_REPLY[] = {
    0xF0u,      /* SysEx */
    0x7Eu,      /* Non-real time */
    0x7Fu,      /* ID of target device (7F - "All Call") */
    0x06u,      /* Sub-ID#1 - General Information */
    0x02u,      /* Sub-ID#2 - Identity Reply */
    0x7Du,      /* Manufacturer's ID: 7D - Educational Use */
    0xB4u, 0x04u,               /* Family code */
    0x32u, 0xD2u,               /* Model number */
    0x01u, 0x00u, 0x00u, 0x00u, /* Version number */
    /*0xF7         End of SysEx automatically appended */
};

/* Need for Identity Reply message */
extern volatile uint8 USB_MIDI1_InqFlags;
extern volatile uint8 USB_MIDI2_InqFlags;

volatile uint8 usbActivityCounter = 0u;

uint8 csButtStates = 0u;
uint8 csButtStatesOld = 0u;
uint8 csButtChange = 0u;
uint8 inqFlagsOld = 0u;


/*******************************************************************************
* Function Name: SleepIsr
********************************************************************************
* Summary:
*  The sleep interrupt-service-routine used to determine a sleep condition.
*  The device goes into the Suspend state when there is a constant Idle 
*  state on its upstream-facing bus-lines for more than 3.0 ms. 
*  The device must be suspended drawing only suspend current from the 
*  bus after no more than 10 ms of the bus inactivity on all its ports.
*  This ISR is run each 4 ms, so after a second turn without the USB activity,  
*  the device should be suspended.
*
*******************************************************************************/
CY_ISR(SleepIsr)
{
    /* Check USB activity */
    if(0u != USB_CheckActivity()) 
    {
        usbActivityCounter = 0u;
    } 
    else 
    {
        usbActivityCounter++;
    }
    /* Clear Pending Interrupt */
    SleepTimer_GetStatus();
}


/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
*       1. Starts the USBFS device and waits for enumaration.
*
*******************************************************************************/
int main()
{
    uint8 midiMsg[MIDI_MSG_SIZE];    

    /* Enable Global Interrupts */
    CyGlobalIntEnable;

    /* Start USBFS device 0 with VDDD operation */
    USB_Start(DEVICE, USB_DWR_VDDD_OPERATION); 

    while(1u)
    {
      
        /* Host can send double SET_INTERFACE request */
        if(0u != USB_IsConfigurationChanged())
        {
            /* Initialize IN endpoints when device configured */
            if(0u != USB_GetConfiguration())   
            {
                /* Power ON CY8CKIT-044 board */
                MIDI_PWR_Write(0u); 
                
                /* Start ISR to determine sleep condition */  
                Sleep_isr_StartEx(SleepIsr);
                
                /* Start SleepTimer's operation */
                SleepTimer_Start();
                
             /* Enable output endpoint */
                USB_MIDI_Init();
            }
            else
            {
                SleepTimer_Stop();
            }    
        }        
        
        /* Service USB MIDI when device is configured */
        if(0u != USB_GetConfiguration())    
        {
            /* Call this API from UART RX ISR for Auto DMA mode */
            #if(!USB_EP_MANAGEMENT_DMA_AUTO) 
                USB_MIDI_IN_Service();
            #endif
            /* In Manual EP Memory Management mode OUT_EP_Service() 
            *  may have to be called from main foreground or from OUT EP ISR
            */
            #if(!USB_EP_MANAGEMENT_DMA_AUTO) 
                USB_MIDI_OUT_Service();
            #endif

            /* Sending Identity Reply Universal System Exclusive message 
             * back to computer */
            if(0u != (USB_MIDI1_InqFlags & USB_INQ_IDENTITY_REQ_FLAG))
            {
                USB_PutUsbMidiIn(sizeof(MIDI_IDENTITY_REPLY), \
                            (uint8 *)MIDI_IDENTITY_REPLY, USB_MIDI_CABLE_00);
                USB_MIDI1_InqFlags &= ~USB_INQ_IDENTITY_REQ_FLAG;
            }
            #if (USB_MIDI_EXT_MODE >= USB_TWO_EXT_INTRF)
                if(0u != (USB_MIDI2_InqFlags & USB_INQ_IDENTITY_REQ_FLAG))
                {
                    USB_PutUsbMidiIn(sizeof(MIDI_IDENTITY_REPLY), \
                            (uint8 *)MIDI_IDENTITY_REPLY, USB_MIDI_CABLE_01);
                    USB_MIDI2_InqFlags &= ~USB_INQ_IDENTITY_REQ_FLAG;
                }
            #endif /* End USB_MIDI_EXT_MODE >= USB_TWO_EXT_INTRF */
            
            
            
            //ここから
            /* Service Keys */
      if (0u == SW1_Read()) 
            {
                csButtStates |= BUTT1;
            }
            else
            {
                csButtStates &= ~BUTT1;
            }
      if (0u == SW2_Read()) 
            {
                csButtStates |= BUTT2;
            }
            else
            {
                csButtStates &= ~BUTT2;
            }
            /* Process any button change
            ボタンのステータスが変更されたとき*/
      if (0u != (csButtChange = csButtStates ^ csButtStatesOld)) 
            {
       csButtStatesOld = csButtStates;

       /* All buttons are mapped to Note-On/Off messages */
       midiMsg[MIDI_MSG_TYPE] = USB_MIDI_NOTE_ON;
       
       /* Button 1 */
                
                /*
                midiMsg[MIDI_NOTE_NUMBER] = NOTE;
                midiのノートナンバー。
                60がc3(ド)
                69が440Hz(時報の音)

                midiMsg[MIDI_NOTE_VELOCITY]
                midiのvelocity、つまり音の強弱(鍵盤を押す強弱なイメージ)
                
                midiMsg[MIDI_MSG_TYPE]
                midiのデータ・タイプを格納する
                */
       if (0u != (csButtChange & BUTT1)) 
                {
        /* Button determines note number */   
        midiMsg[MIDI_NOTE_NUMBER] = 60u;
        if (0u != (csButtStates & BUTT1))
                    {
                        /* Note On */
                        midiMsg[MIDI_NOTE_VELOCITY] = VOLUME_ON;  
                    }
        else
                    {
                        /* Note Off */
                        midiMsg[MIDI_NOTE_VELOCITY] = VOLUME_OFF;   
                    }    
           /* Put MIDI Note-On/Off message into input endpoint */
                    USB_PutUsbMidiIn(USB_3BYTE_COMMON, midiMsg, USB_MIDI_CABLE_00);
       }

       /* Button 2 */
       if (0u != (csButtChange & BUTT2))
                {
        /* Button determines note number */   
        midiMsg[MIDI_NOTE_NUMBER] = 72;//c4
        if (0u != (csButtStates & BUTT2))
                    {
                        /* Note On */
                        midiMsg[MIDI_NOTE_VELOCITY] = VOLUME_ON;  
                        //midimsgはuint8
                    }
        else
                    {
                        /* Note Off */
                        midiMsg[MIDI_NOTE_VELOCITY] = VOLUME_OFF; 
                    }    
           /* Put MIDI Note-On/Off message into input endpoint 
                    多分、送信処理*/
        USB_PutUsbMidiIn(USB_3BYTE_COMMON, midiMsg, USB_MIDI_CABLE_00);
                    /* Second Note message */
                    midiMsg[MIDI_MSG_TYPE] = USB_MIDI_NOTE_ON;
                 midiMsg[MIDI_NOTE_NUMBER] = NOTE_72;
                    if (0u != (csButtStates & BUTT2))
                    {
                        /* Note On */
                        midiMsg[MIDI_NOTE_VELOCITY] = VOLUME_ON;      
                    }
        else
                    {
                        /* Note Off */
                        midiMsg[MIDI_NOTE_VELOCITY] = VOLUME_OFF;   
                    }    
           /* Put MIDI Note-On/Off message into input endpoint */
                    USB_PutUsbMidiIn(USB_3BYTE_COMMON, midiMsg, USB_MIDI_CABLE_00);
       }
                #if(USB_EP_MANAGEMENT_DMA_AUTO) 
                   #if (USB_MIDI_EXT_MODE >= USB_ONE_EXT_INTRF)
                        MIDI1_UART_DisableRxInt();
                        #if (USB_MIDI_EXT_MODE >= USB_TWO_EXT_INTRF)
                            MIDI2_UART_DisableRxInt();
                        #endif /* End USB_MIDI_EXT_MODE >= USB_TWO_EXT_INTRF */
                    #endif /* End USB_MIDI_EXT_MODE >= USB_ONE_EXT_INTRF */            
                    USB_MIDI_IN_Service();
                    #if (USB_MIDI_EXT_MODE >= USB_ONE_EXT_INTRF)
                        MIDI1_UART_EnableRxInt();
                        #if (USB_MIDI_EXT_MODE >= USB_TWO_EXT_INTRF)
                            MIDI2_UART_EnableRxInt();
                        #endif /* End USB_MIDI_EXT_MODE >= USB_TWO_EXT_INTRF */
                    #endif /* End USB_MIDI_EXT_MODE >= USB_ONE_EXT_INTRF */                
                #endif
      }
            
            //ここまでがあやしい
            
        
            /* Check if host requested USB Suspend */
            if( usbActivityCounter >= USB_SUSPEND_TIMEOUT ) 
            {
                MIDI1_UART_Sleep();
                MIDI2_UART_Sleep();
                
                /* Power OFF CY8CKIT-044 board */
                MIDI_PWR_Write(1u);     
                
                /***************************************************************
                * Disable USBFS block and set DP Interrupt for wake-up 
                * from sleep mode. 
                ***************************************************************/
                USB_Suspend(); 
                /* Prepares system clocks for sleep mode */
                CyPmSaveClocks();
                /***************************************************************
                * Switch to the Sleep Mode for the PSoC 3 or PSoC 5LP devices:
                *  - PM_SLEEP_TIME_NONE: wakeup time is defined by PICU source
                *  - PM_SLEEP_SRC_PICU: PICU wakeup source 
                ***************************************************************/
                CyPmSleep(PM_SLEEP_TIME_NONE, PM_SLEEP_SRC_PICU);
                /* Restore clock configuration */
                CyPmRestoreClocks();
                /* Enable USBFS block after power-down mode */
                USB_Resume();
                
                /* Enable output endpoint */
                USB_MIDI_Init();
                
                /* Power ON CY8CKIT-044 board */
                MIDI_PWR_Write(0u);
                
                MIDI1_UART_Wakeup();
                MIDI2_UART_Wakeup();
                usbActivityCounter = 0u; /* Re-init USB Activity Counter*/
            }
        }
    }
}


/*******************************************************************************
* Function Name: USB_callbackLocalMidiEvent
********************************************************************************
* Summary: Local processing of the USB MIDI out-events.
*
*******************************************************************************/
void USB_callbackLocalMidiEvent(uint8 cable, uint8 *midiMsg) CYREENTRANT
{
    /* Support General System On/Off Message. */
    if((0u == (USB_MIDI1_InqFlags & USB_INQ_SYSEX_FLAG)) \
            && (0u != (inqFlagsOld & USB_INQ_SYSEX_FLAG)))
    {
        if(midiMsg[USB_EVENT_BYTE0] == USB_MIDI_SYSEX_GEN_MESSAGE)
        {
            if(midiMsg[USB_EVENT_BYTE1] == USB_MIDI_SYSEX_SYSTEM_ON)
            {
                MIDI_PWR_Write(0u); /* Power ON */
            }
            else if(midiMsg[USB_EVENT_BYTE1] == USB_MIDI_SYSEX_SYSTEM_OFF)
            {
                MIDI_PWR_Write(1u); /* Power OFF */
            }
        }
    }
    inqFlagsOld = USB_MIDI1_InqFlags;
    cable = cable;
}    


/*******************************************************************************
* Function Name: USB_MIDI1_ProcessUsbOut_EntryCallback
********************************************************************************
* Summary:  Turn the LED_OutA on at the beginning of the function
* USB_MIDI1_ProcessUsbOut() when data comes to be put in the UART1 out
* buffer.
* 
*******************************************************************************/
void USB_MIDI1_ProcessUsbOut_EntryCallback(void)
{
    LED_OutA_Write(1);
}


/*******************************************************************************
* Function Name: USB_MIDI1_ProcessUsbOut_ExitCallback
********************************************************************************
* Summary:  Turn the LED_OutA off at the end of the function  
* USB_MIDI1_ProcessUsbOut() when data is put in the UART1 out-buffer.
* 
*******************************************************************************/
void USB_MIDI1_ProcessUsbOut_ExitCallback(void)
{
    LED_OutA_Write(0);
}


/*******************************************************************************
* Function Name: USB_MIDI2_ProcessUsbOut_EntryCallback
********************************************************************************
* Summary:  Turn the LED_OutB on at the beginning of the function  
* USB_MIDI2_ProcessUsbOut() when data comes to be put in the UART2 out- 
* buffer  
* 
*******************************************************************************/
void USB_MIDI2_ProcessUsbOut_EntryCallback(void)
{
    LED_OutB_Write(1);
}


/*******************************************************************************
* Function Name: USB_MIDI2_ProcessUsbOut_ExitCallback
********************************************************************************
* Summary:  Turn the LED_OutB off at the end of the function  
* USB_MIDI2_ProcessUsbOut() when data is put in the UART2 out-buffer.
* 
*******************************************************************************/
void USB_MIDI2_ProcessUsbOut_ExitCallback(void)
{
    LED_OutB_Write(0);
}


/*******************************************************************************
* Function Name: MIDI1_UART_RXISR_EntryCallback
********************************************************************************
* Summary:  Turn the LED_InA on at the beginning of the MIDI1_UART_RXISR ISR  
* when data comes to UART1 to be put in the USBFS_MIDI IN endpoint
* buffer.
*
*******************************************************************************/
void MIDI1_UART_RXISR_EntryCallback(void)
{
    /* These LEDs indicate MIDI input activity */
    LED_InA_Write(1);
}


/*******************************************************************************
* Function Name: MIDI1_UART_RXISR_ExitCallback
********************************************************************************
* Summary:  Turn the LED_InA off at the end of the MIDI1_UART_RXISR ISR  
* when data is put in the USBFS_MIDI IN endpoint buffer.
*
*******************************************************************************/
void MIDI1_UART_RXISR_ExitCallback(void)
{
    #if (USB_EP_MANAGEMENT_DMA_AUTO) 
        USB_MIDI_IN_Service();
    #endif /* (USB_EP_MANAGEMENT_DMA_AUTO) */
    
    LED_InA_Write(0);
}


/*******************************************************************************
* Function Name: MIDI2_UART_RXISR_EntryCallback
********************************************************************************
* Summary:  Turn the LED_InB on at the beginning of the MIDI2_UART_RXISR ISR  
* when data comes to UART2 to be put in the USBFS_MIDI IN endpoint  
* buffer.
*
*******************************************************************************/
void MIDI2_UART_RXISR_EntryCallback(void)
{
    /* These LEDs indicate MIDI input activity */
    LED_InB_Write(1);
}


/*******************************************************************************
* Function Name: MIDI2_UART_RXISR_ExitCallback
********************************************************************************
* Summary:  Turn the LED_InB off at the end of the MIDI2_UART_RXISR ISR  
* when data is put in the USBFS_MIDI IN endpoint buffer
*
*******************************************************************************/
void MIDI2_UART_RXISR_ExitCallback(void)
{
    #if (USB_EP_MANAGEMENT_DMA_AUTO) 
        USB_MIDI_IN_Service();
    #endif /* (USB_EP_MANAGEMENT_DMA_AUTO) */
    
    LED_InB_Write(0);
}


/* [] END OF FILE */


コメントで
//ここから(167行目)
//ここまでがあやしい(278行目)
と記述されているところ(露骨過ぎますかね??)以外は基本ノータッチです。

ここでざっくりとMIDIについておさらいしましょう。
MIDIの詳細についてはggrばたくさん出てくるので割愛しますが、
  • ノートオン・オフ
    ここにはチャンネルの番号が格納されている)
  • ノートナンバー
    (音程のデータが入っている)
  • ベロシティー
    (音の強弱データが入っている)
という解釈で大体合ってますかね。

ノートナンバーの方はこちらのサイトを参考にさせていただきました。


このやっつけ感満載のおさらい通りにやっていけばなんとなくは
コメントでくくったあたりを弄くれば、
オリジナルMIDIデバイスが作れそうな気がしますねぇ




PSoCCreatorでバージョン管理系統が搭載されてないのは少しめんどくさいと感じるわけです。
その他にも今回のを見る限りボタンが多そうなデバイスを制作するにあたって
オブジェクト指向プログラムができないのも少しやりづらいかなと。

もっとコードエディターの補完機能とか充実していただけませんかね??
あと正式にC++対応お願いしますm(_ _)m

2018年2月10日土曜日

node.jsのURL追加をクラスで実装してみる。

ブログには大きい記事しか載せるべきではないかと思ってはいたのですが、
備忘録などに使えるしどうせ見る人もそんなにいないであろう(笑)と思ったので、
これからは小さいネタも載せて更新頻度の向上も目指したいと思ってます(^_^;)

さて、今回はひょんなことからWebアプリを作らないといけなくなったわけなのですが、
その際にnode.jsを使わせて頂いているわけです。

そこでまぁnpmを使って他のパッケージを利用するのもいいのですが、
今回はJsのクラスで勉強がてら(というか興味)に実装しようと考えたわけです。

まぁこのwebアプリ自体公にするものではなさそうなので大丈夫かなと(おい)

var http = require('http');
var fs = require('fs');

var server = http.createServer();
server.on('request', getJs);
console.log('Server running ...');

var Link_creator = function(source,linkname,type){
    this.source = source;
    this.linkname = linkname;
    this.type = type;
}

Link_creator.prototype.add = function(url,Res){
    
    console.log("function in");
    console.log(url);
    console.log("type is "+this.type);
    if(this.type == 'html'){

        if (this.linkname == url) {
            fs.readFile(this.source, 'UTF-8', function (err, data) {
                Res.writeHead(200, {'Content-Type': 'text/html'});
                Res.write(data);
                Res.end();
            });
        }
        return Res;
    }else if(this.type == 'js'){
        if (this.linkname == url) {
            fs.readFile(this.source, 'UTF-8', function (err, data) {
                Res.writeHead(200, {'Content-Type': 'application/javascript'});
                Res.write(data);
                Res.end();
            });
        }
        return Res;
    }else if(this.type == 'jpg'){
        if (this.linkname == url) {
            Res.writeHead(200, {'Content-Type': 'image/jpeg'});
                Res.end(fs.readFileSync(this.source, 'binary' ),'binary');
        }
        return Res;
    }else if(this.type == 'gif'){
        if (this.linkname == url) {
            Res.writeHead(200, {'Content-Type': 'image/gif'});
                Res.end(fs.readFileSync(this.source, 'binary' ),'binary');
        }
        return Res;
    }else if(this.type == 'css'){
        if (this.linkname == url) {
            fs.readFile(this.source, 'UTF-8', function (err, data) {
                Res.writeHead(200, {'Content-Type': 'text/css'});
                Res.write(data);
                Res.end();
            });
        }
        return Res;
    }
}
// new Link_creator('ローカルファイルの指定','URIの指定','タイプの指定')
var sites = [new Link_creator('./main.html','/index.html','html')
            ,new Link_creator('./home.html','/','html')];



function getJs(req, res) {
    var url = req.url;
    console.log(url);
    sites.forEach(function(site){
        if(site.linkname == url){
            console.log(site.source);
            res = site.add(url,res);
        }
    });
}


server.listen(8080);


とまぁこんな感じになります。
ミソは
new Link_creator('ローカルファイルの指定','URIの指定','タイプの指定')
ですね。
タイプの指定はクラス内のadd関数をゴニョゴニョしてオリジナルのものを
追加すればいいです。

やっぱりオブジェクト指向便利だなぁ…(しみじみ)