Ewon Technical Forum

Full Version: Accents Json
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a problem with the display of accents in Thingworx. In the ewon script, I put my variables with accents in a Json file but arrived in thingworx I get "?" instead of the accent.
I put my code in ewon and the script in thingworx.

Ewon:

Code:
Rem --- eWON start section: Cyclic Section
eWON_cyclic_section:
Rem --- eWON user (start)

Rem --- eWON user (end)
End
Rem --- eWON end section: Cyclic Section
Rem --- eWON start section: Init Section
eWON_init_section:
Rem --- eWON user (start)
//################################################
//##########   eWOn API REST v0.3   ##############
//##########     UPDATE INFO        ##############
//################################################
//
//  - Ajout de la gestion de fichier tampon
//  - Ajout de la transmission de decription
//
//################ CONFIGURATION #################
SETSYS INF, "LOAD"
SerNum$ = GETSYS INF, "SERNUM"
appKey$="58876e67-52de-4a70-a19f-a7a96a85963e"
ThingworxIP_URL$="185.253.39.139:8443"//LOCAL "185.253.39.139:8443" //DISTANT
Changepushtime% = 1 //Timer to push only Tags that has changed
Fullpushtime% = 60 //Timer to push all values
PublishTime% = 20 //Timer to publish values
//Select the Tag Group to publish -> 0 or 1
//Tag must be created and at least set in one of the groups.
GROUPA% = 1
GROUPB% = 0
GROUPC% = 0
GROUPD% = 0
//Taille Limite d'un fichier tampon (300 fichier x ~100ko)
limit% = 102400
NbTmpFile% = 300
//################ ENDCONFIGURATION #################
//################ INIT #################
ThingName$ = "eWon_" + SerNum$
actionID% = 0
//Record the Tag ONCHANGE events into an array.
//Allows to post only values that have changed
json$ = ""
FileNumber% = 1 // Numéro du fichier tampon en cours
DIM fileState(NbTmpFile%) // Tableau d'état des fichier tampon (0 = vide // 1 = plein ou utilisé)
DIM file$(NbTmpFile%,21) // Tableau des fichier tampon
// Création des fichiers tampon
FOR a% = 1 TO 9
  file$(a%) = "file:/usr/tmp_00" + STR$ a% + ".csv"
  //RAZ du fichier
  OPEN file$(a%) FOR BINARY OUTPUT AS 1
  CLOSE 1
  fileState(a%) = 0
NEXT a%
FOR a% = 10 TO 99
  file$(a%) = "file:/usr/tmp_0" + STR$ a% + ".csv"//RAZ du fichier
  OPEN file$(a%) FOR BINARY OUTPUT AS 1
  CLOSE 1
  fileState(a%) = 0
NEXT a%
FOR a% = 100 TO NbTmpFile%
  file$(a%) = "file:/usr/tmp_" + STR$ a% + ".csv"//RAZ du fichier
  OPEN file$(a%) FOR BINARY OUTPUT AS 1
  CLOSE 1
  fileState(a%) = 0
NEXT a%
NB%= GETSYS PRG,"NBTAGS"
DIM a(NB%,2)
FOR i% = 0 TO NB%-1
  k%=i%+1
  SETSYS Tag, "load",-i%
  a(k%,1)=-i%
  a(k%,2) = 0
  GroupA$= GETSYS TAG,"IVGROUPA"
  GroupB$= GETSYS TAG,"IVGROUPB"
  GroupC$= GETSYS TAG,"IVGROUPC"
  GroupD$= GETSYS TAG,"IVGROUPD"
  tagName$= GETSYS TAG,"Name"
  tagDescription$ = GETSYS TAG,"Description"
  TagIsSelected% = 0
  IF GroupA$ = "1" And GROUPA%= 1 THEN TagIsSelected% = 1
  IF GroupB$ = "1" And GROUPB%= 1 THEN TagIsSelected% = 1
  IF GroupC$ = "1" And GROUPC%= 1 THEN TagIsSelected% = 1
  IF GroupD$ = "1" And GROUPD%= 1 THEN TagIsSelected% = 1
  IF TagIsSelected% = 1 THEN
      Onchange -i%, "a("+ STR$ k%+",2)= 1"

      IF fileState(FileNumber%) = 1 THEN
        json$ = json$ + ","
      ELSE
        fileState(FileNumber%) = 1
      ENDIF
      json$ = json$ + '{"property":"' + tagName$ + '",' + '"description":"' + tagDescription$ + '"}'
  ENDIF
NEXT i%
IF json$ <> "" THEN
  @WriteTmpProperties(json$)
ELSE
  FileNumber% = FileNumber% + 1
ENDIF
             
//################ SCRIPT CALL #################
ONSTATUS "goto onEvent"
ONTIMER 1,"goto PublishAllValues"
ONTIMER 2, "goto PublishChangedValues"
ONTIMER 3, "goto GestionTmp"
TSET 1,Fullpushtime%
TSET 2,Changepushtime%
TSET 3,PublishTime%
End

//################ PublishAllValues #################
PublishAllValues:
  json$ = ''
  FOR i% = 0 TO NB% -1
    SETSYS Tag, "load",-i%
    tagName$= GETSYS TAG,"Name"
    GroupA$= GETSYS TAG,"IVGROUPA"
    GroupB$= GETSYS TAG,"IVGROUPB"
    GroupC$= GETSYS TAG,"IVGROUPC"
    GroupD$= GETSYS TAG,"IVGROUPD"
    TagIsSelected% = 0
    IF GroupA$ = "1" And GROUPA%= 1 THEN TagIsSelected% = 1
    IF GroupB$ = "1" And GROUPB%= 1 THEN TagIsSelected% = 1
    IF GroupC$ = "1" And GROUPC%= 1 THEN TagIsSelected% = 1
    IF GroupD$ = "1" And GROUPD%= 1 THEN TagIsSelected% = 1
    IF TagIsSelected% = 1 THEN
      If fileState(FileNumber%) = 1 Then
        json$ = json$ + ","
      ELSE
        fileState(FileNumber%) = 1
      ENDIF
      json$ = json$ + '{' + @ComputeJson$(tagName$) + '}'
    ENDIF
  NEXT i%
  IF json$ <> "" THEN
    @WriteTmp(json$)
  ENDIF
End
//################ END PublishAllValues #################

//################ PublishChangeValues #################
//Publish just the changed tags
PublishChangedValues:
json$ = ''
FOR r% = 1 TO NB%
  IF a( r%,2) = 1 THEN
    a(r%,2) = 0
    negIndex% = a(r%,1)
    SETSYS Tag, "LOAD", negIndex%
    tagName$= GETSYS Tag, "name"
      If fileState(FileNumber%) = 1 Then
        json$ = json$ + ","
      ELSE
        fileState(FileNumber%) = 1
      ENDIF
    json$ = json$ + '{' + @ComputeJson$(tagName$) + '}'
  ENDIF
NEXT r%
IF json$ <> "" THEN
  @WriteTmp(json$)
ENDIF
END
//################ END PublishChangeValues #################
//################ ComputeJson #################
FUNCTION ComputeJson$($tagname$)
    SETSYS TAG, "LOAD", $tagname$
    $TagFormat$ = GETSYS TAG, "Type"
    DateTime$ = TIME$
    DateTime% = FCNV DateTime$, 40
    IF $TagFormat$ = "6" THEN //String
      $ComputeJson$ = '"property":"' + $tagname$ + '",'
      $ComputeJson$ = $ComputeJson$ + '"value":' + GETIO $tagname$ + ','
      $ComputeJson$ = $ComputeJson$ + '"date":' + STR$ DateTime%
    ELSE //ANALOG
        $ComputeJson$ = '"property":"' + $tagname$ + '",'
        $ComputeJson$ = $ComputeJson$ + '"value":' + STR$ GETIO $tagname$ + ','
        $ComputeJson$ = $ComputeJson$ + '"date":' + STR$ DateTime%
    ENDIF
ENDFN
//################ END ComputeJson #################

//################ GestionTmp #################
GestionTmp:
result$ = GETSYS PRG, "WANIP"
IF result$ <> "0.0.0.0" THEN
    //PRINT "WAN online with address" ; GETSYS PRG, "WANIP"
    FOR f% = 1 TO NbTmpFile%
        IF fileState(f%) = 1 THEN
            //PRINT f%
            OPEN file$(f%) FOR BINARY INPUT AS 1
            json$ = '{"data":['
            ReadNext:
            IF EOF 1 THEN GOTO "ReadDone"
            CHAR$ = GET 1, 5000
            json$ = json$ + CHAR$
            GOTO "ReadNext"
            ReadDone:
            CLOSE 1
           
            json$ = json$ + ']}'
           
            IF f% = 1 THEN
              @UpdateThingWorxData(json$, "parseTagProperty")
            ELSE
              @UpdateThingWorxData(json$, "parseData")
              //RAZ du fichier
              OPEN file$(f%) FOR BINARY OUTPUT AS 1
              CLOSE 1
            ENDIF
           
            fileState(f%) = 0
        ENDIF
    NEXT f%
ELSE
    PRINT "WAN offline"
ENDIF
END
//################ END GestionTmp #################
//################ UpdateThingWorxTagProperty #################
Function UpdateThingWorxData($json$,$services$)
  $url$ = "https://"+ThingworxIP_URL$+"/Thingworx/Things/"+ThingName$+"/Services/" + $services$
  $method$ = "POST"
  $header$ = "Content-Type=application/json;charset=UTF-8&Accept=application/json&appKey=" + appKey$
  REQUESTHTTPX $url$, $method$, $header$, $json$
  actionID% = GETSYS PRG, "ACTIONID"
ENDFN
//################ END UpdateThingWorxTagProperty #################
//################ WriteTmpProperties #################
Function WriteTmpProperties($datajson$)
 
  OPEN file$(FileNumber%) FOR BINARY OUTPUT AS 1
  PUT 1, $datajson$
  CLOSE 1
  FileNumber% = FileNumber% + 1
ENDFN
//################ END WriteTmp #################
//################ WriteTmp #################
Function WriteTmp($datajson$)
  OPEN file$(FileNumber%) FOR BINARY APPEND AS 1
  PUT 1, $datajson$
  CLOSE 1
  OPEN file$(FileNumber%) FOR BINARY INPUT AS 1
  a% = GET 1, "SIZE"
  CLOSE 1
  IF a% > limit% THEN
    IF FileNumber% < NbTmpFile% THEN
      FileNumber% = FileNumber% + 1
    ELSE
      FileNumber% = 2
    ENDIF
    //RAZ du nouveau fichier
    OPEN file$(FileNumber%) FOR BINARY OUTPUT AS 1
    CLOSE 1
  ENDIF
ENDFN
//################ END WriteTmp #################
//################ onEvent #################
onEvent:
eventId% = GETSYS PRG, "EVTINFO"
IF (eventId% = actionID%) THEN
  //PRINT "ID de l'event " + STR$ eventID%
  SETSYS PRG, "ACTIONID", eventId%
  stat% = GETSYS PRG, "ACTIONSTAT"
  IF (stat% = 0) THEN
    GOTO "response"
  ELSE
    PRINT "Error (ERROR = "+Str$(stat%) + ")"
  ENDIF
ENDIF
END
//################ END onEvent #################
//################ response #################
response:
//a$ = RESPONSEHTTPX "STATUSCODE"
//PRINT "status: "; a$
//a$ = RESPONSEHTTPX "HEADER"
//PRINT "all headers: "; a$
//a$ = RESPONSEHTTPX "HEADER", "Server"
//PRINT "server header: "; a$
  a$ = RESPONSEHTTPX "RESPONSE-BODY"
  IF (Len(a$) < 1000) THEN
  PRINT "response body: "; a$
  Else
  PRINT "response body size: "; Len(a$)
ENDIF
//################ END response #################

Rem --- eWON user (end)
End
Rem --- eWON end section: Init Section



Thingworx:

Code:
    var performed = false;
    var nbPropertyAdd;



    if (me.machine !== undefined && me.machine !== null && me.machine !== "" && Things[me.machine] !== undefined)
    {
        var machine = Things[me.machine];

        if (machine.customer !== undefined && machine.customer !== null && machine.customer !== "" )
        {
            //Liste des propriètés déjà existante
            var properties = machine.GetPropertyDefinitions({type: "NUMBER"});

            //Liste des propriètés reçus
            var tableLength = data.array.length;
            for (var x = 0; x < tableLength; x++)
            {
               
                var property = JSON.parse(data.array[x].property);
                var description = JSON.parse(data.array[x].description);
               
               

                logger.debug("property="+ property +";"+"description="+  description);

                //Si la propriété n'existe pas on la créé
                //Si elle existe on update la description
                var Trouvé = 0;
                for (var i = 0; i < properties.length; i++) {
                    if ( properties[i].name == property)
                    {
                        Trouvé = 1;   
                    }
                }
                if (Trouvé === 1)
                {
                    try {
                        machine.RemovePropertyDefinition({
                            name: property,
                        });                   
                    }

                    catch(ex2) {//Property was not created
                    }
                }

                try {
                    machine.AddPropertyDefinition({
                        type: "NUMBER",
                        name: property,
                        description : description,
                        persistent: true,
                        logged: true
                    });
                    nbPropertyAdd += 1;
                    //logger.debug("property="+ property+";"+"description="+ description);
                }
                catch (err) {
                    // If an exception is caught, we need to attempt to delete everything
                    // that was created to roll back the entire transaction.
                    // If we do not do this a "ghost" entity will remain in memory
                    // We must do this in reverse order of creation so there are no dependency conflicts
                    // We also do not know where it failed so we must attempt to remove all of them,
                    // but also handle exceptions in case they were not created
                    nbPropertyAdd = 0;
                    try {
                        machine.RemovePropertyDefinition({
                            name: property,
                        });                   
                    }

                    catch(ex2) {//Property was not created
                    }



                    performed = true;
                }
            }
            if (nbPropertyAdd !== 0)
            {
                machine.RestartThing();
            }

        }
    }
    var resultJSON = {
        Success:performed
    };
    result = resultJSON;


Thank you in advance for your help
Hello,

It is most likely due to the fact that the Ewon firmware does not support UTF8 but iso-8859-1.

Simon
(01-02-2021, 02:52 PM)simon Wrote: [ -> ]Hello,

It is most likely due to the fact that the Ewon firmware does not support UTF8 but iso-8859-1.

Simon

Thank you for your answer.
I was thinking maybe using iconv with requireJS but apparently it's not compatible with thingworx.
Do you have any idea how to solve my accent problem?

Cordially,

Mathis