Commit 752c697f authored by Laurent Velez's avatar Laurent Velez
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

camera-agent/.DS_Store

0 → 100644
+6 KiB

File added.

No diff preview for this file type.

camera-agent/app.js

0 → 100644
+128 −0
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2018 Sensinov (www.sensinov.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

var express = require('express');
var request = require('request');
var fs = require('fs');
var config = require('./config.json');

createAE();
function createAE(){
	console.log("\n▶▶▶▶▶");
	var representation = {
		"m2m:ae":{
			"rn":config.aeName,			
			"api": config.appId,
			"rr":"false"
		}
	};
	console.log("POST "+config.cseUri);
	console.log(representation);

	var options = {
		uri: config.cseUri,
		method: "POST",
		headers: {
			"X-M2M-Origin": config.aeId,
			"X-M2M-RI": "123456",
			"Content-Type": "application/json;ty=2"
		},
		json: representation
	};

	request(options, function (error, response, body) {
		console.log("◀◀◀◀◀");
		if(error){
			console.log(error);
		}else{
			console.log(response.statusCode);
			console.log(body);
			createContainer();
		}
	});
}


function createContainer(){
	console.log("\n▶▶▶▶▶");

	var representation = {
		"m2m:cnt":{
			"rn":config.cntName,
			"mni":config.cntMni		

		}
	};

	console.log("POST "+config.cseUri+"/"+config.aeName);
	console.log(representation);

	var options = {
		uri: config.cseUri+"/"+config.aeName,
		method: "POST",
		headers: {
			"X-M2M-Origin": config.aeId,
			"X-M2M-RI": "123456",
			"Content-Type": "application/json;ty=3"
		},
		json: representation
	};

	request(options, function (error, response, body) {
		console.log("◀◀◀◀◀");
		if(error){
			console.log(error);
		}else{
			console.log(response.statusCode);
			createContentInstance();
			console.log(body);
			setInterval(function() {
				createContentInstance();
			}, config.sleepTime);
		}
	});
}

function createContentInstance(){
	console.log("\n▶▶▶▶▶");
	var image= base64_encode(config.imageFile)
	var representation = {
		"m2m:cin":{
			"con": "{\"cameraid\":\""+config.cameraId+"\",\"timestamp\":\""+Date.now()+"\", \"base64image\":\""+image+"\"}"
		}
	};

	console.log("POST "+config.cseUri+"/"+config.aeName+"/"+config.cntName);
	console.log(representation);

	var options = {
		uri: config.cseUri+"/"+config.aeName+"/"+config.cntName,
		method: "POST",
		headers: {
			"X-M2M-Origin": config.aeId,
			"X-M2M-RI": "123456",
			"Content-Type": "application/json;ty=4"
		},
		json: representation
	};

	request(options, function (error, response, body) {
		console.log("◀◀◀◀◀");
		if(error){
			console.log(error);
		}else{
			console.log(response.statusCode);
			console.log(body);
		}
	});
}

function base64_encode(file) {
    var image = fs.readFileSync(file);
    return new Buffer(image).toString('base64');
}
+12 −0
Original line number Diff line number Diff line
{
   "cseUri": "http://127.0.0.1:8080/~/server/server",
   "aeId": "Cae-camera",
   "aeName": "camera",
   "cntName": "images",
   "cntMni": 100,
   "appId": "app.company.com",
   "cameraId": "CAM123456",
   "sleepTime": 5000,
   "imageFile": "./image.jpg"
}

camera-agent/image.jpg

0 → 100644
+7.94 KiB
Loading image diff...
+66.6 KiB
Loading image diff...