summaryrefslogtreecommitdiff
path: root/test/html-based-panel/server.js
blob: 487096b19dd2e2f4dcd402216d660f7a232ecf90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* @licence app begin@
* SPDX-License-Identifier: MPL-2.0
*
* \copyright Copyright (C) 2016, PCA Peugeot Citroen
*
* \file main.cpp
*
* \brief This file is part of the Navigation Web API proof of concept.
*
* \author Philippe Colliot <philippe.colliot@mpsa.com>
*
* \version 0.1
*
* This Source Code Form is subject to the terms of the
* Mozilla Public License (MPL), v. 2.0.
* If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* For further information see http://www.genivi.org/.
*
* List of changes:
* <date>, <name>, <description of change>
*
* @licence end@
*/
// Requirement of external JS files
var resource = require('./js/resource.js');

// Requirements of nodejs modules
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var gcontext = require('gcontext');
var python_shell = require('python-shell');
var Enum = require('enum');

// Configuration of the python script for simulating log replayer data
var python_script_enum = new Enum({'START': 1, 'INITIALIZATION': 2, 'HIGH_TANK_LEVEL': 3, 'LOW_TANK_LEVEL': 4});
var python_script_options = {
  mode: 'text',
  pythonPath: '/usr/bin/python3',
  pythonOptions: ['-u'],
  scriptPath: '../script',
  args: [python_script_enum.get('START').key]
};

// Requirements of LBS add-on modules
var positioningEnhancedPositionWrapper = require(resource.generatedNodejsModulePath+'/PositioningEnhancedPositionWrapper');
var fuelStopAdvisorWrapper = require(resource.generatedNodejsModulePath+'/FuelStopAdvisorWrapper');

// Create instances
var i_positioningEnhancedPositionWrapper = new positioningEnhancedPositionWrapper.PositioningEnhancedPositionWrapper();
var i_fuelStopAdvisorWrapper = new fuelStopAdvisorWrapper.FuelStopAdvisorWrapper();

// Create and init server
var port = 8080;
var hostname = '127.0.0.1';
var server = http.createServer(function(req, res) {
    var page = url.parse(req.url).pathname;
    var full_path = path.join(process.cwd(),page);
    // Check if page exists (for the moment only index.html)
    fs.exists(full_path,function(exists){
        if(!exists){
            res.writeHeader(404, {"Content-Type": "text/plain"});
            res.write("404 Not Found\n");
            res.end();
        }
        else{
            fs.readFile(full_path, "binary", function(err, file) {
                 if(err) {
                     res.writeHeader(500, {"Content-Type": "text/plain"});
                     res.write(err + "\n");
                     res.end();

                 }
                 else{
                    res.writeHeader(200);
                    res.write(file, "binary");
                    res.end();
                }
            });
        }
    });
});

// Launch server
server.listen(port);

// Load socket.io and connect it to the server
var io = require('socket.io').listen(server);

// Manage socket for the namespace /simulation
var socket_simulation = io.of('/simulation');
// signals
function positionUpdate(changedValues) {
    console.log('positionUpdate: ' + changedValues);
    socket_simulation.emit('positioning_signal', {signal: 'positionUpdate', data: changedValues});
}
var setPositionUpdateListener = i_positioningEnhancedPositionWrapper.setPositionUpdateListener(positionUpdate);
function tripDataUpdated(changedValues) {
    console.log('tripDataUpdated: ' + changedValues);
    socket_simulation.emit('demonstrator_signal', {signal: 'tripDataUpdated', data: changedValues});
}
var setTripDataUpdatedListener = i_fuelStopAdvisorWrapper.setTripDataUpdatedListener(tripDataUpdated);
function fuelStopAdvisorWarning(changedValues) {
    console.log('fuelStopAdvisorWarning: ' + changedValues);
    socket_simulation.emit('demonstrator_signal', {signal: 'fuelStopAdvisorWarning', data: changedValues});
}
var setFuelStopAdvisorWarningListener = i_fuelStopAdvisorWrapper.setFuelStopAdvisorWarningListener(fuelStopAdvisorWarning);
function tripDataResetted(changedValues) {
    console.log('tripDataResetted: ' + changedValues);
    socket_simulation.emit('demonstrator_signal', {signal: 'tripDataResetted', data: changedValues});
}
var setTripDataResettedListener = i_fuelStopAdvisorWrapper.setTripDataResettedListener(tripDataResetted);

// Start the gmainloop (to be done after the initialisation of listeners !
gcontext.init();

// connection
socket_simulation.on('connection', function (client) {
    console.log('Client connected');
    client.on('positioning_request', function (message) {
        switch(message.interface) {
        case "PositioningEnhancedPosition":
            console.log('Message received: Interface-->' + message.interface +' Method-->', message.method +' Parameters-->' + message.parameters);
            if (message.method in i_positioningEnhancedPositionWrapper && typeof i_positioningEnhancedPositionWrapper[message.method] === "function") {
                var data = i_positioningEnhancedPositionWrapper[message.method](message.parameters);
                if(data) {
                    client.emit('positioning_answer', {request: message.method, answer: data});
                }
            }
            else {
                console.log("Could not find " + message.method + " function");
                client.emit('feedback', "Could not find " + message.method + " function");
            }
            break;
        default:
            console.log("Could not find " + message.interface);
            client.emit('feedback', "Could not find " + message.interface);
        }
    });
    client.on('demonstrator_request', function (message) {
        switch(message.interface) {
        case "FuelStopAdvisor":
            console.log('Message received: Interface-->' + message.interface +' Method-->', message.method +' Parameters-->' + message.parameters);
            if (message.method in i_fuelStopAdvisorWrapper && typeof i_fuelStopAdvisorWrapper[message.method] === "function") {
                var data = i_fuelStopAdvisorWrapper[message.method](message.parameters);
                if(data) {
                    console.log('data' + data);
                    client.emit('demonstrator_answer', {request: message.method, answer: data});
                }
            }
            else {
                console.log("Could not find " + message.method + " function");
                client.emit('feedback', "Could not find " + message.method + " function");
            }
            break;
        default:
            console.log("Could not find " + message.interface);
            client.emit('feedback', "Could not find " + message.interface);
        }
    });
    client.on('panel_request', function (message) {
        switch(message.request) {
        case "LogReplayerScenario":
            console.log('Message received: Request-->', message.request +' Parameters-->' + message.parameters);
            python_script_options.args = [message.parameters];
            break;
        default:
            console.log("Could not find " + message.interface);
            client.emit('feedback', "Could not find " + message.interface);
        }
    });
});

// Timer
setInterval(function(){
    python_shell.run('simulation-launch.py', python_script_options, function (err, results) {
        if (err) throw err;
//        console.log('results: %j', results);
        });
    console.log('Refresh simulated values')
}, 1000);

// Log info
console.log('Server listening at: %s', server.address().port);