From 8478f1ebf006f43ef360908cc02c33f19d07e66a Mon Sep 17 00:00:00 2001 From: charitha Date: Wed, 29 Jun 2016 11:45:53 +0530 Subject: [PATCH] Update agent code and analytics scripts. Fixed bugs --- .../relay_script/relay_script.xml | 2 +- .../org.wso2.iot.devices.relay_1.0.0.json | 2 +- .../watertank_execution.siddhiql | 4 +- ...rm_receiver.xml => watertank_receiver.xml} | 0 .../org.wso2.iot.watertank_1.0.0.json | 2 +- .../watertank/api/DeviceTypeService.java | 40 +++---- .../watertank/api/DeviceTypeServiceImpl.java | 29 +++-- .../feature/src/main/resources/agent/init.lua | 2 +- .../src/main/resources/agent/water-tank.lua | 100 ++++++++++++------ 9 files changed, 109 insertions(+), 72 deletions(-) rename modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_receiver/{firealarm_receiver.xml => watertank_receiver.xml} (100%) diff --git a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/relay_sensor/relay_script/relay_script.xml b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/relay_sensor/relay_script/relay_script.xml index 9b2eeb5c..054a7837 100644 --- a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/relay_sensor/relay_script/relay_script.xml +++ b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/relay_sensor/relay_script/relay_script.xml @@ -23,7 +23,7 @@ CREATE TEMPORARY TABLE DevicerelayData USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_IOT_DEVICES_relay"); CREATE TEMPORARY TABLE DevicerelaySummaryData USING CarbonAnalytics OPTIONS (tableName "DEVICE_relay_SUMMARY", - schema "relay FLOAT, deviceType STRING -i, deviceId STRING -i, owner STRING -i, time LONG -i",primaryKeys + schema "relay BOOLEAN, deviceType STRING -i, deviceId STRING -i, owner STRING -i, time LONG -i",primaryKeys "deviceType, deviceId, owner, time"); insert into table DevicerelaySummaryData select relay, meta_deviceType as deviceType, meta_deviceId as deviceId, diff --git a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/relay_sensor/relay_stream/org.wso2.iot.devices.relay_1.0.0.json b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/relay_sensor/relay_stream/org.wso2.iot.devices.relay_1.0.0.json index 579e9c45..9e6649ca 100644 --- a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/relay_sensor/relay_stream/org.wso2.iot.devices.relay_1.0.0.json +++ b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/relay_sensor/relay_stream/org.wso2.iot.devices.relay_1.0.0.json @@ -24,7 +24,7 @@ "payloadData": [ { "name": "relay", - "type": "BOOLEAN" + "type": "BOOL" } ] } diff --git a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_execution/watertank_execution.siddhiql b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_execution/watertank_execution.siddhiql index 75be22f3..bc39749a 100644 --- a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_execution/watertank_execution.siddhiql +++ b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_execution/watertank_execution.siddhiql @@ -7,10 +7,10 @@ /* define streams/tables and write queries here ... */ @Import('org.wso2.iot.watertank:1.0.0') -define stream watertank (meta_owner string, meta_deviceId string, relay float, waterlevel float); +define stream watertank (meta_owner string, meta_deviceId string, relay bool, waterlevel float); @Export('org.wso2.iot.devices.relay:1.0.0') -define stream relay (meta_owner string, meta_deviceType string, meta_deviceId string, meta_time long, relay float); +define stream relay (meta_owner string, meta_deviceType string, meta_deviceId string, meta_time long, relay bool); @Export('org.wso2.iot.devices.waterlevel:1.0.0') define stream waterlevel (meta_owner string, meta_deviceType string, meta_deviceId string, meta_time long, waterlevel float); diff --git a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_receiver/firealarm_receiver.xml b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_receiver/watertank_receiver.xml similarity index 100% rename from modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_receiver/firealarm_receiver.xml rename to modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_receiver/watertank_receiver.xml diff --git a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_stream/org.wso2.iot.watertank_1.0.0.json b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_stream/org.wso2.iot.watertank_1.0.0.json index f707afbd..dd4adb7c 100644 --- a/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_stream/org.wso2.iot.watertank_1.0.0.json +++ b/modules/samples/watertank/component/analytics/src/main/resources/carbonapps/watertank/watertank_stream/org.wso2.iot.watertank_1.0.0.json @@ -16,7 +16,7 @@ "payloadData": [ { "name": "relay", - "type": "BOOLEAN" + "type": "BOOL" }, { "name": "waterlevel", diff --git a/modules/samples/watertank/component/api/src/main/java/org/homeautomation/watertank/api/DeviceTypeService.java b/modules/samples/watertank/component/api/src/main/java/org/homeautomation/watertank/api/DeviceTypeService.java index 553ade61..3ad5df0b 100644 --- a/modules/samples/watertank/component/api/src/main/java/org/homeautomation/watertank/api/DeviceTypeService.java +++ b/modules/samples/watertank/component/api/src/main/java/org/homeautomation/watertank/api/DeviceTypeService.java @@ -19,22 +19,21 @@ package org.homeautomation.watertank.api; import org.homeautomation.watertank.api.dto.DeviceJSON; - import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.Feature; import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Path; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; -import javax.ws.rs.Produces; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; -import javax.ws.rs.PUT; -import javax.ws.rs.DELETE; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -58,34 +57,37 @@ interface DeviceTypeService { Response registerDevice(final DeviceJSON agentInfo); /** - * @param deviceId unique identifier for given device type - * @param onLevel level to turn on the relay - * @param offLevel level to turn off thr relay + * @param deviceId unique identifier for given device type + * @param onLevel level to turn on the relay + * @param offLevel level to turn off the relay + * @param sensorHeight height to water level sensor from bottom of the tank */ @Path("device/{deviceId}/change-levels") @POST - @Feature(code = "change-levels", name = "Change on/off water levels", - description = "Change on/off water levels") + @Feature(code = "change-levels", name = "Update configurations", + description = "Change on/off water levels and set sensor height from the bottom of the tank") @Permission(scope = "watertank_user", permissions = {"/permission/admin/device-mgt/change-levels"}) - Response changeOnOffLevels(@PathParam("deviceId") String deviceId, - @QueryParam("on") int onLevel, - @QueryParam("off") int offLevel, - @Context HttpServletResponse response); + Response updateConfigs(@PathParam("deviceId") String deviceId, + @QueryParam("on") int onLevel, + @QueryParam("off") int offLevel, + @QueryParam("height") int sensorHeight, + @Context HttpServletResponse response); /** * Retrieve Sensor data for the given time period - * @param deviceId unique identifier for given device type instance + * + * @param deviceId unique identifier for given device type instance * @param sensorName name of the sensor - * @param from starting time - * @param to ending time - * @return response with List object which includes sensor data which is requested + * @param from starting time + * @param to ending time + * @return response with List object which includes sensor data which is requested */ @Path("device/stats/{deviceId}/sensors/{sensorName}") @GET @Consumes("application/json") @Produces("application/json") Response getSensorStats(@PathParam("deviceId") String deviceId, @PathParam("sensorName") String sensorName, - @QueryParam("from") long from, @QueryParam("to") long to); + @QueryParam("from") long from, @QueryParam("to") long to); @Path("/device/{device_id}") @DELETE diff --git a/modules/samples/watertank/component/api/src/main/java/org/homeautomation/watertank/api/DeviceTypeServiceImpl.java b/modules/samples/watertank/component/api/src/main/java/org/homeautomation/watertank/api/DeviceTypeServiceImpl.java index 60364b50..2ac6244a 100644 --- a/modules/samples/watertank/component/api/src/main/java/org/homeautomation/watertank/api/DeviceTypeServiceImpl.java +++ b/modules/samples/watertank/component/api/src/main/java/org/homeautomation/watertank/api/DeviceTypeServiceImpl.java @@ -26,7 +26,6 @@ import org.homeautomation.watertank.api.dto.SensorRecord; import org.homeautomation.watertank.api.util.APIUtil; import org.homeautomation.watertank.api.util.ZipUtil; import org.homeautomation.watertank.plugin.constants.DeviceTypeConstants; -import org.json.JSONObject; import org.wso2.carbon.analytics.dataservice.commons.SORT; import org.wso2.carbon.analytics.dataservice.commons.SortByField; import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; @@ -109,35 +108,33 @@ public class DeviceTypeServiceImpl implements DeviceTypeService { } /** - /** - * @param deviceId unique identifier for given device type - * @param onLevel level to turn on the relay - * @param offLevel level to turn off thr relay + * @param deviceId unique identifier for given device type + * @param onLevel level to turn on the relay + * @param offLevel level to turn off the relay + * @param sensorHeight height to water level sensor from bottom of the tank */ @Path("device/{deviceId}/change-levels") @POST - @Feature(code = "change-levels", name = "Change on/off water levels", - description = "Change on/off water levels") - public Response changeOnOffLevels(@PathParam("deviceId") String deviceId, - @QueryParam("on") int onLevel, - @QueryParam("off") int offLevel, - @Context HttpServletResponse response) { + @Feature(code = "change-levels", name = "Update configurations", + description = "Change on/off water levels and set sensor height from the bottom of the tank") + public Response updateConfigs(@PathParam("deviceId") String deviceId, + @QueryParam("on") int onLevel, + @QueryParam("off") int offLevel, + @QueryParam("height") int sensorHeight, + @Context HttpServletResponse response) { try { if (!APIUtil.getDeviceAccessAuthorizationService() .isUserAuthorized(new DeviceIdentifier(deviceId, DeviceTypeConstants.DEVICE_TYPE))) { return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build(); } - JSONObject jsonObject = new JSONObject(); - jsonObject.put("on", onLevel); - jsonObject.put("off", offLevel); - + String configs = onLevel + "," + offLevel + "," + sensorHeight; Map dynamicProperties = new HashMap<>(); String publishTopic = APIUtil.getAuthenticatedUserTenantDomain() + "/" + DeviceTypeConstants.DEVICE_TYPE + "/" + deviceId + "/command"; dynamicProperties.put(DeviceTypeConstants.ADAPTER_TOPIC_PROPERTY, publishTopic); APIUtil.getOutputEventAdapterService().publish(DeviceTypeConstants.MQTT_ADAPTER_NAME, - dynamicProperties, jsonObject.toString()); + dynamicProperties, configs); return Response.ok().build(); } catch (DeviceAccessAuthorizationException e) { log.error(e.getErrorMessage(), e); diff --git a/modules/samples/watertank/feature/feature/src/main/resources/agent/init.lua b/modules/samples/watertank/feature/feature/src/main/resources/agent/init.lua index d499faae..b33ddd34 100644 --- a/modules/samples/watertank/feature/feature/src/main/resources/agent/init.lua +++ b/modules/samples/watertank/feature/feature/src/main/resources/agent/init.lua @@ -1,4 +1,4 @@ tmr.alarm(0, 1000, 0, function() dofile("wifi-connect.lua"); - dofile("read-sensor.lua"); + dofile("water-tank.lua"); end) diff --git a/modules/samples/watertank/feature/feature/src/main/resources/agent/water-tank.lua b/modules/samples/watertank/feature/feature/src/main/resources/agent/water-tank.lua index a7f2ee41..7ee9f38b 100644 --- a/modules/samples/watertank/feature/feature/src/main/resources/agent/water-tank.lua +++ b/modules/samples/watertank/feature/feature/src/main/resources/agent/water-tank.lua @@ -1,24 +1,76 @@ -DHT = require("dht_lib") - -dht_data = 2 -buzzer = 1 -gpio.mode(buzzer, gpio.OUTPUT) +trig = 5 --IO14 +echo = 7 --IO13 +relay = 0 --IO16 +is_relay_on = false +pulse_time = 0 +water_level = 0 +relay_on = 10 +relay_off = 100 +tank_height = 120 client_connected = false + m = mqtt.Client("ESP8266-" .. node.chipid(), 120, "${DEVICE_TOKEN}", "") -tmr.alarm(0, 10000, 1, function() - DHT.read(dht_data) +function save_config() + file.open("config", "w+") + file.writeline(relay_on .. "," .. relay_off .. "," .. tank_height) + file.close() + print("Configs saved") +end + +function read_config() + if (file.open("config") ~= nil) then + local result = string.sub(file.readline(), 1, -2) -- to remove newline character + file.close() + local v1, v2, v3 = result:match("([^,]+),([^,]+)") + relay_on = tonumber(v1) + relay_off = tonumber(v2) + tank_height = tonumber(v3) + print("Loaded configs:" .. relay_on .. "," .. relay_off .. "," .. tank_height) + else + print("Using default configs") + end +end + +gpio.mode(relay, gpio.OUTPUT) +gpio.mode(trig, gpio.OUTPUT) +gpio.mode(echo, gpio.INT) + +read_config() + +gpio.trig(echo, "both", function(level) + local du = tmr.now() - pulse_time + if (level == 1) then + pulse_time = tmr.now() + else + -- 1cm ==> 40 + water_level = tank_height - (du / 40); + if (water_level < relay_on) then + gpio.write(relay, gpio.HIGH) + is_relay_on = true + elseif (water_level > relay_off) then + gpio.write(relay, gpio.LOW) + is_relay_on = false + end + print("Water Level: " .. water_level .. " cm") + collectgarbage() + end +end) - local t = DHT.getTemperature() - local h = DHT.getHumidity() +tmr.alarm(0, 5000, 1, function() + gpio.write(trig, gpio.HIGH) + tmr.delay(10) + gpio.write(trig, gpio.LOW) +end) +tmr.alarm(0, 10000, 1, function() if t == nil then print("Error reading from DHTxx") else if (client_connected) then - local payload = "{event:{metaData:{owner:\"${DEVICE_OWNER}\",deviceId:\"${DEVICE_ID}\"},payloadData:{temperature:" .. t .. ", humidity:" .. h .. "}}}" + local payload = "{event:{metaData:{owner:\"${DEVICE_OWNER}\",deviceId:\"${DEVICE_ID}\"},payloadData:{relay:" .. is_relay_on .. ", waterlevel:" .. water_level .. "}}}" m:publish("carbon.super/watertank/${DEVICE_ID}/data", payload, 0, 0, function(client) - print("Published> Temperature: " .. t .. "C Humidity: " .. h .. "%") + print("Published> Water Level: " .. water_level .. "cm Relay: " .. is_relay_on .. "%") end) else connectMQTTClient() @@ -39,7 +91,6 @@ function connectMQTTClient() subscribeToMQTTQueue() end) end - end function subscribeToMQTTQueue() @@ -49,28 +100,15 @@ end) m: on("message", function(client, topic, message) print("MQTT message received") print(message) -buzz(message == "on") +local v1, v2, v3 = message: match("([^,]+),([^,]+)") +relay_on = tonumber(v1) +relay_off = tonumber(v2) +tank_height = tonumber(v3) +print("Received configs:".. relay_on.. ",".. relay_off.. ",".. tank_height) +save_config(); end) m: on("offline", function(client) print("Disconnected") client_connected = false end) end - -function buzz(status) -local buzzerOn = true -if(status) then -tmr. alarm(1, 500, 1, function() -if buzzerOn then -buzzerOn = false -gpio. write(buzzer, gpio. HIGH) -else -buzzerOn = true -gpio. write(buzzer, gpio. LOW) -end -end) -else -tmr. stop(1) -gpio. write(buzzer, gpio. LOW) -end -end \ No newline at end of file