mbed based IoT Gateway More details http://blog.thiseldo.co.uk/wp-filez/IoTGateway.pdf
Dependencies: NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem
Routing/IoTRouting.h@4:d460406ac780, 2012-05-01 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Tue May 01 21:43:40 2012 +0000
- Revision:
- 4:d460406ac780
- Parent:
- 0:a29a0225f203
- Child:
- 5:0dbc27a7af55
Added output module for Sen.Se and emonCMS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:a29a0225f203 | 1 | /** IoT Gateway Routing for incoming messages |
SomeRandomBloke | 0:a29a0225f203 | 2 | * |
SomeRandomBloke | 0:a29a0225f203 | 3 | * @author Andrew Lindsay |
SomeRandomBloke | 0:a29a0225f203 | 4 | * |
SomeRandomBloke | 0:a29a0225f203 | 5 | * @section LICENSE |
SomeRandomBloke | 0:a29a0225f203 | 6 | * |
SomeRandomBloke | 0:a29a0225f203 | 7 | * Copyright (c) 2012 Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk) |
SomeRandomBloke | 0:a29a0225f203 | 8 | * |
SomeRandomBloke | 0:a29a0225f203 | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
SomeRandomBloke | 0:a29a0225f203 | 10 | * of this software and associated documentation files (the "Software"), to deal |
SomeRandomBloke | 0:a29a0225f203 | 11 | * in the Software without restriction, including without limitation the rights |
SomeRandomBloke | 0:a29a0225f203 | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
SomeRandomBloke | 0:a29a0225f203 | 13 | * copies of the Software, and to permit persons to whom the Software is |
SomeRandomBloke | 0:a29a0225f203 | 14 | * furnished to do so, subject to the following conditions: |
SomeRandomBloke | 0:a29a0225f203 | 15 | |
SomeRandomBloke | 0:a29a0225f203 | 16 | * The above copyright notice and this permission notice shall be included in |
SomeRandomBloke | 0:a29a0225f203 | 17 | * all copies or substantial portions of the Software. |
SomeRandomBloke | 0:a29a0225f203 | 18 | * |
SomeRandomBloke | 0:a29a0225f203 | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
SomeRandomBloke | 0:a29a0225f203 | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
SomeRandomBloke | 0:a29a0225f203 | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
SomeRandomBloke | 0:a29a0225f203 | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
SomeRandomBloke | 0:a29a0225f203 | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
SomeRandomBloke | 0:a29a0225f203 | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
SomeRandomBloke | 0:a29a0225f203 | 25 | * THE SOFTWARE. |
SomeRandomBloke | 0:a29a0225f203 | 26 | * |
SomeRandomBloke | 0:a29a0225f203 | 27 | * @section DESCRIPTION |
SomeRandomBloke | 0:a29a0225f203 | 28 | * |
SomeRandomBloke | 0:a29a0225f203 | 29 | * |
SomeRandomBloke | 0:a29a0225f203 | 30 | */ |
SomeRandomBloke | 0:a29a0225f203 | 31 | |
SomeRandomBloke | 0:a29a0225f203 | 32 | #ifndef _IOTROUTING_H |
SomeRandomBloke | 0:a29a0225f203 | 33 | #define _IOTROUTING_H |
SomeRandomBloke | 0:a29a0225f203 | 34 | |
SomeRandomBloke | 0:a29a0225f203 | 35 | #include "mbed.h" |
SomeRandomBloke | 0:a29a0225f203 | 36 | #include "HTTPClient.h" |
SomeRandomBloke | 0:a29a0225f203 | 37 | #include "OutputDef.h" |
SomeRandomBloke | 0:a29a0225f203 | 38 | #include "OutputPachube.h" |
SomeRandomBloke | 0:a29a0225f203 | 39 | #include "OutputMqtt.h" |
SomeRandomBloke | 4:d460406ac780 | 40 | #include "OutputEmonCms.h" |
SomeRandomBloke | 4:d460406ac780 | 41 | #include "OutputSenSe.h" |
SomeRandomBloke | 0:a29a0225f203 | 42 | #include "PayloadV1.h" |
SomeRandomBloke | 0:a29a0225f203 | 43 | #include "PayloadV2.h" |
SomeRandomBloke | 0:a29a0225f203 | 44 | #include "PayloadSimple.h" |
SomeRandomBloke | 0:a29a0225f203 | 45 | #include <vector> |
SomeRandomBloke | 0:a29a0225f203 | 46 | |
SomeRandomBloke | 0:a29a0225f203 | 47 | #define STATUS_PAYLOAD_VMASK 0xf8 |
SomeRandomBloke | 0:a29a0225f203 | 48 | #define STATUS_PAYLOAD_VERSION 0x06 |
SomeRandomBloke | 0:a29a0225f203 | 49 | #define STATUS_PAYLOAD_V1 0x00 |
SomeRandomBloke | 0:a29a0225f203 | 50 | #define STATUS_PAYLOAD_V2 0x02 |
SomeRandomBloke | 0:a29a0225f203 | 51 | #define STATUS_LOW_BATTERY 0x01 |
SomeRandomBloke | 0:a29a0225f203 | 52 | |
SomeRandomBloke | 0:a29a0225f203 | 53 | #define PAYLOAD_TYPE_UNKNOWN 99 |
SomeRandomBloke | 0:a29a0225f203 | 54 | #define PAYLOAD_TYPE_SIMPLE 0 |
SomeRandomBloke | 0:a29a0225f203 | 55 | #define PAYLOAD_TYPE_V1 1 |
SomeRandomBloke | 0:a29a0225f203 | 56 | #define PAYLOAD_TYPE_V2 2 |
SomeRandomBloke | 0:a29a0225f203 | 57 | |
SomeRandomBloke | 0:a29a0225f203 | 58 | #define OUTPUT_UNKNOWN 0 |
SomeRandomBloke | 0:a29a0225f203 | 59 | #define OUTPUT_TYPE_PACHUBE 1 |
SomeRandomBloke | 0:a29a0225f203 | 60 | #define OUTPUT_TYPE_MQTT 2 |
SomeRandomBloke | 4:d460406ac780 | 61 | #define OUTPUT_TYPE_EMONCMS 3 |
SomeRandomBloke | 4:d460406ac780 | 62 | #define OUTPUT_TYPE_SENSE 4 |
SomeRandomBloke | 0:a29a0225f203 | 63 | |
SomeRandomBloke | 0:a29a0225f203 | 64 | /** Routing definition for a reading |
SomeRandomBloke | 0:a29a0225f203 | 65 | * Input source, node ID, sensor ID, output |
SomeRandomBloke | 0:a29a0225f203 | 66 | * default is OuptutDef but casts to Pachube or MQTT |
SomeRandomBloke | 0:a29a0225f203 | 67 | */ |
SomeRandomBloke | 0:a29a0225f203 | 68 | typedef struct { |
SomeRandomBloke | 0:a29a0225f203 | 69 | short nodeId; |
SomeRandomBloke | 0:a29a0225f203 | 70 | short sensorId; |
SomeRandomBloke | 0:a29a0225f203 | 71 | float factor; |
SomeRandomBloke | 0:a29a0225f203 | 72 | byte outType; |
SomeRandomBloke | 0:a29a0225f203 | 73 | OutputDef *output; |
SomeRandomBloke | 0:a29a0225f203 | 74 | char *param1; |
SomeRandomBloke | 0:a29a0225f203 | 75 | char *param2; |
SomeRandomBloke | 0:a29a0225f203 | 76 | char *param3; |
SomeRandomBloke | 0:a29a0225f203 | 77 | char *param4; |
SomeRandomBloke | 0:a29a0225f203 | 78 | } __attribute__((packed)) PayloadRouting; |
SomeRandomBloke | 0:a29a0225f203 | 79 | |
SomeRandomBloke | 0:a29a0225f203 | 80 | /** Node definition |
SomeRandomBloke | 0:a29a0225f203 | 81 | */ |
SomeRandomBloke | 0:a29a0225f203 | 82 | typedef struct { |
SomeRandomBloke | 0:a29a0225f203 | 83 | uint8_t groupId; |
SomeRandomBloke | 0:a29a0225f203 | 84 | uint8_t nodeId; |
SomeRandomBloke | 0:a29a0225f203 | 85 | uint8_t length; |
SomeRandomBloke | 0:a29a0225f203 | 86 | uint8_t type; |
SomeRandomBloke | 0:a29a0225f203 | 87 | } __attribute__((packed)) IoTNodeDef; |
SomeRandomBloke | 0:a29a0225f203 | 88 | |
SomeRandomBloke | 0:a29a0225f203 | 89 | |
SomeRandomBloke | 0:a29a0225f203 | 90 | /** Main IoT Gateway RFM12B payload routing class |
SomeRandomBloke | 0:a29a0225f203 | 91 | */ |
SomeRandomBloke | 0:a29a0225f203 | 92 | class IoTRouting { |
SomeRandomBloke | 0:a29a0225f203 | 93 | public: |
SomeRandomBloke | 0:a29a0225f203 | 94 | /** Default Constructor |
SomeRandomBloke | 0:a29a0225f203 | 95 | */ |
SomeRandomBloke | 0:a29a0225f203 | 96 | IoTRouting(); |
SomeRandomBloke | 0:a29a0225f203 | 97 | |
SomeRandomBloke | 0:a29a0225f203 | 98 | /** Initialise routing class |
SomeRandomBloke | 0:a29a0225f203 | 99 | */ |
SomeRandomBloke | 0:a29a0225f203 | 100 | void initRouting(); |
SomeRandomBloke | 0:a29a0225f203 | 101 | |
SomeRandomBloke | 0:a29a0225f203 | 102 | /** Write a list of nodes to IOTNODE.TXT |
SomeRandomBloke | 0:a29a0225f203 | 103 | * Format is: group,node,length,type |
SomeRandomBloke | 0:a29a0225f203 | 104 | * Where: |
SomeRandomBloke | 0:a29a0225f203 | 105 | * group, node, length are first 3 bytes of received packet |
SomeRandomBloke | 0:a29a0225f203 | 106 | * type is 99 for unknown, 0 for simple, 1 for Type 1, 2 for Type 2 |
SomeRandomBloke | 0:a29a0225f203 | 107 | */ |
SomeRandomBloke | 0:a29a0225f203 | 108 | bool writeNodeList(); |
SomeRandomBloke | 0:a29a0225f203 | 109 | |
SomeRandomBloke | 0:a29a0225f203 | 110 | /** Write main routing list to file IOTRTR.TXT |
SomeRandomBloke | 0:a29a0225f203 | 111 | * Format is: nodeId,sensorId,factor,outputType,param1,param2,param3,param4 |
SomeRandomBloke | 0:a29a0225f203 | 112 | * Where: |
SomeRandomBloke | 0:a29a0225f203 | 113 | * nodeId is the Node ID of the node to the sensor is attached to |
SomeRandomBloke | 0:a29a0225f203 | 114 | * sensorId is the ID of the sensor to define |
SomeRandomBloke | 0:a29a0225f203 | 115 | * factor is a conversion factor to convert from integer to real value. can be 100.0,10.0,1.0,0.1,0.01,0.001. For no conversion use 1.0 |
SomeRandomBloke | 0:a29a0225f203 | 116 | * outputType is the type of output, 1 is Pachube, 2 is MQTT |
SomeRandomBloke | 0:a29a0225f203 | 117 | * param1 is Pachube feed number or MQTT topic |
SomeRandomBloke | 0:a29a0225f203 | 118 | * param2 is Pachube datastream number |
SomeRandomBloke | 0:a29a0225f203 | 119 | * param3 and 4 are currently unused |
SomeRandomBloke | 0:a29a0225f203 | 120 | */ |
SomeRandomBloke | 0:a29a0225f203 | 121 | bool writeRoutingList(); |
SomeRandomBloke | 0:a29a0225f203 | 122 | |
SomeRandomBloke | 0:a29a0225f203 | 123 | /** Add a node to the nodelist |
SomeRandomBloke | 0:a29a0225f203 | 124 | * |
SomeRandomBloke | 0:a29a0225f203 | 125 | * @param groupId Group ID, byte 0 of received packet |
SomeRandomBloke | 0:a29a0225f203 | 126 | * @param nodeId Node ID, byte 1 |
SomeRandomBloke | 0:a29a0225f203 | 127 | * @param length Length of payload, byte 2 |
SomeRandomBloke | 0:a29a0225f203 | 128 | * @param type Type of payload, simple, v1, v2 or undefined |
SomeRandomBloke | 0:a29a0225f203 | 129 | */ |
SomeRandomBloke | 0:a29a0225f203 | 130 | void addNodeToList(uint8_t groupId, uint8_t nodeId, uint8_t length, uint8_t type ); |
SomeRandomBloke | 0:a29a0225f203 | 131 | |
SomeRandomBloke | 0:a29a0225f203 | 132 | /** Add an item to routing list |
SomeRandomBloke | 0:a29a0225f203 | 133 | * |
SomeRandomBloke | 0:a29a0225f203 | 134 | * @param nodeId ID of the node payload came from |
SomeRandomBloke | 0:a29a0225f203 | 135 | * @param sensorId ID of the sensor within the node |
SomeRandomBloke | 0:a29a0225f203 | 136 | * @param factor Sensor Reading conversion factor, 100, 10, 1, 0.1, 0.01, 0.001 etc |
SomeRandomBloke | 0:a29a0225f203 | 137 | * @param outType Output type, 1 = Pachube, 2 = MQTT |
SomeRandomBloke | 0:a29a0225f203 | 138 | * @param param1 Parameter 1 |
SomeRandomBloke | 0:a29a0225f203 | 139 | * @param param2 Parameter 2 |
SomeRandomBloke | 0:a29a0225f203 | 140 | * @param param3 Parameter 3 |
SomeRandomBloke | 0:a29a0225f203 | 141 | * @param param4 Parameter 4 |
SomeRandomBloke | 0:a29a0225f203 | 142 | */ |
SomeRandomBloke | 0:a29a0225f203 | 143 | void addRoutingToList( short nodeId, short sensorId, float factor, byte outType, |
SomeRandomBloke | 0:a29a0225f203 | 144 | char *param1, char *param2, char *param3, char *param4 ); |
SomeRandomBloke | 0:a29a0225f203 | 145 | |
SomeRandomBloke | 0:a29a0225f203 | 146 | /** get Payload type |
SomeRandomBloke | 0:a29a0225f203 | 147 | * |
SomeRandomBloke | 0:a29a0225f203 | 148 | * @param data Pointer to start of payload data |
SomeRandomBloke | 0:a29a0225f203 | 149 | * @param dataLen Length of payload data |
SomeRandomBloke | 0:a29a0225f203 | 150 | * @returns payload type, either simple, v1, v2 or unknown |
SomeRandomBloke | 0:a29a0225f203 | 151 | */ |
SomeRandomBloke | 0:a29a0225f203 | 152 | short getPayloadType( uint8_t *data, short dataLen ); |
SomeRandomBloke | 0:a29a0225f203 | 153 | |
SomeRandomBloke | 0:a29a0225f203 | 154 | /** Get routing data for specified node/sensor combination |
SomeRandomBloke | 0:a29a0225f203 | 155 | * |
SomeRandomBloke | 0:a29a0225f203 | 156 | * @param nodeId The ID of the node |
SomeRandomBloke | 0:a29a0225f203 | 157 | * @param sensorNum The ID of the sensor |
SomeRandomBloke | 0:a29a0225f203 | 158 | * @returns pointer to routing data |
SomeRandomBloke | 0:a29a0225f203 | 159 | */ |
SomeRandomBloke | 0:a29a0225f203 | 160 | PayloadRouting* getRouting( short nodeId, short sensorNum ); |
SomeRandomBloke | 0:a29a0225f203 | 161 | |
SomeRandomBloke | 0:a29a0225f203 | 162 | /** Add output definition to output list |
SomeRandomBloke | 0:a29a0225f203 | 163 | * |
SomeRandomBloke | 0:a29a0225f203 | 164 | * @param output Pointer to output object |
SomeRandomBloke | 0:a29a0225f203 | 165 | * @param outType Numeric value for the output type |
SomeRandomBloke | 0:a29a0225f203 | 166 | */ |
SomeRandomBloke | 0:a29a0225f203 | 167 | void addOutput( OutputDef *output, short outType ); |
SomeRandomBloke | 0:a29a0225f203 | 168 | |
SomeRandomBloke | 0:a29a0225f203 | 169 | /** Main payload routing function |
SomeRandomBloke | 0:a29a0225f203 | 170 | * |
SomeRandomBloke | 0:a29a0225f203 | 171 | * @param payload Pointer to payload data |
SomeRandomBloke | 0:a29a0225f203 | 172 | * @param payloadLen Length of th epayload |
SomeRandomBloke | 0:a29a0225f203 | 173 | * @returns flag to indicate payload routed to output or not. |
SomeRandomBloke | 0:a29a0225f203 | 174 | */ |
SomeRandomBloke | 0:a29a0225f203 | 175 | bool routePayload( uint8_t *payload, short payloadLen ); |
SomeRandomBloke | 0:a29a0225f203 | 176 | |
SomeRandomBloke | 0:a29a0225f203 | 177 | private: |
SomeRandomBloke | 0:a29a0225f203 | 178 | int sendCount; |
SomeRandomBloke | 0:a29a0225f203 | 179 | OutputDef *pachubeOutput; |
SomeRandomBloke | 0:a29a0225f203 | 180 | OutputDef *mqttOutput; |
SomeRandomBloke | 4:d460406ac780 | 181 | OutputDef *emonCmsOutput; |
SomeRandomBloke | 4:d460406ac780 | 182 | OutputDef *senSeOutput; |
SomeRandomBloke | 0:a29a0225f203 | 183 | |
SomeRandomBloke | 0:a29a0225f203 | 184 | std::vector<PayloadRouting*> _routing; |
SomeRandomBloke | 0:a29a0225f203 | 185 | std::vector<IoTNodeDef*> _nodeDefList; |
SomeRandomBloke | 0:a29a0225f203 | 186 | |
SomeRandomBloke | 0:a29a0225f203 | 187 | }; |
SomeRandomBloke | 0:a29a0225f203 | 188 | |
SomeRandomBloke | 0:a29a0225f203 | 189 | #endif /* _IOTROUTING_H */ |