顯示具有 angular2 標籤的文章。 顯示所有文章
顯示具有 angular2 標籤的文章。 顯示所有文章

2017年11月26日 星期日

Ionic3+ firebase

FireBase推出最新的雲端資料庫的版本(cloud Firestore), 有別之前的RealTime的資料庫,更引進rxjsoberserablesubscription的模式,讓程式更具有高彈性,我們就簡單的實作範例。

一、建立Colud Firestore
選擇下拉式選單è Cloud Firestore



二、引用firestore

安裝angularfire2  
npm install angularfire2 –save 

import { AngularFireModule } from 'angularfire2';
import {AngularFirestoreModule} from 'angularfire2/firestore';

新版的colud firestore的設定檔需要加上projectId
// AF2 Settings
export const firebaseConfig = {
  apiKey: "xxxxxxxxxxx",
  authDomain: "fiyy4.firebaseapp.com",
  databaseURL: "https://firstapp-xxxx.firebaseio.com",
  projectId: "fyyyy4",
  storageBucket: "firstyy.appspot.com",
  messagingSenderId: "1233456789"
};

注入AngularFireModuleAngularFirestoreModule
imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp),
    AngularFireModule.initializeApp(firebaseConfig),
    AngularFirestoreModule
  ],

三、ProvideràMoney_service.ts建立CRUD的函式

AngularFirestoreCollection 用來取得資料列表(查詢)
AngularFirestoreDocument 用來允許編輯/刪除資料
Observable 用來取得資料陣列有更新的值
import { Observable } from 'rxjs/Observable';
import { AngularFirestore, AngularFirestoreCollection, AngularFirestoreDocument } from 'angularfire2/firestore';

宣告
valueChange: 傳回Json的物件,不具有metadata的屬性
export class MoneyServiceProvider {
  itemCollections: AngularFirestoreCollection<any>; //資料庫傳回值型態
  items: Observable<any[]>;
  constructor(public afs: AngularFirestore) {
    this.itemCollections = this.afs.collection('moneyCost', ref => ref.orderBy('costDate')); //取回資料庫moneyCost,並依日期排序
    this.items = this.itemCollections.valueChanges();//取回值
  }

新增
snapshotChanges:可以用來異動資料庫,本身具有metadata的屬性
  public add(item) {
    this.itemCollections.add(item);
    const listObservable = this.itemCollections.snapshotChanges()
  }

刪除
AngularFirestoreDocument:取回Firestore Collection 的物件
public remove(itemID) {
    const itemDoc: AngularFirestoreDocument<any> = this.afs.doc<any>('moneyCost/' + itemID);
    itemDoc.delete();
  }

List:取得id
  public list() {
    return this.itemCollections.snapshotChanges().map(actions => {
      return actions.map(a => {
        const data = a.payload.doc.data() as any;
        data.id = a.payload.doc.id;
        return data;
      });
    });
  }

整理陣列的資料 data[key]=[{},{}]
  public groupList(items:any[]){
    let total=0;
    const groupItem=[];
    items.forEach(eachObj => {
      if(groupItem[eachObj.costDate] == undefined){
        groupItem[eachObj.costDate]=[];
      }
total=+eachObj.cost;
groupItem[eachObj.costDate].push({key:eachObj.costDate,
value:eachObj,sum:total});
    });
    return groupItem;
  }

Reduce:累加器,會自動依序陣陣值取出作運算後,傳回最終結果
public getCostSum(items):Observable<number> {
    // return 1000;
    return items.map((items: any[]) => {
      return items.reduce((prev, curr: any) => {            
              return prev + Number(curr.cost);               
      }, 0);
  })}

四、建立Componentàmoney-list.ts
import { MoneyServiceProvider } from '../../providers/money-service/money-service';
import { AngularFirestore, AngularFirestoreCollection } from 'angularfire2/firestore';

宣告物件
interface money {
  productId: string;
  costDate: string;
  cost: number;
}

export class MoneyListComponent implements OnDestroy {
  private subscription: ISubscription;
  itemCollections: AngularFirestoreCollection<money>;
  items:any[];
  obserItem:Observable<any[]>;
  groupList: any[];
  sumCost: Number;
  constructor(public afs: AngularFirestore, public moneyService: MoneyServiceProvider) {
    this.obserItem= moneyService.list(); //取回資料
    this.obserItem.subscribe(x=>{
//將取回來的值放回到變數中,透過item 在畫面上呈現
      this.items=x;
      this.groupList =moneyService.groupList(this.items);
      this.doSum (this.items); //計算總合
    });
  }
}

//累加器,會自動傳回最後值的運算結果
doSum(items) {
    this.items.reduce((prev, curr: any) =>{
         return this.sumCost = prev + Number(curr.cost);
    } ,0);
  }

  doDelete(itemId: any) {
    this.moneyService.remove(itemId); //刪除document
  }

  ngOnDestroy() {
//解除訂閱
    this.obserItem.subscribe().unsubscribe();
  }
}

Component: money-list.html

建立group pipe 呈現複雜資料結構 data[key]=[{},{}],轉換資料結構 [{key:1,value=data},{key:2,value=data}]
@Pipe({
  name: 'group',
})
export class GroupByPipe implements PipeTransform {
  transform(value: any, args: any[] = null): any {
    let keys = [];
    for (let key in value) {
      keys.push({ key: key, value: value[key] });
    }
    return keys;
  }
}

加上關鍵字group 就會將資料置換{key,value}
<ion-list>
        <ion-item-group *ngFor="let item of groupList|group">
                <ion-item-divider color="light">
                        <h3>
                                <font color=black>{{item.key|date}}</font>
                        </h3>
                </ion-item-divider>
                <ion-item *ngFor="let subItem of item.value">
                        <ion-row>
                                <ion-col col-lg-4>
                                   <font color=blue>{{subItem.value.productId}}</font>
                                </ion-col>
                                <ion-col col-lg-4>
                                <font>{{subItem.value.cost|currency}} </font>
                                </ion-col>
                                <ion-col col-lg-4>
                                        <button ion-button color="danger" small  round (click)="doDelete(subItem.value.id)">刪除</button>
                                </ion-col>
                        </ion-row>
                </ion-item>
        </ion-item-group>
       <h1><font color=red>總額<input type="hidden" [(ngModel)]="sumCost"> {{sumCost}}</font></h1>
     </ion-list>

範例如下
新增

 

列表

以上總結 Colud Firestore 可以讓資料傳回時,可先轉換資料型態/結構/運算值等等,更具有彈性。


2017年9月18日 星期一

Angular2 簡易版的webRTC

html5推出webrtc的線上影音串流,我們就能立即取得即時視訊,但門檻仍高,
所以有人佛心來研究這款好用的工具peer.js 簡化複雜認證過程,我就簡單示範一下。

一、下戴相關Peer.jsjavaScript

將其檔案放置assets/js

二、修改anguar-cli.json
scripts區塊中引用
      "scripts": [
        "./assets/js/peer.min.js"
      ],

default的區塊中設定IP Port
"defaults": {
    "styleExt": "css",
    "component": {},
     "serve": {
      "port": 4300,
      "host": "192.168.4.88"
     }

三、使用Peer.js 的範例

修改 app/app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule }   from '@angular/forms';
import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

修改app/app.component.html
<h1>My id - {{mypeerid}}</h1>
<input type="text" [(ngModel)]="anotherid">
<button (click)="connect()">Connect</button>
<button (click)="videoconnect()">VideoChat</button>
<video #myvideo></video>

修改app/app.component.ts
import { Component, OnInit,ViewChild} from '@angular/core';
declare var Peer: any;
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  title = 'app';
  mypeerid: any;
  peer;
  anotherid;
  @ViewChild('myvideo') myVideo: any; //取得htmlvideo元素IDmyvideo

  ngOnInit() {
    let video = this.myVideo.nativeElement;
//建立peer物件,如需建立https 才能跨電腦取得對方的視訊
    this.peer = new Peer({ host: 'peerjs-server.herokuapp.com', secure: true, port: 443 })
    //建立逾期時間,如果太久自動失效
setTimeout(() => {
      this.mypeerid = this.peer.id;
    }, 3000);
    //接受連線
    this.peer.on('connection', function (conn) {
     //取得資料
      conn.on('data', function (data) {
        console.log(data);
      });
    });
 
    var n = <any>navigator;
    n.getUserMedia = (n.getUserMedia || n.webkitGetUserMedia || n.mozGetUserMedia || n.msGetUserMedia);
    //回應另一端電腦所建立Peer連線
   this.peer.on('call', function(call) {
      n.getUserMedia({video: true, audio: true}, function(stream) {
        call.answer(stream); //回傳本機端的視訊或聲音
        //取得遠端電腦傳回的視訊或聲音
        call.on('stream', function(remotestream){ 
          video.src = URL.createObjectURL(remotestream);
          video.play();
        })
      }, function(err) {
        console.log('Failed to get stream', err);
      })
    })
 }

  connect() {
    var conn = this.peer.connect(this.anotherid); //與遠端電腦建立peer連線
    conn.on('open', function () {
      conn.send('Message from that id');
    });
  }

  videoconnect(){
    let video = this.myVideo.nativeElement;
    var localvar = this.peer;
    var fname = this.anotherid;
    var n = <any>navigator;
   
    n.getUserMedia = ( n.getUserMedia || n.webkitGetUserMedia || n.mozGetUserMedia  || n.msGetUserMedia );
   
    n.getUserMedia({video: true, audio: true}, function(stream) {
      var call = localvar.call(fname, stream); //與遠端電腦建立peer連線
call.on('stream', function(remotestream) {
        video.src = URL.createObjectURL(remotestream); //取得遠端電腦資訊
        video.play();
      })
    }, function(err){
      console.log('Failed to get stream', err);
    })
  }
}

四、安裝openSSL



五、啟動專案

啟動WebRTC需要有https的協定下,所以需要建立自我憑證的金鑰

ng serve --ssl 1 --ssl-key "D:\joyce\angular2\angularWebRTC\cert\private.key" --ssl-cert "D:\joyce\angular2\angularWebRTC\cert\mycert.crt"

畫面示範