目的:

1. 通过网页读取watchdog的信息

2. 通过网页设置watchdog

准备工作:

1. 选择一个web框架,选用 cherrypy

$ sudo apt-get install python-cherrypy3

2. 熟悉 RESTFUL , 参考 RESR_API(Mark McLoughlin) redhat  REST API 指导

步骤:

我们选择了一个cherrypy作为web框架。 cherrypy的部署简单。

这只是个demo,没有实现MVC,大家自己练习。

此外也没有实现模板,距离一个正式的网站还差的很远。

界面实现如下:

代码如下:

代码中,喂狗代码采用了一个线程。需要改进。

发现不是很好,因为每个wsgi的请求都是一个线程。

最好是采用cherrypy提供的background, 或者将该逻辑独立为一个单例模式。

watchdog.py

     import cherrypy
import json
import time
import threading index_html = """
<html>
<head>
<style type="text/css">
.watchdog-state {
display: inline-block;
height: 16px;
width: 16px;
border-radius: 8px;
margin-left: 10px;
margin-right: 10px;
}
.up {
background: linear-gradient(to bottom, #BFD255 0%%, #8EB92A 50%%,
#72AA00 51%%, #9ECB2D 100%%) repeat scroll 0 0 transparent;
} .down {
background: linear-gradient(to bottom, #AFAFAF 0%%, #AFAFAF 50%%,
#AFAFAF 51%%, #AFAFAF 100%%) repeat scroll 0 0 transparent;
}
</style>
</head> <body>
<script type="text/javascript">
function requestJSON(url, suc, done, err) {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", url, true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.setRequestHeader("dataType", "application/json");
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var responseContext = xmlhttp.responseText;
var jsonobj=eval('('+responseContext+')');
suc(jsonobj);
} else{
// alert("readyState: " + xmlhttp.readyState + "status" + xmlhttp.status);
}
}
xmlhttp.send();
} function operationWatchdog()
{
// alert("operationWatchdog");
var url = "watchdog/deactivate";
var action = document.getElementById("watchdog-action");
if (action.innerHTML == "Activate"){
url = "watchdog/activate";
}
requestJSON(url, function(data){
// alert(data.value);
});
if (action.innerHTML == "Activate"){
action.innerHTML = "Deactivate";
} else {
action.innerHTML = "Activate";
}
var status = document.getElementById("watch-status");
if (status.className.match("up$")){
status.className = "watchdog-state down";
} else{
status.className = "watchdog-state up";
}
}
function FeedWatchod()
{
// alert("FeedWatchod");
var url = "watchdog/feedstop";
var feed = document.getElementById("feed-watchdog");
if (feed.innerHTML == "Start"){
url = "watchdog/feedstart";
}
requestJSON(url, function(data){
//alert(data.value);
});
if (feed.innerHTML == "Start"){
feed.innerHTML = "Stop";
} else {
feed.innerHTML = "Start";
}
}
</script>
<div>
Status of watchdog: <span id="watch-status" class="watchdog-state %s"> </span>
<button id="watchdog-action" type="button" onclick="operationWatchdog()">%s</button>
</div>
<div>
Feed watchdog:
<button id="feed-watchdog" type="button" onclick="FeedWatchod()">Start</button>
</div>
</body>
</html> """ def jsonObj(data):
cherrypy.response.headers['Content-Type'] = 'application/json;charset=utf-8'
return json.dumps(data, indent=2, separators=(',', ':')) watchdog_hadle = open("/dev/cwd_demo", "w+")
def watchdogStatus():
vals = watchdog_hadle.readlines();
return vals[0] == ['B\x01\n'] def watchdogAction(action):
watchdog_hadle.write(action)
watchdog_hadle.flush() class WelcomePage(object):
def __init__(self):
self.watchdog = Watchdog() def index(self):
watchdog_state = "down"
watchdog_action = "Activate"
if watchdogStatus():
watchdog_state = "up"
watchdog_action = "Deactivate"
a = index_html % (watchdog_state, watchdog_action)
print a
return a
index.exposed = True def default(*args, **kwargs):
data = {"hello": "world"}
return jsonObj(data)
default.exposed = True class Watchdog(object):
exposed = True
feed_running = False
feed_thread = None def __init__(self):
pass @classmethod
def worker(cls):
while cls.feed_running:
print "worker"
watchdogAction('t')
time.sleep(1)
return @cherrypy.expose
def index(self, *args, **kwargs):
data = {"value": False}
data = {"value": watchdogStatus()}
return jsonObj(data) @cherrypy.tools.accept(media='application/json')
@cherrypy.expose
def activate(self, *args, **kwargs):
watchdogAction('a')
print "activate"
raise cherrypy.InternalRedirect("/watchdog") @cherrypy.tools.accept(media='application/json')
@cherrypy.expose
def deactivate(self, *args, **kwargs):
watchdogAction('d')
print "deactivate"
raise cherrypy.InternalRedirect("/watchdog") @cherrypy.tools.accept(media='application/json')
@cherrypy.expose
def feedstart(self, *args, **kwargs):
watchdogAction('t')
print "feed start"
if self.__class__.feed_thread == None:
self.__class__.feed_thread = threading.Thread(
target=self.__class__.worker)
if self.__class__.feed_running == False:
self.__class__.feed_running = True
self.__class__.feed_thread.start()
raise cherrypy.InternalRedirect("/watchdog") @cherrypy.expose
def feedstop(self, *args, **kwargs):
print "feed stop"
if self.__class__.feed_thread.isAlive():
self.__class__.feed_running = False
self.__class__.feed_thread.join(1.5)
self.__class__.feed_thread = None
raise cherrypy.InternalRedirect("/watchdog") if __name__ == '__main__':
# CherryPy always starts with app.root when trying to map request URIs
# to objects, so we need to mount a request handler root. A request
# to '/' will be mapped to HelloWorld().index().
cherrypy.quickstart(WelcomePage())
else:
# This branch is for the test suite; you can ignore it.
cherrypy.tree.mount(WelcomePage())

[虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(八)的更多相关文章

  1. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(九)

    目的 1. 使用verilog/vhdl设计一个PCI的watchdog设备. 2. 通过systemverilog 写testbench. 很久之前研究过AC97的verilog代码.但是很久没用v ...

  2. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(四)

    通过前面的操作,我们已经可以创建一个带有我们自己的PCI的watchdog外设qemu 虚拟机了. 目的: 1. 了解我们的外设情况. 2. 为在guest中开发我们自己的linux PCI驱动程序做 ...

  3. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(一)

    目的: 结合现在比较流行的技术,通过一个demo 展示一个全栈式设计的各种技能. 一个全栈式的工程师,应该能设计通过verilog/VHDL做logical设计.能写内核驱动,能架站. 要熟悉veri ...

  4. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(六)

    目的: 1. 为我们自己的watchdog写一个驱动 步骤: 通过之前的介绍,我们很容易猜想到写我们基于PCI的watchdog驱动,可以分2个步骤. 1. 探测加载PCI设备 这部分代码跟我们的设备 ...

  5. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(二)

    这篇文章的理解,需要一些专业知识了. 我们可以创建模拟自己的外设吗? 我们已经知道什么是qemu了,我们可以通过qmeu的提供的外设,DIY一个计算机了. 但是我们可能还不满足,我们可以自己制造一个外 ...

  6. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(五)

    目的: 1. 了解PCI的基本知识,为完成watchdog的设备做准备. 准备知识: 简单的说,PCI 设备分3个空间. 配置空间,IO空间,内存地址空间. PCI设备厂家决定了外设是使用IO空间还是 ...

  7. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(七)

    目标: 1. 完成最终的设备驱动,增加具体的watchdog设备操作的代码. 测试代码: 代码最终实现见cwd_demo.c 代码只实现了read与write.  没有实现ioctl. 因此,我们可以 ...

  8. [虚拟化/云][全栈demo] 为qemu增加一个PCI的watchdog外设(三)

    我们已经设计了一个基于qemu的watchdog了.下一步工作就是创建一个含有我们的watchdog的虚拟计算机器了. 准备工作: 1. 使用virt-manager或者virsh创建一个虚拟机器. ...

  9. 从零开始的全栈工程师——利用CSS3画一个正方体 ( css3 )

    transform属性 CSS3的变形(transform)属性让元素在一个坐标系统中变形.transform属性的基本语法如下: transform:none | <transform-fun ...

随机推荐

  1. Node.js包管理器:

    Node.js包管理器: 当我们要把某个包作为工程运行的一部分时,通过本地模式获取,如果要在命令行下使用,则使用全局模式安装 使用全局模式安装的包并不能直接在JavaScript文件中用require ...

  2. Android显示YUV图像

    需要流畅显示YUV图像需要使用Opengl库调用GPU资源,网上在这部分的资料很少.实际上Android已经为我们提供了相关的Opengl方法 主体过程如下: 1.建立GLSurfaceView 2. ...

  3. 论山寨手机与Android联姻 【3】手机是怎样生产出来的

    要说清楚MTK在商业模式上有什么优势,以及Android对于MTK未来的手机开发会有什么影响,首先得了解手机从设计,开发到生产的整个过程.让我们先来看看手机的生产过程.在生产制造环节,山寨手机和正牌手 ...

  4. 浅析C++内存分配与释放操作过程——三种方式可以分配内存new operator, operator new,placement new

    引言:C++中总共有三种方式可以分配内存,new operator, operator new,placement new. 一,new operator 这就是我们最常使用的 new 操作符.查看汇 ...

  5. javascript圆形排列

    显示效果如下: 需要用到的知识: 等于半径长的圆弧所对的圆心角叫做1弧度的角,用符号rad表示,读作弧度.用弧度作单位来度量角的制度叫做弧度制.另外一种度量角的方法是角度制.弧度制的精髓就在于统一了度 ...

  6. 在OpenCV中利用鼠标绘制矩形和截取图像的矩形区域

    这是两个相关的程序,前者是后者的基础.实际上前一个程序也是在前面博文的基础上做的修改,请参考<在OpenCV中利用鼠标绘制直线> .下面贴出代码. 程序之一,在OpenCV中利用鼠标绘制矩 ...

  7. POJ 2976 Dropping tests(二分答案)

    [题目链接]  http://poj.org/problem?id=2976 [题目大意] 给出每门成绩的总分和得分,去除k门成绩之后 使得剩余的成绩分数和除以总分得到的数字最大,要求精度在三位小数之 ...

  8. cf-公式专场

    A. Again Twenty Five! time limit per test 0.5 seconds memory limit per test 64 megabytes input stand ...

  9. 软交所--微软将对IE浏览器进行关键性安全更新

    微软于当地时间周四宣布下周二,即本月的"补丁星期二"推送九个安全升级. 当中最重要的就是解决IE浏览器远程运行代码(RCE)漏洞,这个漏洞影响从IE6至IE11全版本号,全部Win ...

  10. jcenter那些事儿

    jcenter是一个server托管在bintray.com的maven仓库. in project's build.gradle file allprojects { repositories { ...