Skip to content

Commit

Permalink
PR1132: Отправка команд на шлюз
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNeuronix committed Nov 19, 2014
1 parent 3834416 commit f937f2c
Show file tree
Hide file tree
Showing 7 changed files with 736 additions and 9 deletions.
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ mvn package
``` java
public static void main(String[] args) {
...
PR1132 pr = new PR1132();
PC1116 pc = new PC1116();
RX2164 rx = new RX2164();

Expand Down Expand Up @@ -64,6 +65,11 @@ mvn package
byte channel = 1;
byte level = 85;

PR1132.setHost("192.168.10.20");

pr.turnOn(channel);
pr.turnOff(channel);

pc.open();

pc.turnOn(channel);
Expand All @@ -79,7 +85,7 @@ mvn package
}
```

В скомпилированную библиотеку входят программы для тестирования приемника RX2164 и передатчика PC11xx.
В скомпилированную библиотеку входят программы для тестирования приемника RX2164, передатчика PC11xx и Ethernet-шлюза PR1132.

Примеры использования передатчика:

Expand All @@ -103,15 +109,33 @@ java -cp noolite4j-x.jar ru.iris.noolite4j.TestPC11xx "bind" "5"
java -cp noolite4j-x.jar ru.iris.noolite4j.TestPC11xx "unbind" "5"
```

Примеры использования приемника:
Примеры использования Ethenet-шлюза PR1132:

```
java -cp noolite4j-x.jar ru.iris.noolite4j.TestRX2164
java -cp noolite4j-x.jar ru.iris.noolite4j.TestPR1132 "192.168.10.20" "turnon" "1"
```

```
java -cp noolite4j-x.jar ru.iris.noolite4j.TestPR1132 "192.168.10.20" "turnoff" "1"
```

```
java -cp noolite4j-x.jar ru.iris.noolite4j.TestPR1132 "192.168.10.20" "setlevel" "1" "55"
```

```
java -cp noolite4j-x.jar ru.iris.noolite4j.TestPR1132 "192.168.10.20" "bind" "5"
```

```
java -cp noolite4j-x.jar ru.iris.noolite4j.TestPR1132 "192.168.10.20" "unbind" "5"
```

## Планы
- Добавить поддержку Ethernet-шлюза PR1132
Примеры использования приемника:

```
java -cp noolite4j-x.jar ru.iris.noolite4j.TestRX2164
```

## Лицензия
Apache 2.0
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>ru.iris</groupId>
<artifactId>noolite4j</artifactId>
<version>1.0</version>
<version>1.1</version>

<dependencies>
<dependency>
Expand All @@ -19,6 +19,12 @@
<artifactId>usb4java</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>

</dependencies>

<build>
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/ru/iris/noolite4j/TestPR1132.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright 2014 Nikolay A. Viguro
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ru.iris.noolite4j;


import ru.iris.noolite4j.gateway.PR1132;

public class TestPR1132 {

public static void main(String[] args)
{
PR1132 pc = new PR1132();

if(args[0].isEmpty() || args[1].isEmpty() || args[2].isEmpty())
{
System.out.println("Нет адреса, команды или канала!");
System.exit(-1);
}

PR1132.setHost(args[0]);

byte channel = Byte.valueOf(args[2]);

switch (args[0])
{
case "turnon":
System.out.println("Включается нагрузка на канале " + channel);
pc.turnOn(channel);
break;

case "turnoff":
System.out.println("Выключается нагрузка на канале " + channel);
pc.turnOff(channel);
break;

case "setlevel":

if(args[3].isEmpty())
{
System.out.println("Не указан уровень нагрузки!");
System.exit(-1);
}

byte level = Byte.valueOf(args[2]);

System.out.println("Устанавливается уровень " + level + " нагрузки на канале {}" + channel);
pc.setLevel(channel, level);
break;

case "bind":
System.out.println("Включен режим привзяки для канала " + channel);
pc.bindChannel(channel);
break;

case "unbind":
System.out.println("Выключен режим привзяки для канала" + channel);
pc.unbindChannel(channel);
break;

default:
System.out.println("Команда не опознана!");
System.exit(-1);
break;
}

}

}
160 changes: 160 additions & 0 deletions src/main/java/ru/iris/noolite4j/gateway/HTTPCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2014 Nikolay A. Viguro
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package ru.iris.noolite4j.gateway;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import ru.iris.noolite4j.watchers.CommandType;
import ru.iris.noolite4j.watchers.DataFormat;

import java.io.IOException;

public class HTTPCommand {

private byte channel;
private CommandType cmd;
private byte br;
private DataFormat fmt;
private byte d0;
private byte d1;
private byte d2;
private byte d3;

/**
* Шлет данные на сервер R1132
* @return успешно или нет
*/
public boolean send() {

String buildUrl = "http://" + PR1132.getHost() + "/api.htm?ch=" + channel + "&cmd=" + cmd;

if(br != 0)
buildUrl += "&br=" + br;

if(fmt != null)
buildUrl += "&fmt=" + fmt;

if(d0 != 0)
buildUrl += "&d0=" + d0;

if(d1 != 0)
buildUrl += "&d1=" + d1;

if(d2 != 0)
buildUrl += "&d2=" + d2;

if(d3 != 0)
buildUrl += "&d3=" + d3;

try
{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(buildUrl);
CloseableHttpResponse response = httpclient.execute(httpget);

return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK;

} catch (IOException e)
{
e.printStackTrace();
}

return false;
}

public byte getChannel() {
return channel;
}

public void setChannel(byte channel) {
this.channel = channel;
}

public CommandType getCmd() {
return cmd;
}

public void setCmd(CommandType cmd) {
this.cmd = cmd;
}

public DataFormat getFmt() {
return fmt;
}

public void setFmt(DataFormat fmt) {
this.fmt = fmt;
}

public byte getBr() {
return br;
}

public void setBr(byte br) {
this.br = br;
}

public byte getD0() {
return d0;
}

public void setD0(byte d0) {
this.d0 = d0;
}

public byte getD1() {
return d1;
}

public void setD1(byte d1) {
this.d1 = d1;
}

public byte getD2() {
return d2;
}

public void setD2(byte d2) {
this.d2 = d2;
}

public byte getD3() {
return d3;
}

public void setD3(byte d3) {
this.d3 = d3;
}

@Override
public String toString() {
return "HTTPCommand{" +
"channel=" + channel +
", cmd=" + cmd +
", br=" + br +
", fmt=" + fmt +
", d0=" + d0 +
", d1=" + d1 +
", d2=" + d2 +
", d3=" + d3 +
'}';
}
}
Loading

0 comments on commit f937f2c

Please sign in to comment.