diff --git a/modules/samples/doormanager/feature/feature/src/main/resources/agent/deviceConfig.properties b/modules/samples/doormanager/feature/feature/src/main/resources/agent/deviceConfig.properties index a9abf738..c0b9ccc7 100644 --- a/modules/samples/doormanager/feature/feature/src/main/resources/agent/deviceConfig.properties +++ b/modules/samples/doormanager/feature/feature/src/main/resources/agent/deviceConfig.properties @@ -1,4 +1,3 @@ -# # Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,18 +11,20 @@ # 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} +username=${DEVICE_OWNER} deviceId=${DEVICE_ID} device-name=${DEVICE_NAME} +manager-context=/doormanager/controller/get_user_info controller-context=/doormanager/controller +https-ep=${HTTPS_EP} +http-ep=${HTTP_EP} +apim-ep=${APIM_EP} mqtt-ep=${MQTT_EP} auth-method=token auth-token=${DEVICE_TOKEN} refresh-token=${DEVICE_REFRESH_TOKEN} push-interval=15 - - diff --git a/modules/samples/doormanager/feature/feature/src/main/resources/agent/sketch.properties b/modules/samples/doormanager/feature/feature/src/main/resources/agent/sketch.properties index b520c3f9..9d8c6fd2 100644 --- a/modules/samples/doormanager/feature/feature/src/main/resources/agent/sketch.properties +++ b/modules/samples/doormanager/feature/feature/src/main/resources/agent/sketch.properties @@ -1,16 +1,3 @@ -#/* -# * Copyright (c) 2016, 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. -# */ templates=deviceConfig.properties zipfilename=doormanager.zip + diff --git a/modules/samples/doormanager/feature/feature/src/main/resources/agent/src/DoorManagerAgent.log b/modules/samples/doormanager/feature/feature/src/main/resources/agent/src/DoorManagerAgent.log new file mode 100644 index 00000000..e69de29b diff --git a/modules/samples/doormanager/feature/feature/src/main/resources/agent/src/DoorManagerAgent.py b/modules/samples/doormanager/feature/feature/src/main/resources/agent/src/DoorManagerAgent.py new file mode 100644 index 00000000..ad24a818 --- /dev/null +++ b/modules/samples/doormanager/feature/feature/src/main/resources/agent/src/DoorManagerAgent.py @@ -0,0 +1,194 @@ +#!/usr/bin/env python +""" +/** +* Copyright (c) 2016, 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 argparse +import httplib +import logging +import logging.handlers +import signal +import ssl +import sys +import threading +import time +from functools import wraps + +import RPi.GPIO as GPIO + +import MQTTHandler +import RFIDReader +import iotUtils + + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Overriding the default SSL version used in some of the Python (2.7.x) versions +# This is a known issue in earlier Python releases +# But was fixed in later versions. Ex-2.7.11 +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +def sslwrap(func): + @wraps(func) + def bar(*args, **kw): + kw['ssl_version'] = ssl.PROTOCOL_TLSv1 + return func(*args, **kw) + + return bar +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +PUSH_INTERVAL = 5000 # time interval between successive data pushes in seconds + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Logger defaults +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +LOG_FILENAME = "DoorManagerAgent.log" +logging_enabled = False +LOG_LEVEL = logging.INFO # Could be e.g. "DEBUG" or "WARNING" +#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Python version +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +if sys.version_info < (2, 6, 0): + sys.stderr.write("You need python 2.6.0 or later to run this script\n") + exit(1) +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Define and parse command line arguments +# If the log file is specified on the command line then override the default +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +parser = argparse.ArgumentParser(description="Python service to push RPi info to the Device Cloud") +parser.add_argument("-l", "--log", help="file to write log to (default '" + LOG_FILENAME + "')") + +help_string_for_data_push_interval = "time interval between successive locker status push to server(default '" + str( + PUSH_INTERVAL) + "')" +parser.add_argument("-i", "--interval", type=int, help=help_string_for_data_push_interval) + +args = parser.parse_args() +if args.log: + LOG_FILENAME = args.log + +if args.interval: + PUSH_INTERVAL = args.interval +#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# A class we can use to capture stdout and sterr in the log +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class IOTLogger(object): + def __init__(self, logger, level): + """Needs a logger and a logger level.""" + self.logger = logger + self.level = level + + def write(self, message): + if message.rstrip() != "": # Only log if there is a message (not just a new line) + self.logger.log(self.level, message.rstrip()) +#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# Configure logging to log to a file, +# making a new file at midnight and keeping the last 3 day's data +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +def configureLogger(loggerName): + logger = logging.getLogger(loggerName) + logger.setLevel(LOG_LEVEL) # Set the log level to LOG_LEVEL + handler = logging.handlers.TimedRotatingFileHandler(LOG_FILENAME, when="midnight", + backupCount=3) # Handler that writes to a file, + # ~~~make new file at midnight and keep 3 backups + formatter = logging.Formatter('%(asctime)s %(levelname)-8s %(message)s') # Format each log message like this + handler.setFormatter(formatter) # Attach the formatter to the handler + logger.addHandler(handler) # Attach the handler to the logger + + if (logging_enabled): + sys.stdout = IOTLogger(logger, logging.INFO) # Replace stdout with logging to file at INFO level + sys.stderr = IOTLogger(logger, logging.ERROR) # Replace stderr with logging to file at ERROR level +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# This is a Thread object for reading RFID card +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class ListenRFIDhread(object): + def __init__(self): + thread = threading.Thread(target=self.run, args=()) + thread.daemon = True # Daemonize thread + thread.start() # Start the execution + + def run(self): + RFIDReader.main() +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# This is a Thread object for listening for MQTT Messages +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +class ListenMQTTThread(object): + def __init__(self): + thread = threading.Thread(target=self.run, args=()) + thread.daemon = True # Daemonize thread + thread.start() # Start the execution + + def run(self): + MQTTHandler.main() +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# When sysvinit sends the TERM signal, cleanup before exiting +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +def sigterm_handler(_signo, _stack_frame): + print("[] received signal {}, exiting...".format(_signo)) + sys.exit(0) +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +signal.signal(signal.SIGTERM, sigterm_handler) + + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# This method used to initialize GPIO ports +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +def initDoorLock(): + # GPIO.setmode(GPIO.BOARD) + GPIO.setup(iotUtils.DOOR_LOCKER_1_PORT, GPIO.OUT, initial=GPIO.HIGH) + GPIO.setup(iotUtils.DOOR_LOCKER_2_PORT, GPIO.OUT, initial=GPIO.HIGH) + GPIO.setup(iotUtils.LOCK_STATE_ON_NOTIFY_PORT, GPIO.OUT, initial=GPIO.LOW) + GPIO.setup(iotUtils.LOCK_STATE_OFF_NOTIFY_PORT, GPIO.OUT, initial=GPIO.LOW) +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# The Main method of the Automatic Door Locker Agent +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +def main(): + configureLogger("AutomaticDoorLocker") + ListenMQTTThread() + ListenRFIDhread() + initDoorLock() + # registerUIDofRFIDCard() # Call the register endpoint and register Device I + # ListenMQTTThread() + while True: + try: + time.sleep(PUSH_INTERVAL) + except (KeyboardInterrupt, Exception) as e: + print "AutomaticDoorLockerStats: Exception in RaspberryAgentThread (either KeyboardInterrupt or Other)" + print ("AutomaticDoorLockerStats: " + str(e)) + print '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' + pass +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +if __name__ == "__main__": + main() diff --git a/modules/samples/doormanager/feature/feature/src/main/resources/agent/src/MFRC522.py b/modules/samples/doormanager/feature/feature/src/main/resources/agent/src/MFRC522.py new file mode 100644 index 00000000..9e15c843 --- /dev/null +++ b/modules/samples/doormanager/feature/feature/src/main/resources/agent/src/MFRC522.py @@ -0,0 +1,396 @@ +#!/usr/bin/env python +# -*- coding: utf8 -*- + +import RPi.GPIO as GPIO +import spi +import signal +import time + +class MFRC522: + NRSTPD = 22 + + MAX_LEN = 16 + + PCD_IDLE = 0x00 + PCD_AUTHENT = 0x0E + PCD_RECEIVE = 0x08 + PCD_TRANSMIT = 0x04 + PCD_TRANSCEIVE = 0x0C + PCD_RESETPHASE = 0x0F + PCD_CALCCRC = 0x03 + + PICC_REQIDL = 0x26 + PICC_REQALL = 0x52 + PICC_ANTICOLL = 0x93 + PICC_SElECTTAG = 0x93 + PICC_AUTHENT1A = 0x60 + PICC_AUTHENT1B = 0x61 + PICC_READ = 0x30 + PICC_WRITE = 0xA0 + PICC_DECREMENT = 0xC0 + PICC_INCREMENT = 0xC1 + PICC_RESTORE = 0xC2 + PICC_TRANSFER = 0xB0 + PICC_HALT = 0x50 + + MI_OK = 0 + MI_NOTAGERR = 1 + MI_ERR = 2 + + Reserved00 = 0x00 + CommandReg = 0x01 + CommIEnReg = 0x02 + DivlEnReg = 0x03 + CommIrqReg = 0x04 + DivIrqReg = 0x05 + ErrorReg = 0x06 + Status1Reg = 0x07 + Status2Reg = 0x08 + FIFODataReg = 0x09 + FIFOLevelReg = 0x0A + WaterLevelReg = 0x0B + ControlReg = 0x0C + BitFramingReg = 0x0D + CollReg = 0x0E + Reserved01 = 0x0F + + Reserved10 = 0x10 + ModeReg = 0x11 + TxModeReg = 0x12 + RxModeReg = 0x13 + TxControlReg = 0x14 + TxAutoReg = 0x15 + TxSelReg = 0x16 + RxSelReg = 0x17 + RxThresholdReg = 0x18 + DemodReg = 0x19 + Reserved11 = 0x1A + Reserved12 = 0x1B + MifareReg = 0x1C + Reserved13 = 0x1D + Reserved14 = 0x1E + SerialSpeedReg = 0x1F + + Reserved20 = 0x20 + CRCResultRegM = 0x21 + CRCResultRegL = 0x22 + Reserved21 = 0x23 + ModWidthReg = 0x24 + Reserved22 = 0x25 + RFCfgReg = 0x26 + GsNReg = 0x27 + CWGsPReg = 0x28 + ModGsPReg = 0x29 + TModeReg = 0x2A + TPrescalerReg = 0x2B + TReloadRegH = 0x2C + TReloadRegL = 0x2D + TCounterValueRegH = 0x2E + TCounterValueRegL = 0x2F + + Reserved30 = 0x30 + TestSel1Reg = 0x31 + TestSel2Reg = 0x32 + TestPinEnReg = 0x33 + TestPinValueReg = 0x34 + TestBusReg = 0x35 + AutoTestReg = 0x36 + VersionReg = 0x37 + AnalogTestReg = 0x38 + TestDAC1Reg = 0x39 + TestDAC2Reg = 0x3A + TestADCReg = 0x3B + Reserved31 = 0x3C + Reserved32 = 0x3D + Reserved33 = 0x3E + Reserved34 = 0x3F + + serNum = [] + + def __init__(self, dev='/dev/spidev0.0', spd=1000000): + spi.openSPI(device=dev,speed=spd) + GPIO.setmode(GPIO.BOARD) + GPIO.setup(22, GPIO.OUT) + GPIO.output(self.NRSTPD, 1) + self.MFRC522_Init() + + def MFRC522_Reset(self): + self.Write_MFRC522(self.CommandReg, self.PCD_RESETPHASE) + + def Write_MFRC522(self, addr, val): + spi.transfer(((addr<<1)&0x7E,val)) + + def Read_MFRC522(self, addr): + val = spi.transfer((((addr<<1)&0x7E) | 0x80,0)) + return val[1] + + def SetBitMask(self, reg, mask): + tmp = self.Read_MFRC522(reg) + self.Write_MFRC522(reg, tmp | mask) + + def ClearBitMask(self, reg, mask): + tmp = self.Read_MFRC522(reg); + self.Write_MFRC522(reg, tmp & (~mask)) + + def AntennaOn(self): + temp = self.Read_MFRC522(self.TxControlReg) + if(~(temp & 0x03)): + self.SetBitMask(self.TxControlReg, 0x03) + + def AntennaOff(self): + self.ClearBitMask(self.TxControlReg, 0x03) + + def MFRC522_ToCard(self,command,sendData): + backData = [] + backLen = 0 + status = self.MI_ERR + irqEn = 0x00 + waitIRq = 0x00 + lastBits = None + n = 0 + i = 0 + + if command == self.PCD_AUTHENT: + irqEn = 0x12 + waitIRq = 0x10 + if command == self.PCD_TRANSCEIVE: + irqEn = 0x77 + waitIRq = 0x30 + + self.Write_MFRC522(self.CommIEnReg, irqEn|0x80) + self.ClearBitMask(self.CommIrqReg, 0x80) + self.SetBitMask(self.FIFOLevelReg, 0x80) + + self.Write_MFRC522(self.CommandReg, self.PCD_IDLE); + + while(i self.MAX_LEN: + n = self.MAX_LEN + + i = 0 + while i - + + org.wso2 + wso2 + 1 + org.homeautomation doormanager 1.0.0-SNAPSHOT @@ -140,11 +146,11 @@ 4.1 provided - - com.google.code.gson - gson - 2.2.4 - + + com.google.code.gson + gson + 2.2.4 + @@ -234,14 +240,14 @@ 4.4.3 1.1.0-SNAPSHOT 1.1.0.SNAPSHOT + 1.5.3 1.0.2 3.0.0.wso2v1 3.8.1.v20120830-144521 - 1.9.2-SNAPSHOT - 1.9.2.SNAPSHOT + 2.0.4-SNAPSHOT + 2.0.4.SNAPSHOT 1.1.1 2.4 - 3.0.4.wso2v1 3.0.4.wso2v1 2.6.1