跳转至

HTTP上报


概述

演示HTTP上报,第一次先获取Token,第二次上报数据。


代码


var server = "iot.easelive.cn";
var port = 80;
var unitCode = "easelive";
var regCode = "0123456789ABCDEF";
var equid = "DG02000001";

var tokenId = "";

var report_period = 30 * 60 * 1000; //发送周期

//获取Token
function getToken()
{
    print("getToken");
    var params = "unitCode=" + unitCode + "&regCode=" + regCode;
    var http = Packet.HTTP.packet("POST", server, "/authTokenId", params);
    Net.send(0, http);
    Timer.start(2, 20000);
}
function timeString()
{
    var t = Box.getTime();
    return Coder.num(t.year, "%04d") + "-" + Coder.num(t.month, "%02d") + "-" + Coder.num(t.day, "%02d") +
                    " " + Coder.num(t.hour, "%02d") + ":" + Coder.num(t.minute, "%02d") + ":" + Coder.num(t.second, "%02d");
}
//上报数据
function report()
{
    print("report");
    var params = "tokenId=" + tokenId + "&datajson=";
    var data = {ud: timeString(), equid:equid, rc: "0.02"};
    params += JSON.stringify(data);


    var http = Packet.HTTP.packet("POST", server, "/swimmingwaterUp", params);
    Net.send(0, http);
    Timer.start(2, 20000);
}
//处理服务器返回的数据
function httpCB(data)
{
    print("receive",JSON.stringify(data));
    if(data.code == 200)
    {
        var body = JSON.parse(data.body);
        if(body)
        {
            if(body.tokenId)
            {
                Timer.stop(2);
                tokenId = body.tokenId;
                report();
            }
            if(body.type)
            {
                Timer.stop(2);
                print(body.type);
                if(body.type == "02")
                    getToken();
                else
                {
                    Net.close(0);
                    Timer.start(1, report_period);
                }
            }
        }
    }
}
var httpParser = Packet.HTTP.Parser.create(httpCB);

function netCB(m)
{
    switch(m.event)
    {
        case Net.READY:
            Net.syncTime();
            Timer.start(1, 1000);
            break;
        case Net.CONN_OK:
            if(tokenId == "") getToken();
            else report();
            break;
        case Net.CONN_FAIL:
            Net.connect(0, server, port);//网络连接失败重新连接并重启下次连接定时器
            break;  
        case Net.CONN_CLOSE:
            break;
        case Net.RECV:
            httpParser.parse(m.data);//解析服务器下发数据包
            break;
    }
}


function timerCB(id)
{
    switch(id)
    {
        case 1: //连接网络
            Net.connect(0, server, port);
            Timer.start(1, report_period);
            break;
        case 2: //请求超时
            print("Timeout");
            Net.close(0);
            Timer.start(1, report_period);
    }
}

//初始化硬件,程序从这里开始跑
GPIO.set(GPIO.POWER, 1);
Net.init();

while(true)
{
    var m = Event.get();
    switch(m.msg)
    {
        case Event.NET_EVENT:
            netCB(m);   //网络事件处理
            break;
        case Event.TIMER_OUT:
            timerCB(m.id);  //定时器处理
            break;
        case Event.UART_RECV:           
            break;
        case Event.PIO_CHANGE:
            break;
    }
}

下载代码(右键另存为):http.js