forked from community/device-mgt-plugins
Merge pull request #107 from GPrathap/IoTS-1.0.0-M1
removed xmpp dependencies
commit
3fac373e5d
Before Width: | Height: | Size: 151 KiB After Width: | Height: | Size: 148 KiB |
@ -0,0 +1,33 @@
|
||||
#
|
||||
# Copyright (c) 2015, 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.
|
||||
#
|
||||
#
|
||||
[Device-Configurations]
|
||||
server-name=${SERVER_NAME}
|
||||
owner=${DEVICE_OWNER}
|
||||
deviceId=${DEVICE_ID}
|
||||
device-name=${DEVICE_NAME}
|
||||
controller-context=/raspberrypi/controller
|
||||
https-ep=${HTTPS_EP}
|
||||
http-ep=${HTTP_EP}
|
||||
apim-ep=${APIM_EP}
|
||||
mqtt-ep=${MQTT_EP}
|
||||
xmpp-ep=${XMPP_EP}
|
||||
auth-method=token
|
||||
auth-token=${DEVICE_TOKEN}
|
||||
refresh-token=${DEVICE_REFRESH_TOKEN}
|
||||
push-interval=15
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
@ -1,108 +0,0 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
/**
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you 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 time
|
||||
import iotUtils
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
|
||||
# The callback for when the client receives a CONNACK response from the server.
|
||||
def on_connect(client, userdata, flags, rc):
|
||||
print("MQTT_LISTENER: Connected with result code " + str(rc))
|
||||
|
||||
# Subscribing in on_connect() means that if we lose the connection and
|
||||
# reconnect then subscriptions will be renewed.
|
||||
print ("MQTT_LISTENER: Subscribing with topic " + TOPIC)
|
||||
client.subscribe(TOPIC)
|
||||
|
||||
|
||||
|
||||
# The callback for when a PUBLISH message is received from the server.
|
||||
def on_message(client, userdata, msg):
|
||||
print( "MQTT_LISTENER: " + msg.topic + " " + str(msg.payload) )
|
||||
|
||||
request = str(msg.payload)
|
||||
|
||||
resource = request.split(":")[0].upper()
|
||||
state = request.split(":")[1].upper()
|
||||
|
||||
print "MQTT_LISTENER: Resource- " + resource
|
||||
|
||||
if resource == "TEMP":
|
||||
pass
|
||||
#request.send_response(200)
|
||||
#request.send_header("Content-type", "text/plain")
|
||||
#request.end_headers()
|
||||
#request.wfile.write(LAST_TEMP)
|
||||
# return
|
||||
|
||||
elif resource == "BULB":
|
||||
iotUtils.switchBulb(state)
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# The Main method of the server script
|
||||
# This method is invoked from RaspberryStats.py on a new thread
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
def main():
|
||||
|
||||
MQTT_ENDPOINT = iotUtils.MQTT_EP.split(":")
|
||||
MQTT_IP = MQTT_ENDPOINT[1].replace('//','')
|
||||
MQTT_PORT = int(MQTT_ENDPOINT[2])
|
||||
|
||||
DEV_OWNER = iotUtils.DEVICE_OWNER
|
||||
DEV_ID = iotUtils.DEVICE_ID
|
||||
|
||||
global TOPIC
|
||||
TOPIC = "wso2/iot/" + DEV_OWNER + "/firealarm/" + DEV_ID
|
||||
|
||||
print ("MQTT_LISTENER: MQTT_ENDPOINT is " + str(MQTT_ENDPOINT))
|
||||
print ("MQTT_LISTENER: MQTT_TOPIC is " + TOPIC)
|
||||
|
||||
mqttClient = mqtt.Client()
|
||||
mqttClient.on_connect = on_connect
|
||||
mqttClient.on_message = on_message
|
||||
|
||||
while True:
|
||||
try:
|
||||
mqttClient.connect(MQTT_IP, MQTT_PORT, 60)
|
||||
print "MQTT_LISTENER: " + time.asctime(), "Connected to MQTT Broker - %s:%s" % (MQTT_IP, MQTT_PORT)
|
||||
|
||||
# Blocking call that processes network traffic, dispatches callbacks and
|
||||
# handles reconnecting.
|
||||
# Other loop*() functions are available that give a threaded interface and a
|
||||
# manual interface.
|
||||
mqttClient.loop_forever()
|
||||
|
||||
except (KeyboardInterrupt, Exception) as e:
|
||||
print "MQTT_LISTENER: Exception in MQTTServerThread (either KeyboardInterrupt or Other)"
|
||||
print ("MQTT_LISTENER: " + str(e))
|
||||
|
||||
mqttClient.disconnect()
|
||||
print "MQTT_LISTENER: " + time.asctime(), "Connection to Broker closed - %s:%s" % (MQTT_IP, MQTT_PORT)
|
||||
print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
iotUtils.setUpGPIOPins()
|
||||
main()
|
||||
|
Binary file not shown.
@ -1,156 +0,0 @@
|
||||
#!/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 sleekxmpp
|
||||
import getpass
|
||||
import sys
|
||||
import ssl, pyasn1
|
||||
|
||||
from urllib import urlopen
|
||||
import iotUtils
|
||||
import running_mode
|
||||
|
||||
# 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.
|
||||
def initSleekXMPP():
|
||||
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
|
||||
|
||||
def beClientOrServer(self, server=True, clientJID=None):
|
||||
self.beServer = True
|
||||
|
||||
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')
|
||||
|
||||
def message(self, msg):
|
||||
if msg['type'] in ('chat', 'normal'):
|
||||
print ("XMPP_SERVER: Got normal chat message" + str(msg))
|
||||
ip = urlopen('http://icanhazip.com').read()
|
||||
msg.reply("XMPP_SERVER: Hi I am " + self.boundjid.full + " and I am on IP " + ip).send()
|
||||
else:
|
||||
print ("XMPP_SERVER: 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)
|
||||
|
||||
def refresh(self, fields):
|
||||
"""
|
||||
the implementation of the refresh method
|
||||
"""
|
||||
# global LAST_TEMP
|
||||
# self._set_momentary_timestamp(self._get_timestamp())
|
||||
# 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(iotUtils.LAST_TEMP), flags={"automaticReadout": "true"})
|
||||
|
||||
|
||||
def main():
|
||||
XMPP_ENDP = iotUtils.XMPP_EP.split(":")[0]
|
||||
|
||||
XMPP_ENDPOINT = iotUtils.XMPP_EP.split(":")
|
||||
XMPP_IP = XMPP_ENDPOINT[1].replace('//', '')
|
||||
XMPP_PORT = int(XMPP_ENDPOINT[2])
|
||||
|
||||
XMPP_OWN = iotUtils.DEVICE_OWNER
|
||||
XMPP_JID = iotUtils.DEVICE_ID + "@" + XMPP_IP + "/raspi"
|
||||
XMPP_PWD = iotUtils.AUTH_TOKEN
|
||||
|
||||
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
|
||||
print "XMPP_SERVER: Owner - " + XMPP_OWN
|
||||
print "XMPP_SERVER: AccountID - " + XMPP_JID
|
||||
print "XMPP_SERVER: AccountPass - " + XMPP_PWD
|
||||
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
|
||||
|
||||
xmpp = IoT_TestDevice(XMPP_JID, XMPP_PWD)
|
||||
xmpp.ssl_version = ssl.PROTOCOL_SSLv3
|
||||
|
||||
xmpp.register_plugin('xep_0030')
|
||||
xmpp.register_plugin('xep_0323')
|
||||
xmpp.register_plugin('xep_0325')
|
||||
|
||||
if XMPP_OWN:
|
||||
# xmpp['xep_0030'].add_feature(feature='urn:xmpp:sn',
|
||||
# node=opts.nodeid,
|
||||
# jid=xmpp.boundjid.full)
|
||||
|
||||
myDevice = TheDevice(XMPP_OWN)
|
||||
# myDevice._add_field(name="Relay", typename="numeric", unit="Bool");
|
||||
myDevice._add_field(name="Temperature", typename="numeric", unit="C")
|
||||
myDevice._set_momentary_timestamp("2013-03-07T16:24:30")
|
||||
myDevice._add_field_momentary_data("Temperature", "23.4", flags={"automaticReadout": "true"})
|
||||
|
||||
xmpp['xep_0323'].register_node(nodeId=XMPP_OWN, device=myDevice, commTimeout=10)
|
||||
xmpp.beClientOrServer(server=True)
|
||||
|
||||
while not (xmpp.testForRelease()):
|
||||
try:
|
||||
xmpp.connect()
|
||||
xmpp.process(block=True)
|
||||
print ("XMPP_SERVER: Lost Connection")
|
||||
except Exception as e:
|
||||
print "XMPP_SERVER: Exception in XMPPServerThread (either KeyboardInterrupt or Other)"
|
||||
print ("XMPP_SERVER: " + str(e))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
initSleekXMPP()
|
||||
main()
|
Loading…
Reference in new issue