Advertisement
Guest User

OPC.cpp - OPCEthernet

a guest
Jan 14th, 2016
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.18 KB | None | 0 0
  1. /************************************* OPCEthernet */
  2.  
  3. OPCEthernet::OPCEthernet() {
  4. }
  5.  
  6. void OPCEthernet::after_setup(uint8_t listen_port)
  7. {
  8.     internal_ethernet_server = new WiFiServer(listen_port);
  9.     internal_ethernet_server->begin();
  10. }
  11.  
  12. int OPCEthernet::setup(uint8_t listen_port, char *ssid, char *pass)
  13. {
  14.     WiFi.begin(ssid, pass);
  15.     after_setup(listen_port);
  16. }
  17.  
  18. void OPCEthernet::sendOPCItemsMap()
  19. {
  20.     client.print(F("["));
  21.  
  22.     for(int k=0;k<OPCItemsCount;k++) {
  23.         if (k) client.print(F(","));
  24.         client.print(F("{\"ItemId\":\""));
  25.         client.print(OPCItemList[k].itemID);
  26.         client.print(F("\",\"AccessRight\":\""));
  27.         client.print(int(OPCItemList[k].opcAccessRight));
  28.         client.print(F("\",\"ItemType\":\""));
  29.         client.print(int(OPCItemList[k].itemType));
  30.         client.print(F("\"}"));
  31.     }
  32.  
  33.     client.print(F("]"));
  34. }
  35.  
  36. void OPCEthernet::processClientCommand()
  37. {
  38.     char *p,*j;
  39.     bool matched = false;
  40.     bool (*bool_callback)(const char *itemID, const opcOperation opcOP, const bool value);
  41.     byte (*byte_callback)(const char *itemID, const opcOperation opcOP, const byte value);
  42.     int (*int_callback)(const char *itemID, const opcOperation opcOP, const int value);
  43.     float (*float_callback)(const char *itemID, const opcOperation opcOP, const float value);
  44.  
  45.     client.println(F("HTTP/1.1 200 OK\r\nContent-Type: text/json\r\nConnection: close\r\n"));
  46.  
  47.     if (!strcmp(buffer, "itemsmap")) {
  48.         sendOPCItemsMap();
  49.     }
  50.     else
  51.     {
  52.         p = strtok_r(buffer,"=",&j);
  53.         if (!j[0])  {
  54.             for (int i = 0; i < OPCItemsCount; i++) {
  55.                 if (!strcmp(buffer, OPCItemList[i].itemID))  {
  56.                     // Execute the stored handler function for the command
  57.                     client.print(F("[{\"ItemId\":\""));
  58.                     client.print(buffer);
  59.                     client.print(F("\",\"ItemValue\":\""));
  60.  
  61.                     switch (OPCItemList[i].itemType) {
  62.                         case opc_bool :
  63.                         bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback);
  64.                         client.print(bool_callback(OPCItemList[i].itemID,opc_opread,NULL));
  65.                         break;
  66.                         case opc_byte :
  67.                         byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback);
  68.                         client.print(byte_callback(OPCItemList[i].itemID,opc_opread,NULL));
  69.                         break;
  70.                         case opc_int :
  71.                         int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback);
  72.                         client.print(int_callback(OPCItemList[i].itemID,opc_opread,(int)NULL));
  73.                         break;
  74.                         case opc_float :
  75.                         float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float value))(OPCItemList[i].ptr_callback);
  76.                         client.print(float_callback(OPCItemList[i].itemID,opc_opread,NULL));
  77.                         break;
  78.                     } /* end switch */
  79.  
  80.                     client.print(F("\"}]"));
  81.  
  82.                     matched = true;
  83.                     break;
  84.                 } /* end if */
  85.             } /* end for */
  86.         } /* end if */
  87.         else
  88.         {
  89.             for (int i = 0; i < OPCItemsCount; i++) {
  90.                 if (!strcmp(buffer, OPCItemList[i].itemID))  {
  91.  
  92.                     // Call the stored handler function for the command
  93.                     switch (OPCItemList[i].itemType) {
  94.                         case opc_bool :
  95.                         bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback);
  96.                         bool_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j));
  97.                         break;
  98.                         case opc_byte :
  99.                         byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback);
  100.                         byte_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j));
  101.                         break;
  102.                         case opc_int :
  103.                         int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback);
  104.                         int_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j));
  105.                         break;
  106.                         case opc_float :
  107.                         float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float))(OPCItemList[i].ptr_callback);
  108.                         float_callback(OPCItemList[i].itemID,opc_opwrite,atof(j));
  109.                         break;
  110.                     } /* end case */
  111.  
  112.                     matched = true;
  113.                     break;
  114.                 } /* end if */
  115.             } /* end for */
  116.         } /* end else */
  117.     } /* end else */
  118. }
  119.  
  120. void OPCEthernet::processOPCCommands()
  121. {
  122.     client = internal_ethernet_server->available();
  123.     if (client) {
  124.         boolean currentLineIsBlank = true;
  125.  
  126.         byte s = 0;
  127.         boolean responsed = false;
  128.  
  129.         while (!responsed && client.connected()) {
  130.             if (client.available()) {
  131.                 char c = client.read();
  132.  
  133.                 if (c == '\n' && currentLineIsBlank) {
  134.                     processClientCommand();
  135.                     responsed = true;
  136.                 }
  137.                 else if (c == '\n') {
  138.                     currentLineIsBlank = true;
  139.                 }
  140.                 else if (c != '\r') {
  141.                     currentLineIsBlank = false;
  142.  
  143.                     switch (s) {
  144.                         case 0 : if (c == 'G') s++; break;
  145.                         case 1 : if (c == 'E') s++; else s = 0; break;
  146.                         case 2 : if (c == 'T') s++; else s = 0; break;
  147.                         case 3 : if (c == ' ') s++; else s = 0; break;
  148.                         case 4 : if (c == '/') { s++; bufPos = 0;} else s = 0; break;
  149.                         case 5 : if (c != ' ') {
  150.                             buffer[bufPos++] = c;
  151.                             buffer[bufPos] = '\0';
  152.                         }
  153.                         else s = 0;
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.         delay(1); // Espera para dar tiempo al navegador a recibir los datos.
  159.         client.stop(); // Cierra la conexión.
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement