1import * as ONEWIRE from 'ONEWIRE'
2
3class HW_ONEWIRE {
4    constructor(options) {
5        if (!options || !options.id) {
6            throw new Error("options is invalid");
7        }
8        this.options = {
9            id: options.id
10        };
11
12        this.success = options.success || function(){};
13        this.fail = options.fail || function(){};
14        this._open();
15    }
16
17    _open() {
18        this.onewireinstance = ONEWIRE.open(this.options.id);
19        if (!this.onewireinstance) {
20            this.fail();
21            return;
22        }
23        this.success();
24    }
25    setspeed(standard) {
26        if (!this.onewireinstance) {
27            throw new Error("onewire not init");
28        }
29        this.onewireinstance.setspeed(standard);
30    }
31
32    reset() {
33        if (!this.onewireinstance) {
34            throw new Error("onewire not init");
35        }
36        this.onewireinstance.reset();
37    }
38
39    readByte() {
40        if (!this.onewireinstance) {
41            throw new Error("onewire not init");
42        }
43        return this.onewireinstance.readByte();
44    };
45
46    writeByte(data) {
47        if (!this.onewireinstance) {
48            throw new Error("onewire not init");
49        }
50        return this.onewireinstance.writeByte(data);
51    };
52
53    close() {
54        if (!this.onewireinstance) {
55            throw new Error("onewire not init");
56        }
57        this.onewireinstance.close();
58    }
59}
60
61export function open(options) {
62    return new HW_ONEWIRE(options);
63}
64