From 0c6bc2e400f35e53484fef8be770126d36fa3a22 Mon Sep 17 00:00:00 2001 From: Shabir Mohamed Date: Thu, 20 Aug 2015 15:05:17 +0530 Subject: [PATCH] changes to implement XMPP communication --- .../firealarm/sketch/RaspberryAgent.zip | Bin 13111 -> 13106 bytes .../impl/FireAlarmControllerService.java | 276 ++++++++++++------ .../distribution/src/scripts/xmpp_client.py | 123 ++++---- 3 files changed, 250 insertions(+), 149 deletions(-) diff --git a/modules/distribution/src/samples/firealarm/sketch/RaspberryAgent.zip b/modules/distribution/src/samples/firealarm/sketch/RaspberryAgent.zip index 3823a6c82ad0d23f9fb1989e88fdcbf9af27b7cd..7c529b586e1557c1ed5ab5990b27abc9a9bbce95 100644 GIT binary patch delta 2269 zcmV<32qO2lX0m1sP)h>@3IG5A004}VkqjjU4_(w%k9)B<$tDAFk`s|iAd?6)4+snZ zk9*WrU3-%{CmnyqHfd-a@BogR@pv?gw7@2kR^45ha69d9?>Q?83GBpf=a#WYX!pF| zhu6RB=2~?ZBJJ|@0kW-G%e3_xj@}$WC?k=s&LB6-qo3)dVAdQ484K}I-gMv-S6avvPW&Et1mKicPW#V9Vk>h<*cMW0AlDpe2n|aS zbKu`Xo*57+s+450U}?x+pivIz*xR8lw8WL8>Iv>O+2 zx_dRc7><8u!y}Zq!njFet~DtBk6b8Jcd-SQp-3THprA3^fKZhugt)IvCGK z!x>Dba4{KQj^?Au7~kH(VEh?AjK-H8;Ak)8!@qxJiZnnm1=+In;f(V=5tgzLNoPD1 zOA(@K=_+R{4lDV`7|C6@-OS%%mhiO58bm|aMtsTg*!VD)c%ySgMq&n|Kp>cP)EYI9(Mg#? z_A1^YqFF1Muhud*Fe3&3{_*;{0~-oi%plE;HA+dl$x-G?^Z0>l3LPtt-x5o*f?o{)OT(XPSTH{dWxAx_`OgGi$s4T;=Gb%3cdz7+|7`>q2*3gLVo- z|7~<>IYQHjKcH*S_3xyb79v@y4NQMb9*H@L8N^uxnk{Q#6*sv`4-~5DMsivm{ue|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-z=rI}2b`XP-laURI?KN^4L3A-Fy z1hZ*hefB(546~+CV^KJcV#WRk(qI>i<7ZF&8f2jd|2;r^k%ys*1I=S#`brfBc{Ji6|Sj$uZB8jz0>oP9{&Ale)`9Y zv)32ry+;W=w~jiv=DvS+o>|2zHVrRR+}zjULVoj#mff#xUu_)yO1cjH{*}y8DP+(; z?X?dG`;V+_`vt59-GAAx>9@618qZ=MEoSYbN!zI1;lT@w70NWlS?XxwEbcNTL!9Qk zMT`r^krWo{(P8i!*B!Vl$YSa`hdAX=fkXSC%*NfHKb)^0D_USR4i0H_Q&?$Utc70r z``CNFTSy%8Ml>?09C=9v6S`_U-(hp4K(%IYnp6c1Zxp~m0R29=K|tLs2+EzHXl 0: logging.info("Name\t\tType\tValue\tUnit") for field in fields: - logging.info(" - " + field["name"] + "\t" + field["typename"] + "\t" + field["value"] + "\t" + field["unit"]) + logging.info(" - " + field["name"] + "\t" + field["typename"] + "\t" + field[ + "value"] + "\t" + field["unit"]) + + if self.requestType in ("/" + field["name"].upper() + "/."): + print field["value"] + logging.info('-' * PRINT_HEADER_LENGTH) self.disconnect() - print field["value"] def testForRelease(self): # todo thread safe @@ -95,20 +102,20 @@ class IoT_TestDevice(sleekxmpp.ClientXMPP): def doReleaseMe(self): # todo thread safe - self.releaseMe=True + self.releaseMe = True def addDevice(self, device): - self.device=device + self.device = device def session_start(self, event): self.send_presence() self.get_roster() # tell your preffered friend that you are alive using generic xmpp chat protocol - #self.send_message(mto='jocke@jabber.sust.se', mbody=self.boundjid.bare +' is now online use xep_323 stanza to talk to me') + # self.send_message(mto='jocke@jabber.sust.se', mbody=self.boundjid.bare +' is now online use xep_323 stanza to talk to me') - #------------------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------------------- # Service Discovery - #------------------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------------------- try: # By using block=True, the result stanza will be # returned. Execution will block until the reply is @@ -125,7 +132,7 @@ class IoT_TestDevice(sleekxmpp.ClientXMPP): else: header = 'XMPP Service Discovery' logging.info('-' * PRINT_HEADER_LENGTH) - gap = ' '* ((PRINT_HEADER_LENGTH - len(header)) / 2) + gap = ' ' * ((PRINT_HEADER_LENGTH - len(header)) / 2) logging.info(gap + header) logging.info('-' * PRINT_HEADER_LENGTH) @@ -134,41 +141,35 @@ class IoT_TestDevice(sleekxmpp.ClientXMPP): for feature in info['disco_info']['features']: logging.info(' - %s' % feature) - #------------------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------------------- # Requesting data through XEP0323 - #------------------------------------------------------------------------------------------- - session = self['xep_0323'].request_data(self.boundjid.full, self.target_jid, - self.datacallback, flags={"momentary": "true"}) + # ------------------------------------------------------------------------------------------- + + logging.info('-' * PRINT_HEADER_LENGTH) + logging.info("Sending Request: %s to %s", self.requestType, self.target_jid) + + if self.requestType in ('/TEMPERATURE/.', '/SONAR/.'): + session = self['xep_0323'].request_data(self.boundjid.full, self.target_jid, + self.datacallback, flags={"momentary": "true"}) + else: + self.send_message(mto=self.target_jid, + mbody=self.requestType, + mtype='chat') + + # Using wait=True ensures that the send queue will be + # emptied before ending the session. + self.disconnect(wait=True) + print ("Bulb state switched - " + self.requestType) def message(self, msg): if msg['type'] in ('chat', 'normal'): - logging.debug("got normal chat message" + str(msg)) + logging.info("got normal chat message" + str(msg)) ipPublic = urlopen('http://icanhazip.com').read() ipSocket = socket.gethostbyname(socket.gethostname()) - msg.reply("Hi I am " + self.boundjid.full + " and I am on IP " + ipSocket + " use xep_323 stanza to talk to me").send() + msg.reply( + "Hi I am " + self.boundjid.full + " and I am on IP " + ipSocket + " use xep_323 stanza to talk to me").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"}) + logging.info("got unknown message type %s", str(msg['type'])) if __name__ == '__main__': @@ -206,36 +207,42 @@ if __name__ == '__main__': optp.add_option("-c", "--sensorjid", dest="sensorjid", help="Another device to call for data on", default=None) + optp.add_option("-r", "--sensorType", dest="sensorType", + help="The type of the sensor info requested", default="/TEMPERATURE/") + + optp.add_option("-s", "--sensorState", dest="sensorState", + help="The state of the sensor to switch to", default="") + opts, args = optp.parse_args() - # Setup logging. + # 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@204.232.188.215/raspi" + opts.jid = "admin@204.232.188.215/admin" if opts.password is None: - opts.password = "wso2iot123" - #opts.password = getpass.getpass("Password: ") - if opts.sensorjid is None: - opts.sensorjid = "1hrdqr4r2ymhy@204.232.188.215/raspi" - #opts.sensorjid = getpass.getpass("Sensor JID: ") + opts.password = "admin" + # if opts.sensorjid is None: + # opts.sensorjid = "t4ibkqs8t7ox@204.232.188.215/admin" - #------------------------------------------------------------------------------------------- + # ------------------------------------------------------------------------------------------- # Starting XMPP with XEP0030, XEP0323, XEP0325 - #------------------------------------------------------------------------------------------- - xmpp = IoT_TestDevice(opts.jid, opts.password, opts.sensorjid) + # ------------------------------------------------------------------------------------------- + + requestState = opts.sensorType + opts.sensorState + + xmpp = IoT_TestDevice(opts.jid, opts.password, opts.sensorjid, requestState) xmpp.register_plugin('xep_0030') xmpp.register_plugin('xep_0323') xmpp.register_plugin('xep_0325') if opts.sensorjid: - logging.debug("will try to call another device for data") + 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" + print "ID of the client to communicate-to not given..."