mycontroller-org / mycontroller-v1-legacy

The Open Source Controller
http://www.MyController.org
Apache License 2.0
148 stars 90 forks source link

[Question] Jython unicode #416

Closed cimba007 closed 6 years ago

cimba007 commented 6 years ago

@jkandasa

This is no "bug" in mycontroller but still somehow related.

2017-11-01 15:44:06,235 ERROR [Thread-6] [org.mycontroller.standalone.operation.model.OperationExecuteScript:137] Exception on McScript(engineName=null, mimeType=null, extension=py, name=/home/pi/mycontroller/conf/resources/scripts/operations/telegram_bot.py, canonicalPath=null, type=OPERATION, size=0, lastModified=0, bindings={notification=Rule definition: Alarm durch Kontakt an der Terrassentuer innen\nCondition: State [ if {[G]:mGW >> [N]:0:GW >> [S]:1:433 Terrassent?r (T?r) >> [SV]:Tripped} == {On} ]\nPresent value: 1\nTriggered at: Nov 01, 2017 15:44:06 CET\n--- www.mycontroller.org, message=Alarm durch Kontakt an der Terrassent?r innen})
javax.script.ScriptException: UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 37: ordinal not in range(128) in <script> at line number 4

The critical code is this

if 'notification' in locals() or 'notification' in globals():
   with open("test.txt", "a") as myfile:
      myfile.write(str(notification)+"\n")

Do you have any idea how to solve the encoding problem?

cimba007 commented 6 years ago

I solved my issue:

if 'notification' in locals() or 'notification' in globals():
   with open("test.txt", "a") as myfile:
      vv = unicode(notification,'utf-8')
      myfile.write(vv.encode('utf8')+"\n")