BaseService开发- nodeJS 客户端

36 篇文章 21 订阅
16 篇文章 0 订阅

  本文介绍基于nodejs 的App如何访问modular2edge 上的baseservice ,modular2edge 是我们正在开发的基于RK3399 的工业物联网边缘设备。它基于ubuntu OS ,我们在其上采用了容器技术,并且在linux上开发了基础服务层baseservice。如果你对modular2Edge,BaseService 不了解,请看本人以往的博文。

   为了验证baseService 的运行效率和适应性,我们开发了各种类型的App,本文是nodeJS 的App 演示程序称为 bs-client。

Ubuntu上安装 nodeJS

sudo apt-get update
sudo apt-get install nodejs

安装NPM

sudo apt-get install npm

安装expressJS

npm install express 

安装 ws 插件

npm install ws

node JS 代码

var express = require('express');
var app = express();
var fs = require('fs');
const WebSocket = require('ws');
var multer  = require('multer');
var http = require('http').Server(app);
 var bodyParser = require('body-parser');
 var jsonParser= bodyParser.json();
 uploadSingle = multer({
    dest: 'uploadDockerImage'
});
var  outgoing_id=0;
var  flipflop_interval;
var analog_id; 
var analog_on;
app.use("/css", express.static(__dirname + '/css'));
app.use("/js", express.static(__dirname + '/js'));
app.use("/img", express.static(__dirname + '/images'));
app.use("/views", express.static(__dirname + '/views'));
app.use("/uploadImage", express.static(__dirname + '/uploadImage'));
app.use(bodyParser.json({limit:'1mb'}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true }));
app.get('/index', function(req, res) {
   res.sendfile('./views/index.html');
});
app.post("/baseservice",function(req, res){
     
    var method=Number(req.body.method);
    var pin=Number(req.body.params.pin);
    var board=Number(req.body.params.board);
    console.log("method:"+method+"pin:"+pin+"board:"+board); 
    sendCommand(method,pin,board);
	var result = {result:"OK"};
	res.send(result);
});
const ws = new WebSocket("ws://localhost:2019/iosocket");
ws.on('open', function open() {
   console.log("connect");
  });
   
  ws.on('message', function incoming(data) {
    
          
    var messageBody=JSON.parse(data);
    console.log(messageBody);
  });
http.listen(8000, function(){
  console.log('listening on 8000');
});
 
function get_outgoing_id()
{
  outgoing_id++;
  if (outgoing_id>5000) outgoing_id=0;
  return outgoing_id;
}
function sendCommand (method,pin,board)
{   var jsonrpc;
    var address=pin+board*256;
   switch (method)
   {
     case 1:{
           console.log("digitalWrite");
          jsonrpc={
            "jsonrpc":"2.0",
            "method":"digitalOut.write",
            "params":{"pin":address,"value":1},
            "id": get_outgoing_id()
          }
          ws.send(JSON.stringify(jsonrpc));
       break;
     }
     case 2:{ 
      console.log("digitalWrite");
       jsonrpc={
            "jsonrpc":"2.0",
            "method":"digitalOut.write",
            "params":{"pin":address,"value":0},
            "id": get_outgoing_id()
          }

          ws.send(JSON.stringify(jsonrpc));
       break;
     }
     case 3:{ 
      flipflop_interval =  setInterval(function(){
        var jsonrpc={
            "jsonrpc":"2.0",
            "method":"digitalOut.flipflop",
            "params":{pin:address},
            "id": get_outgoing_id()
           };
           ws.send(JSON.stringify(jsonrpc));
          },500);  
        
       break;
     }
     case 4:{ 
      clearInterval(flipflop_interval);
       break;
     }
     case 5:{ 
        var  id= get_outgoing_id();
        analog_id=id;
        analog_on=1;
          jsonrpc={
            "jsonrpc":"2.0",
            "method":"analog.start",
            "params":{"pin":address,"value":1},
            "id": id
          }
          ws.send(JSON.stringify(jsonrpc));
       break;
     }
     case 6:{
         analog_on=0; 
          jsonrpc={
            "jsonrpc":"2.0",
            "method":"analog.start",
            "params":{"pin":address,"value":0,"id":analog_id},
            "id":get_outgoing_id()
             }
             ws.send(JSON.stringify(jsonrpc));
       break;
     }
     case 7:{ 
          jsonrpc={
            "jsonrpc":"2.0",
            "method":"register",
            "params":{name:method_name,type:1},
            "id": get_outgoing_id()
           }
           ws.send(JSON.stringify(jsonrpc));
       break;
     }
     case 8:{ 
          jsonrpc={
            "jsonrpc":"2.0",
            "method":"sum",
            "params":[600,2],
            "id": get_outgoing_id()
           }
           ws.send(JSON.stringify(jsonrpc));
       break;
     }
   }
}

index.html 

<!DOCTYPE html>
<html lang="en" dir="ltr">
 <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>baseService Client</title>
  <link href="/css/bootstrap.css" rel="stylesheet">
  <script src="/js/jquery.js"></script>
  <script>
  function sendCommand()
  {
  var board=parseInt($('#board').val());
 var method=parseInt($('#method').val());
 var pin=parseInt($('#pin').val()); 
 var jsonrpc={
  "jsonrpc":"2.0",
  "method":method,
  "params":{"pin":pin,"board":board},
  "id": 1
 }
console.log(jsonrpc);
 $.ajax({ type: "POST",
 url: "/baseservice", 
 contentType: "application/x-www-form-urlencoded", 
 data: jsonrpc , 
 dataType: "json", 
 success:function (message) { console.log(JSON.stringify(message)); }, 
 error:function (message) { console.log("error"+JSON.stringify(message)); } });
  }
  </script>
   </head>
  <body class="container">
  <div class="jumbotron text-info" >
  <h1 >BaseService NodeJS Client</h1>
  <p>An microkernel for industry App</p>
  <p><a class="btn btn-primary btn-lg" href="*" role="button">了解更多</a></p>
  </div>  
  <div class="container">
    <h2>测试</h2>
  <form >
      <div class="form-group col-xs-6" >
          <label for="board">模块:</label>
          <select class="selectpicker col-md-4" id="board">
            <option value="0">IO module 1</option>
            <option value="1">IO module 2</option>
          </select>
        </div>
        <div class="form-group col-xs-6 " >
            <label for="board">操作:</label>
            <select class="selectpicker col-md-4" id="method" >
              <option value="1">LED On</option>
              <option value="2">LED off</option>
              <option value="3">LED FlipFlop On</option>
              <option value="4">LED FlipFlop off</option>
              <option value="5">ADC On</option>
              <option value="6">ADC off</option>
              <option value="7">register method</option>
              <option value="8">sum</option>
            </select>
          </div>
          <div class="form-group col-xs-6 " >
              <label for="board">地址:</label>
              <select class="selectpicker col-md-4"  id="pin" >
                <option value="1">1</option>
                <option value="2">2</option>
              </select>
            </div>
          
          </form>
          <p id="result"></p>
          <p id="delay"></p>
          <button class="btn btn-primary btn-lg" onclick="sendCommand()">发送</button>
        </div>  
  </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值