Skip to content

Commit

Permalink
Outlets can now be switched on and off via the configuration page
Browse files Browse the repository at this point in the history
  • Loading branch information
Machriam committed Jun 11, 2024
1 parent 7fc0c33 commit 17dae9a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public IEnumerable<OutletModel> GetOutlets()
}

[HttpPost("switchoutlet")]
public void SwitchOutlet([FromServices] IDeviceApiFactory apiFactory, string ip, long code)
public void SwitchOutlet([FromServices] IDeviceApiFactory apiFactory, string ip, long codeId)
{
var code = context.SwitchableOutletCodes.First(c => c.Id == codeId).Code;
apiFactory.SwitchOutletsClient(ip).SwitchoutletAsync(code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
let previewImage = new ThermalImage();
let pictureStreamer: PictureStreamer;
let existingOutlets: OutletModel[] = [];
let selectedOutlet: OutletModel | undefined;
let logData = "";
let searchingForDevices = true;
Expand Down Expand Up @@ -111,6 +112,10 @@
pictureStreamer.stopStreaming();
pictureStreamer.showPreview(device, CameraType.Vis, 1);
}
async function checkStatus(ip: string) {
const deviceClient = new DeviceConfigurationClient();
await deviceClient.recheckDevice(ip);
}
async function updateIpRange() {
const client = new AppConfigurationClient();
client.updateIpRanges(configurationData.ipFrom, configurationData.ipTo);
Expand All @@ -125,6 +130,14 @@
configurationData.userName = "";
configurationData.userPassword = "";
}
function switchPowerOutlet(code: number | undefined) {
if (code == undefined) return;
const switchDevices = devices.filter((d) => d.health.state != undefined && d.health.state & HealthState.CanSwitchOutlets);
const outletClient = new PowerOutletClient();
switchDevices.forEach(async (d) => {
outletClient.switchOutlet(d.ip, code);
});
}
async function getDeviceStatus() {
const client = new DeviceConfigurationClient();
const outletClient = new PowerOutletClient();
Expand Down Expand Up @@ -220,12 +233,22 @@
{/if}
{/if}
<button on:click={() => openConsole(device.ip)} class="btn btn-primary"> Open Console </button>
<button class="btn btn-primary" on:click={async () => await checkStatus(device.ip)}>Check Device</button>
</td>
</tr>
</tbody>
</table>
{/each}
<button on:click={async () => await getDeviceStatus()} class="btn btn-primary">Update</button>
<Select
selectedItemChanged={(x) => (selectedOutlet = x)}
textSelector={(x) => `${x.name} Channel: ${x.channel} Button: ${x.buttonNumber}`}
items={existingOutlets}
class="col-md-6"></Select>
{#if selectedOutlet != undefined}
<button on:click={() => switchPowerOutlet(selectedOutlet?.switchOnId)} class="btn btn-success">Power On</button>
<button on:click={() => switchPowerOutlet(selectedOutlet?.switchOffId)} class="btn btn-danger">Power Off</button>
{/if}
</div>
<div class="col-md-6">
{#if !previewImage.dataUrl?.isEmpty()}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script lang="ts" generics="T">
export let idSelector: (x: T) => string;
export let textSelector: (x: T) => string;
export let idSelector: (x: T) => string = (x) => JSON.stringify(x);
export let textSelector: (x: T) => string = (x) => x + "";
export let items: T[];
export let initialSelectedItem: string | undefined;
export let initialSelectedItem: string | undefined = undefined;
let firstRender = true;
let selectedItem: T | undefined;
let isInitialized = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ export interface IPowerOutletClient {

powerOutletForDevice(deviceId?: string | undefined): Promise<AssociatePowerOutletModel | null>;

switchOutlet(ip?: string | undefined, code?: number | undefined): Promise<void>;
switchOutlet(ip?: string | undefined, codeId?: number | undefined): Promise<void>;
}

export class PowerOutletClient extends GatewayAppApiBase implements IPowerOutletClient {
Expand Down Expand Up @@ -476,16 +476,16 @@ export class PowerOutletClient extends GatewayAppApiBase implements IPowerOutlet
return Promise.resolve<AssociatePowerOutletModel | null>(null as any);
}

switchOutlet(ip?: string | undefined, code?: number | undefined): Promise<void> {
switchOutlet(ip?: string | undefined, codeId?: number | undefined): Promise<void> {
let url_ = this.baseUrl + "/api/PowerOutlet/switchoutlet?";
if (ip === null)
throw new Error("The parameter 'ip' cannot be null.");
else if (ip !== undefined)
url_ += "ip=" + encodeURIComponent("" + ip) + "&";
if (code === null)
throw new Error("The parameter 'code' cannot be null.");
else if (code !== undefined)
url_ += "code=" + encodeURIComponent("" + code) + "&";
if (codeId === null)
throw new Error("The parameter 'codeId' cannot be null.");
else if (codeId !== undefined)
url_ += "codeId=" + encodeURIComponent("" + codeId) + "&";
url_ = url_.replace(/[?&]$/, "");

let options_: RequestInit = {
Expand Down
4 changes: 2 additions & 2 deletions PlantMonitorControl/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"PythonExecutable": "/srv/pythonClick/bin/python3"
},
"PowerSwitchPrograms": {
"SwitchOutlet": "/srv/SwitchableOutlets/SwitchOutlet",
"ReceiveTest": "/srv/SwitchableOutlets/ReceiveTest"
"SwitchOutlet": "/srv/switchableOutlet/SwitchOutlet",
"ReceiveTest": "/srv/switchableOutlet/ReceiveTest"
},
"MotorPinout": {
"Enable": 15,
Expand Down

0 comments on commit 17dae9a

Please sign in to comment.