Skip to content

Instantly share code, notes, and snippets.

@scottire
Last active August 4, 2017 13:07
Show Gist options
  • Save scottire/10278b0a14c3a06355589806b929f710 to your computer and use it in GitHub Desktop.
Save scottire/10278b0a14c3a06355589806b929f710 to your computer and use it in GitHub Desktop.
HID POST
import 'rxjs/add/operator/map';
import { Events } from 'ionic-angular'
import { getHidParamsFromUrl } from '../lib/utils'
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Headers } from '@angular/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
// curl -X POST -H "Content-Type: application/json" -d '{"systemUserName": "flomio", "systemPassword": "Flom1oPilot01", "tagID": "3D4C010202E577", "tac": "D499BA754B38E64061939647D9C50AC220D8995D"}' https://activid.hidglobal.com/RESTGateway/TTAuthentication/TTS
//
@Injectable()
export class HidServiceProvider {
private hidUrl = 'https://test.activid.hidglobal.com/RESTGateway/TTAuthentication/TTS'
private headers: any;
constructor(
public http: Http,
private events: Events,
){
this.headers = new Headers({ 'Content-Type': 'application/json' });
console.log('Hello HidServiceProvider Provider');
}
public async listenToUrlScanEvents() {
this.events.subscribe('tag:urlRead', (url: string) => {
let params = getHidParamsFromUrl(url)
if ((params.tagId) && (params.tac)){
console.log('tagID: ' + params.tagId + ' tac: ' +params.tac)
this.post(params)
}
});
}
post (params) {
let body = JSON.stringify({
systemUserName: 'Flomio',
systemPassword: 'Flom1oPilot01',
tac: params.tac,
tagID: params.tagId
})
this.http.post(this.hidUrl, body, this.headers)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
//User / pass is: Flomio / Flom1oPilot01
//This is the URL which the web app will use to POST an authentication request:
//https://activid.hidglobal.com/RESTGateway/TTAuthentication/TTS
private extractData(res: Response) {
let body = res.json();
console.log(body.data)
return body.data || { };
}
private handleError (error: Response | any) {
// In a real world app, we might use a remote logging infrastructure
console.log('handleError')
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.log('handleError: ' + errMsg)
return Promise.reject(errMsg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment