#!/usr/bin/env python # -*- coding: utf-8 -*- """ demeter web name:pub.py author:rabin """ from demeter.mqtt import * from demeter.tcp import * import ConfigParser import random import time import base64 """ pub = Pub() key = 'status/valve/10086/100107' value = 'ok' state = pub.push(key, value) """ pub = Pub() key = 'control/airdryer/10011/1001151' value = '0' state = pub.push(key, value) print state """ def callback(param, client, userdata, mid): print mid pub = Pub() key = 'update/test/222/222' value = 2 state = pub.push(key, value, 2, callback=callback, param='ttt') print state """ def push(): # 模拟传感器设备数据 gateway = 10086 def device(min): max = min + 9 return str(random.randint(min, max)) def val(min, max): return str(round(random.uniform(min, max), 2)) def switch(): return str(random.randint(0, 1)) hard = 'sensor' pubType = ( {'hard':'sensor','type':'temprature','device':100000,'value':(-20, 30)} ,{'hard':'sensor','type':'humidity','device':100010,'value':(30, 80)} ,{'hard':'sensor','type':'light','device':100020,'value':(3000, 13000)} ,{'hard':'power','type':'power','device':100090,'value':(1, 50)} ,{'hard':'control','type':'valve','device':100100} ,{'hard':'control','type':'blower','device':100110} ,{'hard':'power','type':'power','device':10086,'value':(1, 50)} ) pub = Pub() while True: config = Demeter.temp() for v in pubType: if v['device'] == gateway: id = v['device'] else: id = device(v['device']) key = v['hard']+'/'+v['type']+'/'+str(gateway)+'/'+str(id) if 'value' in v: value = val(v['value'][0], v['value'][1]) else: value = config[v['type']][id] print 'key:' + key + ' value:' + value pub.push(key, value) time.sleep(1) def pic(): gateway = '10086' pub = Pub() key = 'pic/pic_' gateway = '/' + gateway + '/200001' with open('front/static/pc/images/376a00e6.tmp2.jpg', 'rb') as f: data = f.readlines() total = len(data) key = key + str(total) + '_' i = 0 print key for value in data: k = key + str(i) + gateway pub.push(k, value) print k i = i+1 #push() #pic() def tcp_sensor(): # 模拟设备数据 gateway = '10086' def device(): return str(random.randint(100000, 100010)) def val(): return str(random.randint(1, 50)) hard = 'sensor' pubType = ('temperature', 'humidity') client = Client('0.0.0.0', 8000) while True: for v in pubType: key = hard+'/'+v+'/'+gateway+'/'+device() value = val() print 'key:' + key + ' value:' + value client.send(key+'|:|'+value) time.sleep(1) def tcp_pic(): gateway = '10086' client = Client('0.0.0.0', 8000) key = 'pic/' key = 'pic/pic_' gateway = '/' + gateway + '/200001' with open('admin/static/images/banner.png', 'rb') as f: data = f.readlines() total = len(data) key = key + str(total) + '_' i = 0 #print key for value in data: k = key + str(i) + gateway #print value #print value print k client.send(k + '|:|' +value) #print k i = i+1 client.close() #tcp_sensor() #tcp_pic()