forked from community/product-iots
parent
201ff62664
commit
954341a90a
@ -0,0 +1,195 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
"""
|
||||||
|
import getpass
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from optparse import OptionParser
|
||||||
|
from urllib import urlopen
|
||||||
|
|
||||||
|
import sleekxmpp
|
||||||
|
# Python versions before 3.0 do not use UTF-8 encoding
|
||||||
|
# by default. To ensure that Unicode is handled properly
|
||||||
|
# throughout SleekXMPP, we will set the default encoding
|
||||||
|
# ourselves to UTF-8.
|
||||||
|
if sys.version_info < (3, 0):
|
||||||
|
from sleekxmpp.util.misc_ops import setdefaultencoding
|
||||||
|
setdefaultencoding('utf8')
|
||||||
|
else:
|
||||||
|
raw_input = input
|
||||||
|
|
||||||
|
from sleekxmpp.plugins.xep_0323.device import Device
|
||||||
|
|
||||||
|
class IoT_TestDevice(sleekxmpp.ClientXMPP):
|
||||||
|
|
||||||
|
"""
|
||||||
|
A simple IoT device that can act as server or client
|
||||||
|
"""
|
||||||
|
def __init__(self, jid, password):
|
||||||
|
sleekxmpp.ClientXMPP.__init__(self, jid, password)
|
||||||
|
self.add_event_handler("session_start", self.session_start)
|
||||||
|
self.add_event_handler("message", self.message)
|
||||||
|
self.device=None
|
||||||
|
self.releaseMe=False
|
||||||
|
self.beServer=True
|
||||||
|
self.clientJID=None
|
||||||
|
|
||||||
|
def datacallback(self,from_jid,result,nodeId=None,timestamp=None,fields=None,error_msg=None):
|
||||||
|
"""
|
||||||
|
This method will be called when you ask another IoT device for data with the xep_0323
|
||||||
|
se script below for the registration of the callback
|
||||||
|
"""
|
||||||
|
logging.info("we got data %s from %s",str(result),from_jid)
|
||||||
|
if(result=="fields"):
|
||||||
|
logging.info("===========Fields===========")
|
||||||
|
logging.info(fields)
|
||||||
|
logging.info("============================")
|
||||||
|
|
||||||
|
if len(fields) > 0:
|
||||||
|
logging.info("Name\tType\tUnit\tValue")
|
||||||
|
for field in fields:
|
||||||
|
logging.info(field["name"] + "\t" + field["typename"] + "\t" + field["unit"] + "\t" +field["value"])
|
||||||
|
print field["value"]
|
||||||
|
logging.info("============================")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
def beClientOrServer(self,server=True,clientJID=None ):
|
||||||
|
self.beServer=False
|
||||||
|
self.clientJID=clientJID
|
||||||
|
|
||||||
|
def testForRelease(self):
|
||||||
|
# todo thread safe
|
||||||
|
return self.releaseMe
|
||||||
|
|
||||||
|
def doReleaseMe(self):
|
||||||
|
# todo thread safe
|
||||||
|
self.releaseMe=True
|
||||||
|
|
||||||
|
def addDevice(self, device):
|
||||||
|
self.device=device
|
||||||
|
|
||||||
|
def session_start(self, event):
|
||||||
|
self.send_presence()
|
||||||
|
self.get_roster()
|
||||||
|
# tell your preffered friend that you are alive
|
||||||
|
#self.send_message(mto='jocke@jabber.sust.se', mbody=self.boundjid.bare +' is now online use xep_323 stanza to talk to me')
|
||||||
|
|
||||||
|
logging.debug("----------|Trying to connect|-----------");
|
||||||
|
session=self['xep_0323'].request_data(self.boundjid.full,self.clientJID,self.datacallback,flags={"momentary": "true"})
|
||||||
|
|
||||||
|
def message(self, msg):
|
||||||
|
if msg['type'] in ('chat', 'normal'):
|
||||||
|
logging.debug("got normal chat message" + str(msg))
|
||||||
|
ip=urlopen('http://icanhazip.com').read()
|
||||||
|
msg.reply("Hi I am " + self.boundjid.full + " and I am on IP " + ip).send()
|
||||||
|
else:
|
||||||
|
logging.debug("got unknown message type %s", str(msg['type']))
|
||||||
|
|
||||||
|
class TheDevice(Device):
|
||||||
|
"""
|
||||||
|
This is the actual device object that you will use to get information from your real hardware
|
||||||
|
You will be called in the refresh method when someone is requesting information from you
|
||||||
|
"""
|
||||||
|
def __init__(self,nodeId):
|
||||||
|
Device.__init__(self,nodeId)
|
||||||
|
self.counter=0
|
||||||
|
|
||||||
|
def refresh(self,fields):
|
||||||
|
"""
|
||||||
|
the implementation of the refresh method
|
||||||
|
"""
|
||||||
|
#self._set_momentary_timestamp(self._get_timestamp())
|
||||||
|
self.counter+=1
|
||||||
|
#self._add_field_momentary_data(self, "Temperature", self.counter)
|
||||||
|
|
||||||
|
self._add_field(name="Temperature", typename="numeric", unit="C")
|
||||||
|
self._set_momentary_timestamp(self._get_timestamp())
|
||||||
|
self._add_field_momentary_data("Temperature", str(self.counter), flags={"automaticReadout": "true"})
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
# Setup the command line arguments.
|
||||||
|
#
|
||||||
|
# This script can act both as
|
||||||
|
# "server" an IoT device that can provide sensorinformation
|
||||||
|
# python IoT_TestDevice.py -j "serverjid@yourdomain.com" -p "password" -n "TestIoT" --debug
|
||||||
|
#
|
||||||
|
# "client" an IoT device or other party that would like to get data from another device
|
||||||
|
|
||||||
|
optp = OptionParser()
|
||||||
|
|
||||||
|
# Output verbosity options.
|
||||||
|
optp.add_option('-q', '--quiet', help='set logging to ERROR',
|
||||||
|
action='store_const', dest='loglevel',
|
||||||
|
const=logging.ERROR, default=logging.INFO)
|
||||||
|
optp.add_option('-d', '--debug', help='set logging to DEBUG',
|
||||||
|
action='store_const', dest='loglevel',
|
||||||
|
const=logging.DEBUG, default=logging.INFO)
|
||||||
|
optp.add_option('-v', '--verbose', help='set logging to COMM',
|
||||||
|
action='store_const', dest='loglevel',
|
||||||
|
const=5, default=logging.INFO)
|
||||||
|
optp.add_option('-t', '--pingto', help='set jid to ping',
|
||||||
|
action='store', type='string', dest='pingjid',
|
||||||
|
default=None)
|
||||||
|
|
||||||
|
# JID and password options.
|
||||||
|
optp.add_option("-j", "--jid", dest="jid",
|
||||||
|
help="JID to use")
|
||||||
|
optp.add_option("-p", "--password", dest="password",
|
||||||
|
help="password to use")
|
||||||
|
|
||||||
|
# IoT test
|
||||||
|
optp.add_option("-c", "--sensorjid", dest="sensorjid",
|
||||||
|
help="Another device to call for data on", default=None)
|
||||||
|
|
||||||
|
opts, args = optp.parse_args()
|
||||||
|
|
||||||
|
# Setup logging.
|
||||||
|
logging.basicConfig(level=opts.loglevel,
|
||||||
|
format='%(levelname)-8s %(message)s')
|
||||||
|
|
||||||
|
if opts.jid is None:
|
||||||
|
#opts.jid = raw_input("Username: ")
|
||||||
|
opts.jid = "admin@192.168.57.129/raspi"
|
||||||
|
if opts.password is None:
|
||||||
|
opts.password = "admin"
|
||||||
|
#opts.password = getpass.getpass("Password: ")
|
||||||
|
if opts.sensorjid is None:
|
||||||
|
opts.sensorjid = "1hrud08yceu01@192.168.57.129/raspi"
|
||||||
|
#opts.sensorjid = getpass.getpass("Sensor JID: ")
|
||||||
|
|
||||||
|
xmpp = IoT_TestDevice(opts.jid,opts.password)
|
||||||
|
xmpp.register_plugin('xep_0030')
|
||||||
|
#xmpp['xep_0030'].add_feature(feature='urn:xmpp:iot:sensordata',
|
||||||
|
# node=None,
|
||||||
|
# jid=None)
|
||||||
|
xmpp.register_plugin('xep_0323')
|
||||||
|
xmpp.register_plugin('xep_0325')
|
||||||
|
|
||||||
|
if opts.sensorjid:
|
||||||
|
logging.debug("will try to call another device for data")
|
||||||
|
xmpp.beClientOrServer(server=False,clientJID=opts.sensorjid)
|
||||||
|
xmpp.connect()
|
||||||
|
xmpp.process(block=True)
|
||||||
|
logging.debug("ready ending")
|
||||||
|
|
||||||
|
else:
|
||||||
|
print "noopp didn't happen"
|
Loading…
Reference in new issue