MangoAutomation / BACnet4J

BACnet/IP stack written in Java. Forked from http://sourceforge.net/projects/bacnet4j/
GNU General Public License v3.0
188 stars 111 forks source link

Write Array properties #36

Closed Sujay-M closed 5 years ago

Sujay-M commented 5 years ago

Hi,

I am trying to write to weekly schedule property in a schedule object. I have little knowledge of the spec regarding this, please let me know if i am doing any mistakes. This is the daily schedule i am trying to write. final DailySchedule dailySchedule = new DailySchedule(new SequenceOf<>(new TimeValue(new Time(8, 0, 0, 0), new Real(10)), new TimeValue(new Time(17, 0, 0, 0), new Real(11))));

final ScheduleObject so = new ScheduleObject(localDevice, 0, "sch0", effectivePeriod, weeklySchedule, exceptionSchedule, new Real(8), listOfObjectPropertyReferences, 12, false);

so.writeProperty(null, PropertyIdentifier.weeklySchedule, 1, dailySchedule);

When i do this, i get the following exception.

Exception in thread "main" java.lang.ClassCastException: com.serotonin.bacnet4j.type.constructed.DailySchedule cannot be cast to com.serotonin.bacnet4j.type.constructed.BACnetArray at com.serotonin.bacnet4j.obj.ScheduleObject.validateProperty(ScheduleObject.java:187) at com.serotonin.bacnet4j.obj.BACnetObject.writeProperty(BACnetObject.java:486) at com.serotonin.bacnet4j.obj.BACnetObject.writeProperty(BACnetObject.java:455) at com.serotonin.bacnet4j.TestArray.main(TestArray.java:75)

Question 1: Is writing to a specific array index not allowed?

When i do this it works fine, so.writeProperty(null, PropertyIdentifier.weeklySchedule, weeklySchedule); where weeklySchedule is array[7] of dailySchedules.

Question 2. But when i try to write from a client, the BACnetArray is getting deserialized as SequenceOf. So i get a class cast exception. Is there any difference in SequenceOf and BACnetArray or am i writing it wrongly?

Following is the code snippet used.

`

public static void main(String[] args) throws Exception {
    final TestNetworkMap map = new TestNetworkMap();
    LocalDevice remoteLocalDevice = new LocalDevice(1, new DefaultTransport(new TestNetwork(map, 1, 100))).initialize();
    LocalDevice localDevice = new LocalDevice(2, new DefaultTransport(new TestNetwork(map, 2, 100))).initialize();
    final AnalogValueObject av0 = new AnalogValueObject(remoteLocalDevice, 0, "av0", 98, EngineeringUnits.amperes, false)
            .supportCommandable(-2);
    final AnalogValueObject av1 = new AnalogValueObject(remoteLocalDevice, 1, "av1", 99, EngineeringUnits.amperesPerMeter, false)
            .supportCommandable(-1);

    final SequenceOf<CalendarEntry> dateList = new SequenceOf<>( //
            new CalendarEntry(new Date(-1, null, -1, DayOfWeek.FRIDAY)), // Every Friday.
            new CalendarEntry(
                    new DateRange(new Date(-1, Month.NOVEMBER, -1, null), new Date(-1, Month.FEBRUARY, -1, null))), // November to February
            new CalendarEntry(new WeekNDay(Month.UNSPECIFIED, WeekNDay.WeekOfMonth.days22to28, DayOfWeek.WEDNESDAY)) // The Wednesday during the 4th week of each month.
    );
    final CalendarObject co = new CalendarObject(remoteLocalDevice, 0, "cal0", dateList);
    final DailySchedule dailySchedule = new DailySchedule(new SequenceOf<>(new TimeValue(new Time(8, 0, 0, 0), new Real(10)),
            new TimeValue(new Time(17, 0, 0, 0), new Real(11))));
    final DateRange effectivePeriod = new DateRange(Date.UNSPECIFIED, Date.UNSPECIFIED);
    final BACnetArray<DailySchedule> weeklySchedule = new BACnetArray<>( //
            new DailySchedule(new SequenceOf<>(new TimeValue(new Time(8, 0, 0, 0), new Real(10)),
                    new TimeValue(new Time(17, 0, 0, 0), new Real(11)))), //
            new DailySchedule(new SequenceOf<>(new TimeValue(new Time(8, 0, 0, 0), new Real(12)),
                    new TimeValue(new Time(17, 0, 0, 0), new Real(13)))), //
            new DailySchedule(new SequenceOf<>(new TimeValue(new Time(8, 0, 0, 0), new Real(14)),
                    new TimeValue(new Time(17, 0, 0, 0), new Real(15)))), //
            new DailySchedule(new SequenceOf<>(new TimeValue(new Time(9, 0, 0, 0), new Real(16)),
                    new TimeValue(new Time(20, 0, 0, 0), new Real(17)))), //
            new DailySchedule(new SequenceOf<>(new TimeValue(new Time(9, 0, 0, 0), new Real(18)),
                    new TimeValue(new Time(21, 30, 0, 0), new Real(19)))), //
            new DailySchedule(new SequenceOf<TimeValue>()), //
            new DailySchedule(new SequenceOf<TimeValue>()));
    final SequenceOf<SpecialEvent> exceptionSchedule = new SequenceOf<>( //
            new SpecialEvent(co.getId(),
                    new SequenceOf<>(new TimeValue(new Time(8, 0, 0, 0), new Real(20)),
                            new TimeValue(new Time(22, 0, 0, 0), new Real(21))),
                    new UnsignedInteger(10)), // Calendar
            new SpecialEvent(co.getId(),
                    new SequenceOf<>(new TimeValue(new Time(13, 0, 0, 0), new Real(22)),
                            new TimeValue(new Time(14, 0, 0, 0), new Real(23))),
                    new UnsignedInteger(7)), // Calendar
            new SpecialEvent(new CalendarEntry(new Date(-1, null, 8, DayOfWeek.WEDNESDAY)),
                    new SequenceOf<>(new TimeValue(new Time(10, 30, 0, 0), new Real(24)),
                            new TimeValue(new Time(17, 0, 0, 0), new Real(25))),
                    new UnsignedInteger(6)) // 7th is a Wednesday
    );
    final SequenceOf<DeviceObjectPropertyReference> listOfObjectPropertyReferences = new SequenceOf<>(
            new DeviceObjectPropertyReference(av1.getId(), PropertyIdentifier.presentValue, null, null) //
    );

    final ScheduleObject so = new ScheduleObject(remoteLocalDevice, 0, "sch0", effectivePeriod, weeklySchedule, exceptionSchedule,
            new Real(8), listOfObjectPropertyReferences, 12, false);
    RemoteDevice remoteDevice = localDevice.getRemoteDevice(1).get();
    localDevice.send(remoteDevice, new WritePropertyRequest(so.getId(), PropertyIdentifier.weeklySchedule,
            null, weeklySchedule, new UnsignedInteger(8)));
}

`

Exception:

java.lang.ClassCastException: com.serotonin.bacnet4j.type.constructed.SequenceOf cannot be cast to com.serotonin.bacnet4j.type.constructed.BACnetArray at com.serotonin.bacnet4j.obj.ScheduleObject.validateProperty(ScheduleObject.java:187) at com.serotonin.bacnet4j.obj.BACnetObject.writeProperty(BACnetObject.java:486) at com.serotonin.bacnet4j.service.confirmed.WritePropertyRequest.handle(WritePropertyRequest.java:102) at com.serotonin.bacnet4j.transport.DefaultTransport.handleConfirmedRequest(DefaultTransport.java:870) at com.serotonin.bacnet4j.transport.DefaultTransport.incomingConfirmedRequest(DefaultTransport.java:827) at com.serotonin.bacnet4j.transport.DefaultTransport.receiveAPDU(DefaultTransport.java:640) at com.serotonin.bacnet4j.transport.DefaultTransport.receiveImpl(DefaultTransport.java:578) at com.serotonin.bacnet4j.transport.DefaultTransport.run(DefaultTransport.java:498) at java.base/java.lang.Thread.run(Thread.java:844)

terrypacker commented 5 years ago

You may want to take a look at the Test written for the ScheduleObject here https://github.com/infiniteautomation/BACnet4J/blob/master/src/test/java/com/serotonin/bacnet4j/obj/ScheduleObjectTest.java

Sujay-M commented 5 years ago

@terrypacker , sorry for the delayed response. I might have missed the notification. Thank you, I'll check the above mentioned file..