自定义weatherMap数据源之URL

2 篇文章 0 订阅
2 篇文章 0 订阅
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:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值