Contents

PowerShell on Windows system uses OpenAPI interface

Powershell It is a command line script environment that runs on Windows machines to realize system and application management automation. In the Windows operating system, click Start->Run->Enter PowerShell to enter Windows PowerShell.

一、Use the curl command to read the real-time temperature and humidity of the device

Example

curl http://t-api.tzonedigital.cn/openapi/terminaldata/realtime/2814?access_token=40aeb04bcba14f41a5e90a0df940600a -method get

二、Use the curl command to read the historical temperature and humidity of the device

Example

curl http://t-api.tzonedigital.cn/openapi/terminaldata/history/2814?access_token=40aeb04bcba14f41a5e90a0df940600a -method get



Terminal on Mac system uses OpenAPI interface

Terminal is a command line scripting environment that comes with Mac operating system. The terminal is an application in the system, and you can find it directly in all programs.

一、Use the curl command to read the real-time temperature and humidity of the device

Example

curl http://t-api.tzonedigital.cn/openapi/terminaldata/realtime/2814?access_token=40aeb04bcba14f41a5e90a0df940600a

二、Use the curl command to read the historical temperature and humidity of the device

Example

curl http://t-api.tzonedigital.cn/openapi/terminaldata/history/2814?access_token=40aeb04bcba14f41a5e90a0df940600a



Terminal on Ubuntu system uses OpenAPI interface

The terminal Linux operating system comes with a command line script environment. The terminal is an application in the system, and you can find it directly in all programs.

一、Use the curl command to read the real-time temperature and humidity of the device

Example

curl http://t-api.tzonedigital.cn/openapi/terminaldata/realtime/2814?access_token=40aeb04bcba14f41a5e90a0df940600a

二、Use the curl command to read the historical temperature and humidity of the device

Example

curl http://t-api.tzonedigital.cn/openapi/terminaldata/history/2814?access_token=40aeb04bcba14f41a5e90a0df940600a



WPF (C#) uses OpenAPI interface

WPF (Windows Presentation Foundation) is a Windows-based user interface framework launched by Microsoft for application development.

一、Read the real-time temperature and humidity of the device

Example

string url = "http://t-api.tzonedigital.cn/openapi/terminaldata/realtime/2814?access_token=40aeb04bcba14f41a5e90a0df940600a";
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse res;

try
{
    res = (HttpWebResponse)hwr.GetResponse();
}
catch
{
    return;
}

if (res.StatusCode == HttpStatusCode.OK)
{
    using (Stream mystream = res.GetResponseStream())
    {
        using (StreamReader reader = new StreamReader(mystream, Encoding.UTF8))
        {
            string json = reader.ReadToEnd();
            if (Convert.ToInt32(JObject.Parse(json)["Status"].ToString()) == 1)
            {
                JObject jobj = JObject.Parse(JObject.Parse(json)["Content"].ToString());
                double temperature = Convert.ToDouble(jobj["Temperature"].ToString());
                double humidity = Convert.ToDouble(jobj["Humidity"].ToString());
                long serverTime = Convert.ToInt64(jobj["ServerTime"].ToString());

                this.Dispatcher.Invoke(new Action(() => {
                    labTemperature1.Content = temperature + "℃";
                    labHumidity1.Content = humidity + "%";
                    labServerTime1.Content = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)).AddMilliseconds(serverTime).ToString("yyyy/MM/dd HH:mm:ss");    
                }));
            }
        }
    }
}

Download the sample program

WPF_Example.zip



Android App Use OpenAPI interface

一、Read the real-time temperature and humidity of the device

Example

String url = "http://t-api.tzonedigital.cn/openapi/terminaldata/realtime/2814?access_token=40aeb04bcba14f41a5e90a0df940600a";
// Generate request object
HttpGet httpGet = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();

// Send request
try
{
    HttpResponse response = httpClient.execute(httpGet);
    if(response == null)
        return;
    HttpEntity httpEntity = response.getEntity();
    try {
        InputStream inputStream = httpEntity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                inputStream));
        String result = "";
        String line = "";
        while (null != (line = reader.readLine()))
            result += line;

        JSONObject jobj  = new JSONObject(new JSONObject(result).getString("Content"));
        final String imei = jobj.getString("IMEI");
        final String name = jobj.getString("ProductName");
        final double temperature = jobj.getDouble("Temperature");
        final double humidity = jobj.getDouble("Humidity");
        final long serverTime = jobj.getLong("ServerTime");

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                txtName1.setText(name + "("+imei+")");
                txtTemperature1.setText(temperature + "℃");
                txtHumidity1.setText(humidity + "%");
                Date d = new Date();
                d.setTime(serverTime);
                SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                txtDateTime1.setText(df.format(d));
            }
        });

    }catch (Exception e){}
}
catch (Exception ex){}

Download the sample program

Android_Example.zip



Receive “Data Forwarding” HTTP protocol example

HTTP protocol is the abbreviation of Hyper Text Transfer Protocol (Hyper Text Transfer Protocol), which is a transfer protocol used to transfer hypertext from a World Wide Web (WWW: World Wide Web) server to a local browser.


C# Example

string ip = string.Empty;
foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
{
    if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
    {
        ip = _IPAddress.ToString();
    }
}
int port = 80;
HttpListener httpListener = new HttpListener();
httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
httpListener.Prefixes.Add(string.Format("http://" +ip + "{0}/", port == 80 ? "" : ":" + port));
httpListener.Start();

bool isRunning = true;
new Task(() =>
{
    while (isRunning)
    {
        try
        {
            //Block the main function until a client request is received
            HttpListenerContext httpListenerContext = httpListener.GetContext();
            //Block the main function until a client request is received
            HttpListenerRequest request = httpListenerContext.Request;
            //Get request path
            string url = httpListenerContext.Request.RawUrl.ToString().ToLower();
            string[] urlPath = url.Replace("//", "/").Trim('/').Split('/');
            //Get request data
            StreamReader reader = new StreamReader(request.InputStream);
            string input = reader.ReadToEnd();
            reader.Close();

            string log = "Request Url:" + url + "\r\n";
            log += "Request Data:" + input + "\r\n";

            httpListenerContext.Response.StatusCode = 200;
            using (StreamWriter writer = new StreamWriter(httpListenerContext.Response.OutputStream))
            {
                string strHtml = "";
                strHtml = "OK";
                writer.WriteLine(strHtml);
            }
        }
        catch (Exception) { }
    }
}).Start();

Download the sample program

HTTP_Example.zip



Receive “data forwarding” TCP/IP Sokect example

Socket network communication programming API interface is an encapsulation of the transport layer protocol based on TCP/IP.


C# Example

int port = 10064;
//Listening port
TcpListener tcpListener = new TcpListener(new IPEndPoint(IPAddress.Any, port));
tcpListener.Start();
bool isRunning = true;
Console.WriteLine("Listener (" + port + ") was started");

new Task(() =>
{
    while (isRunning)
    {
        //Blocking function, waiting for the client to connect
        TcpClient tcpClient = tcpListener.AcceptTcpClient();
        Console.WriteLine("A new connected! remote:" + tcpClient.Client.RemoteEndPoint.ToString());
        //Get network flow operation object
        NetworkStream netStream = tcpClient.GetStream();
        while (isRunning)
        {
            if (tcpClient.Connected && netStream.DataAvailable)
            {
                byte[] buffer = null;
                buffer = new byte[tcpClient.Available];
                //Get the client to send data
                netStream.Read(buffer, 0, buffer.Length);
                string hexString = BitConverter.ToString(buffer, 0, buffer.Length).Replace("-", "");
                string asciiString = Encoding.ASCII.GetString(buffer, 0, buffer.Length).Trim('\0');
                Console.WriteLine("hexString => " + hexString);
                Console.WriteLine("asciiString => " + asciiString);
            }
        }
    }
}).Start();

Download the sample program

Sokect_Example.zip