changes to jaggery app, sketch

application-manager-new
Rasika Perera 9 years ago
parent 813f67c44a
commit eae80dd834

@ -35,7 +35,6 @@ var result;
if (uriMatcher.match("/{context}/api/device/sketch/download/{downloadId}")) {
downloadId = uriMatcher.elements().downloadId;
if(downloadId){
//Just download the already created zip archive
var CarbonUtils = Packages.org.wso2.carbon.utils.CarbonUtils;
var sketchFolder = "repository/resources/sketches";
@ -57,36 +56,34 @@ if (uriMatcher.match("/{context}/api/device/sketch/download/{downloadId}")) {
}
}
}else {
} else if (uriMatcher.match("/{context}/api/device/sketch/download")) {
//Create a new zip archive and register user calling endpoint
/* This should match with $CARBON_HOME/repository/resources/sketches/{sketchType} */
sketchType = request.getParameter("sketchType");
/* This should be registered device type of the CDMF(Connected Device Management Framework) */
deviceType = request.getParameter("deviceType");
if (!sketchType) {
log.error("Sketch Type is empty!");
// http status code 400 refers to - Bad request.
result = 400;
} else {
var user = session.get(constants.USER_SESSION_KEY);
if (!user) {
response.sendRedirect(dcProps.appContext + "login?#login-required");
exit();
}
/* This should match with $CARBON_HOME/repository/resources/sketches/{sketchType} */
sketchType = request.getParameter("sketchType");
/* This should be registered device type of the CDMF(Connected Device Management Framework) */
deviceType = request.getParameter("deviceType");
//URL: https://localhost:9443/{deviceType}/download?owner={username}
deviceManagerService = carbonHttpsServletTransport + "/" + deviceType + "/manager";
if (!sketchType) {
log.error("Sketch Type is empty!");
// http status code 400 refers to - Bad request.
result = 400;
} else {
var user = session.get(constants.USER_SESSION_KEY);
sketchDownloadEndPoint = deviceManagerService + "/device/" + sketchType + "/download";
response.sendRedirect(sketchDownloadEndPoint + "?owner=" + user.username);
exit();//stop execution
if (!user) {
response.sendRedirect(dcProps.appContext + "login?#login-required");
exit();
}
}
}
//URL: https://localhost:9443/{deviceType}/download?owner={username}
deviceManagerService = carbonHttpsServletTransport + "/" + deviceType + "/manager";
sketchDownloadEndPoint = deviceManagerService + "/device/" + sketchType + "/download";
response.sendRedirect(sketchDownloadEndPoint + "?owner=" + user.username);
exit();//stop execution
}
} else if (uriMatcher.match("/{context}/api/device/sketch/generate_link")) {
var contents = request.getContent();

@ -1,83 +0,0 @@
byte mac[6] = { 0x90, 0xA2, 0xDA, 0x0D, 0x30, 0xD7}; //mac - 90a2da0d30d7
byte dns2[] = { 8, 8, 8, 8 };
byte subnet[] = { 255, 255, 255, 0 };
byte gateway[] = { 192, 168, 1, 1 };
byte deviceIP[4] = { 192, 168, 1, 219 };
byte server[4] = { 192, 168, 1, 216 };
String connecting = "connecting.... ";
void connectHttp() {
if(DEBUG) Serial.println("-------------------------------");
Ethernet.begin(mac, deviceIP, dns2, gateway, subnet);
delay(2000);
if(DEBUG) {
Serial.print("My IP: ");
Serial.println(Ethernet.localIP());
}
connecting += httpClient.connect(server, SERVICE_PORT);
delay(2000);
if(DEBUG) Serial.println(connecting);
if (httpClient.connected()) {
if(DEBUG) Serial.println("connected");
} else {
if(DEBUG) Serial.println("connection failed");
while(!httpClient.connected()){
if(DEBUG) Serial.println("retrying to connect......");
httpClient.connect(server, SERVICE_PORT);
delay(2000);
}
if(DEBUG) Serial.println("connected to server!");
}
if(DEBUG) Serial.println("-------------------------------");
}
void setupResource(){
String hostIP = getHostIP(server);
String port = String(SERVICE_PORT);
host = "Host: " + hostIP + ":" + port;
if(DEBUG) Serial.println(host);
jsonPayLoad = String(OWNER_JSON);
jsonPayLoad += String(DEVICE_OWNER);
jsonPayLoad += String(DEVICE_ID_JSON);
jsonPayLoad += String(DEVICE_ID);
jsonPayLoad += String(REPLY_JSON);
if(DEBUG) {
Serial.print("JSON Payload: ");
Serial.println(jsonPayLoad);
Serial.println("-------------------------------");
}
}
String getMyIP(){
String myIP = "";
myIP = String(Ethernet.localIP()[0]);
for ( int index = 1; index < 4; index++) {
myIP += "." + String(Ethernet.localIP()[index]);
}
return myIP;
}
String getHostIP(byte server[4]){
String hostIP = String(server[0]);
for ( int index = 1; index < 4; index++) {
hostIP += "." + String(server[index]);
}
return hostIP;
}

@ -1,48 +0,0 @@
#ifndef FireAlarmEthernetAgent_H
#define FireAlarmEthernetAgent_H
#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define HTTP_POST "POST"
#define HTTP_GET "GET"
#define HTTP_VERSION "HTTP/1.1"
#define HTTP_CONTENT_TYPE "Content-Type: application/json"
#define HTTP_CONTENT_LEN "Content-Length: "
#define DEVICE_OWNER "${DEVICE_OWNER}" //"Smeansbeer"
#define DEVICE_ID "${DEVICE_ID}" //"vbhenqyt85yq"
#define DEVICE_TOKEN "${DEVICE_TOKEN}"
#define PUSH_ALARM_DATA "pushalarmdata"
#define READ_CONTROLS "readcontrols/"
#define REPLY "reply"
#define OWNER_JSON "{\"owner\":\""
#define DEVICE_ID_JSON "\",\"deviceId\":\""
#define REPLY_JSON "\",\"replyMessage\":\""
#define TIME_JSON "\",\"time\":\""
#define KEY_JSON "\",\"key\":\""
#define VALUE_JSON "\",\"value\":\""
#define END_JSON "\"}"
#define SERVICE_PORT 9763
#define SERVICE_EPOINT "/firealarm/controller/"
// pushalarmdata - application/json - {"owner":"","deviceId":"","replyMessage":"","time":"","key":"","value":""}
// readcontrols/{owner}/{deviceId}
// reply - application/json - {"owner":"","deviceId":"","replyMessage":""}
#define TEMP_PIN 3
#define BULB_PIN 4
#define FAN_PIN 5
#define DEBUG false
#define POLL_INTERVAL 1000
#endif

@ -1,143 +0,0 @@
#include "FireAlarmEthernetAgent.h"
#include <Ethernet.h>
#include <SPI.h>
#include "dht.h"
/**********************************************************************************************
0. Check with a sample Ethernet code of the Ethernet library to ensure that the sheild is working
1. Set the ip of the server(byte array below) where the Web-Rest API for the FireAlarm is running
2. Check whether the "SERVICE_EPOINT" is correct in the 'FireAlarmWifiAgent.h' file
3. Check whether the "SERVICE_PORT" is the same (9763) for the server running. Change it if needed
4. Check whether the pins have been attached accordingly in the Arduino
5. Check whether all reqquired pins are added to the 'digitalPins' array
***********************************************************************************************/
int digitalPins[] = { TEMP_PIN, BULB_PIN, FAN_PIN };
int analogPins[] = { 0, 1, 2, 3, 4, 5 };
EthernetClient httpClient;
String host, jsonPayLoad, replyMsg;
void setup() {
if(DEBUG) Serial.begin(9600);
pinMode(BULB_PIN, OUTPUT);
pinMode(FAN_PIN, OUTPUT);
connectHttp();
setupResource();
}
void loop() {
if (httpClient.connected()) {
pushDigitalPinData();
// pushData(); // Use this method to batch all data together and send in one call
delay(POLL_INTERVAL);
String responseMsg = readControls();
int index = responseMsg.lastIndexOf(":");
int newLine = responseMsg.lastIndexOf("\n");
String subStrn = responseMsg.substring(index + 1);
if (subStrn.equals("IN")) {
responseMsg = responseMsg.substring(newLine + 1, index);
if (responseMsg.equals("TEMPERATURE")) {
replyMsg = "Temperature is " + String(getTemperature()) + "C.";
reply(replyMsg);
} else if (responseMsg.equals("BULB")) {
replyMsg = "Bulb was switched " + switchBulb();
} else if (responseMsg.equals("FAN")) {
replyMsg = "Bulb was switched " + switchFan();
}
}
} else {
if(DEBUG) {
Serial.println("client not found...");
Serial.println("disconnecting.");
}
httpClient.stop();
connectHttp();
}
}
String getDataType(int pin){
switch(pin){
case TEMP_PIN:
return "Temperature";
case BULB_PIN:
return "Bulb";
case FAN_PIN:
return "Fan";
default:
return String(pin);
}
}
String switchBulb() {
if (digitalRead(BULB_PIN) == HIGH) {
digitalWrite(BULB_PIN, LOW);
return "OFF";
} else {
digitalWrite(BULB_PIN, HIGH);
return "ON";
}
}
String switchFan() {
if (digitalRead(FAN_PIN) == HIGH) {
digitalWrite(FAN_PIN, LOW);
return "OFF";
} else {
digitalWrite(FAN_PIN, HIGH);
return "ON";
}
}
double getTemperature(){
dht DHT;
if(DEBUG) {
Serial.println("-------------------------------");
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
Serial.print("DHT11, \t");
}
int chk = DHT.read11(TEMP_PIN);
switch (chk)
{
case DHTLIB_OK:
if(DEBUG) Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
if(DEBUG) Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
if(DEBUG) Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
if(DEBUG) Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
if(DEBUG) Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
if(DEBUG) Serial.print("Ack High error,\t");
break;
default:
if(DEBUG) Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
if(DEBUG) {
Serial.print("\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t\t");
Serial.println(DHT.humidity, 1);
Serial.println("-------------------------------");
}
return DHT.temperature;
}

@ -1,79 +0,0 @@
String readControls() {
String responseMsg;
String resource = " " + String(SERVICE_EPOINT) + String(READ_CONTROLS) + String(DEVICE_OWNER) + "/" + String(DEVICE_ID) + " ";
httpClient.print(HTTP_GET);
httpClient.print(resource);
httpClient.println(HTTP_VERSION);
httpClient.println(host);
httpClient.println();
if(DEBUG) {
Serial.print(HTTP_GET);
Serial.print(resource);
Serial.println(HTTP_VERSION);
Serial.println(host);
Serial.println();
}
delay(1000);
while (httpClient.available()) {
char response = httpClient.read();
responseMsg += response;
}
if(DEBUG) {
Serial.print(responseMsg);
Serial.println();
Serial.println("-------------------------------");
}
delay(1000);
return responseMsg;
}
void reply(String replyMsg) {
String resource = " " + String(SERVICE_EPOINT) + String(REPLY) + " ";
String payLoad = jsonPayLoad + replyMsg + String(END_JSON);
httpClient.print(HTTP_POST);
httpClient.print(resource);
httpClient.println(HTTP_VERSION);
httpClient.println(host);
httpClient.println(HTTP_CONTENT_TYPE);
httpClient.print(HTTP_CONTENT_LEN);
httpClient.println(payLoad.length());
httpClient.println();
httpClient.println(payLoad);
httpClient.println();
if(DEBUG) {
Serial.print(HTTP_POST);
Serial.print(resource);
Serial.println(HTTP_VERSION);
Serial.println(host);
Serial.println(HTTP_CONTENT_TYPE);
Serial.print(HTTP_CONTENT_LEN);
Serial.println(payLoad.length());
Serial.println();
Serial.println(payLoad);
Serial.println();
}
delay(1000);
while (httpClient.available()) {
char response = httpClient.read();
if(DEBUG) Serial.print(response);
}
if(DEBUG) {
Serial.println();
Serial.println("-------------------------------");
}
delay(1000);
}

@ -1,199 +0,0 @@
/**********************************************************************************************
This method will traverse the array of digital pins and batch the data from the those pins together.
It makes a single call to the server and sends all pin values as a batch.
Server dis-assembles it accordingly and makes multiple publish calls for each sensor type.
***********************************************************************************************/
void pushData(){
String resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " ";
String payLoad = jsonPayLoad + "DigitalPinData";
payLoad = payLoad + String(TIME_JSON) + "9999";
payLoad = payLoad + String(KEY_JSON) + "Data";
payLoad += String(VALUE_JSON);
for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) {
if ( digitalPins[pin] == TEMP_PIN ) {
payLoad += String(getTemperature());
} else if ( digitalRead(digitalPins[pin]) == HIGH ) {
payLoad += "ON";
} else if ( digitalRead(digitalPins[pin]) == LOW ) {
payLoad += "OFF";
}
if ( ((sizeof(digitalPins)/sizeof(int)) - 1) != pin ) {
payLoad += "-";
}
}
payLoad = payLoad + String(END_JSON);
if(DEBUG) Serial.println(payLoad);
httpClient.print(HTTP_POST);
httpClient.print(resource);
httpClient.println(HTTP_VERSION);
httpClient.println(host);
httpClient.println(HTTP_CONTENT_TYPE);
httpClient.print(HTTP_CONTENT_LEN);
httpClient.println(payLoad.length());
httpClient.println();
httpClient.println(payLoad);
httpClient.println();
if(DEBUG) {
Serial.print(HTTP_POST);
Serial.print(resource);
Serial.println(HTTP_VERSION);
Serial.println(host);
Serial.println(HTTP_CONTENT_TYPE);
Serial.print(HTTP_CONTENT_LEN);
Serial.println(payLoad.length());
Serial.println();
Serial.println(payLoad);
Serial.println();
}
delay(1000);
while (httpClient.available()) {
char response = httpClient.read();
if(DEBUG) Serial.print(response);
}
if(DEBUG) {
Serial.println();
Serial.println("-------------------------------");
}
delay(1000);
}
/**********************************************************************************************
This method will traverse the array of digital pins and publish the data from the those pins.
It differs from the above method such that the pin data is published one after the other in
seperate calls to the server
***********************************************************************************************/
void pushDigitalPinData(){
for ( int pin = 0; pin < (sizeof(digitalPins)/sizeof(int)); pin++) {
String resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " ";
String payLoad = jsonPayLoad + "DigitalPinData";
payLoad = payLoad + String(TIME_JSON) + "9999";
payLoad = payLoad + String(KEY_JSON) + getDataType(digitalPins[pin]);
payLoad += String(VALUE_JSON);
if ( digitalPins[pin] == TEMP_PIN ) {
payLoad += String(getTemperature());
} else if ( digitalRead(digitalPins[pin]) == HIGH ) {
payLoad += "ON";
} else if ( digitalRead(digitalPins[pin]) == LOW ) {
payLoad += "OFF";
}
payLoad = payLoad + String(END_JSON);
if(DEBUG) Serial.println(payLoad);
httpClient.print(HTTP_POST);
httpClient.print(resource);
httpClient.println(HTTP_VERSION);
httpClient.println(host);
httpClient.println(HTTP_CONTENT_TYPE);
httpClient.print(HTTP_CONTENT_LEN);
httpClient.println(payLoad.length());
httpClient.println();
httpClient.println(payLoad);
httpClient.println();
if(DEBUG) {
Serial.print(HTTP_POST);
Serial.print(resource);
Serial.println(HTTP_VERSION);
Serial.println(host);
Serial.println(HTTP_CONTENT_TYPE);
Serial.print(HTTP_CONTENT_LEN);
Serial.println(payLoad.length());
Serial.println();
Serial.println(payLoad);
Serial.println();
}
delay(1000);
while (httpClient.available()) {
char response = httpClient.read();
if(DEBUG) Serial.print(response);
}
if(DEBUG) {
Serial.println();
Serial.println("-------------------------------");
}
delay(1000);
}
}
/**********************************************************************************************
Only required for cases of reading analog pin values.
An int Array of analog pins that needs to be read has to be initialised.
This method will traverse the array and publish the data from the selected pins
***********************************************************************************************/
void pushAnalogPinData(){
for ( int pin = 0; pin < (sizeof(analogPins)/sizeof(int)); pin++) {
String resource = " " + String(SERVICE_EPOINT) + String(PUSH_ALARM_DATA) + " ";
String payLoad = jsonPayLoad + "AnalogPinData";
payLoad = payLoad + String(TIME_JSON) + "9999";
payLoad = payLoad + String(KEY_JSON) + getDataType(analogPins[pin]);
payLoad = payLoad + String(VALUE_JSON) + analogRead(analogPins[pin]);
payLoad = payLoad + String(END_JSON);
if(DEBUG) Serial.println(payLoad);
httpClient.print(HTTP_POST);
httpClient.print(resource);
httpClient.println(HTTP_VERSION);
httpClient.println(host);
httpClient.println(HTTP_CONTENT_TYPE);
httpClient.print(HTTP_CONTENT_LEN);
httpClient.println(payLoad.length());
httpClient.println();
httpClient.println(payLoad);
httpClient.println();
if(DEBUG) {
Serial.print(HTTP_POST);
Serial.print(resource);
Serial.println(HTTP_VERSION);
Serial.println(host);
Serial.println(HTTP_CONTENT_TYPE);
Serial.print(HTTP_CONTENT_LEN);
Serial.println(payLoad.length());
Serial.println();
Serial.println(payLoad);
Serial.println();
}
delay(1000);
while (httpClient.available()) {
char response = httpClient.read();
if(DEBUG) Serial.print(response);
}
if(DEBUG) {
Serial.println();
Serial.println("-------------------------------");
}
delay(1000);
}
}

@ -0,0 +1,6 @@
[Device-Configurations]
owner = ${DEVICE_OWNER}
deviceId = ${DEVICE_ID}
auth-method = token
auth-token = ${DEVICE_TOKEN}
refresh-token = ${DEVICE_REFRESH_TOKEN}

@ -1,2 +1,2 @@
templates=FireAlarmEthernetAgent.h
zipfilename=FireAlarmEthernetAgent.zip
templates=deviceConfigs.cfg
zipfilename=FireAlarmAgent.zip

@ -0,0 +1,130 @@
#!/bin/bash
echo "----------------------------------------------------------------"
echo "| WSO2 IOT Sample "
echo "| RaspiAlarm "
echo "| ---------------- "
echo "| ....initializing startup-script "
echo "----------------------------------------------------------------"
while true; do
read -p "Do you wish to run 'apt-get update' and continue? [Yes/No] " yn
case $yn in
[Yy]* ) sudo apt-get update;
break;;
[Nn]* ) echo "Continuing without apt-get update...";
break;;
* ) echo "Please answer yes or no.";
esac
done
if [ $? -ne 0 ]; then
echo "apt-get update failed.... Some dependencies may not get installed"
echo "If an already installed version of the package exists, try running:"
echo "----------------------------------------------------------------"
echo "sudo -i"
echo "cd /var/lib/dpkg/info"
echo "rm -rf wso2-raspi-alarm*"
echo "dpkg --remove --force-remove-reinstreq wso2-raspi-alarm"
echo "exit"
echo "----------------------------------------------------------------"
echo "Retry Installation...."
break;
fi
echo "Installing 'gdebi' package..."
sudo apt-get install gdebi # installation of gdebi
if [ $? -ne 0 ]; then
echo "gdebi installation failed.... dependencies will not be installed without gdebi"
read -p "Do you wish to continue without gdebi? [Yes/No] " yn
case $yn in
[Yy]* ) echo "Continueing without gdebi.....";;
[Nn]* ) echo "Try to resolve errors and re-run the script.";
exit;;
* ) exit;;
esac
fi
for f in ./wso2-raspi-alarm_1.0_armhf.deb; do
## Check if the glob gets expanded to existing files.
## If not, f here will be exactly the pattern above
## and the exists test will evaluate to false.
# [ -e "$f" ] && echo "'wso2-raspi-alarm_1.0_armhf.deb' file found and installing" || echo "'wso2-raspi-alarm_1.0_armhf.deb' file does not exist in current path"; exit;
if [ -e "$f" ]; then
echo "'wso2-raspi-alarm_1.0_armhf.deb' file found and installing now...."
else
echo "'wso2-raspi-alarm_1.0_armhf.deb' file does not exist in current path. \nExiting installation...";
exit;
fi
## This is all we needed to know, so we can break after the first iteration
break
done
echo "Installing the 'wso2-raspi-alarm deb package'"
sudo gdebi wso2-raspi-alarm_1.0_armhf.deb
if [ $? -ne 0 ]; then
echo "Installation Failed...."
exit;
fi
for f in ./deviceConfigs.cfg; do
## Check if the glob gets expanded to existing files.
## If not, f here will be exactly the pattern above
## and the exists test will evaluate to false.
# [ -e "$f" ] && echo "'wso2-raspi-alarm_1.0_armhf.deb' file found and installing" || echo "'wso2-raspi-alarm_1.0_armhf.deb' file does not exist in current path"; exit;
if [ -e "$f" ]; then
echo "Configuration file found......"
else
echo "'deviceConfigs.cfg' file does not exist in current path. \nExiting installation...";
exit;
fi
## This is all we needed to know, so we can break after the first iteration
break
done
echo "Copying configurations file to /usr/local/src/RaspberryAgent"
sudo cp ./deviceConfigs.cfg /usr/local/src/RaspberryAgent/
if [ $? -ne 0 ]; then
echo "Copying configuration file failed...."
exit;
fi
echo "Running the RaspberryAgent service...."
# sudo service RaspberryService.sh start
while true; do
read -p "Whats the time-interval (in seconds) between successive Data-Pushes to the WSO2-DC (ex: '60' indicates 1 minute) > " input
if [ $input -eq $input 2>/dev/null ]
then
echo "Setting data-push interval to $input seconds."
break;
else
echo "Input needs to be an integer indicating the number seconds between successive data-pushes."
fi
done
cd /usr/local/src/RaspberryAgent/
sudo nohup ./RaspberryStats.py -i $input > /dev/null 2>&1 &
if [ $? -ne 0 ]; then
echo "Could not start the service..."
exit;
fi
echo "--------------------------------------------------------------------------"
echo "| Successfully Started "
echo "| -------------------------- "
#echo "| run 'sudo service RaspberryService.sh status' to check status"
#echo "| run 'sudo service RaspberryService.sh stop' to stop service"
#echo "| -------------------------- "
echo "| Find logs at: /usr/local/src/RaspberryAgent/logs/RaspberryStats.log"
echo "---------------------------------------------------------------------------"
Loading…
Cancel
Save