def start(): r = redis.Redis(host="127.0.0.1", port=6379, db=0) # load the plugins for every device plugin_manager = PluginManager() plugin_manager.LoadAllPlugin() plugins = {} for device in r.lrange("devices", 0, -1): protocol = r.get("protocol:%s" % device) plugin = plugin_manager.GetPluginByName(protocol.title()) host = r.get("ip:%s" % device) port = r.get("port:%s" % device) plugin.init(host, port) plugins[device] = plugin while True: print 'in start' for device in r.lrange("devices", 0, -1): addr = r.get("addr:%s" % device) for spot in r.lrange("spots:%s" % device, 0, -1): command = r.get("device:%s:spot:%s:%s" % (device, spot[1:], 'command')) cmd_param = r.get("device:%s:spot:%s:%s" % (device, spot[1:], 'cmd_param')) cmd_length = r.get("device:%s:spot:%s:%s" % (device, spot[1:], 'cmd_length')) ratio = r.get("device:%s:spot:%s:%s" % (device, spot[1:], 'ratio')) value = plugins[device].get(addr, command, cmd_param, cmd_length) if value is None: print 'device:%s:spot:%s get error' % (device, spot) continue if spot[0] == 'A': value_type = r.get("device:%s:spot:%s:%s" % (device, spot[1:], 'value_type')) precision = r.get("device:%s:spot:%s:%s" % (device, spot[1:], 'precision')) min_value = r.get("device:%s:spot:%s:%s" % (device, spot[1:], 'min_value')) max_value = r.get("device:%s:spot:%s:%s" % (device, spot[1:], 'max_value')) value, status = plugins[device].transformAnalog(value, ratio, value_type, precision, min_value, max_value) elif spot[0] == 'D': mapper = r.get("device:%s:spot:%s:%s" % (device, spot[1:], 'mapper')) value, status = plugins[device].transformDigit(value, ratio, mapper) r.set("device:%s:spot:%s:value" % (device, spot[1:]), value) r.set("device:%s:spot:%s:status" % (device, spot[1:]), status) time.sleep(2) """
def LoadAllPlugin(): PluginManager.LoadAllPlugin()
reload(sys) sys.setdefaultencoding('utf-8') global author author = 1 @itchat.msg_register([itchat.content.TEXT]) def wechat_onmessage(msg): if msg['Type'] == TEXT: output = "" try: if msg['User'] and u'\u6ce5\u4eba\u5f20' != msg['User']['NickName']: return for SingleModel in __ALLMODEL__: plugins = SingleModel.GetPluginObject() for item in plugins: item.Start(msg['Content'], author) except Exception, e: exstr = traceback.format_exc() print exstr print "sth error" if __name__ == "__main__": PluginManager.LoadAllPlugin() itchat.auto_login(enableCmdQR=2, hotReload=True) author = itchat.search_friends(nickName=r'泥人张')[0] itchat.run()