Commit 137c951b authored by nextime's avatar nextime

Remove unused files in zwave daemon

parent d8d37a69
This source diff could not be displayed because it is too large. You can view the blob instead.
from openzwave.scene import ZWaveScene
from openzwave.controller import ZWaveController
from openzwave.network import ZWaveNetwork
from openzwave.option import ZWaveOption
import time, sys
DEVICE="/dev/ttyUSB0"
options = ZWaveOption(DEVICE, config_path="/home/domotika/plugins/zwave/openzwave/",
user_path="/home/domotika/plugins/zwave/user", cmd_line="")
options.set_log_file("/home/domotika/logs/OZW_Log.log")
options.set_append_log_file(False)
options.set_console_output(False)
#options.set_logging("Debug")
options.set_logging("Error")
options.lock()
#Create a network object
network = ZWaveNetwork(options, log=None)
time_started = 0
print "------------------------------------------------------------"
print "Waiting for network awaked : "
print "------------------------------------------------------------"
for i in range(0,300):
if network.state>=network.STATE_AWAKED:
print(" done")
print("Memory use : %s Mo" % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024.0))
break
else:
sys.stdout.write(".")
sys.stdout.flush()
time_started += 1
time.sleep(1.0)
if network.state<network.STATE_AWAKED:
print "."
print "Network is not awake but continue anyway"
print "------------------------------------------------------------"
print "Use openzwave library : %s" % network.controller.ozw_library_version
print "Use python library : %s" % network.controller.python_library_version
print "Use ZWave library : %s" % network.controller.library_description
print "Network home id : %s" % network.home_id_str
print "Controller node id : %s" % network.controller.node.node_id
print "Controller node version : %s" % (network.controller.node.version)
print "Nodes in network : %s" % network.nodes_count
print "------------------------------------------------------------"
print "Waiting for network ready : "
print "------------------------------------------------------------"
for i in range(0,300):
if network.state>=network.STATE_READY:
print " done in %s seconds" % time_started
break
else:
sys.stdout.write(".")
time_started += 1
#sys.stdout.write(network.state_str)
#sys.stdout.write("(")
#sys.stdout.write(str(network.nodes_count))
#sys.stdout.write(")")
#sys.stdout.write(".")
sys.stdout.flush()
time.sleep(1.0)
if not network.is_ready:
print "."
print "Network is not ready but continue anyway"
print "------------------------------------------------------------"
print "Controller capabilities : %s" % network.controller.capabilities
print "Controller node capabilities : %s" % network.controller.node.capabilities
print "Nodes in network : %s" % network.nodes_count
print "Driver statistics : %s" % network.controller.stats
print "------------------------------------------------------------"
for node in network.nodes:
print
print "------------------------------------------------------------"
print "%s - Name : %s" % (network.nodes[node].node_id,network.nodes[node].name)
print "%s - Manufacturer name / id : %s / %s" % (network.nodes[node].node_id,network.nodes[node].manufacturer_name, network.nodes[node].manufacturer_id)
print "%s - Product name / id / type : %s / %s / %s" % (network.nodes[node].node_id,network.nodes[node].product_name, network.nodes[node].product_id, network.nodes[node].product_type)
print "%s - Version : %s" % (network.nodes[node].node_id, network.nodes[node].version)
print "%s - Command classes : %s" % (network.nodes[node].node_id,network.nodes[node].command_classes_as_string)
print "%s - Capabilities : %s" % (network.nodes[node].node_id,network.nodes[node].capabilities)
print "%s - Neigbors : %s" % (network.nodes[node].node_id,network.nodes[node].neighbors)
print "%s - Can sleep : %s" % (network.nodes[node].node_id,network.nodes[node].can_wake_up())
groups = {}
for grp in network.nodes[node].groups :
groups[network.nodes[node].groups[grp].index] = {'label':network.nodes[node].groups[grp].label, 'associations':network.nodes[node].groups[grp].associations}
print "%s - Groups : %s" % (network.nodes[node].node_id, groups)
values = {}
for val in network.nodes[node].values :
values[network.nodes[node].values[val].object_id] = {
'label':network.nodes[node].values[val].label,
'help':network.nodes[node].values[val].help,
'command_class':network.nodes[node].values[val].command_class,
'max':network.nodes[node].values[val].max,
'min':network.nodes[node].values[val].min,
'units':network.nodes[node].values[val].units,
'data':network.nodes[node].values[val].data_as_string,
'ispolled':network.nodes[node].values[val].is_polled
}
#print "%s - Values : %s" % (network.nodes[node].node_id, values)
#print "------------------------------------------------------------"
for cmd in network.nodes[node].command_classes:
print " --------- "
#print "cmd = ",cmd
values = {}
for val in network.nodes[node].get_values_for_command_class(cmd) :
values[network.nodes[node].values[val].object_id] = {
'label':network.nodes[node].values[val].label,
'help':network.nodes[node].values[val].help,
'max':network.nodes[node].values[val].max,
'min':network.nodes[node].values[val].min,
'units':network.nodes[node].values[val].units,
'data':network.nodes[node].values[val].data,
'data_str':network.nodes[node].values[val].data_as_string,
'genre':network.nodes[node].values[val].genre,
'type':network.nodes[node].values[val].type,
'ispolled':network.nodes[node].values[val].is_polled,
'readonly':network.nodes[node].values[val].is_read_only,
'writeonly':network.nodes[node].values[val].is_write_only,
}
print "%s - Values for command class : %s : %s" % (network.nodes[node].node_id,
network.nodes[node].get_command_class_as_string(cmd),
values)
print "------------------------------------------------------------"
print
print "------------------------------------------------------------"
print "Driver statistics : %s" % network.controller.stats
print "------------------------------------------------------------"
print
print "------------------------------------------------------------"
print "Try to autodetect nodes on the network"
print "------------------------------------------------------------"
print "Nodes in network : %s" % network.nodes_count
print "------------------------------------------------------------"
print "Retrieve switches on the network"
print "------------------------------------------------------------"
values = {}
for node in network.nodes:
for val in network.nodes[node].get_switches() :
print("node/name/index/instance : %s/%s/%s/%s" % (node,network.nodes[node].name,network.nodes[node].values[val].index,network.nodes[node].values[val].instance))
print(" label/help : %s/%s" % (network.nodes[node].values[val].label,network.nodes[node].values[val].help))
print(" id on the network : %s" % (network.nodes[node].values[val].id_on_network))
print(" state: %s" % (network.nodes[node].get_switch_state(val)))
print "------------------------------------------------------------"
print "Retrieve dimmers on the network"
print "------------------------------------------------------------"
values = {}
for node in network.nodes:
for val in network.nodes[node].get_dimmers() :
print("node/name/index/instance : %s/%s/%s/%s" % (node,network.nodes[node].name,network.nodes[node].values[val].index,network.nodes[node].values[val].instance))
print(" label/help : %s/%s" % (network.nodes[node].values[val].label,network.nodes[node].values[val].help))
print(" id on the network : %s" % (network.nodes[node].values[val].id_on_network))
print(" level: %s" % (network.nodes[node].get_dimmer_level(val)))
print "------------------------------------------------------------"
print "Retrieve sensors on the network"
print "------------------------------------------------------------"
values = {}
for node in network.nodes:
for val in network.nodes[node].get_sensors() :
print("node/name/index/instance : %s/%s/%s/%s" % (node,network.nodes[node].name,network.nodes[node].values[val].index,network.nodes[node].values[val].instance))
print(" label/help : %s/%s" % (network.nodes[node].values[val].label,network.nodes[node].values[val].help))
print(" id on the network : %s" % (network.nodes[node].values[val].id_on_network))
print(" value: %s %s" % (network.nodes[node].get_sensor_value(val), network.nodes[node].values[val].units))
print "------------------------------------------------------------"
print "Retrieve switches all compatibles devices on the network "
print "------------------------------------------------------------"
values = {}
for node in network.nodes:
for val in network.nodes[node].get_switches_all() :
print("node/name/index/instance : %s/%s/%s/%s" % (node,network.nodes[node].name,network.nodes[node].values[val].index,network.nodes[node].values[val].instance))
print(" label/help : %s/%s" % (network.nodes[node].values[val].label,network.nodes[node].values[val].help))
print(" id on the network : %s" % (network.nodes[node].values[val].id_on_network))
print(" value / items: %s / %s" % (network.nodes[node].get_switch_all_item(val), network.nodes[node].get_switch_all_items(val)))
print(" state: %s" % (network.nodes[node].get_switch_all_state(val)))
print "------------------------------------------------------------"
print "Retrieve protection compatibles devices on the network "
print "------------------------------------------------------------"
values = {}
for node in network.nodes:
for val in network.nodes[node].get_protections() :
print("node/name/index/instance : %s/%s/%s/%s" % (node,network.nodes[node].name,network.nodes[node].values[val].index,network.nodes[node].values[val].instance))
print(" label/help : %s/%s" % (network.nodes[node].values[val].label,network.nodes[node].values[val].help))
print(" id on the network : %s" % (network.nodes[node].values[val].id_on_network))
print(" value / items: %s / %s" % (network.nodes[node].get_protection_item(val), network.nodes[node].get_protection_items(val)))
print "------------------------------------------------------------"
print "------------------------------------------------------------"
print "Retrieve battery compatibles devices on the network "
print "------------------------------------------------------------"
values = {}
for node in network.nodes:
for val in network.nodes[node].get_battery_levels() :
print("node/name/index/instance : %s/%s/%s/%s" % (node,network.nodes[node].name,network.nodes[node].values[val].index,network.nodes[node].values[val].instance))
print(" label/help : %s/%s" % (network.nodes[node].values[val].label,network.nodes[node].values[val].help))
print(" id on the network : %s" % (network.nodes[node].values[val].id_on_network))
print(" value : %s" % (network.nodes[node].get_battery_level(val)))
print "------------------------------------------------------------"
print "------------------------------------------------------------"
print "Retrieve power level compatibles devices on the network "
print "------------------------------------------------------------"
values = {}
for node in network.nodes:
for val in network.nodes[node].get_power_levels() :
print("node/name/index/instance : %s/%s/%s/%s" % (node,network.nodes[node].name,network.nodes[node].values[val].index,network.nodes[node].values[val].instance))
print(" label/help : %s/%s" % (network.nodes[node].values[val].label,network.nodes[node].values[val].help))
print(" id on the network : %s" % (network.nodes[node].values[val].id_on_network))
print(" value : %s" % (network.nodes[node].get_power_level(val)))
print "------------------------------------------------------------"
#print
#print "------------------------------------------------------------"
#print "Activate the switches on the network"
#print "Nodes in network : %s" % network.nodes_count
#print "------------------------------------------------------------"
#for node in network.nodes:
# for val in network.nodes[node].get_switches() :
# print("Activate switch %s on node %s" % \
# (network.nodes[node].values[val].label,node))
# network.nodes[node].set_switch(val,True)
# print("Sleep 10 seconds")
# time.sleep(10.0)
# print("Dectivate switch %s on node %s" % \
# (network.nodes[node].values[val].label,node))
# network.nodes[node].set_switch(val,False)
#print "%s" % ('Done')
#print "------------------------------------------------------------"
print
print "------------------------------------------------------------"
print "Driver statistics : %s" % network.controller.stats
print "Driver label : %s" % \
network.controller.get_stats_label('retries')
#print "------------------------------------------------------------"
#print
#print "------------------------------------------------------------"
#print "Stop network"
#print "------------------------------------------------------------"
#network.stop()
#!/usr/bin/env python
from twisted.internet import epollreactor
epollreactor.install()
from twisted.internet import reactor
from twisted.application import service, internet, app, reactors
from twisted.spread import pb
from twisted.manhole.telnet import ShellFactory
import sys
from dmlib.daemonizer import Daemonizer
from logging import handlers as loghandlers
import logging
import sys
from datetime import datetime
import base64, os
import subprocess
from openzwave.scene import ZWaveScene
from openzwave.controller import ZWaveController
from openzwave.network import ZWaveNetwork
from openzwave.option import ZWaveOption
from louie import dispatcher, All
import json
import pickle
import time
BASEDIR=os.path.abspath(os.path.dirname(os.path.abspath(sys.argv[0]))+"/../../")
CONFIGDIR=os.path.join(BASEDIR, "conf")
CURDIR=os.path.abspath(os.path.dirname(os.path.abspath(sys.argv[0])))
DEVICE="/dev/ttyUSB0"
loglevels = {
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL,
'debug': logging.DEBUG
}
#LOGLEN=104857600 # 100 mega
#LOGLEN=10485760 # 10 mega
LOGLEN=26214400 # 25 mega
class ZWaveDaemon(Daemonizer):
def __init__(self):
Daemonizer.__init__(self, BASEDIR+'/run/zwaved.pid')
def main_loop(self):
application = service.Application("ZWaved")
ZWaveService=ZWaveServer()
#serviceCollection = service.IServiceCollection(application)
#ZWaveService.setServiceParent(serviceCollection)
zpb=ZWavePB(ZWaveService)
shell_factory = ShellFactory()
shell_factory.username = 'admin'
shell_factory.password = 'domotika'
shell_factory.namespace['domotika'] = ZWaveService
reactor.listenTCP(4041, shell_factory, interface="127.0.0.1")
reactor.listenTCP(8800, pb.PBServerFactory(zpb), interface="127.0.0.1")
reactor.run()
class ZWavePB(pb.Root):
clients=[]
def __init__(self, zwserver):
self.zwserver = zwserver
self.zwserver.pb = self
def remote_setClient(self, client):
self.clients.append(client)
def remote_ZWaveNetwork(self, call, *a, **kw):
cs=call.split(".")
log.debug("Remote ZWaveNetwork called "+str(cs))
f=getattr(self.zwserver.network, cs[0], None)
if len(cs)>1:
for i in cs[1:]:
f=getattr(f, i, None)
if not f:
return None
if f and callable(f):
return f(*a, **kw)
return f
def notifyClients(self, msg, *a, **kw):
log.debug("Client notify "+str(msg))
for client in self.clients:
try:
client.callRemote("zwevent", msg, *a, **kw)
except:
self.clients.remove(client)
#class ZWaveServer(service.Service):
class ZWaveServer(object):
def __init__(self):
options = ZWaveOption(DEVICE, config_path=BASEDIR+"/daemons/zwave/openzwave/",
user_path=BASEDIR+"/daemons/zwave/user", cmd_line="")
options.set_log_file(BASEDIR+"/logs/OZW_Log.log")
options.set_append_log_file(False)
options.set_console_output(False)
#options.set_logging("Debug")
options.set_logging("Error")
options.lock()
self.options = options
self.starttime=time.time()
#Create a network object
self.network = ZWaveNetwork(options, log=None)
log.info("Waiting for network awaked...")
dispatcher.connect(self.waitForNetworkWakeUp, ZWaveNetwork.SIGNAL_NETWORK_AWAKED)
dispatcher.connect(self.waitForNetworkReady, ZWaveNetwork.SIGNAL_NETWORK_READY)
def waitForNetworkWakeUp(self, network):
self.pb.notifyClients("network.wakeup")
log.info('Network is wakedup in '+str(time.time()-self.starttime)+' seconds. Waiting for network ready...')
def waitForNetworkReady(self, network):
self.pb.notifyClients("network.ready")
log.info('Network is ready in '+str(time.time()-self.starttime)+' seconds')
self.zwaveStarted()
def zwaveStarted(self):
dispatcher.connect(self.node_update, ZWaveNetwork.SIGNAL_NODE)
dispatcher.connect(self.node_value_update, ZWaveNetwork.SIGNAL_VALUE)
network=self.network
log.info("Network home id : %s" % network.home_id_str)
log.info("Controller node id : %s" % network.controller.node.node_id)
log.info("Nodes in network : %s" % network.nodes_count)
for node in network.nodes:
log.info("Node %s - Name : %s" % (network.nodes[node].node_id,network.nodes[node].name))
log.info("Node %s - Manufacturer name / id : %s / %s" % (network.nodes[node].node_id,network.nodes[node].manufacturer_name, network.nodes[node].manufacturer_id))
log.info("Node %s - Product name / id / type : %s / %s / %s" % (network.nodes[node].node_id,network.nodes[node].product_name, network.nodes[node].product_id, network.nodes[node].product_type))
def node_update(self, network, node):
log.debug("node."+str(node)+".updated")
self.pb.notifyClients("node."+str(node)+".updated")
def node_value_update(self, network, node, value):
log.debug("node."+str(node)+":VALUE:"+str(value))
self.pb.notifyClients("node."+pickle.dumps(node)+":VALUE:"+pikle.dumps(value))
if __name__ == '__main__':
formatter = logging.Formatter('%(asctime)s => %(name)-12s: %(levelname)-8s %(message)s')
logdict={"corelog": {"file":"zwaved.log","name":[("ZWave","general")]}}
for l in logdict.keys():
logdict[l]["handler"] = loghandlers.RotatingFileHandler(
BASEDIR+'/logs/'+logdict[l]["file"], 'a', LOGLEN, 5)
logdict[l]["handler"].setLevel(logging.DEBUG)
logdict[l]["handler"].setFormatter(formatter)
logging.basicConfig()
log = logging.getLogger( 'ZWave' )
log.addHandler(logdict["corelog"]["handler"])
log.setLevel( logging.INFO )
if len(sys.argv) > 1:
log.debug("Starting daemon with option "+sys.argv[1])
if sys.argv[1]=='debug':
log.setLevel( logging.DEBUG )
ZWaveDaemon().process_command_line(sys.argv)
else:
print 'Please specify start, stop or debug option'
from twisted.internet import reactor, stdio
from twisted.spread import pb
import pickle
import sys
try:
from domotika.plugins import BasePlugin
except:
sys.path.append("/home/domotika/")
from domotika.plugins import BasePlugin
from dmlib.utils.genutils import configFile
from domotika.db import dmdb
from dmlib import dmdomain
from datetime import datetime
import base64, os
import subprocess
import time
NAME="ZWavePlugin"
VERSION=0.1
DESCRIPTION=""
AUTHOR="Franco (nextime) Lanza"
COPYRIGHT=""
LICENSE=""
PLUGINENGINE=0.1
BASEDIR=os.path.abspath(os.path.dirname(os.path.abspath(sys.argv[0]))+"/../../")
CONFIGDIR=os.path.join(BASEDIR, "conf")
class ReconnectingPBFactory(pb.PBClientFactory):
def clientConnectionLost(self, connector, reason, reconnecting=1):
print "Reconnecting..."
reconnecting=1
return pb.PBClientFactory.clientConnectionLost(self, connector, reason, reconnecting)
class ZWavePlugin(BasePlugin, pb.Referenceable):
def _checkPing(self):
pass
def pluginStart(self):
factory = ReconnectingPBFactory()
factory.getRootObject().addCallback(self.gotZWave)
reactor.connectTCP("localhost", 8800, factory)
def gotZWave(self, zw):
zw.callRemote("setClient", self)
def print_remote(res):
print res
zw.callRemote("ZWaveNetwork", "state").addCallback(print_remote)
zw.callRemote("ZWaveNetwork", "controller.ozw_library_version").addCallback(print_remote)
def remote_zwevent(self, event):
print str(event)
#print pickle.loads(event)#
return 'remoteCalled zwevent'
if __name__ == '__main__':
plugin=ZWavePlugin()
stdio.StandardIO(plugin)
reactor.run()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment