Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************* OPCEthernet */
- OPCEthernet::OPCEthernet() {
- }
- void OPCEthernet::after_setup(uint8_t listen_port)
- {
- internal_ethernet_server = new WiFiServer(listen_port);
- internal_ethernet_server->begin();
- }
- int OPCEthernet::setup(uint8_t listen_port, char *ssid, char *pass)
- {
- WiFi.begin(ssid, pass);
- after_setup(listen_port);
- }
- void OPCEthernet::sendOPCItemsMap()
- {
- client.print(F("["));
- for(int k=0;k<OPCItemsCount;k++) {
- if (k) client.print(F(","));
- client.print(F("{\"ItemId\":\""));
- client.print(OPCItemList[k].itemID);
- client.print(F("\",\"AccessRight\":\""));
- client.print(int(OPCItemList[k].opcAccessRight));
- client.print(F("\",\"ItemType\":\""));
- client.print(int(OPCItemList[k].itemType));
- client.print(F("\"}"));
- }
- client.print(F("]"));
- }
- void OPCEthernet::processClientCommand()
- {
- char *p,*j;
- bool matched = false;
- bool (*bool_callback)(const char *itemID, const opcOperation opcOP, const bool value);
- byte (*byte_callback)(const char *itemID, const opcOperation opcOP, const byte value);
- int (*int_callback)(const char *itemID, const opcOperation opcOP, const int value);
- float (*float_callback)(const char *itemID, const opcOperation opcOP, const float value);
- client.println(F("HTTP/1.1 200 OK\r\nContent-Type: text/json\r\nConnection: close\r\n"));
- if (!strcmp(buffer, "itemsmap")) {
- sendOPCItemsMap();
- }
- else
- {
- p = strtok_r(buffer,"=",&j);
- if (!j[0]) {
- for (int i = 0; i < OPCItemsCount; i++) {
- if (!strcmp(buffer, OPCItemList[i].itemID)) {
- // Execute the stored handler function for the command
- client.print(F("[{\"ItemId\":\""));
- client.print(buffer);
- client.print(F("\",\"ItemValue\":\""));
- switch (OPCItemList[i].itemType) {
- case opc_bool :
- bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback);
- client.print(bool_callback(OPCItemList[i].itemID,opc_opread,NULL));
- break;
- case opc_byte :
- byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback);
- client.print(byte_callback(OPCItemList[i].itemID,opc_opread,NULL));
- break;
- case opc_int :
- int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback);
- client.print(int_callback(OPCItemList[i].itemID,opc_opread,(int)NULL));
- break;
- case opc_float :
- float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float value))(OPCItemList[i].ptr_callback);
- client.print(float_callback(OPCItemList[i].itemID,opc_opread,NULL));
- break;
- } /* end switch */
- client.print(F("\"}]"));
- matched = true;
- break;
- } /* end if */
- } /* end for */
- } /* end if */
- else
- {
- for (int i = 0; i < OPCItemsCount; i++) {
- if (!strcmp(buffer, OPCItemList[i].itemID)) {
- // Call the stored handler function for the command
- switch (OPCItemList[i].itemType) {
- case opc_bool :
- bool_callback = (bool (*)(const char *itemID, const opcOperation opcOP, const bool value))(OPCItemList[i].ptr_callback);
- bool_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j));
- break;
- case opc_byte :
- byte_callback = (byte (*)(const char *itemID, const opcOperation opcOP, const byte value))(OPCItemList[i].ptr_callback);
- byte_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j));
- break;
- case opc_int :
- int_callback = (int (*)(const char *itemID, const opcOperation opcOP, const int value))(OPCItemList[i].ptr_callback);
- int_callback(OPCItemList[i].itemID,opc_opwrite,atoi(j));
- break;
- case opc_float :
- float_callback = (float (*)(const char *itemID, const opcOperation opcOP, const float))(OPCItemList[i].ptr_callback);
- float_callback(OPCItemList[i].itemID,opc_opwrite,atof(j));
- break;
- } /* end case */
- matched = true;
- break;
- } /* end if */
- } /* end for */
- } /* end else */
- } /* end else */
- }
- void OPCEthernet::processOPCCommands()
- {
- client = internal_ethernet_server->available();
- if (client) {
- boolean currentLineIsBlank = true;
- byte s = 0;
- boolean responsed = false;
- while (!responsed && client.connected()) {
- if (client.available()) {
- char c = client.read();
- if (c == '\n' && currentLineIsBlank) {
- processClientCommand();
- responsed = true;
- }
- else if (c == '\n') {
- currentLineIsBlank = true;
- }
- else if (c != '\r') {
- currentLineIsBlank = false;
- switch (s) {
- case 0 : if (c == 'G') s++; break;
- case 1 : if (c == 'E') s++; else s = 0; break;
- case 2 : if (c == 'T') s++; else s = 0; break;
- case 3 : if (c == ' ') s++; else s = 0; break;
- case 4 : if (c == '/') { s++; bufPos = 0;} else s = 0; break;
- case 5 : if (c != ' ') {
- buffer[bufPos++] = c;
- buffer[bufPos] = '\0';
- }
- else s = 0;
- }
- }
- }
- }
- delay(1); // Espera para dar tiempo al navegador a recibir los datos.
- client.stop(); // Cierra la conexión.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement