当前位置: 首页 > 工具软件 > WeatherMap > 使用案例 >

自定义weatherMap数据源之URL

鄂慈
2023-12-01
WeatherMap  使用http连接当数据源
把WeatherMapDataSource_url.php文件放入lib/datasources文件夹下

数据格式:URL:url
url 返回数据格式如下
{
    "in": 1000000,
    "out": 20000000,
}

例如URL:http://172.16.1.97/data.html

使用http://172.16.1.97/data.html返回的数据当数据源


我的配置文件如下

# Automatically generated by php-weathermap v0.98

FONTDEFINE 100 docs/example/Vera.ttf 10

WIDTH 1400
HEIGHT 1000
HTMLSTYLE overlib
KEYFONT 100
TITLE beijing
TIMEPOS 676 17 Created: %b %d %Y %H:%M:%S

KEYPOS DEFAULT 73 100 Traffic Load
KEYTEXTCOLOR 0 0 0
KEYOUTLINECOLOR 0 0 0
KEYBGCOLOR 255 255 255
BGCOLOR 255 255 255
TITLECOLOR 0 0 0
TIMECOLOR 0 0 0
SCALE DEFAULT 0    0    192 192 192
SCALE DEFAULT 0    1    255 255 255
SCALE DEFAULT 1    10   140   0 255
SCALE DEFAULT 10   25    32  32 255
SCALE DEFAULT 25   40     0 192 255
SCALE DEFAULT 40   55     0 240   0
SCALE DEFAULT 55   70   240 240   0
SCALE DEFAULT 70   85   255 192   0
SCALE DEFAULT 85   100  255   0   0

SET nowarn_clipping 1

# End of global section


# TEMPLATE-only NODEs:
NODE DEFAULT
        MAXVALUE 100


# TEMPLATE-only LINKs:
LINK DEFAULT
        WIDTH 4
        BWLABEL bits
        BANDWIDTH 200M


# regular NODEs:
NODE node00352fish
        LABEL Node
        ICON images/router_up.gif
        POSITION 427 118

NODE node08813
        LABEL Node
        ICON images/router_up.gif
        POSITION 392 265


# regular LINKs:
LINK node00352fish-node08813
        INFOURL /images/router_up.gif
        OVERLIBGRAPH /images/router_up.gif
        TARGET URL:http://172.16.2.123/data.html
        NODES node00352fish node08813



# That's All Folks!

http://172.16.2.123/data.html 返回Json字符串如下

{
    "in": 1000000,
    "out": 20000000,
}

执行下面命令生成html或png文件

./weathermap  --config configs/url.conf  --htmloutput url.html   
./weathermap  --config configs/url.conf  --output url.png  

插件WeatherMapDataSource_url.php代码如下php版本要求5.4

<?php
// Sample Pluggable datasource for PHP Weathermap 0.9
// - read a pair of values from a database, and return it

// TARGET URL:url

class WeatherMapDataSource_url extends WeatherMapDataSource {

    function Init(&$map)
    {
        if(! function_exists("curl_init") ) return FALSE;
        if(! function_exists("curl_exec") ) return FALSE;

        return(TRUE);
    }

    function Recognise($targetstring)
    {
        if(preg_match("/^URL:(http.*)$/",$targetstring, $matches))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }

    function ReadData($targetstring, &$map, &$item)
    {
        $data[IN] = NULL;
        $data[OUT] = NULL;
        $data_time = 0;

        if(preg_match("/^URL:(http.*)$/",$targetstring,$matches))
        {
            $url = $matches[1];

            wm_warn($url . "\n");
            $con = curl_init($url);
            curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($con, CURLOPT_HEADER, FALSE);
            curl_setopt($con, CURLOPT_NOBODY, FALSE);
            $body = curl_exec($con);
            if ($body === false) {
                wm_warn("curl error \n");
                curl_close($con);
                return;
            }
            $arr = json_decode($body, true);
            $data[IN] = $arr["in"];
            $data[OUT] = $arr["out"];
            $data_time = time();
            curl_close($con);
        }

        wm_debug ("URL ReadData: Returning (".($data[IN]===NULL?'NULL':$data[IN]).",".($data[OUT]===NULL?'NULL':$data[IN]).",$data_time)\n");

        return( array($data[IN], $data[OUT], $data_time) );
    }
}

// vim:ts=4:sw=4:


 类似资料: