This page last changed on Aug 25, 2015 by robnas.

Hello,

Im trying to get a random hexvalue to a string:
according to http://www.paulirish.com/2009/random-hex-color-code-snippets/
i would like to use this code:

(Math.random().toString(16) + '0000000').slice(2, 8)

it has to be converted to a string
i tried:

String test = (int)(Math.random().toString(16) + '0000000').slice(2, 8);

and:

String test = String.format("%x",(int)(Math.random().toString(16) + '0000000').slice(2, 8);

but they both fail, can someone help me to get this right?


Naamloos3.jpg (image/jpeg)
Naamloos11.jpg (image/jpeg)

Your first line already returns string.

Posted by aktur at Aug 26, 2015 08:51

Correct but I need 16 strings of this and would like to have something like the second line. So string1 = string2 = and so on. Do you know how? They will be combined later in one string and send to a Wifi370 rgb controller

Posted by robnas at Sep 01, 2015 14:46

The second line should be simply

String test = (Math.random().toString(16) + '0000000').slice(2, 8);
Posted by aktur at Sep 01, 2015 14:56

i tried that but the boot.log shows an error:

ERROR 2015-09-02 13:23:15,664 : Rule definition 'modeler_rules.drl' could not be deployed. See errors below.
ERROR 2015-09-02 13:23:15,664 : Rule Compilation error Invalid character constant
ERROR 2015-09-02 13:23:15,665 : There was an error parsing the rule definition 'modeler_rules.drl' : Could not parse knowledge.
java.lang.IllegalArgumentException: Could not parse knowledge.
	at org.drools.builder.impl.KnowledgeBuilderImpl.newKnowledgeBase(KnowledgeBuilderImpl.java:58)
	at org.openremote.controller.statuscache.rules.RuleEngine.getValidKnowledgePackages(RuleEngine.java:532)
	at org.openremote.controller.statuscache.rules.RuleEngine.start(RuleEngine.java:253)
	at org.openremote.controller.statuscache.EventProcessorChain.start(EventProcessorChain.java:112)
	at org.openremote.controller.statuscache.StatusCache.start(StatusCache.java:120)
	at org.openremote.controller.deployer.Version20ModelBuilder.buildSensorModel(Version20ModelBuilder.java:659)
	at org.openremote.controller.deployer.Version20ModelBuilder.build(Version20ModelBuilder.java:557)
	at org.openremote.controller.deployer.AbstractModelBuilder.buildModel(AbstractModelBuilder.java:154)
	at org.openremote.controller.service.Deployer.startup(Deployer.java:872)
	at org.openremote.controller.service.Deployer.softRestart(Deployer.java:454)
	at org.openremote.controller.service.Deployer$ControllerDefinitionWatch.run(Deployer.java:1411)
	at java.lang.Thread.run(Thread.java:662)
INFO 2015-09-02 13:23:15,702 : Initialized event processor : Drools Rule Engine
INFO 2015-09-02 13:23:15,831 : Initialized event processor : RRD4J Data Logger
INFO 2015-09-02 13:23:15,831 : Initialized event processor : EmonCMS Data Logger

i tried without the ' and with "000000" but then the folowing error:

ERROR 2015-09-02 13:38:04,645 : Rule definition 'modeler_rules.drl' could not be deployed. See errors below.
ERROR 2015-09-02 13:38:04,646 : Rule Compilation error Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
Cannot invoke toString(int) on the primitive type double
ERROR 2015-09-02 13:38:04,648 : There was an error parsing the rule definition 'modeler_rules.drl' : Could not parse knowledge.
java.lang.IllegalArgumentException: Could not parse knowledge.

Posted by robnas at Sep 02, 2015 14:28

Seems like this code is not compatible with Java that Openremote uses. You will need to find different code snippets with less hacking, or write your own, clean code.

Posted by aktur at Sep 02, 2015 15:55

i found a solution already, i need a string of 96 bytes, mostly the string will be 94, 95 or 96 bytes. if lower then 96 bytes a "0" or "00" will be added to the header of the string

heres the code for controlling 2 wificontrollers with a switch its possible to chain them so the rgb controllers will output the same colours

rule "wifi rgb controller 1 + 2 random" 
timer (int: 2s 180s)
when eval (true)
Event( source=="random.wifi1", $val01 : value)
Event( source=="random.wifi2", $val02 : value)
Event( source=="koppeling", $val03 : value)
then
  String val01a = $val01.toString();
  String val02a = $val02.toString();
  String val03a = $val03.toString();
if (val01a.length() == 2) {

String valStra1 = String.format("%x",(int)(Math.random()*16777216));
String valStra2 = String.format("%x",(int)(Math.random()*16777216));
String valStra3 = String.format("%x",(int)(Math.random()*16777216));
String valStra4 = String.format("%x",(int)(Math.random()*16777216));
String valStra5 = String.format("%x",(int)(Math.random()*16777216));
String valStra6 = String.format("%x",(int)(Math.random()*16777216));
String valStra7 = String.format("%x",(int)(Math.random()*16777216));
String valStra8 = String.format("%x",(int)(Math.random()*16777216));
String valStra9 = String.format("%x",(int)(Math.random()*16777216));
String valStra10 = String.format("%x",(int)(Math.random()*16777216));
String valStra11 = String.format("%x",(int)(Math.random()*16777216));
String valStra12 = String.format("%x",(int)(Math.random()*16777216));
String valStra13 = String.format("%x",(int)(Math.random()*16777216));
String valStra14 = String.format("%x",(int)(Math.random()*16777216));
String valStra15 = String.format("%x",(int)(Math.random()*16777216));
String valStra16 = String.format("%x",(int)(Math.random()*16777216));

String hex1b = valStra1.toString() + valStra2.toString() + valStra3.toString() + valStra4.toString() + valStra5.toString() + valStra6.toString() + valStra7.toString() + valStra8.toString() + valStra9.toString() + valStra10.toString() + valStra11.toString() + valStra12.toString() + valStra13.toString() + valStra14.toString() + valStra15.toString() + valStra16.toString() ;

String snelh = "1f";
String fade = "3a";
String foot2 = "ff";
String foot3 = "66";

if (hex1b.length() == 96) {
String total1 = "99" + hex1b.toString() +  snelh.toString() + fade.toString() + foot2.toString() + foot3.toString();

execute.command("wifi1.shell.sed.fade", total1.toString());
log("Verzonden RGB string Wifi1 controller:" + " " + total1.toString());

}
if (hex1b.length() == 95) {
String total1 = "99" + "0"  + hex1b.toString() +  snelh.toString() + fade.toString() + foot2.toString() + foot3.toString();

execute.command("wifi1.shell.sed.fade", total1.toString());
log("Verzonden RGB string Wifi1 controller:" + " " + total1.toString());

}
if (hex1b.length() == 94) {
String total1 = "99" + "00" +  hex1b.toString() +  snelh.toString() + fade.toString() + foot2.toString() + foot3.toString();

execute.command("wifi1.shell.sed.fade", total1.toString());
log("Verzonden RGB string Wifi1 controller:" + " " + total1.toString());
 }
}
if (val02a.length() == 2) {
String valStra1 = String.format("%x",(int)(Math.random()*16777216));
String valStra2 = String.format("%x",(int)(Math.random()*16777216));
String valStra3 = String.format("%x",(int)(Math.random()*16777216));
String valStra4 = String.format("%x",(int)(Math.random()*16777216));
String valStra5 = String.format("%x",(int)(Math.random()*16777216));
String valStra6 = String.format("%x",(int)(Math.random()*16777216));
String valStra7 = String.format("%x",(int)(Math.random()*16777216));
String valStra8 = String.format("%x",(int)(Math.random()*16777216));
String valStra9 = String.format("%x",(int)(Math.random()*16777216));
String valStra10 = String.format("%x",(int)(Math.random()*16777216));
String valStra11 = String.format("%x",(int)(Math.random()*16777216));
String valStra12 = String.format("%x",(int)(Math.random()*16777216));
String valStra13 = String.format("%x",(int)(Math.random()*16777216));
String valStra14 = String.format("%x",(int)(Math.random()*16777216));
String valStra15 = String.format("%x",(int)(Math.random()*16777216));
String valStra16 = String.format("%x",(int)(Math.random()*16777216));

String hex1a = valStra1.toString() + valStra2.toString() + valStra3.toString() + valStra4.toString() + valStra5.toString() + valStra6.toString() + valStra7.toString() + valStra8.toString() + valStra9.toString() + valStra10.toString() + valStra11.toString() + valStra12.toString() + valStra13.toString() + valStra14.toString() + valStra15.toString() + valStra16.toString() ;

String snelh = "1f";
String fade = "3a";
String foot2 = "ff";
String foot3 = "66";

if (hex1a.length() == 96) {
String total2 = "99" + hex1a.toString() +  snelh.toString() + fade.toString() + foot2.toString() + foot3.toString();

execute.command("wifi2.shell.sed.fade", total2.toString());
log("Verzonden RGB string Wifi2 controller:" + " " + total2.toString());
if (val03a.length() == 2) {
execute.command("wifi1.shell.sed.fade", total2.toString());
log("Koppeling actief, command verzonden naar Wifi Controller1");
 }
}
if (hex1a.length() == 95) {
String total2 = "99" + "0"  + hex1a.toString() +  snelh.toString() + fade.toString() + foot2.toString() + foot3.toString();

execute.command("wifi2.shell.sed.fade", total2.toString());
log("Verzonden RGB string Wifi2 controller:" + " " + total2.toString());
if (val03a.length() == 2) {
execute.command("wifi1.shell.sed.fade", total2.toString());
log("Koppeling actief, command verzonden naar Wifi Controller1");
 }
}
if (hex1a.length() == 94) {
String total2 = "99" + "00"  + hex1a.toString() +  snelh.toString() + fade.toString() + foot2.toString() + foot3.toString();

execute.command("wifi2.shell.sed.fade", total2.toString());
log("Verzonden RGB string Wifi2 controller:" + " " + total2.toString());
if (val03a.length() == 2) {
execute.command("wifi1.shell.sed.fade", total2.toString());
log("Koppeling actief, command verzonden naar Wifi Controller1");
 }
}
}
end

wifi1.shell.sed.fade is a shell command,

shell command contains this code:

echo -ne "$(echo $1 | sed -e 's/../\\x&/g')" | nc -q 3 192.168.1.43 5577

note: Bootstrap, Netcat and Sed needs to be installed. Sed will put a \x after each colour in the string and send it to the controller

more info: http://www.openremote.org/pages/viewpage.action?pageId=23600633

Posted by robnas at Sep 03, 2015 12:38

Nice to hear that you've got it working.

I still don't find it pretty that you need to use netcat through shell. It is not easy portable and uses too many resources -
shell command allocates the same memory on heap as the running openremote process causing problems on smaller systems with less memory.
The reason is that TCP command is blocking and you use -q switch in netcat to force quit. If your wificontroller accepts UDP instead of TCP then
it would not block. Otherwise we would need to amend the TCP command implementation. Would you mind to report it in jira?
IMHO, this addition would be useful with other systems too, i.e. non-blocking TCP command which terminates connection after EOF of input data.

Posted by aktur at Sep 03, 2015 12:55

Michal, i agree that this is not the best solution via shell. but the controller only accepts TCP not UDP ive tried UDP but noting happends.
if the TCP protocol could be amended that would be great and the code will be a lot easyier. i will report this in JIRA and hopefully it could be fixed.

Posted by robnas at Sep 03, 2015 13:05

jira issue: http://jira.openremote.org/browse/INFRA-14

Posted by robnas at Sep 03, 2015 13:22

this also works:

String valStra1 = String.format("%06x",(int)(Math.random()*16777216));

%06 ensures that the output will be 6 chars, if not the beginchars will be filled with zeros, x converts them in to HEX, use X for Capital HEX chars

Posted by robnas at Sep 03, 2015 23:41

i think i solved this tcp/udp thing.
you could login the rgb controller and set it to udp or tcp, standaard the tcp port is set to 300sec which can be ajusted (see pic below)

you can enter the ip of the rgb controller in yourbrowser (port 80) with login: admin and pass: nimda
after that you can adjust it.

i tried to set to tcp with 1 second and it works but its not the best solution as sometimes you have to push the button twice probably of the lag or else, but set the time to 5 seconds would freeze your rules again (as descibed above)
note that the android app for this controller will not work when setting this to 1 sec

im expirimenting with udp now because that works to and there is no closing time. also your sandard android app will not work this app works only in tcp mode, so if you want everyting to work then netcat and sed is a good alternative.
i had also installed my openremote controller on a rasberry pi2 which works excellent will all functions see: http://www.openremote.org/display/forums/New+Raspberry+Pi+2?focusedCommentId=23601122#comment-23601122

the rule code could be the same, put this code in your TCP or UDP command line

0x${param}

so that rules will send the hex code to

${param}

this will show when you login the rgb controller:

Posted by robnas at Sep 09, 2015 22:48
Document generated by Confluence on Jun 05, 2016 09:33