condens

Dependencies:   mbed MbedJSONValue

Committer:
duchonic
Date:
Thu Aug 22 22:27:16 2019 +0000
Revision:
2:d5f4f429f3db
Child:
3:26d5a21bbc66
communication class

Who changed what in which revision?

UserRevisionLine numberNew contents of line
duchonic 2:d5f4f429f3db 1 #pragma once
duchonic 2:d5f4f429f3db 2
duchonic 2:d5f4f429f3db 3 #include "MbedJSONValue/MbedJSONValue.h"
duchonic 2:d5f4f429f3db 4
duchonic 2:d5f4f429f3db 5 enum Commands {TYPE, CONF, STATUS};
duchonic 2:d5f4f429f3db 6
duchonic 2:d5f4f429f3db 7 /** HeartBeat Class
duchonic 2:d5f4f429f3db 8 *
duchonic 2:d5f4f429f3db 9 *
duchonic 2:d5f4f429f3db 10 */
duchonic 2:d5f4f429f3db 11 class HeartBeat{
duchonic 2:d5f4f429f3db 12 public:
duchonic 2:d5f4f429f3db 13 HeartBeat(){
duchonic 2:d5f4f429f3db 14 currentKey = "A";
duchonic 2:d5f4f429f3db 15 flowKey = "DF";
duchonic 2:d5f4f429f3db 16 tempKey = "T";
duchonic 2:d5f4f429f3db 17 pumpStatusKey = "PS";
duchonic 2:d5f4f429f3db 18 }
duchonic 2:d5f4f429f3db 19 void sendFrame(){
duchonic 2:d5f4f429f3db 20 MbedJSONValue send;
duchonic 2:d5f4f429f3db 21 send[currentKey] = 11.2;
duchonic 2:d5f4f429f3db 22 send[flowKey] = 33;
duchonic 2:d5f4f429f3db 23 send[tempKey] = 12.2f;
duchonic 2:d5f4f429f3db 24 send[pumpStatusKey] = 0;
duchonic 2:d5f4f429f3db 25
duchonic 2:d5f4f429f3db 26 printf("%s\n", send.serialize().c_str() );
duchonic 2:d5f4f429f3db 27 }
duchonic 2:d5f4f429f3db 28 private:
duchonic 2:d5f4f429f3db 29 std::string currentKey;
duchonic 2:d5f4f429f3db 30 std::string flowKey;
duchonic 2:d5f4f429f3db 31 std::string tempKey;
duchonic 2:d5f4f429f3db 32 std::string pumpStatusKey;
duchonic 2:d5f4f429f3db 33 };
duchonic 2:d5f4f429f3db 34
duchonic 2:d5f4f429f3db 35 /** Type Class
duchonic 2:d5f4f429f3db 36 *
duchonic 2:d5f4f429f3db 37 *
duchonic 2:d5f4f429f3db 38 */
duchonic 2:d5f4f429f3db 39 class Type{
duchonic 2:d5f4f429f3db 40 public:
duchonic 2:d5f4f429f3db 41 Type(int actId=1) {
duchonic 2:d5f4f429f3db 42 idKey = "ID";
duchonic 2:d5f4f429f3db 43 id = actId;
duchonic 2:d5f4f429f3db 44 typeKey = "TYPE";
duchonic 2:d5f4f429f3db 45 type = "DC";
duchonic 2:d5f4f429f3db 46 }
duchonic 2:d5f4f429f3db 47
duchonic 2:d5f4f429f3db 48 /**
duchonic 2:d5f4f429f3db 49 * Set new id
duchonic 2:d5f4f429f3db 50 * @param newId new id of the device
duchonic 2:d5f4f429f3db 51 */
duchonic 2:d5f4f429f3db 52 void setId(int newId){
duchonic 2:d5f4f429f3db 53 id=newId;
duchonic 2:d5f4f429f3db 54 }
duchonic 2:d5f4f429f3db 55 void sendFrame(void){
duchonic 2:d5f4f429f3db 56 MbedJSONValue send;
duchonic 2:d5f4f429f3db 57 send[idKey] = id;
duchonic 2:d5f4f429f3db 58 send[typeKey] = type;
duchonic 2:d5f4f429f3db 59 printf("%s\n", send.serialize().c_str() );
duchonic 2:d5f4f429f3db 60 }
duchonic 2:d5f4f429f3db 61 private:
duchonic 2:d5f4f429f3db 62 std::string idKey;
duchonic 2:d5f4f429f3db 63 int id;
duchonic 2:d5f4f429f3db 64 std::string typeKey;
duchonic 2:d5f4f429f3db 65 std::string type;
duchonic 2:d5f4f429f3db 66 };
duchonic 2:d5f4f429f3db 67
duchonic 2:d5f4f429f3db 68
duchonic 2:d5f4f429f3db 69 class Conf{
duchonic 2:d5f4f429f3db 70 public:
duchonic 2:d5f4f429f3db 71 Conf(int newTemperature=20){
duchonic 2:d5f4f429f3db 72 responseKey = "RSP";
duchonic 2:d5f4f429f3db 73 tempKey = "T";
duchonic 2:d5f4f429f3db 74 temperature = newTemperature;
duchonic 2:d5f4f429f3db 75 }
duchonic 2:d5f4f429f3db 76
duchonic 2:d5f4f429f3db 77 /**
duchonic 2:d5f4f429f3db 78 * Set the configs
duchonic 2:d5f4f429f3db 79 * @param receive received json data
duchonic 2:d5f4f429f3db 80 * @returns status of the setConfig
duchonic 2:d5f4f429f3db 81 */
duchonic 2:d5f4f429f3db 82 bool setConfig(MbedJSONValue receive){
duchonic 2:d5f4f429f3db 83 temperature = receive[tempKey].get<int>();
duchonic 2:d5f4f429f3db 84 if(temperature > 50){
duchonic 2:d5f4f429f3db 85 temperature=50;
duchonic 2:d5f4f429f3db 86 return false;
duchonic 2:d5f4f429f3db 87 }
duchonic 2:d5f4f429f3db 88 else if(temperature < -20){
duchonic 2:d5f4f429f3db 89 temperature = -20;
duchonic 2:d5f4f429f3db 90 return false;
duchonic 2:d5f4f429f3db 91 }
duchonic 2:d5f4f429f3db 92 else{
duchonic 2:d5f4f429f3db 93 return true;
duchonic 2:d5f4f429f3db 94 }
duchonic 2:d5f4f429f3db 95 }
duchonic 2:d5f4f429f3db 96
duchonic 2:d5f4f429f3db 97 int getTemparature(void){
duchonic 2:d5f4f429f3db 98 return temperature;
duchonic 2:d5f4f429f3db 99 }
duchonic 2:d5f4f429f3db 100 void sendAck(void){
duchonic 2:d5f4f429f3db 101 MbedJSONValue send;
duchonic 2:d5f4f429f3db 102 send[responseKey] = "OK";
duchonic 2:d5f4f429f3db 103 printf("%s\n", send.serialize().c_str() );
duchonic 2:d5f4f429f3db 104 }
duchonic 2:d5f4f429f3db 105 private:
duchonic 2:d5f4f429f3db 106 int temperature;
duchonic 2:d5f4f429f3db 107 std::string responseKey;
duchonic 2:d5f4f429f3db 108 std::string tempKey;
duchonic 2:d5f4f429f3db 109 };
duchonic 2:d5f4f429f3db 110
duchonic 2:d5f4f429f3db 111 class Status{
duchonic 2:d5f4f429f3db 112 public:
duchonic 2:d5f4f429f3db 113 Status(){
duchonic 2:d5f4f429f3db 114 n1Key[0] = "N1.1";
duchonic 2:d5f4f429f3db 115 n1Key[1] = "N1.2";
duchonic 2:d5f4f429f3db 116 n1Key[2] = "N1.3";
duchonic 2:d5f4f429f3db 117 n1Key[3] = "N1.4";
duchonic 2:d5f4f429f3db 118 n2Key = "N2";
duchonic 2:d5f4f429f3db 119 n3Key = "N3";
duchonic 2:d5f4f429f3db 120 t1Key = "T1";
duchonic 2:d5f4f429f3db 121 hsKey = "HS";
duchonic 2:d5f4f429f3db 122 statusKey = "STATUS";
duchonic 2:d5f4f429f3db 123 }
duchonic 2:d5f4f429f3db 124 void sendFrame(){
duchonic 2:d5f4f429f3db 125 MbedJSONValue send;
duchonic 2:d5f4f429f3db 126 send[n1Key[0]] = n1[0];
duchonic 2:d5f4f429f3db 127 send[n1Key[1]] = n1[1];
duchonic 2:d5f4f429f3db 128 send[n1Key[2]] = n1[2];
duchonic 2:d5f4f429f3db 129 send[n1Key[3]] = n1[3];
duchonic 2:d5f4f429f3db 130 send[t1Key] = t1;
duchonic 2:d5f4f429f3db 131 send[hsKey] = hs;
duchonic 2:d5f4f429f3db 132 send[statusKey] = status;
duchonic 2:d5f4f429f3db 133 printf("%s\n", send.serialize().c_str() );
duchonic 2:d5f4f429f3db 134
duchonic 2:d5f4f429f3db 135 }
duchonic 2:d5f4f429f3db 136 int n1[4];
duchonic 2:d5f4f429f3db 137 int t1;
duchonic 2:d5f4f429f3db 138 int hs;
duchonic 2:d5f4f429f3db 139 int status;
duchonic 2:d5f4f429f3db 140
duchonic 2:d5f4f429f3db 141 private:
duchonic 2:d5f4f429f3db 142 std::string n1Key[4];
duchonic 2:d5f4f429f3db 143 std::string n2Key;
duchonic 2:d5f4f429f3db 144 std::string n3Key;
duchonic 2:d5f4f429f3db 145 std::string t1Key;
duchonic 2:d5f4f429f3db 146 std::string hsKey;
duchonic 2:d5f4f429f3db 147 std::string statusKey;
duchonic 2:d5f4f429f3db 148 };
duchonic 2:d5f4f429f3db 149
duchonic 2:d5f4f429f3db 150 class Command{
duchonic 2:d5f4f429f3db 151 public:
duchonic 2:d5f4f429f3db 152 Command(){
duchonic 2:d5f4f429f3db 153 cmdKey = "CMD";
duchonic 2:d5f4f429f3db 154 }
duchonic 2:d5f4f429f3db 155 Commands get(MbedJSONValue receive){
duchonic 2:d5f4f429f3db 156 std::string cmd = receive[cmdKey].get<std::string>();
duchonic 2:d5f4f429f3db 157
duchonic 2:d5f4f429f3db 158 if(cmd == std::string("TYPE") ){
duchonic 2:d5f4f429f3db 159 return TYPE;
duchonic 2:d5f4f429f3db 160 }
duchonic 2:d5f4f429f3db 161 else if(cmd == std::string("CONF") ){
duchonic 2:d5f4f429f3db 162 return CONF;
duchonic 2:d5f4f429f3db 163 }
duchonic 2:d5f4f429f3db 164 else if(cmd == std::string("STATUS")) {
duchonic 2:d5f4f429f3db 165 return STATUS;
duchonic 2:d5f4f429f3db 166 }
duchonic 2:d5f4f429f3db 167 else{
duchonic 2:d5f4f429f3db 168 assert(0);
duchonic 2:d5f4f429f3db 169 return TYPE;
duchonic 2:d5f4f429f3db 170 }
duchonic 2:d5f4f429f3db 171 }
duchonic 2:d5f4f429f3db 172 private:
duchonic 2:d5f4f429f3db 173 std::string cmdKey;
duchonic 2:d5f4f429f3db 174 };